]> git.mjollnir.org Git - moodle.git/commitdiff
- External webpage resources now are parsed and automatic links are added to the...
authorwillcast <willcast>
Thu, 27 Nov 2003 19:04:36 +0000 (19:04 +0000)
committerwillcast <willcast>
Thu, 27 Nov 2003 19:04:36 +0000 (19:04 +0000)
mod/resource/fetch.php [new file with mode: 0644]
mod/resource/lib.php

diff --git a/mod/resource/fetch.php b/mod/resource/fetch.php
new file mode 100644 (file)
index 0000000..d200ab0
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+    require_once("../../config.php");
+    require_once("lib.php");
+    require_once("../../lib/snoopy/Snoopy.class.inc");
+    require_variable($id);    // Course Module ID
+    require_variable($url);   // url to fetch
+    
+    if (! $cm = get_record("course_modules", "id", $id)) {
+        error("Course Module ID was incorrect");
+    }
+
+    if (! $course = get_record("course", "id", $cm->course)) {
+        error("Course is misconfigured");
+    }
+
+    if (! $resource = get_record("resource", "id", $cm->instance)) {
+        error("Resource ID was incorrect");
+    }
+
+    $content = resource_fetch_remote_file($url);
+    
+    echo format_text($content->results,FORMAT_HTML);
+?>
\ No newline at end of file
index 85b7345d79b08ca69ea7a136b5bc262b7cde7525..6acb902d1f32e22277f15731611616a607e1cb76 100644 (file)
@@ -160,6 +160,123 @@ function resource_get_coursemodule_info($coursemodule) {
 
    return false;
 }
+function resource_fetch_remote_file ($url, $headers = "" ) {
+       // Snoopy is an HTTP client in PHP
+       $client = new Snoopy();
+       $client->agent = MAGPIE_USER_AGENT;
+       $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
+       $client->use_gzip = MAGPIE_USE_GZIP;
+       if (is_array($headers) ) {
+               $client->rawheaders = $headers;
+       }
+       
+       @$client->fetch($url);
+    
+    $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 = "";
+        }
+        $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
+    }
+       return $client;
+}
+
+function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
+    $valid = 0;
+    if ( strpos($url,"?") == FALSE ) {
+        $valid = 1;
+    }
+    if ( $valid ) {
+        $lastpoint = strrpos($url,".");
+        $lastslash = strrpos($url,"/");
+        if ( $lastpoint > $lastslash ) {
+            $root = substr($url,0,$lastslash+1);
+        } else {
+            $root = $url;
+        }
+        if ( $root == "http://" or 
+             $root == "https://") {
+            $root = $url;
+        }
+        if ( substr($root,strlen($root)-1) == '/' ) {
+            $root = substr($root,0,-1);
+        }
+        
+        $mainroot = $root;
+        $lastslash = strrpos($mainroot,"/");
+        while ( $lastslash > 9) {
+            $mainroot = substr($mainroot,0,$lastslash);
+        
+            $lastslash = strrpos($mainroot,"/");
+        }
 
+        $regex = "/<$tagtoparse (.+?)>/is";    
+        $count = preg_match_all($regex, $text, $hrefs);    
+        for ( $i = 0; $i < $count; $i++) {
+            $tag = $hrefs[1][$i];
+            
+            $poshref = strpos(strtolower($tag),strtolower($keytoparse));
+            $start = $poshref + strlen($keytoparse);
+            $left = substr($tag,0,$start);
+            if ( $tag[$start] == '"' ) {
+                $left .= '"';
+                $start++;
+            }
+            $posspace   = strpos($tag," ", $start+1);
+            $right = "";
+            if ( $posspace != FALSE) {
+                $right = substr($tag, $posspace);
+            }
+            $end = strlen($tag)-1;
+            if ( $tag[$end] == '"' ) {
+                $right = '"' . $right;
+            }
+            $finalurl = substr($tag,$start,$end-$start+$diff);
+            // Here, we could have these possible values for $finalurl:
+            //     file.ext                             Add current root dir
+            //     http://(domain)                      don't care
+            //     http://(domain)/                     don't care
+            //     http://(domain)/folder               don't care
+            //     http://(domain)/folder/              don't care
+            //     http://(domain)/folder/file.ext      don't care
+            //     folder/                              Add current root dir
+            //     folder/file.ext                      Add current root dir
+            //     /folder/                             Add main root dir
+            //     /folder/file.ext                     Add main root dir
+
+            // Special case: If finalurl contains a ?, it won't be parsed
+            $valid = 0;
+
+            if ( strpos($finalurl,"?") == FALSE ) {
+                $valid = 1;
+            }
+            if ( $valid ) {
+                if ( $finalurl[0] == "/" ) {
+                    $finalurl = $mainroot . $finalurl;
+                } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and 
+                           strtolower(substr($finalurl,0,8)) != "https://") {
+                     if ( $finalurl[0] == "/") {
+                        $finalurl = $mainroot . $finalurl;
+                     } else {
+                        $finalurl = "$root/$finalurl";
+                     }
+                }
+    
+                $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
+            }
+        }
+    }
+    return $text;
+}
 
 ?>