From c7ac239b80113062189a32fe01c14618b031c30c Mon Sep 17 00:00:00 2001 From: dhawes Date: Wed, 19 Jan 2005 23:51:27 +0000 Subject: [PATCH] updated rss_client functions to return any errors encountered along the way (assuming that the feed could not be loaded) in an object as ->ERROR. This matches the object that is returned, it would have a ->ERROR string if there was a problem as well. --- rss/rsslib.php | 118 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 35 deletions(-) diff --git a/rss/rsslib.php b/rss/rsslib.php index 27da3bf6d3..a221921e94 100644 --- a/rss/rsslib.php +++ b/rss/rsslib.php @@ -347,7 +347,9 @@ function rss_get_feed($rssid, $url, $type) { global $CFG; $writetofile = false; - $urlfailurestring = 'Failed to open remote feed at: ' . $url .'
allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to http://us2.php.net/filesystem'; + $errorstring = ''; + + $urlfailurestring = '

Failed to open remote news feed at: ' . $url .'

To troubleshoot this problem please ensure that the setting allow_url_fopen is On in the php.ini. For more details on why this setting is needed for this file wrapper call to work please refer to http://us2.php.net/filesystem'; $filefailurestring = 'Could not open the file located at: '; $secs = $CFG->block_rss_timeout * 60; @@ -367,74 +369,100 @@ function rss_get_feed($rssid, $url, $type) { if (file_exists($file)) { //check age of cache file // echo "file exists $file"; //debug - if ($CFG->debug){ - $data = stat($file); - } else { - $data = @stat($file); - } + + //get file information capturing any error information + ob_start(); + $data = stat($file); + $errorstring .= ob_get_contents(); + ob_end_clean(); + $now = time(); + //Note: there would be a problem here reading data[10] if the above stat() call failed if (($now - $data[10]) > $secs) { // The cached file has expired. Attempt to read fresh from source $xml = load_feed_from_url($url); - if ($xml) { + if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { //success $writetofile = true; } else { // Failed to load remote feed. Since the file exists attempt to read from cache if ($CFG->debug) { - print $urlfailurestring; + if (isset($xml) && isset($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring .'
'; + } + $errorstring = $urlfailurestring .'

'. $errorstring .'
'; } $xml = load_feed_from_file($file); - if (!$xml) { + if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { // Failed to load from cache as well! if ($CFG->debug) { - print $filefailurestring . $file; - return; + if (isset($xml) && isset($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring; + } + $errorstring = $filefailurestring . $file .'

'. $errorstring .'
'; + $err->ERROR = $errorstring .'
'; + return $err; } } } } else { // Cached file has not expired. Attempt to read from cached file. $xml = load_feed_from_file($file); - if (!$xml) { + if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { // Failed to load from cache, attempt to read from source if ($CFG->debug) { - print $filefailurestring . $file; + if (isset($xml) && isset($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring .'
'; + } + $errorstring = $filefailurestring . $file .'

'. $errorstring .'
'; } $xml = load_feed_from_url($url); - if ($xml) { + if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { // success $writetofile = true; } else { // Failed to read from source as well! if ($CFG->debug) { - print $urlfailurestring; + if (!empty($xml) && !empty($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring; + } + $errorstring = $urlfailurestring .'

'. $errorstring .'
'; + $err->ERROR = $errorstring .'
'; + return $err; } return; } } } } else { - // No cached fil at all, read from source + // No cached file at all, read from source $xml = load_feed_from_url($url); - if ($xml) { + if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { //success $writetofile = true; } else { // Failed to read from source url! if ($CFG->debug) { - print $urlfailurestring; + if (!empty($xml) && !empty($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring .'
'; + } + $errorstring = $urlfailurestring .'

'. $errorstring .'
'; + $err->ERROR = $errorstring .'
'; + return $err; } return; } } + // echo 'DEBUG: feed\'s raw xml was loaded:
';//debug //print_object($xml); //debug - if ($CFG->debug){ - $xmlstr = implode(' ', $xml); - } else { - $xmlstr = @implode(' ', $xml); - } + + //implode xml file. in some cases this operation may fail, capture failure info to errorstring. + ob_start(); + $xmlstr = implode(' ', $xml->xml); + $errorstring .= ob_get_contents(); + ob_end_clean(); + //print_object($xmlstr); if ( $writetofile && !empty($xmlstr) ) { //write file to cache // jlb: adding file:/ to the start of the file name fixed @@ -443,6 +471,11 @@ function rss_get_feed($rssid, $url, $type) { file_put_contents($file, $xmlstr); } + if (empty($xmlstr) && !empty($errorstring)) { + $err->ERROR = 'XML file failed to implode correctly:

'. $errorstring .'
'; + return $err; + } + if ($type == 'A') { //note: Atom is being modified by a working group //http://www.mnot.net/drafts/draft-nottingham-atom-format-02.html @@ -464,32 +497,47 @@ function rss_get_feed($rssid, $url, $type) { /** * @param string $file The path to the cached feed to load + * @return stdObject Object with ->xml string value and ->ERROR string value if applicable. */ function load_feed_from_file($file) { global $CFG; + $errorstring = ''; // echo "read from cache"; //debug //read in from cache - if ($CFG->debug){ - $xml = file($file); - } else { - $xml = @file($file); + ob_start(); + $xml = file($file); + $errorstring .= ob_get_contents(); + ob_end_clean(); + + $returnobj->xml = $xml; + if (!empty($errorstring)){ + $returnobj->ERROR = 'XML file failed to load:

'. $errorstring .'
'; } - return $xml; + return $returnobj; } /** * @param string $url The url of the remote news feed to load + * @return stdObject Object with ->xml string value and ->ERROR string value if applicable. */ function load_feed_from_url($url) { global $CFG; -// echo "read from original"; //debug - //read from source - if ($CFG->debug){ - $xml = file($url); - } else { - $xml = @file($url); +// echo "read from source url"; //debug + //read from source url + $errorstring = ''; +// echo "read from cache"; //debug + //read in from cache + ob_start(); + $xml = file($url); + $errorstring .= ob_get_contents(); + ob_end_clean(); + + $returnobj->xml = $xml; + if (!empty($errorstring)){ + $returnobj->ERROR = 'XML url failed to load:
'. $errorstring; } - return $xml; + return $returnobj; + } /** -- 2.39.5