ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
4require_once './Services/Http/classes/class.ilProxySettings.php';
5
6if (!defined("MAGPIE_DIR")) {
7 define("MAGPIE_DIR", "./Services/Feeds/magpierss/");
8}
9define("MAGPIE_CACHE_ON", true);
10if (defined('ILIAS_DATA_DIR') && defined('CLIENT_ID')) {
11 define("MAGPIE_CACHE_DIR", ILIAS_DATA_DIR . "/" . CLIENT_ID . "/magpie_cache");
12}
13define('MAGPIE_OUTPUT_ENCODING', "UTF-8");
14define('MAGPIE_CACHE_AGE', 900); // 900 seconds = 15 minutes
15define('MAGPIE_DEBUG', false);
16include_once(MAGPIE_DIR . "/rss_fetch.inc");
17
18include_once("./Services/Feeds/classes/class.ilExternalFeedItem.php");
19
28{
29 protected $items = array();
30
34 public function __construct()
35 {
36 // IF YOU ADD THINGS HERE, THEY MAY ALSO BE ADDED TO
37 // SOME OF THE STATIC METHODS
38 $this->_createCacheDirectory();
39
40 if (ilProxySettings::_getInstance()->isActive()) {
41 define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
42 define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
43 } else {
44 define('IL_FEED_PROXY_HOST', "");
45 define('IL_FEED_PROXY_PORT', "");
46 }
47 }
48
54 public function setUrl($a_url)
55 {
56 $this->url = $a_url;
57 }
58
64 public function getUrl()
65 {
66 return $this->url;
67 }
68
74 public function setError($a_error)
75 {
76 $this->error = $a_error;
77 }
78
84 public function getError()
85 {
86 return $this->error;
87 }
88
92 public static function _createCacheDirectory()
93 {
94 if (!is_dir(ilUtil::getDataDir() . "/magpie_cache")) {
95 ilUtil::makeDir(ilUtil::getDataDir() . "/magpie_cache");
96 }
97 }
98
105 public static function _checkUrl($a_url)
106 {
107 if (!defined('IL_FEED_PROXY_HOST')) {
109
110 if (ilProxySettings::_getInstance()->isActive()) {
111 define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
112 define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
113 } else {
114 define('IL_FEED_PROXY_HOST', "");
115 define('IL_FEED_PROXY_PORT', "");
116 }
117 }
118
119 $feed = @fetch_rss($a_url);
120 if (!$feed) {
121 $error = magpie_error();
122
123 if ($error != "") {
124 return $error;
125 } else {
126 return "Unknown Error.";
127 }
128 }
129
130 return true;
131 }
132
136 public function fetch()
137 {
138 if ($this->getUrl() != "") {
139 $this->feed = @fetch_rss($this->getUrl());
140 }
141
142 if (!$this->feed) {
143 $error = magpie_error();
144 if ($error == "") {
145 $this->setError("Unknown Error.");
146 } else {
147 $this->setError(magpie_error());
148 }
149 return false;
150 }
151
152 if (is_array($this->feed->items)) {
153 foreach ($this->feed->items as $item) {
154 $item_obj = new ilExternalFeedItem();
155 $item_obj->setMagpieItem($item);
156 $this->items[] = $item_obj;
157 }
158 }
159 }
160
164 public function checkCacheHit()
165 {
167
168 $cache = new RSSCache(MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE);
169
170 $cache_status = 0; // response of check_cache
171 $request_headers = array(); // HTTP headers to send with fetch
172 $rss = 0; // parsed RSS object
173 $errormsg = 0; // errors, if any
174
175 $cache_key = $this->getUrl() . MAGPIE_OUTPUT_ENCODING;
176
177 if (!$cache->ERROR) {
178 // return cache HIT, MISS, or STALE
179 $cache_status = $cache->check_cache($cache_key);
180 }
181
182 // if object cached, and cache is fresh, return cached obj
183 if ($cache_status == 'HIT') {
184 return true;
185 }
186
187 return false;
188 }
189
193 public function getChannelTitle()
194 {
195 return $this->feed->channel["title"];
196 }
197
201 public function getChannelDescription()
202 {
203 return $this->feed->channel["description"];
204 }
205
209 public function getItems()
210 {
211 return $this->items;
212 }
213
219 public static function _determineFeedUrl($a_url)
220 {
221 if (!defined('IL_FEED_PROXY_HOST')) {
222 if (ilProxySettings::_getInstance()->isActive()) {
223 define('IL_FEED_PROXY_HOST', ilProxySettings::_getInstance()->getHost());
224 define('IL_FEED_PROXY_PORT', ilProxySettings::_getInstance()->getPort());
225 } else {
226 define('IL_FEED_PROXY_HOST', "");
227 define('IL_FEED_PROXY_PORT', "");
228 }
229 }
230
231 $res = @fopen($a_url, "r");
232
233 if (!$res) {
234 return "";
235 }
236
237 $contents = '';
238 while (!feof($res)) {
239 $contents .= fread($res, 8192);
240 }
241 fclose($res);
242
243 return ilExternalFeed::_getRSSLocation($contents, $a_url);
244 }
245
250 public static function _getRSSLocation($html, $location)
251 {
252 if (!$html or !$location) {
253 return false;
254 } else {
255 #search through the HTML, save all <link> tags
256 # and store each link's attributes in an associative array
257 preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
258 $links = $matches[1];
259 $final_links = array();
260 $link_count = count($links);
261 for ($n = 0; $n < $link_count; $n++) {
262 $attributes = preg_split('/\s+/s', $links[$n]);
263 foreach ($attributes as $attribute) {
264 $att = preg_split('/\s*=\s*/s', $attribute, 2);
265 if (isset($att[1])) {
266 $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
267 $final_link[strtolower($att[0])] = $att[1];
268 }
269 }
270 $final_links[$n] = $final_link;
271 }
272 #now figure out which one points to the RSS file
273 for ($n = 0; $n < $link_count; $n++) {
274 if (strtolower($final_links[$n]['rel']) == 'alternate') {
275 if (strtolower($final_links[$n]['type']) == 'application/rss+xml') {
276 $href = $final_links[$n]['href'];
277 }
278 if (!$href and strtolower($final_links[$n]['type']) == 'text/xml') {
279 #kludge to make the first version of this still work
280 $href = $final_links[$n]['href'];
281 }
282 if ($href) {
283 if (strstr($href, "http://") !== false) { #if it's absolute
284 $full_url = $href;
285 } else { #otherwise, 'absolutize' it
286 $url_parts = parse_url($location);
287 #only made it work for http:// links. Any problem with this?
288 $full_url = "http://$url_parts[host]";
289 if (isset($url_parts['port'])) {
290 $full_url .= ":$url_parts[port]";
291 }
292 if ($href[0] != '/') { #it's a relative link on the domain
293 $full_url .= dirname($url_parts['path']);
294 if (substr($full_url, -1) != '/') {
295 #if the last character isn't a '/', add it
296 $full_url .= '/';
297 }
298 }
299 $full_url .= $href;
300 }
301 return $full_url;
302 }
303 }
304 }
305 return false;
306 }
307 }
308}
$n
Definition: RandomTest.php:85
$location
Definition: buildRTE.php:44
An exception for terminatinating execution or to throw for unit testing.
const MAGPIE_OUTPUT_ENCODING(defined('ILIAS_DATA_DIR') &&defined('CLIENT_ID'))
const MAGPIE_CACHE_AGE
error($a_errmsg)
set error message @access public
Wraps $item arrays from magpie.
Handles external Feeds via Magpie library.
setError($a_error)
Set Error.
getChannelTitle()
Get Channel.
getChannelDescription()
Get Description.
setUrl($a_url)
Set Url.
static _getRSSLocation($html, $location)
This one is by Keith Devens , see http://keithdevens.com/weblog/archive/2002/Jun/03/RSSAuto-Discovery...
checkCacheHit()
Check cache hit.
__construct()
Constructor.
fetch()
Fetch the feed.
static _checkUrl($a_url)
Check Url.
static _createCacheDirectory()
Create magpie cache directorry (if not existing)
static _determineFeedUrl($a_url)
Determine Feed Url.
static _getInstance()
Getter for unique instance.
static getDataDir()
get data directory (outside webspace)
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
$attributes
Definition: metadata.php:231
$url
foreach($_POST as $key=> $value) $res