ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExternalFeed.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 define("MAGPIE_DIR", "./Services/Feeds/magpierss/");
25 define("MAGPIE_CACHE_ON", true);
26 define("MAGPIE_CACHE_DIR", "./".ILIAS_WEB_DIR."/".CLIENT_ID."/magpie_cache");
27 define('MAGPIE_OUTPUT_ENCODING', "UTF-8");
28 define('MAGPIE_CACHE_AGE', 900); // 900 seconds = 15 minutes
29 include_once(MAGPIE_DIR."/rss_fetch.inc");
30 
31 include_once("./Services/Feeds/classes/class.ilExternalFeedItem.php");
32 
41 {
42  protected $items = array();
43 
47  function ilExternalFeed()
48  {
49  // IF YOU ADD THINGS HERE, THEY MAY ALSO BE ADDED TO
50  // SOME OF THE STATIC METHODS
51  $this->_createCacheDirectory();
52  $feed_set = new ilSetting("feed");
53  define('IL_FEED_PROXY_HOST', $feed_set->get("proxy"));
54  define('IL_FEED_PROXY_PORT', $feed_set->get("proxy_port"));
55  }
56 
62  function setUrl($a_url)
63  {
64  $this->url = $a_url;
65  }
66 
72  function getUrl()
73  {
74  return $this->url;
75  }
76 
82  function setError($a_error)
83  {
84  $this->error = $a_error;
85  }
86 
92  function getError()
93  {
94  return $this->error;
95  }
96 
100  static function _createCacheDirectory()
101  {
102  if (!is_dir(ilUtil::getWebspaceDir()."/magpie_cache"))
103  {
104  ilUtil::makeDir(ilUtil::getWebspaceDir()."/magpie_cache");
105  }
106  }
107 
114  static function _checkUrl($a_url)
115  {
116  if (!defined('IL_FEED_PROXY_HOST'))
117  {
119  $feed_set = new ilSetting("feed");
120  define('IL_FEED_PROXY_HOST', $feed_set->get("proxy"));
121  define('IL_FEED_PROXY_PORT', $feed_set->get("proxy_port"));
122  }
123 
124  $feed = @fetch_rss($a_url);
125  if (!$feed)
126  {
127  $error = magpie_error();
128 
129  if ($error != "")
130  {
131  return $error;
132  }
133  else
134  {
135  return "Unknown Error.";
136  }
137  }
138 
139  return true;
140  }
141 
145  function fetch()
146  {
147  if ($this->getUrl() != "")
148  {
149  $this->feed = @fetch_rss($this->getUrl());
150  }
151 
152  if(!$this->feed)
153  {
154  $error = magpie_error();
155  if ($error == "")
156  {
157  $this->setError("Unknown Error.");
158  }
159  else
160  {
161  $this->setError(magpie_error());
162  }
163  return false;
164  }
165 
166  if (is_array($this->feed->items))
167  {
168  foreach($this->feed->items as $item)
169  {
170  $item_obj = new ilExternalFeedItem();
171  $item_obj->setMagpieItem($item);
172  $this->items[] = $item_obj;
173  }
174  }
175  }
176 
180  function checkCacheHit()
181  {
183 
184  $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
185 
186  $cache_status = 0; // response of check_cache
187  $request_headers = array(); // HTTP headers to send with fetch
188  $rss = 0; // parsed RSS object
189  $errormsg = 0; // errors, if any
190 
191  $cache_key = $this->getUrl().MAGPIE_OUTPUT_ENCODING;
192 
193  if (!$cache->ERROR) {
194  // return cache HIT, MISS, or STALE
195  $cache_status = $cache->check_cache( $cache_key);
196  }
197 
198  // if object cached, and cache is fresh, return cached obj
199  if ($cache_status == 'HIT')
200  {
201  return true;
202  }
203 
204  return false;
205  }
206 
210  function getChannelTitle()
211  {
212  return $this->feed->channel["title"];
213  }
214 
219  {
220  return $this->feed->channel["description"];
221  }
222 
226  function getItems()
227  {
228  return $this->items;
229  }
230 
236  static function _determineFeedUrl($a_url)
237  {
238  if (!defined('IL_FEED_PROXY_HOST'))
239  {
240  $feed_set = new ilSetting("feed");
241  define('IL_FEED_PROXY_HOST', $feed_set->get("proxy"));
242  define('IL_FEED_PROXY_PORT', $feed_set->get("proxy_port"));
243  }
244 
245  $res = @fopen($a_url, "r");
246 
247  if (!$res)
248  {
249  return "";
250  }
251 
252  $contents = '';
253  while (!feof($res))
254  {
255  $contents.= fread($res, 8192);
256  }
257  fclose($res);
258 
259  return ilExternalFeed::_getRSSLocation($contents, $a_url);
260  }
261 
266  function _getRSSLocation($html, $location)
267  {
268  if(!$html or !$location){
269  return false;
270  }else{
271  #search through the HTML, save all <link> tags
272  # and store each link's attributes in an associative array
273  preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
274  $links = $matches[1];
275  $final_links = array();
276  $link_count = count($links);
277  for($n=0; $n<$link_count; $n++){
278  $attributes = preg_split('/\s+/s', $links[$n]);
279  foreach($attributes as $attribute){
280  $att = preg_split('/\s*=\s*/s', $attribute, 2);
281  if(isset($att[1])){
282  $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
283  $final_link[strtolower($att[0])] = $att[1];
284  }
285  }
286  $final_links[$n] = $final_link;
287  }
288  #now figure out which one points to the RSS file
289  for($n=0; $n<$link_count; $n++){
290  if(strtolower($final_links[$n]['rel']) == 'alternate'){
291  if(strtolower($final_links[$n]['type']) == 'application/rss+xml'){
292  $href = $final_links[$n]['href'];
293  }
294  if(!$href and strtolower($final_links[$n]['type']) == 'text/xml'){
295  #kludge to make the first version of this still work
296  $href = $final_links[$n]['href'];
297  }
298  if($href){
299  if(strstr($href, "http://") !== false){ #if it's absolute
300  $full_url = $href;
301  }else{ #otherwise, 'absolutize' it
302  $url_parts = parse_url($location);
303  #only made it work for http:// links. Any problem with this?
304  $full_url = "http://$url_parts[host]";
305  if(isset($url_parts['port'])){
306  $full_url .= ":$url_parts[port]";
307  }
308  if($href{0} != '/'){ #it's a relative link on the domain
309  $full_url .= dirname($url_parts['path']);
310  if(substr($full_url, -1) != '/'){
311  #if the last character isn't a '/', add it
312  $full_url .= '/';
313  }
314  }
315  $full_url .= $href;
316  }
317  return $full_url;
318  }
319  }
320  }
321  return false;
322  }
323  }
324 }
325 ?>