Changeset 1164 for trunk/rss/extlib/rss_fetch.inc
- Timestamp:
- 01/09/06 12:35:00 (3 years ago)
- Files:
-
- 1 modified
-
trunk/rss/extlib/rss_fetch.inc (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/rss/extlib/rss_fetch.inc
r1155 r1164 16 16 * 17 17 */ 18 18 19 19 // Setup MAGPIE_DIR for use on hosts that don't include 20 20 // the current path in include_path. … … 51 51 define ('MAGPIE_FEED_ORIGIN_NOT_FETCHED', 64); 52 52 53 /* 53 /* 54 54 * CONSTANTS - redefine these in your script to change the 55 55 * behaviour of fetch_rss() currently, most options effect the cache … … 83 83 Input: url of RSS file 84 84 Output: parsed RSS object (see rss_parse.inc) 85 85 86 86 NOTES ON CACHEING: 87 87 If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache. … … 104 104 // initialize constants 105 105 init(); 106 106 107 107 if ( !isset($url) ) { 108 108 error("fetch_rss called without a url"); 109 109 return false; 110 110 } 111 111 112 112 // if cache is disabled 113 113 if ( !MAGPIE_CACHE_ON ) { … … 115 115 $resp = _fetch_remote_file( $url ); 116 116 if ( is_success( $resp->status ) ) { 117 return _response_to_rss( $resp, 118 MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 119 } 120 else { 117 return _response_to_rss( $resp, 118 MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 119 } else { 121 120 error("Failed to fetch $url and cache is off"); 122 121 return false; 123 122 } 124 } 123 } 125 124 // else cache is ON 126 125 else { … … 130 129 // 3. if cached obj fails freshness check, fetch remote 131 130 // 4. if remote fails, return stale object, or error 132 133 // sameer: changed to use db131 132 // sameer: changed to use db 134 133 $cache = new RSSdbCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE ); 135 134 136 135 if (MAGPIE_DEBUG and $cache->ERROR) { 137 136 debug($cache->ERROR, E_USER_WARNING); 138 137 } 139 140 138 139 141 140 $cache_status = 0; // response of check_cache 142 141 $request_headers = array(); // HTTP headers to send with fetch 143 142 $rss = 0; // parsed RSS object 144 143 $errormsg = 0; // errors, if any 145 144 146 145 // store parsed XML by desired output encoding 147 146 // as character munging happens at parse time 148 147 //$cache_key = $url . MAGPIE_OUTPUT_ENCODING; 149 // sameer: removed the encoding from the key148 // sameer: removed the encoding from the key 150 149 $cache_key = $url; 151 150 152 151 if (!$cache->ERROR) { 153 152 // return cache HIT, MISS, or STALE 154 $cache_status = $cache->check_cache( $cache_key );155 } 156 153 $cache_status = $cache->check_cache( $cache_key ); 154 } 155 157 156 // if object cached, and cache is fresh, return cached obj 158 157 if ( $cache_status == 'HIT' ) { 159 $rss = $cache->get( $cache_key ); 158 $rss = $cache->get 159 ( $cache_key ); 160 160 if ( isset($rss) and $rss ) { 161 161 $rss->from_cache = 1; 162 162 if ( MAGPIE_DEBUG > 1) { 163 debug("MagpieRSS: Cache HIT", E_USER_NOTICE);164 }165 $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE | MAGPIE_FEED_ORIGIN_NOT_FETCHED;163 debug("MagpieRSS: Cache HIT", E_USER_NOTICE); 164 } 165 $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE | MAGPIE_FEED_ORIGIN_NOT_FETCHED; 166 166 return $rss; 167 167 } 168 168 } 169 169 170 170 // else attempt a conditional get 171 171 172 172 // setup headers 173 173 if ( $cache_status == 'STALE' ) { 174 $rss = $cache->get( $cache_key ); 175 if ( isset($rss->etag) && $rss->etag and 176 isset($rss->last_modified) && $rss->last_modified ) { 174 $rss = $cache->get 175 ( $cache_key ); 176 if ( isset($rss->etag) && $rss->etag and 177 isset($rss->last_modified) && $rss->last_modified ) { 177 178 $request_headers['If-None-Match'] = $rss->etag; 178 179 $request_headers['If-Last-Modified'] = $rss->last_modified; 179 } 180 } 181 180 } 181 } 182 182 183 $resp = _fetch_remote_file( $url, $request_headers ); 183 184 184 185 if (isset($resp) and $resp) { 185 186 if ($resp->status == '304' ) { … … 190 191 // reset cache on 304 (at minutillo insistent prodding) 191 192 $rss->rss_origin = MAGPIE_FEED_ORIGIN_CACHE | MAGPIE_FEED_ORIGIN_HTTP_304; 192 $cache->set($cache_key, $rss); 193 $cache->set 194 ($cache_key, $rss); 193 195 return $rss; 194 196 } 195 197 elseif ( is_success( $resp->status ) ) { 196 $rss = _response_to_rss( $resp, MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 );198 $rss = _response_to_rss( $resp, MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 197 199 if ( $rss ) { 198 200 if (MAGPIE_DEBUG > 1) { … … 200 202 } 201 203 // add object to cache 202 $cache->set( $cache_key, $rss ); 204 $cache->set 205 ( $cache_key, $rss ); 203 206 return $rss; 204 207 } … … 210 213 } 211 214 elseif ( $resp->error ) { 212 # compensate for Snoopy's annoying habbit to tacking215 # compensate for Snoopy's annoying habbit to tacking 213 216 # on '\n' 214 217 $http_error = substr($resp->error, 0, -2); … … 219 222 } 220 223 } 221 } 222 else { 224 } else { 223 225 $errormsg = "Unable to retrieve RSS file for unknown reasons."; 224 226 } 225 227 226 228 // else fetch failed 227 229 228 230 // attempt to return cached object 229 231 if ($rss) { … … 231 233 debug("Returning STALE object for $url"); 232 234 } 233 $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE;234 if (is_object($resp) && isset($resp->status)) {235 switch ($resp->status) {236 case '404':237 $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_404;238 break;239 default:240 $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT;241 break; 242 }243 } else {244 $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT;245 }235 $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE; 236 if (is_object($resp) && isset($resp->status)) { 237 switch ($resp->status) { 238 case '404': 239 $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_404; 240 break; 241 default: 242 $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT; 243 break; 244 } 245 } else { 246 $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT; 247 } 246 248 return $rss; 247 249 } 248 250 249 251 // else we totally failed 250 if ($errormsg) {251 global $MAGPIE_ERROR; 252 $MAGPIE_ERROR = $errormsg; 253 }254 //error( $errormsg ); 255 252 if ($errormsg) { 253 global $MAGPIE_ERROR; 254 $MAGPIE_ERROR = $errormsg; 255 } 256 //error( $errormsg ); 257 256 258 return false; 257 259 258 260 } // end if ( !MAGPIE_CACHE_ON ) { 259 261 } // end fetch_rss() … … 265 267 266 268 function error ($errormsg, $lvl=E_USER_WARNING) { 267 global $MAGPIE_ERROR;268 269 // append PHP's error message if track_errors enabled270 if ( isset($php_errormsg) ) {271 $errormsg .= " ($php_errormsg)";272 }273 if ( $errormsg ) {274 $errormsg = "MagpieRSS: $errormsg";275 $MAGPIE_ERROR = $errormsg;276 trigger_error( $errormsg, $lvl);277 }269 global $MAGPIE_ERROR; 270 271 // append PHP's error message if track_errors enabled 272 if ( isset($php_errormsg) ) { 273 $errormsg .= " ($php_errormsg)"; 274 } 275 if ( $errormsg ) { 276 $errormsg = "MagpieRSS: $errormsg"; 277 $MAGPIE_ERROR = $errormsg; 278 trigger_error( $errormsg, $lvl); 279 } 278 280 } 279 281 … … 281 283 trigger_error("MagpieRSS [debug] $debugmsg", $lvl); 282 284 } 283 285 284 286 /*=======================================================================*\ 285 287 Function: magpie_error … … 288 290 function magpie_error ($errormsg="") { 289 291 global $MAGPIE_ERROR; 290 291 if ( isset($errormsg) and $errormsg ) { 292 293 if ( isset($errormsg) and $errormsg ) { 292 294 $MAGPIE_ERROR = $errormsg; 293 295 } 294 295 return $MAGPIE_ERROR; 296 297 return $MAGPIE_ERROR; 296 298 } 297 299 … … 312 314 $client->rawheaders = $headers; 313 315 } 314 316 315 317 @$client->fetch($url); 316 318 return $client; … … 326 328 function _response_to_rss ($resp, $rss_origin = 0) { 327 329 $rss = new MagpieRSS( $resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING ); 328 329 // if RSS parsed successfully 330 331 // if RSS parsed successfully 330 332 if ( $rss and !$rss->ERROR) { 331 332 // find Etag, and Last-Modified333 foreach($resp->headers as $h) {333 334 // find Etag, and Last-Modified 335 foreach($resp->headers as $h) { 334 336 // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1" 335 337 if (strpos($h, ": ")) { 336 338 list($field, $val) = explode(": ", $h, 2); 337 } 338 else { 339 } else { 339 340 $field = $h; 340 341 $val = ""; 341 342 } 342 343 343 344 if ( $field == 'ETag' ) { 344 345 $rss->etag = $val; 345 346 } 346 347 347 348 if ( $field == 'Last-Modified' ) { 348 349 $rss->last_modified = $val; 349 350 } 350 351 } 351 352 $rss -> rss_origin = $rss_origin;353 return $rss; 352 353 $rss -> rss_origin = $rss_origin; 354 return $rss; 354 355 } // else construct error message 355 356 else { 356 357 $errormsg = "Failed to parse RSS file."; 357 358 358 359 if ($rss) { 359 360 $errormsg .= " (" . $rss->ERROR . ")"; 360 361 } 361 362 error($errormsg); 362 363 363 364 return false; 364 365 } // end if ($rss and !$rss->error) … … 373 374 if ( defined('MAGPIE_INITALIZED') ) { 374 375 return; 375 } 376 else { 376 } else { 377 377 define('MAGPIE_INITALIZED', true); 378 378 } 379 379 380 380 if ( !defined('MAGPIE_CACHE_ON') ) { 381 381 define('MAGPIE_CACHE_ON', true); … … 397 397 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); 398 398 } 399 399 400 400 if ( !defined('MAGPIE_INPUT_ENCODING') ) { 401 401 define('MAGPIE_INPUT_ENCODING', null); 402 402 } 403 403 404 404 if ( !defined('MAGPIE_DETECT_ENCODING') ) { 405 405 define('MAGPIE_DETECT_ENCODING', true); 406 406 } 407 407 408 408 if ( !defined('MAGPIE_DEBUG') ) { 409 409 define('MAGPIE_DEBUG', 0); 410 410 } 411 411 412 412 if ( !defined('MAGPIE_USER_AGENT') ) { 413 413 $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net'; 414 414 415 415 if ( MAGPIE_CACHE_ON ) { 416 416 $ua = $ua . ')'; 417 } 418 else { 417 } else { 419 418 $ua = $ua . '; No cache)'; 420 419 } 421 420 422 421 define('MAGPIE_USER_AGENT', $ua); 423 422 } 424 423 425 424 if ( !defined('MAGPIE_FETCH_TIME_OUT') ) { 426 425 define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout 427 426 } 428 427 429 428 // use gzip encoding to fetch rss files if supported? 430 429 if ( !defined('MAGPIE_USE_GZIP') ) { 431 define('MAGPIE_USE_GZIP', true); 430 define('MAGPIE_USE_GZIP', true); 432 431 } 433 432 } … … 442 441 443 442 All of them take an HTTP status code as input, and return true or false 444 443 445 444 All this code is adapted from LWP's HTTP::Status. 446 445 \*=======================================================================*/ … … 451 450 Purpose: return true if Informational status code 452 451 \*=======================================================================*/ 453 function is_info ($sc) { 454 return $sc >= 100 && $sc < 200; 452 function is_info ($sc) { 453 return $sc >= 100 && $sc < 200; 455 454 } 456 455 … … 459 458 Purpose: return true if Successful status code 460 459 \*=======================================================================*/ 461 function is_success ($sc) { 462 return $sc >= 200 && $sc < 300; 460 function is_success ($sc) { 461 return $sc >= 200 && $sc < 300; 463 462 } 464 463 … … 467 466 Purpose: return true if Redirection status code 468 467 \*=======================================================================*/ 469 function is_redirect ($sc) { 470 return $sc >= 300 && $sc < 400; 468 function is_redirect ($sc) { 469 return $sc >= 300 && $sc < 400; 471 470 } 472 471 … … 475 474 Purpose: return true if Error status code 476 475 \*=======================================================================*/ 477 function is_error ($sc) { 478 return $sc >= 400 && $sc < 600; 476 function is_error ($sc) { 477 return $sc >= 400 && $sc < 600; 479 478 } 480 479 … … 483 482 Purpose: return true if Error status code, and its a client error 484 483 \*=======================================================================*/ 485 function is_client_error ($sc) { 486 return $sc >= 400 && $sc < 500; 484 function is_client_error ($sc) { 485 return $sc >= 400 && $sc < 500; 487 486 } 488 487 … … 491 490 Purpose: return true if Error status code, and its a server error 492 491 \*=======================================================================*/ 493 function is_server_error ($sc) { 494 return $sc >= 500 && $sc < 600; 492 function is_server_error ($sc) { 493 return $sc >= 500 && $sc < 600; 495 494 } 496 495
