From: willcast Date: Fri, 28 Nov 2003 03:27:24 +0000 (+0000) Subject: - Adding error trapping to the fetching function and giving error messages accordandly. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=bf46cd22672d238b7885069591fcf9e12f68554f;p=moodle.git - Adding error trapping to the fetching function and giving error messages accordandly. - Adding Moodle as the web agent!!! :) PLEASE, edit the new language strings if necessary. --- diff --git a/lang/en/resource.php b/lang/en/resource.php index 3518c4bb18..0554bc1e2b 100644 --- a/lang/en/resource.php +++ b/lang/en/resource.php @@ -12,6 +12,9 @@ $string['editingaresource'] = 'Editing a resource'; $string['example'] = 'Example'; $string['examplereference'] = 'Tobin, K. & Tippins, D (1993) Constructivism as a Referent for Teaching and Learning. In: K. Tobin (Ed) The Practice of Constructivism in Science Education, pp 3-21, Lawrence-Erlbaum, Hillsdale, NJ.'; $string['exampleurl'] = 'http://www.example.com/somedirectory/somefile.html'; +$string['fetcherror'] = '

ERROR

An error was found when trying to retrieve the web page.

'; +$string['fetchclienterror'] = '

ERROR

An error was found on your web client when trying to retrieve the web page (possibly a wrong URL).

'; +$string['fetchservererror'] = '

ERROR

An error was found on the remote server when trying to retrieve the web page (possibly a program error).

'; $string['filename'] = 'File name'; $string['fulltext'] = 'Full text'; $string['htmlfragment'] = 'HTML fragment'; @@ -31,7 +34,8 @@ $string['newwidth'] = 'Default window width (in pixels)'; $string['newwindow'] = 'New window'; $string['newwindowopen'] = 'Display this resource in a new popup window'; $string['note'] = 'Note'; -$string['notefile'] = 'To upload more files into the course (so they appear in this list) use the +$string['notefile'] = 'To upload more files into the course (so they appear in this list) use the + File Manager.'; $string['notypechosen'] = 'You need to choose a type. Use your back button to go back and retry.'; $string['resourcetype'] = 'Type of resource'; diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 6a5bffb46f..b12a2ded0b 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -173,35 +173,50 @@ function resource_fetch_remote_file ($cm, $url, $headers = "" ) { require_once("$CFG->libdir/snoopy/Snoopy.class.inc"); $client = new Snoopy(); - $client->agent = MAGPIE_USER_AGENT; - $client->read_timeout = MAGPIE_FETCH_TIME_OUT; - $client->use_gzip = MAGPIE_USE_GZIP; + $ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org'; + if ( $CFG->resource_usecache ) { + $ua = $ua . ')'; + } else { + $ua = $ua . '; No cache)'; + } + $client->agent = $ua; + $client->read_timeout = 5; + $client->use_gzip = true; if (is_array($headers) ) { $client->rawheaders = $headers; } @$client->fetch($url); + if ( $client->status >= 200 && $client->status < 300 ) { + $tags = array("A" => "href=", + "IMG" => "src=", + "LINK" => "href=", + "AREA" => "href=", + "FRAME" => "src=", + "IFRAME" => "src=", + "FORM" => "action="); - $tags = array("A" => "href=", - "IMG" => "src=", - "LINK" => "href=", - "AREA" => "href=", - "FRAME" => "src=", - "IFRAME" => "src=", - "FORM" => "action="); - - foreach ($tags as $tag => $key) { - $prefix = "fetch.php?id=$cm->id&url="; - if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") { - $prefix = ""; + foreach ($tags as $tag => $key) { + $prefix = "fetch.php?id=$cm->id&url="; + if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") { + $prefix = ""; + } + $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix); + } + } else { + if ( $client->status >= 400 && $client->status < 500) { + $client->results = get_string("fetchclienterror","resource"); // Client error + } elseif ( $client->status >= 500 && $client->status < 600) { + $client->results = get_string("fetchservererror","resource"); // Server error + } else { + $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error. } - $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix); } return $client; } function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) { - $valid = 0; + $valid = 1; if ( strpos($url,"?") == FALSE ) { $valid = 1; } @@ -264,7 +279,7 @@ function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = // /folder/file.ext Add main root dir // Special case: If finalurl contains a ?, it won't be parsed - $valid = 0; + $valid = 1; if ( strpos($finalurl,"?") == FALSE ) { $valid = 1;