| 1 |
<?php
|
| 2 |
|
| 3 |
/* Copyright 2002-2003 Edward Swindelles (ed@readinged.com)
|
| 4 |
*
|
| 5 |
* This program is free software; you can redistribute it and/or modify
|
| 6 |
* it under the terms of the GNU General Public License as published by
|
| 7 |
* the Free Software Foundation; either version 2 of the License, or
|
| 8 |
* (at your option) any later version.
|
| 9 |
*
|
| 10 |
* This program is distributed in the hope that it will be useful,
|
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
* GNU General Public License for more details.
|
| 14 |
*
|
| 15 |
* You should have received a copy of the GNU General Public License
|
| 16 |
* along with this program; if not, write to the Free Software
|
| 17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 18 |
*/
|
| 19 |
|
| 20 |
/* Default settings, you may change them at your whim. See README. */
|
| 21 |
/*
|
| 22 |
$rss_cache_path = '/tmp/persistent/mod-auth-ibmdb2/';
|
| 23 |
*/
|
| 24 |
$rss_cache_path = '';
|
| 25 |
$rss_default_cache_time = 120; // In minutes
|
| 26 |
$rss_debug_mode = true;
|
| 27 |
|
| 28 |
|
| 29 |
/* Private variables, do not change. */
|
| 30 |
$rss_contents = array();
|
| 31 |
$rss_cache_age = 0;
|
| 32 |
$rss_tag = '';
|
| 33 |
$rss_isItem = false;
|
| 34 |
$rss_isChannel = false;
|
| 35 |
$rss_isImage = false;
|
| 36 |
$rss_isTextInput = false;
|
| 37 |
$rss_index = 0;
|
| 38 |
|
| 39 |
function stream_last_modified($url)
|
| 40 |
{
|
| 41 |
if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') > 0)
|
| 42 |
{
|
| 43 |
if (!($fp = @fopen($url, 'r')))
|
| 44 |
return NULL;
|
| 45 |
|
| 46 |
$meta = stream_get_meta_data($fp);
|
| 47 |
for ($j = 0; isset($meta['wrapper_data'][$j]); $j++)
|
| 48 |
{
|
| 49 |
if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified'))
|
| 50 |
{
|
| 51 |
$modtime = substr($meta['wrapper_data'][$j], 15);
|
| 52 |
break;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
fclose($fp);
|
| 56 |
}
|
| 57 |
else
|
| 58 |
{
|
| 59 |
$parts = parse_url($url);
|
| 60 |
$host = $parts['host'];
|
| 61 |
$path = $parts['path'];
|
| 62 |
|
| 63 |
# if (!($fp = @fsockopen($host, 80, &$errno, &$errstr, 5))) {
|
| 64 |
if (!($fp = @fsockopen($host, 80, $errno, $errstr, 5))) {
|
| 65 |
return NULL;
|
| 66 |
}
|
| 67 |
|
| 68 |
$req = "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host:80\r\nAccept: */*\r\n\r\n";
|
| 69 |
fputs($fp, $req);
|
| 70 |
|
| 71 |
while (!feof($fp))
|
| 72 |
{
|
| 73 |
$str = fgets($fp, 4096);
|
| 74 |
if (strstr(strtolower($str), 'last-modified'))
|
| 75 |
{
|
| 76 |
$modtime = substr($str, 15);
|
| 77 |
break;
|
| 78 |
}
|
| 79 |
}
|
| 80 |
fclose($fp);
|
| 81 |
}
|
| 82 |
return isset($modtime) ? strtotime($modtime) : time();
|
| 83 |
}
|
| 84 |
|
| 85 |
function parseRSS($url, $cache_file=NULL, $cache_time=NULL)
|
| 86 |
{
|
| 87 |
global $rss_contents, $rss_default_cache_time, $rss_isTextInput,
|
| 88 |
$rss_cache_path, $rss_cache_age, $rss_tag, $rss_isImage,
|
| 89 |
$rss_isItem, $rss_isChannel, $rss_index, $rss_debug_mode;
|
| 90 |
|
| 91 |
$rss_error = '<br /><strong>Error on line %s of '.__FILE__.'</strong>: %s<br />';
|
| 92 |
|
| 93 |
if (!function_exists('xml_parser_create'))
|
| 94 |
{
|
| 95 |
if ($rss_debug_mode)
|
| 96 |
printf($rss_error, (__LINE__-3), '<a href="http://www.php.net/manual/en/ref.xml.php">PHP\'s XML Extension</a> is not loaded or available.');
|
| 97 |
|
| 98 |
return false;
|
| 99 |
}
|
| 100 |
|
| 101 |
$rss_contents = array();
|
| 102 |
|
| 103 |
if (!is_null($cache_file))
|
| 104 |
{
|
| 105 |
if (!isset($rss_cache_path) || !strlen($rss_cache_path))
|
| 106 |
$rss_cache_path = dirname(__FILE__);
|
| 107 |
|
| 108 |
$cache_file = str_replace('//', '/', $rss_cache_path.'/'.$cache_file);
|
| 109 |
|
| 110 |
if (is_null($cache_time))
|
| 111 |
$cache_time = $rss_default_cache_time;
|
| 112 |
|
| 113 |
$rss_cache_age = file_exists($cache_file) ? ceil((time() - filemtime($cache_file)) / 60) : 0;
|
| 114 |
|
| 115 |
}
|
| 116 |
|
| 117 |
if (is_null($cache_file) ||
|
| 118 |
(!is_null($cache_file) && !file_exists($cache_file)) ||
|
| 119 |
(!is_null($cache_file) && file_exists($cache_file) && $rss_cache_age > $cache_time))
|
| 120 |
{
|
| 121 |
# No cache file yet or if cache is older than rss_cache_age, so we check header if file has changed
|
| 122 |
$remotemodtime = stream_last_modified($url);
|
| 123 |
if (is_null($remotemodtime))
|
| 124 |
{
|
| 125 |
if ($rss_debug_mode)
|
| 126 |
printf($rss_error, (__LINE__-4), 'Could not connect to remote RSS file ('.$url.').');
|
| 127 |
|
| 128 |
# Change mtime of $cache_file with current time
|
| 129 |
# to shift next network test after $cache_time
|
| 130 |
if ($cache = @fopen($cache_file, 'w+')) { fclose($cache); }
|
| 131 |
}
|
| 132 |
else {
|
| 133 |
if ($remotemodtime > (file_exists($cache_file) ? filemtime($cache_file) : 0)) {
|
| 134 |
# Remote file need to be downloaded
|
| 135 |
$rss_tag = '';
|
| 136 |
$rss_isItem = false;
|
| 137 |
$rss_isChannel = false;
|
| 138 |
$rss_index = 0;
|
| 139 |
|
| 140 |
$saxparser = @xml_parser_create();
|
| 141 |
if (!is_resource($saxparser))
|
| 142 |
{
|
| 143 |
if ($rss_debug_mode)
|
| 144 |
printf($rss_error, (__LINE__-4), 'Could not create an instance of <a href="http://www.php.net/manual/en/ref.xml.php">PHP\'s XML parser</a>.');
|
| 145 |
|
| 146 |
return false;
|
| 147 |
}
|
| 148 |
|
| 149 |
xml_parser_set_option($saxparser, XML_OPTION_CASE_FOLDING, false);
|
| 150 |
xml_set_element_handler($saxparser, 'sax_start', 'sax_end');
|
| 151 |
xml_set_character_data_handler($saxparser, 'sax_data');
|
| 152 |
|
| 153 |
if (!($fp = @fopen($url, 'r')))
|
| 154 |
{
|
| 155 |
if ($rss_debug_mode)
|
| 156 |
printf($rss_error, (__LINE__-3), 'Could not connect to remote RSS file ('.$url.').');
|
| 157 |
|
| 158 |
return false;
|
| 159 |
}
|
| 160 |
|
| 161 |
while ($data = fread($fp, 4096))
|
| 162 |
{
|
| 163 |
$parsedOkay = xml_parse($saxparser, $data, feof($fp));
|
| 164 |
|
| 165 |
if (!$parsedOkay && xml_get_error_code($saxparser) != XML_ERROR_NONE)
|
| 166 |
{
|
| 167 |
if ($rss_debug_mode)
|
| 168 |
printf($rss_error, (__LINE__-3), 'File has an XML error (<em>'.xml_error_string(xml_get_error_code($saxparser)).'</em> at line <em>'.xml_get_current_line_number($saxparser).'</em>).');
|
| 169 |
|
| 170 |
return false;
|
| 171 |
}
|
| 172 |
}
|
| 173 |
|
| 174 |
xml_parser_free($saxparser);
|
| 175 |
fclose($fp);
|
| 176 |
|
| 177 |
if (!is_null($cache_file))
|
| 178 |
{
|
| 179 |
if (!($cache = @fopen($cache_file, 'w')))
|
| 180 |
{
|
| 181 |
if ($rss_debug_mode)
|
| 182 |
printf($rss_error, (__LINE__-3), 'Could not write to cache file (<em>'.$cache_file.'</em>). The path may be invalid or you may not have write permissions.');
|
| 183 |
|
| 184 |
return false;
|
| 185 |
}
|
| 186 |
|
| 187 |
fwrite($cache, serialize($rss_contents));
|
| 188 |
fclose($cache);
|
| 189 |
}
|
| 190 |
} else {
|
| 191 |
# Cache file is obsolete but remote file not modified
|
| 192 |
if ($cache = @fopen($cache_file, 'w+')) { fclose($cache); }
|
| 193 |
}
|
| 194 |
}
|
| 195 |
}
|
| 196 |
else {
|
| 197 |
# File cache is up to date
|
| 198 |
# if ($rss_debug_mode)
|
| 199 |
# printf($rss_error, (__LINE__-4), 'File cache is up to date');
|
| 200 |
}
|
| 201 |
|
| 202 |
if (!is_null($cache_file) && file_exists($cache_file)) {
|
| 203 |
# Cache file is here, we read it
|
| 204 |
if (!($fp = @fopen($cache_file, 'r')))
|
| 205 |
{
|
| 206 |
if ($rss_debug_mode)
|
| 207 |
printf($rss_error, (__LINE__-3), 'Could not read contents of cache file (<em>'.$cache_file.'</em>).');
|
| 208 |
|
| 209 |
return false;
|
| 210 |
}
|
| 211 |
|
| 212 |
$rss_contents = unserialize(fread($fp, filesize($cache_file)));
|
| 213 |
fclose($fp);
|
| 214 |
}
|
| 215 |
|
| 216 |
return $rss_contents;
|
| 217 |
}
|
| 218 |
|
| 219 |
function sax_start($parser, $name, $attribs)
|
| 220 |
{
|
| 221 |
global $rss_tag, $rss_isItem, $rss_isChannel, $rss_isImage, $rss_index, $rss_isTextInput;
|
| 222 |
|
| 223 |
$rss_tag = $name = strtolower($name);
|
| 224 |
|
| 225 |
if ($name == 'channel')
|
| 226 |
{
|
| 227 |
$rss_isChannel = true;
|
| 228 |
$rss_isImage = false;
|
| 229 |
$rss_isItem = false;
|
| 230 |
}
|
| 231 |
elseif ($name == 'image')
|
| 232 |
{
|
| 233 |
$rss_isChannel = false;
|
| 234 |
$rss_isImage = true;
|
| 235 |
$rss_isItem = false;
|
| 236 |
}
|
| 237 |
elseif ($name == 'item')
|
| 238 |
{
|
| 239 |
$rss_index++;
|
| 240 |
$rss_isChannel = false;
|
| 241 |
$rss_isImage = false;
|
| 242 |
$rss_isItem = true;
|
| 243 |
}
|
| 244 |
elseif ($name == 'textinput')
|
| 245 |
{
|
| 246 |
$rss_isChannel = false;
|
| 247 |
$rss_isImage = false;
|
| 248 |
$rss_isItem = false;
|
| 249 |
$rss_isTextInput = true;
|
| 250 |
}
|
| 251 |
}
|
| 252 |
|
| 253 |
function sax_end($parser, $name){}
|
| 254 |
|
| 255 |
function sax_data($parser, $data)
|
| 256 |
{
|
| 257 |
global $rss_tag, $rss_isItem, $rss_isChannel, $rss_contents, $rss_isTextInput, $rss_isImage, $rss_index;
|
| 258 |
if ($data != "\n")
|
| 259 |
{
|
| 260 |
if ($rss_isChannel && !$rss_isItem && strlen($data))
|
| 261 |
(!isset($rss_contents['channel'][$rss_tag]) || !strlen($rss_contents['channel'][$rss_tag])) ?
|
| 262 |
$rss_contents['channel'][$rss_tag] = $data :
|
| 263 |
$rss_contents['channel'][$rss_tag].= $data ;
|
| 264 |
elseif ($rss_isItem && strlen($data))
|
| 265 |
(!isset($rss_contents[$rss_index-1][$rss_tag]) || !strlen($rss_contents[$rss_index-1][$rss_tag])) ?
|
| 266 |
$rss_contents[$rss_index-1][$rss_tag] = $data :
|
| 267 |
$rss_contents[$rss_index-1][$rss_tag].= $data ;
|
| 268 |
elseif ($rss_isImage && strlen($data))
|
| 269 |
(!isset($rss_contents['image'][$rss_tag]) || !strlen($rss_contents['image'][$rss_tag])) ?
|
| 270 |
$rss_contents['image'][$rss_tag] = $data :
|
| 271 |
$rss_contents['image'][$rss_tag].= $data ;
|
| 272 |
elseif ($rss_isTextInput && strlen($data))
|
| 273 |
(!isset($rss_contents['textinput'][$rss_tag]) || !strlen($rss_contents['textinput'][$rss_tag])) ?
|
| 274 |
$rss_contents['textinput'][$rss_tag] = $data :
|
| 275 |
$rss_contents['textinput'][$rss_tag].= $data ;
|
| 276 |
}
|
| 277 |
}
|
| 278 |
|
| 279 |
?>
|