| | 505 | function fetch_rss_timeout ($url, $timeout = false ) { |
| | 506 | // initialize constants |
| | 507 | init(); |
| | 508 | |
| | 509 | if ( !isset($url) ) { |
| | 510 | // error("fetch_rss_timeout called without a url"); |
| | 511 | return false; |
| | 512 | } |
| | 513 | |
| | 514 | // if cache is disabled |
| | 515 | if ( !MAGPIE_CACHE_ON || $timeout === 0) { |
| | 516 | // fetch file, and parse it |
| | 517 | $resp = _fetch_remote_file( $url ); |
| | 518 | if ( is_success( $resp->status ) ) { |
| | 519 | return _response_to_rss( $resp ); |
| | 520 | } |
| | 521 | else { |
| | 522 | // error("Failed to fetch $url and cache is off"); |
| | 523 | return false; |
| | 524 | } |
| | 525 | } |
| | 526 | // else cache is ON |
| | 527 | else { |
| | 528 | // Flow |
| | 529 | // 1. check cache using either MAGPIE_CACHE_AGE, or $timeout if set a |
| | 530 | // 2. if there is a hit, make sure its fresh |
| | 531 | // 3. if cached obj fails freshness check, fetch remote |
| | 532 | // 4. if remote fails, return stale object, or error |
| | 533 | |
| | 534 | $cache = new RSSCache( MAGPIE_CACHE_DIR, ( $timeout === false ? MAGPIE_CACHE_AGE : (int)$timeout ) ); |
| | 535 | |
| | 536 | if (MAGPIE_DEBUG and $cache->ERROR) { |
| | 537 | debug($cache->ERROR, E_USER_WARNING); |
| | 538 | } |
| | 539 | |
| | 540 | |
| | 541 | $cache_status = 0; // response of check_cache |
| | 542 | $request_headers = array(); // HTTP headers to send with fetch |
| | 543 | $rss = 0; // parsed RSS object |
| | 544 | $errormsg = 0; // errors, if any |
| | 545 | |
| | 546 | if (!$cache->ERROR) { |
| | 547 | // return cache HIT, MISS, or STALE |
| | 548 | $cache_status = $cache->check_cache( $url ); |
| | 549 | } |
| | 550 | |
| | 551 | // if object cached, and cache is fresh, return cached obj |
| | 552 | if ( $cache_status == 'HIT' ) { |
| | 553 | $rss = $cache->get( $url ); |
| | 554 | if ( isset($rss) and $rss ) { |
| | 555 | $rss->from_cache = 1; |
| | 556 | if ( MAGPIE_DEBUG > 1) { |
| | 557 | debug("MagpieRSS: Cache HIT", E_USER_NOTICE); |
| | 558 | } |
| | 559 | return $rss; |
| | 560 | } |
| | 561 | } |
| | 562 | |
| | 563 | // else attempt a conditional get |
| | 564 | |
| | 565 | // setup headers |
| | 566 | if ( $cache_status == 'STALE' ) { |
| | 567 | $rss = $cache->get( $url ); |
| | 568 | if ( $rss->etag and $rss->last_modified ) { |
| | 569 | $request_headers['If-None-Match'] = $rss->etag; |
| | 570 | $request_headers['If-Last-Modified'] = $rss->last_modified; |
| | 571 | } |
| | 572 | } |
| | 573 | |
| | 574 | $resp = _fetch_remote_file( $url, $request_headers ); |
| | 575 | |
| | 576 | if (isset($resp) and $resp) { |
| | 577 | if ($resp->status == '304' ) { |
| | 578 | // we have the most current copy |
| | 579 | if ( MAGPIE_DEBUG > 1) { |
| | 580 | debug("Got 304 for $url"); |
| | 581 | } |
| | 582 | // reset cache on 304 (at minutillo insistent prodding) |
| | 583 | $cache->set($url, $rss); |
| | 584 | return $rss; |
| | 585 | } |
| | 586 | elseif ( is_success( $resp->status ) ) { |
| | 587 | $rss = _response_to_rss( $resp ); |
| | 588 | if ( $rss ) { |
| | 589 | if (MAGPIE_DEBUG > 1) { |
| | 590 | debug("Fetch successful"); |
| | 591 | } |
| | 592 | // add object to cache |
| | 593 | $cache->set( $url, $rss ); |
| | 594 | return $rss; |
| | 595 | } |
| | 596 | } |
| | 597 | else { |
| | 598 | $errormsg = "Failed to fetch $url. "; |
| | 599 | if ( $resp->error ) { |
| | 600 | # compensate for Snoopy's annoying habbit to tacking |
| | 601 | # on '\n' |
| | 602 | $http_error = substr($resp->error, 0, -2); |
| | 603 | $errormsg .= "(HTTP Error: $http_error)"; |
| | 604 | } |
| | 605 | else { |
| | 606 | $errormsg .= "(HTTP Response: " . $resp->response_code .')'; |
| | 607 | } |
| | 608 | } |
| | 609 | } |
| | 610 | else { |
| | 611 | $errormsg = "Unable to retrieve RSS file for unknown reasons."; |
| | 612 | } |
| | 613 | |
| | 614 | // else fetch failed |
| | 615 | |
| | 616 | // attempt to return cached object |
| | 617 | if ($rss) { |
| | 618 | if ( MAGPIE_DEBUG ) { |
| | 619 | debug("Returning STALE object for $url"); |
| | 620 | } |
| | 621 | return $rss; |
| | 622 | } |
| | 623 | |
| | 624 | // else we totally failed |
| | 625 | // error( $errormsg ); |
| | 626 | |
| | 627 | return false; |
| | 628 | |
| | 629 | } // end if ( !MAGPIE_CACHE_ON ) { |
| | 630 | } // end fetch_rss_timeout() |
| | 631 | |