Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 define("MAGPIE_DIR", "./Services/Feeds/magpierss/");
00025 define("MAGPIE_CACHE_ON", true);
00026 define("MAGPIE_CACHE_DIR", "./".ILIAS_WEB_DIR."/".CLIENT_ID."/magpie_cache");
00027 define('MAGPIE_OUTPUT_ENCODING', "UTF-8");
00028 define('MAGPIE_CACHE_AGE', 900);
00029 include_once(MAGPIE_DIR."/rss_fetch.inc");
00030
00031 include_once("./Services/Feeds/classes/class.ilExternalFeedItem.php");
00032
00040 class ilExternalFeed
00041 {
00042 protected $items = array();
00043
00047 function ilExternalFeed()
00048 {
00049
00050
00051 $this->_createCacheDirectory();
00052 $feed_set = new ilSetting("feed");
00053 define('IL_FEED_PROXY_HOST', $feed_set->get("proxy"));
00054 define('IL_FEED_PROXY_PORT', $feed_set->get("proxy_port"));
00055 }
00056
00062 function setUrl($a_url)
00063 {
00064 $this->url = $a_url;
00065 }
00066
00072 function getUrl()
00073 {
00074 return $this->url;
00075 }
00076
00082 function setError($a_error)
00083 {
00084 $this->error = $a_error;
00085 }
00086
00092 function getError()
00093 {
00094 return $this->error;
00095 }
00096
00100 static function _createCacheDirectory()
00101 {
00102 if (!is_dir(ilUtil::getWebspaceDir()."/magpie_cache"))
00103 {
00104 ilUtil::makeDir(ilUtil::getWebspaceDir()."/magpie_cache");
00105 }
00106 }
00107
00114 static function _checkUrl($a_url)
00115 {
00116 if (!defined('IL_FEED_PROXY_HOST'))
00117 {
00118 ilExternalFeed::_createCacheDirectory();
00119 $feed_set = new ilSetting("feed");
00120 define('IL_FEED_PROXY_HOST', $feed_set->get("proxy"));
00121 define('IL_FEED_PROXY_PORT', $feed_set->get("proxy_port"));
00122 }
00123
00124 $feed = @fetch_rss($a_url);
00125 if (!$feed)
00126 {
00127 $error = magpie_error();
00128
00129 if ($error != "")
00130 {
00131 return $error;
00132 }
00133 else
00134 {
00135 return "Unknown Error.";
00136 }
00137 }
00138
00139 return true;
00140 }
00141
00145 function fetch()
00146 {
00147 if ($this->getUrl() != "")
00148 {
00149 $this->feed = @fetch_rss($this->getUrl());
00150 }
00151
00152 if(!$this->feed)
00153 {
00154 $error = magpie_error();
00155 if ($error == "")
00156 {
00157 $this->setError("Unknown Error.");
00158 }
00159 else
00160 {
00161 $this->setError(magpie_error());
00162 }
00163 return false;
00164 }
00165
00166 if (is_array($this->feed->items))
00167 {
00168 foreach($this->feed->items as $item)
00169 {
00170 $item_obj = new ilExternalFeedItem();
00171 $item_obj->setMagpieItem($item);
00172 $this->items[] = $item_obj;
00173 }
00174 }
00175 }
00176
00180 function checkCacheHit()
00181 {
00182 ilExternalFeed::_createCacheDirectory();
00183
00184 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
00185
00186 $cache_status = 0;
00187 $request_headers = array();
00188 $rss = 0;
00189 $errormsg = 0;
00190
00191 $cache_key = $this->getUrl().MAGPIE_OUTPUT_ENCODING;
00192
00193 if (!$cache->ERROR) {
00194
00195 $cache_status = $cache->check_cache( $cache_key);
00196 }
00197
00198
00199 if ($cache_status == 'HIT')
00200 {
00201 return true;
00202 }
00203
00204 return false;
00205 }
00206
00210 function getChannelTitle()
00211 {
00212 return $this->feed->channel["title"];
00213 }
00214
00218 function getChannelDescription()
00219 {
00220 return $this->feed->channel["description"];
00221 }
00222
00226 function getItems()
00227 {
00228 return $this->items;
00229 }
00230
00236 static function _determineFeedUrl($a_url)
00237 {
00238 if (!defined('IL_FEED_PROXY_HOST'))
00239 {
00240 $feed_set = new ilSetting("feed");
00241 define('IL_FEED_PROXY_HOST', $feed_set->get("proxy"));
00242 define('IL_FEED_PROXY_PORT', $feed_set->get("proxy_port"));
00243 }
00244
00245 $res = @fopen($a_url, "r");
00246
00247 if (!$res)
00248 {
00249 return "";
00250 }
00251
00252 $contents = '';
00253 while (!feof($res))
00254 {
00255 $contents.= fread($res, 8192);
00256 }
00257 fclose($res);
00258
00259 return ilExternalFeed::_getRSSLocation($contents, $a_url);
00260 }
00261
00266 function _getRSSLocation($html, $location)
00267 {
00268 if(!$html or !$location){
00269 return false;
00270 }else{
00271 #search through the HTML, save all <link> tags
00272 # and store each link's attributes in an associative array
00273 preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
00274 $links = $matches[1];
00275 $final_links = array();
00276 $link_count = count($links);
00277 for($n=0; $n<$link_count; $n++){
00278 $attributes = preg_split('/\s+/s', $links[$n]);
00279 foreach($attributes as $attribute){
00280 $att = preg_split('/\s*=\s*/s', $attribute, 2);
00281 if(isset($att[1])){
00282 $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
00283 $final_link[strtolower($att[0])] = $att[1];
00284 }
00285 }
00286 $final_links[$n] = $final_link;
00287 }
00288 #now figure out which one points to the RSS file
00289 for($n=0; $n<$link_count; $n++){
00290 if(strtolower($final_links[$n]['rel']) == 'alternate'){
00291 if(strtolower($final_links[$n]['type']) == 'application/rss+xml'){
00292 $href = $final_links[$n]['href'];
00293 }
00294 if(!$href and strtolower($final_links[$n]['type']) == 'text/xml'){
00295 #kludge to make the first version of this still work
00296 $href = $final_links[$n]['href'];
00297 }
00298 if($href){
00299 if(strstr($href, "http://") !== false){ #if it's absolute
00300 $full_url = $href;
00301 }else{ #otherwise, 'absolutize' it
00302 $url_parts = parse_url($location);
00303 #only made it work for http:// links. Any problem with this?
00304 $full_url = "http://$url_parts[host]";
00305 if(isset($url_parts['port'])){
00306 $full_url .= ":$url_parts[port]";
00307 }
00308 if($href{0} != '/'){ #it's a relative link on the domain
00309 $full_url .= dirname($url_parts['path']);
00310 if(substr($full_url, -1) != '/'){
00311 #if the last character isn't a '/', add it
00312 $full_url .= '/';
00313 }
00314 }
00315 $full_url .= $href;
00316 }
00317 return $full_url;
00318 }
00319 }
00320 }
00321 return false;
00322 }
00323 }
00324 }
00325 ?>