ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 './Services/Http/classes/class.ilProxySettings.php';
5 
6 define("MAGPIE_DIR", "./Services/Feeds/magpierss/");
7 define("MAGPIE_CACHE_ON", true);
8 define("MAGPIE_CACHE_DIR", ILIAS_DATA_DIR . "/" . CLIENT_ID . "/magpie_cache");
9 define('MAGPIE_OUTPUT_ENCODING', "UTF-8");
10 define('MAGPIE_CACHE_AGE', 900); // 900 seconds = 15 minutes
11 define('MAGPIE_DEBUG', false);
12 include_once(MAGPIE_DIR . "/rss_fetch.inc");
13 
14 include_once("./Services/Feeds/classes/class.ilExternalFeedItem.php");
15 
24 {
25  protected $items = array();
26 
30  public function __construct()
31  {
32  // IF YOU ADD THINGS HERE, THEY MAY ALSO BE ADDED TO
33  // SOME OF THE STATIC METHODS
34  $this->_createCacheDirectory();
35 
36  if (ilProxySettings::_getInstance()->isActive()) {
37  define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
38  define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
39  } else {
40  define('IL_FEED_PROXY_HOST', "");
41  define('IL_FEED_PROXY_PORT', "");
42  }
43  }
44 
50  public function setUrl($a_url)
51  {
52  $this->url = $a_url;
53  }
54 
60  public function getUrl()
61  {
62  return $this->url;
63  }
64 
70  public function setError($a_error)
71  {
72  $this->error = $a_error;
73  }
74 
80  public function getError()
81  {
82  return $this->error;
83  }
84 
88  public static function _createCacheDirectory()
89  {
90  if (!is_dir(ilUtil::getDataDir() . "/magpie_cache")) {
91  ilUtil::makeDir(ilUtil::getDataDir() . "/magpie_cache");
92  }
93  }
94 
101  public static function _checkUrl($a_url)
102  {
103  if (!defined('IL_FEED_PROXY_HOST')) {
105 
106  if (ilProxySettings::_getInstance()->isActive()) {
107  define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
108  define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
109  } else {
110  define('IL_FEED_PROXY_HOST', "");
111  define('IL_FEED_PROXY_PORT', "");
112  }
113  }
114 
115  $feed = @fetch_rss($a_url);
116  if (!$feed) {
117  $error = magpie_error();
118 
119  if ($error != "") {
120  return $error;
121  } else {
122  return "Unknown Error.";
123  }
124  }
125 
126  return true;
127  }
128 
132  public function fetch()
133  {
134  if ($this->getUrl() != "") {
135  $this->feed = @fetch_rss($this->getUrl());
136  }
137 
138  if (!$this->feed) {
139  $error = magpie_error();
140  if ($error == "") {
141  $this->setError("Unknown Error.");
142  } else {
143  $this->setError(magpie_error());
144  }
145  return false;
146  }
147 
148  if (is_array($this->feed->items)) {
149  foreach ($this->feed->items as $item) {
150  $item_obj = new ilExternalFeedItem();
151  $item_obj->setMagpieItem($item);
152  $this->items[] = $item_obj;
153  }
154  }
155  }
156 
160  public function checkCacheHit()
161  {
163 
164  $cache = new RSSCache(MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE);
165 
166  $cache_status = 0; // response of check_cache
167  $request_headers = array(); // HTTP headers to send with fetch
168  $rss = 0; // parsed RSS object
169  $errormsg = 0; // errors, if any
170 
171  $cache_key = $this->getUrl() . MAGPIE_OUTPUT_ENCODING;
172 
173  if (!$cache->ERROR) {
174  // return cache HIT, MISS, or STALE
175  $cache_status = $cache->check_cache($cache_key);
176  }
177 
178  // if object cached, and cache is fresh, return cached obj
179  if ($cache_status == 'HIT') {
180  return true;
181  }
182 
183  return false;
184  }
185 
189  public function getChannelTitle()
190  {
191  return $this->feed->channel["title"];
192  }
193 
197  public function getChannelDescription()
198  {
199  return $this->feed->channel["description"];
200  }
201 
205  public function getItems()
206  {
207  return $this->items;
208  }
209 
215  public static function _determineFeedUrl($a_url)
216  {
217  if (!defined('IL_FEED_PROXY_HOST')) {
218  if (ilProxySettings::_getInstance()->isActive()) {
219  define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
220  define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
221  } else {
222  define('IL_FEED_PROXY_HOST', "");
223  define('IL_FEED_PROXY_PORT', "");
224  }
225  }
226 
227  $res = @fopen($a_url, "r");
228 
229  if (!$res) {
230  return "";
231  }
232 
233  $contents = '';
234  while (!feof($res)) {
235  $contents .= fread($res, 8192);
236  }
237  fclose($res);
238 
239  return ilExternalFeed::_getRSSLocation($contents, $a_url);
240  }
241 
246  public static function _getRSSLocation($html, $location)
247  {
248  if (!$html or !$location) {
249  return false;
250  } else {
251  #search through the HTML, save all <link> tags
252  # and store each link's attributes in an associative array
253  preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
254  $links = $matches[1];
255  $final_links = array();
256  $link_count = count($links);
257  for ($n = 0; $n < $link_count; $n++) {
258  $attributes = preg_split('/\s+/s', $links[$n]);
259  foreach ($attributes as $attribute) {
260  $att = preg_split('/\s*=\s*/s', $attribute, 2);
261  if (isset($att[1])) {
262  $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
263  $final_link[strtolower($att[0])] = $att[1];
264  }
265  }
266  $final_links[$n] = $final_link;
267  }
268  #now figure out which one points to the RSS file
269  for ($n = 0; $n < $link_count; $n++) {
270  if (strtolower($final_links[$n]['rel']) == 'alternate') {
271  if (strtolower($final_links[$n]['type']) == 'application/rss+xml') {
272  $href = $final_links[$n]['href'];
273  }
274  if (!$href and strtolower($final_links[$n]['type']) == 'text/xml') {
275  #kludge to make the first version of this still work
276  $href = $final_links[$n]['href'];
277  }
278  if ($href) {
279  if (strstr($href, "http://") !== false) { #if it's absolute
280  $full_url = $href;
281  } else { #otherwise, 'absolutize' it
282  $url_parts = parse_url($location);
283  #only made it work for http:// links. Any problem with this?
284  $full_url = "http://$url_parts[host]";
285  if (isset($url_parts['port'])) {
286  $full_url .= ":$url_parts[port]";
287  }
288  if ($href[0] != '/') { #it's a relative link on the domain
289  $full_url .= dirname($url_parts['path']);
290  if (substr($full_url, -1) != '/') {
291  #if the last character isn't a '/', add it
292  $full_url .= '/';
293  }
294  }
295  $full_url .= $href;
296  }
297  return $full_url;
298  }
299  }
300  }
301  return false;
302  }
303  }
304 }
getChannelDescription()
Get Description.
Wraps $item arrays from magpie.
static _determineFeedUrl($a_url)
Determine Feed Url.
$location
Definition: buildRTE.php:44
__construct()
Constructor.
const MAGPIE_DIR
on($eventName, callable $callBack, $priority=100)
Subscribe to an event.
getChannelTitle()
Get Channel.
static _checkUrl($a_url)
Check Url.
Handles external Feeds via Magpie library.
foreach($_POST as $key=> $value) $res
setUrl($a_url)
Set Url.
const MAGPIE_CACHE_AGE
const MAGPIE_OUTPUT_ENCODING
fetch()
Fetch the feed.
$n
Definition: RandomTest.php:85
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static getDataDir()
get data directory (outside webspace)
static _createCacheDirectory()
Create magpie cache directorry (if not existing)
checkCacheHit()
Check cache hit.
setError($a_error)
Set Error.
static _getRSSLocation($html, $location)
This one is by Keith Devens , see http://keithdevens.com/weblog/archive/2002/Jun/03/RSSAuto-Discovery...
$url
$links
const MAGPIE_CACHE_DIR
static _getInstance()
Getter for unique instance.
$html
Definition: example_001.php:87