Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Adding error trapping to the fetching function and giving error mes…

…sages accordandly.

- Adding Moodle as the web agent!!! :)

PLEASE, edit the new language strings if necessary.
  • Loading branch information...
commit bf46cd22672d238b7885069591fcf9e12f68554f 1 parent b2b8471
willcast authored
Showing with 38 additions and 19 deletions.
  1. +5 −1 lang/en/resource.php
  2. +33 −18 mod/resource/lib.php
6 lang/en/resource.php
View
@@ -12,6 +12,9 @@
$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'] = '<p align=center><font color=#FF0000><b>ERROR</b></font></p><p align=center>An error was found when trying to retrieve the web page.</p>';
+$string['fetchclienterror'] = '<p align=center><font color=#FF0000><b>ERROR</b></font></p><p align=center>An error was found on your web client when trying to retrieve the web page (possibly a wrong URL).</p>';
+$string['fetchservererror'] = '<p align=center><font color=#FF0000><b>ERROR</b></font></p><p align=center>An error was found on the remote server when trying to retrieve the web page (possibly a program error).</p>';
$string['filename'] = 'File name';
$string['fulltext'] = 'Full text';
$string['htmlfragment'] = 'HTML fragment';
@@ -31,7 +34,8 @@
$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
+
<A HREF="$a">File Manager</A>.';
$string['notypechosen'] = 'You need to choose a type. Use your back button to go back and retry.';
$string['resourcetype'] = 'Type of resource';
51 mod/resource/lib.php
View
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.