ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExternalFeed.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'classes/class.ilProxySettings.php';
5 
6 define("MAGPIE_DIR", "./Services/Feeds/magpierss/");
7 define("MAGPIE_CACHE_ON", true);
8 define("MAGPIE_CACHE_DIR", "./".ILIAS_WEB_DIR."/".CLIENT_ID."/magpie_cache");
9 define('MAGPIE_OUTPUT_ENCODING', "UTF-8");
10 define('MAGPIE_CACHE_AGE', 900); // 900 seconds = 15 minutes
11 include_once(MAGPIE_DIR."/rss_fetch.inc");
12 
13 include_once("./Services/Feeds/classes/class.ilExternalFeedItem.php");
14 
23 {
24  protected $items = array();
25 
29  function ilExternalFeed()
30  {
31  // IF YOU ADD THINGS HERE, THEY MAY ALSO BE ADDED TO
32  // SOME OF THE STATIC METHODS
33  $this->_createCacheDirectory();
34 
35  if(ilProxySettings::_getInstance()->isActive())
36  {
37  define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
38  define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
39  }
40  else
41  {
42  define('IL_FEED_PROXY_HOST', "");
43  define('IL_FEED_PROXY_PORT', "");
44  }
45  }
46 
52  function setUrl($a_url)
53  {
54  $this->url = $a_url;
55  }
56 
62  function getUrl()
63  {
64  return $this->url;
65  }
66 
72  function setError($a_error)
73  {
74  $this->error = $a_error;
75  }
76 
82  function getError()
83  {
84  return $this->error;
85  }
86 
90  static function _createCacheDirectory()
91  {
92  if (!is_dir(ilUtil::getWebspaceDir()."/magpie_cache"))
93  {
94  ilUtil::makeDir(ilUtil::getWebspaceDir()."/magpie_cache");
95  }
96  }
97 
104  static function _checkUrl($a_url)
105  {
106  if (!defined('IL_FEED_PROXY_HOST'))
107  {
109 
110  if(ilProxySettings::_getInstance()->isActive())
111  {
112  define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
113  define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
114  }
115  else
116  {
117  define('IL_FEED_PROXY_HOST', "");
118  define('IL_FEED_PROXY_PORT', "");
119  }
120  }
121 
122  $feed = @fetch_rss($a_url);
123  if (!$feed)
124  {
125  $error = magpie_error();
126 
127  if ($error != "")
128  {
129  return $error;
130  }
131  else
132  {
133  return "Unknown Error.";
134  }
135  }
136 
137  return true;
138  }
139 
143  function fetch()
144  {
145  if ($this->getUrl() != "")
146  {
147  $this->feed = @fetch_rss($this->getUrl());
148  }
149 
150  if(!$this->feed)
151  {
152  $error = magpie_error();
153  if ($error == "")
154  {
155  $this->setError("Unknown Error.");
156  }
157  else
158  {
159  $this->setError(magpie_error());
160  }
161  return false;
162  }
163 
164  if (is_array($this->feed->items))
165  {
166  foreach($this->feed->items as $item)
167  {
168  $item_obj = new ilExternalFeedItem();
169  $item_obj->setMagpieItem($item);
170  $this->items[] = $item_obj;
171  }
172  }
173  }
174 
178  function checkCacheHit()
179  {
181 
182  $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
183 
184  $cache_status = 0; // response of check_cache
185  $request_headers = array(); // HTTP headers to send with fetch
186  $rss = 0; // parsed RSS object
187  $errormsg = 0; // errors, if any
188 
189  $cache_key = $this->getUrl().MAGPIE_OUTPUT_ENCODING;
190 
191  if (!$cache->ERROR) {
192  // return cache HIT, MISS, or STALE
193  $cache_status = $cache->check_cache( $cache_key);
194  }
195 
196  // if object cached, and cache is fresh, return cached obj
197  if ($cache_status == 'HIT')
198  {
199  return true;
200  }
201 
202  return false;
203  }
204 
208  function getChannelTitle()
209  {
210  return $this->feed->channel["title"];
211  }
212 
217  {
218  return $this->feed->channel["description"];
219  }
220 
224  function getItems()
225  {
226  return $this->items;
227  }
228 
234  static function _determineFeedUrl($a_url)
235  {
236  if (!defined('IL_FEED_PROXY_HOST'))
237  {
238  if(ilProxySettings::_getInstance()->isActive())
239  {
240  define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
241  define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
242  }
243  else
244  {
245  define('IL_FEED_PROXY_HOST', "");
246  define('IL_FEED_PROXY_PORT', "");
247  }
248  }
249 
250  $res = @fopen($a_url, "r");
251 
252  if (!$res)
253  {
254  return "";
255  }
256 
257  $contents = '';
258  while (!feof($res))
259  {
260  $contents.= fread($res, 8192);
261  }
262  fclose($res);
263 
264  return ilExternalFeed::_getRSSLocation($contents, $a_url);
265  }
266 
271  function _getRSSLocation($html, $location)
272  {
273  if(!$html or !$location){
274  return false;
275  }else{
276  #search through the HTML, save all <link> tags
277  # and store each link's attributes in an associative array
278  preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
279  $links = $matches[1];
280  $final_links = array();
281  $link_count = count($links);
282  for($n=0; $n<$link_count; $n++){
283  $attributes = preg_split('/\s+/s', $links[$n]);
284  foreach($attributes as $attribute){
285  $att = preg_split('/\s*=\s*/s', $attribute, 2);
286  if(isset($att[1])){
287  $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
288  $final_link[strtolower($att[0])] = $att[1];
289  }
290  }
291  $final_links[$n] = $final_link;
292  }
293  #now figure out which one points to the RSS file
294  for($n=0; $n<$link_count; $n++){
295  if(strtolower($final_links[$n]['rel']) == 'alternate'){
296  if(strtolower($final_links[$n]['type']) == 'application/rss+xml'){
297  $href = $final_links[$n]['href'];
298  }
299  if(!$href and strtolower($final_links[$n]['type']) == 'text/xml'){
300  #kludge to make the first version of this still work
301  $href = $final_links[$n]['href'];
302  }
303  if($href){
304  if(strstr($href, "http://") !== false){ #if it's absolute
305  $full_url = $href;
306  }else{ #otherwise, 'absolutize' it
307  $url_parts = parse_url($location);
308  #only made it work for http:// links. Any problem with this?
309  $full_url = "http://$url_parts[host]";
310  if(isset($url_parts['port'])){
311  $full_url .= ":$url_parts[port]";
312  }
313  if($href{0} != '/'){ #it's a relative link on the domain
314  $full_url .= dirname($url_parts['path']);
315  if(substr($full_url, -1) != '/'){
316  #if the last character isn't a '/', add it
317  $full_url .= '/';
318  }
319  }
320  $full_url .= $href;
321  }
322  return $full_url;
323  }
324  }
325  }
326  return false;
327  }
328  }
329 }
330 ?>