[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Florent Cotton, Olivier Meunier and contributors. 5 # All rights reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 /** 24 @defgroup CB_NET_FEED Clearbricks Feeds Reader 25 @ingroup CB_NET 26 */ 27 28 /** 29 @ingroup CB_NET_FEED 30 @brief netHttp based feed reader. 31 32 Features: 33 34 - Reads RSS 1.0 (rdf), RSS 2.0 and Atom feeds. 35 - HTTP cache negociation support 36 - Cache TTL. 37 */ 38 class feedReader extends netHttp 39 { 40 protected $user_agent = 'Clearbricks Feed Reader/0.2'; 41 protected $timeout = 5; 42 protected $validators = null; ///< <b>array</b> HTTP Cache validators 43 44 protected $cache_dir = null; ///< <b>string</b> Cache temporary directory 45 protected $cache_file_prefix = 'cbfeed'; ///< <b>string</b> Cache file prefix 46 protected $cache_ttl = '-30 minutes'; ///< <b>string</b> Cache TTL 47 48 public function __construct() 49 { 50 parent::__construct(''); 51 } 52 53 /** 54 Returns a new feedParser instance for given URL or false if source URL is 55 not a valid feed. 56 57 @param url <b>string</b> Feed URL 58 @return <b>feedParser</b> 59 */ 60 public function parse($url) 61 { 62 $this->validators = array(); 63 if ($this->cache_dir) 64 { 65 return $this->withCache($url); 66 } 67 else 68 { 69 if (!$this->getFeed($url)) { 70 return false; 71 } 72 73 if ($this->getStatus() != '200') { 74 return false; 75 } 76 77 return new feedParser($this->getContent()); 78 } 79 } 80 81 /** 82 This static method returns a new feedParser instance for given URL. If a 83 <var>$cache_dir</var> is specified, cache will be activated. 84 85 @param url <b>string</b> Feed URL 86 @param cache_dir <b>string</b> Cache directory 87 @return <b>feedParser</b> 88 */ 89 public static function quickParse($url,$cache_dir=null) 90 { 91 $parser = new self(); 92 if ($cache_dir) { 93 $parser->setCacheDir($cache_dir); 94 } 95 96 return $parser->parse($url); 97 } 98 99 /** 100 Returns true and sets <var>cache_dir</var> property if <var>$dir</var> is 101 a writable directory. Otherwise, returns false. 102 103 @param dir <b>string</b> Cache directory 104 @return <b>boolean</b> 105 */ 106 public function setCacheDir($dir) 107 { 108 $this->cache_dir = null; 109 110 if (!empty($dir) && is_dir($dir) && is_writeable($dir)) 111 { 112 $this->cache_dir = $dir; 113 return true; 114 } 115 116 return false; 117 } 118 119 /** 120 Sets cache TTL. <var>$str</var> is a interval readable by strtotime 121 (-3 minutes, -2 hours, etc.) 122 123 @param str <b>string</b> TTL 124 */ 125 public function setCacheTTL($str) 126 { 127 $str = trim($str); 128 if (!empty($str)) 129 { 130 if (substr($str,0,1) != '-') { 131 $str = '-'.$str; 132 } 133 $this->cache_ttl = $str; 134 } 135 } 136 137 /** 138 Returns feed content for given URL. 139 140 @param url <b>string</b> Feed URL 141 @return <b>string</b> 142 */ 143 protected function getFeed($url) 144 { 145 if (!self::readURL($url,$ssl,$host,$port,$path,$user,$pass)) { 146 return false; 147 } 148 $this->setHost($host,$port); 149 $this->useSSL($ssl); 150 $this->setAuthorization($user,$pass); 151 152 return $this->get($path); 153 } 154 155 /** 156 Returns feedParser object from cache if present or write it to cache and 157 returns result. 158 159 @param url <b>string</b> Feed URL 160 @return <b>feedParser</b> 161 */ 162 protected function withCache($url) 163 { 164 $cached_file = $this->cache_dir.'/'.$this->cache_file_prefix.md5($url); 165 $url_md5 = md5($url); 166 $cached_file = sprintf('%s/%s/%s/%s/%s.php', 167 $this->cache_dir, 168 $this->cache_file_prefix, 169 substr($url_md5,0,2), 170 substr($url_md5,2,2), 171 $url_md5 172 ); 173 174 $may_use_cached = false; 175 176 if (@file_exists($cached_file)) 177 { 178 $may_use_cached = true; 179 $ts = @filemtime($cached_file); 180 if ($ts > strtotime($this->cache_ttl)) 181 { 182 # Direct cache 183 return unserialize(file_get_contents($cached_file)); 184 } 185 $this->setValidator('IfModifiedSince', $ts); 186 } 187 188 if (!$this->getFeed($url)) 189 { 190 if ($may_use_cached) 191 { 192 # connection failed - fetched from cache 193 return unserialize(file_get_contents($cached_file)); 194 } 195 return false; 196 } 197 198 switch ($this->getStatus()) 199 { 200 case '304': 201 @files::touch($cached_file); 202 return unserialize(file_get_contents($cached_file)); 203 case '200': 204 if ($feed = new feedParser($this->getContent())) 205 { 206 try { 207 files::makeDir(dirname($cached_file),true); 208 } catch (Exception $e) { 209 return $feed; 210 } 211 212 if (($fp = @fopen($cached_file, 'wb'))) 213 { 214 fwrite($fp, serialize($feed)); 215 fclose($fp); 216 chmod($cached_file,fileperms(dirname($cached_file))); 217 } 218 return $feed; 219 } 220 } 221 222 return false; 223 } 224 225 /** 226 Adds HTTP cache headers to common headers. 227 228 @copydoc netHttp::buildRequest 229 */ 230 protected function buildRequest() 231 { 232 $headers = parent::buildRequest(); 233 234 # Cache validators 235 if (!empty($this->validators)) 236 { 237 if (isset($this->validators['IfModifiedSince'])) { 238 $headers[] = 'If-Modified-Since: '.$this->validators['IfModifiedSince']; 239 } 240 if (isset($this->validators['IfNoneMatch'])) { 241 if (is_array($this->validators['IfNoneMatch'])) { 242 $etags = implode(',',$this->validators['IfNoneMatch']); 243 } else { 244 $etags = $this->validators['IfNoneMatch']; 245 } 246 $headers[] = ''; 247 } 248 } 249 250 return $headers; 251 } 252 253 private function setValidator($key,$value) 254 { 255 if ($key == 'IfModifiedSince') { 256 $value = gmdate('D, d M Y H:i:s',$value).' GMT'; 257 } 258 259 $this->validators[$key] = $value; 260 } 261 } 262 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |