From: willcast Date: Thu, 27 Nov 2003 19:04:36 +0000 (+0000) Subject: - External webpage resources now are parsed and automatic links are added to the... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=af65e103dfee2e2fe0511e1fa0a2656d9a9fc05e;p=moodle.git - External webpage resources now are parsed and automatic links are added to the glossary terms... Only if $CFG->filterexternalpages is enabled. --- diff --git a/mod/resource/fetch.php b/mod/resource/fetch.php new file mode 100644 index 0000000000..d200ab0807 --- /dev/null +++ b/mod/resource/fetch.php @@ -0,0 +1,25 @@ +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 diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 85b7345d79..6acb902d1f 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -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; +} ?>