Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- External webpage resources now are parsed and automatic links are a…

…dded to the glossary terms... Only if $CFG->filterexternalpages is enabled.
  • Loading branch information...
commit af65e103dfee2e2fe0511e1fa0a2656d9a9fc05e 1 parent ef5b38b
authored November 27, 2003
25  mod/resource/fetch.php
... ...
@@ -0,0 +1,25 @@
  1
+<?php
  2
+
  3
+    require_once("../../config.php");
  4
+    require_once("lib.php");
  5
+    require_once("../../lib/snoopy/Snoopy.class.inc");
  6
+ 
  7
+    require_variable($id);    // Course Module ID
  8
+    require_variable($url);   // url to fetch
  9
+    
  10
+    if (! $cm = get_record("course_modules", "id", $id)) {
  11
+        error("Course Module ID was incorrect");
  12
+    }
  13
+
  14
+    if (! $course = get_record("course", "id", $cm->course)) {
  15
+        error("Course is misconfigured");
  16
+    }
  17
+
  18
+    if (! $resource = get_record("resource", "id", $cm->instance)) {
  19
+        error("Resource ID was incorrect");
  20
+    }
  21
+
  22
+    $content = resource_fetch_remote_file($url);
  23
+    
  24
+    echo format_text($content->results,FORMAT_HTML);
  25
+?>
117  mod/resource/lib.php
@@ -160,6 +160,123 @@ function resource_get_coursemodule_info($coursemodule) {
160 160
 
161 161
    return false;
162 162
 }
  163
+ 
  164
+function resource_fetch_remote_file ($url, $headers = "" ) {
  165
+	// Snoopy is an HTTP client in PHP
  166
+	$client = new Snoopy();
  167
+	$client->agent = MAGPIE_USER_AGENT;
  168
+	$client->read_timeout = MAGPIE_FETCH_TIME_OUT;
  169
+	$client->use_gzip = MAGPIE_USE_GZIP;
  170
+	if (is_array($headers) ) {
  171
+		$client->rawheaders = $headers;
  172
+	}
  173
+	
  174
+	@$client->fetch($url);
  175
+    
  176
+    $tags = array("A"      => "href=",
  177
+                  "IMG"    => "src=",
  178
+                  "LINK"   => "href=",
  179
+                  "AREA"   => "href=",
  180
+                  "FRAME"  => "src=",
  181
+                  "IFRAME" => "src=",
  182
+                  "FORM"   => "action=");
  183
+
  184
+    foreach ($tags as $tag => $key) {
  185
+        $prefix = "fetch.php?id=$cm->id&url=";
  186
+        if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
  187
+            $prefix = "";
  188
+        }
  189
+        $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
  190
+    }
  191
+	return $client;
  192
+}
  193
+
  194
+function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
  195
+    $valid = 0;
  196
+    if ( strpos($url,"?") == FALSE ) {
  197
+        $valid = 1;
  198
+    }
  199
+    if ( $valid ) {
  200
+        $lastpoint = strrpos($url,".");
  201
+        $lastslash = strrpos($url,"/");
  202
+        if ( $lastpoint > $lastslash ) {
  203
+            $root = substr($url,0,$lastslash+1);
  204
+        } else {
  205
+            $root = $url;
  206
+        }
  207
+        if ( $root == "http://" or 
  208
+             $root == "https://") {
  209
+            $root = $url;
  210
+        }
  211
+        if ( substr($root,strlen($root)-1) == '/' ) {
  212
+            $root = substr($root,0,-1);
  213
+        }
  214
+        
  215
+        $mainroot = $root;
  216
+        $lastslash = strrpos($mainroot,"/");
  217
+        while ( $lastslash > 9) {
  218
+            $mainroot = substr($mainroot,0,$lastslash);
  219
+        
  220
+            $lastslash = strrpos($mainroot,"/");
  221
+        }
163 222
 
  223
+        $regex = "/<$tagtoparse (.+?)>/is";    
  224
+        $count = preg_match_all($regex, $text, $hrefs);    
  225
+        for ( $i = 0; $i < $count; $i++) {
  226
+            $tag = $hrefs[1][$i];
  227
+            
  228
+            $poshref = strpos(strtolower($tag),strtolower($keytoparse));
  229
+            $start = $poshref + strlen($keytoparse);
  230
+            $left = substr($tag,0,$start);
  231
+            if ( $tag[$start] == '"' ) {
  232
+                $left .= '"';
  233
+                $start++;
  234
+            }
  235
+            $posspace   = strpos($tag," ", $start+1);
  236
+            $right = "";
  237
+            if ( $posspace != FALSE) {
  238
+                $right = substr($tag, $posspace);
  239
+            }
  240
+            $end = strlen($tag)-1;
  241
+            if ( $tag[$end] == '"' ) {
  242
+                $right = '"' . $right;
  243
+            }
  244
+            $finalurl = substr($tag,$start,$end-$start+$diff);
  245
+            // Here, we could have these possible values for $finalurl:
  246
+            //     file.ext                             Add current root dir
  247
+            //     http://(domain)                      don't care
  248
+            //     http://(domain)/                     don't care
  249
+            //     http://(domain)/folder               don't care
  250
+            //     http://(domain)/folder/              don't care
  251
+            //     http://(domain)/folder/file.ext      don't care
  252
+            //     folder/                              Add current root dir
  253
+            //     folder/file.ext                      Add current root dir
  254
+            //     /folder/                             Add main root dir
  255
+            //     /folder/file.ext                     Add main root dir
  256
+
  257
+            // Special case: If finalurl contains a ?, it won't be parsed
  258
+            $valid = 0;
  259
+
  260
+            if ( strpos($finalurl,"?") == FALSE ) {
  261
+                $valid = 1;
  262
+            }
  263
+            if ( $valid ) {
  264
+                if ( $finalurl[0] == "/" ) {
  265
+                    $finalurl = $mainroot . $finalurl;
  266
+                } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and 
  267
+                           strtolower(substr($finalurl,0,8)) != "https://") {
  268
+                     if ( $finalurl[0] == "/") {
  269
+                        $finalurl = $mainroot . $finalurl;
  270
+                     } else {
  271
+                        $finalurl = "$root/$finalurl";
  272
+                     }
  273
+                }
  274
+    
  275
+                $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
  276
+            }
  277
+        }
  278
+    }
  279
+    return $text;
  280
+}
164 281
 
165 282
 ?>

0 notes on commit af65e10

Please sign in to comment.
Something went wrong with that request. Please try again.