[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * This plugin is used to access files by providing an url 20 * 21 * @since Moodle 2.0 22 * @package repository_url 23 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 require_once($CFG->dirroot . '/repository/lib.php'); 27 require_once (__DIR__.'/locallib.php'); 28 29 /** 30 * repository_url class 31 * A subclass of repository, which is used to download a file from a specific url 32 * 33 * @since Moodle 2.0 34 * @package repository_url 35 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org} 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class repository_url extends repository { 39 var $processedfiles = array(); 40 41 /** 42 * @param int $repositoryid 43 * @param object $context 44 * @param array $options 45 */ 46 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()){ 47 global $CFG; 48 parent::__construct($repositoryid, $context, $options); 49 $this->file_url = optional_param('file', '', PARAM_RAW); 50 $this->file_url = $this->escape_url($this->file_url); 51 } 52 53 public function check_login() { 54 if (!empty($this->file_url)) { 55 return true; 56 } else { 57 return false; 58 } 59 } 60 /** 61 * @return mixed 62 */ 63 public function print_login() { 64 $strdownload = get_string('download', 'repository'); 65 $strname = get_string('rename', 'repository_url'); 66 $strurl = get_string('url', 'repository_url'); 67 if ($this->options['ajax']) { 68 $url = new stdClass(); 69 $url->label = $strurl.': '; 70 $url->id = 'fileurl'; 71 $url->type = 'text'; 72 $url->name = 'file'; 73 74 $ret['login'] = array($url); 75 $ret['login_btn_label'] = get_string('download', 'repository_url'); 76 $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js 77 return $ret; 78 } else { 79 echo <<<EOD 80 <table> 81 <tr> 82 <td>{$strurl}: </td><td><input name="file" type="text" /></td> 83 </tr> 84 </table> 85 <input type="submit" value="{$strdownload}" /> 86 EOD; 87 88 } 89 } 90 91 /** 92 * @param mixed $path 93 * @param string $search 94 * @return array 95 */ 96 public function get_listing($path='', $page='') { 97 $ret = array(); 98 $ret['list'] = array(); 99 $ret['nosearch'] = true; 100 $ret['norefresh'] = true; 101 $ret['nologin'] = true; 102 103 $this->file_url = clean_param($this->file_url, PARAM_URL); 104 if (empty($this->file_url)) { 105 throw new repository_exception('validfiletype', 'repository_url'); 106 } 107 108 $this->parse_file(null, $this->file_url, $ret, true); 109 return $ret; 110 } 111 112 /** 113 * Parses one file (either html or css) 114 * 115 * @param string $baseurl (optional) URL of the file where link to this file was found 116 * @param string $relativeurl relative or absolute link to the file 117 * @param array $list 118 * @param bool $mainfile true only for main HTML false and false for all embedded/linked files 119 */ 120 protected function parse_file($baseurl, $relativeurl, &$list, $mainfile = false) { 121 if (preg_match('/([\'"])(.*)\1/', $relativeurl, $matches)) { 122 $relativeurl = $matches[2]; 123 } 124 if (empty($baseurl)) { 125 $url = $relativeurl; 126 } else { 127 $url = htmlspecialchars_decode(url_to_absolute($baseurl, $relativeurl)); 128 } 129 if (in_array($url, $this->processedfiles)) { 130 // avoid endless recursion 131 return; 132 } 133 $this->processedfiles[] = $url; 134 $curl = new curl; 135 $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 3)); 136 $msg = $curl->head($url); 137 $info = $curl->get_info(); 138 if ($info['http_code'] != 200) { 139 if ($mainfile) { 140 $list['error'] = $msg; 141 } 142 } else { 143 $csstoanalyze = ''; 144 if ($mainfile && (strstr($info['content_type'], 'text/html') || empty($info['content_type']))) { 145 // parse as html 146 $htmlcontent = $curl->get($info['url']); 147 $ddoc = new DOMDocument(); 148 @$ddoc->loadHTML($htmlcontent); 149 // extract <img> 150 $tags = $ddoc->getElementsByTagName('img'); 151 foreach ($tags as $tag) { 152 $url = $tag->getAttribute('src'); 153 $this->add_image_to_list($info['url'], $url, $list); 154 } 155 // analyse embedded css (<style>) 156 $tags = $ddoc->getElementsByTagName('style'); 157 foreach ($tags as $tag) { 158 if ($tag->getAttribute('type') == 'text/css') { 159 $csstoanalyze .= $tag->textContent."\n"; 160 } 161 } 162 // analyse links to css (<link type='text/css' href='...'>) 163 $tags = $ddoc->getElementsByTagName('link'); 164 foreach ($tags as $tag) { 165 if ($tag->getAttribute('type') == 'text/css' && strlen($tag->getAttribute('href'))) { 166 $this->parse_file($info['url'], $tag->getAttribute('href'), $list); 167 } 168 } 169 } else if (strstr($info['content_type'], 'css')) { 170 // parse as css 171 $csscontent = $curl->get($info['url']); 172 $csstoanalyze .= $csscontent."\n"; 173 } else if (strstr($info['content_type'], 'image/')) { 174 // download this file 175 $this->add_image_to_list($info['url'], $info['url'], $list); 176 } else { 177 $list['error'] = get_string('validfiletype', 'repository_url'); 178 } 179 180 // parse all found css styles 181 if (strlen($csstoanalyze)) { 182 $urls = extract_css_urls($csstoanalyze); 183 if (!empty($urls['property'])) { 184 foreach ($urls['property'] as $url) { 185 $this->add_image_to_list($info['url'], $url, $list); 186 } 187 } 188 if (!empty($urls['import'])) { 189 foreach ($urls['import'] as $cssurl) { 190 $this->parse_file($info['url'], $cssurl, $list); 191 } 192 } 193 } 194 } 195 } 196 protected function add_image_to_list($baseurl, $url, &$list) { 197 if (empty($list['list'])) { 198 $list['list'] = array(); 199 } 200 $src = url_to_absolute($baseurl, htmlspecialchars_decode($url)); 201 foreach ($list['list'] as $image) { 202 if ($image['source'] == $src) { 203 return; 204 } 205 } 206 $list['list'][] = array( 207 'title'=>$this->guess_filename($url, ''), 208 'source'=>$src, 209 'thumbnail'=>$src, 210 'thumbnail_height'=>84, 211 'thumbnail_width'=>84 212 ); 213 } 214 public function guess_filename($url, $type) { 215 $pattern = '#\/([\w_\?\-.]+)$#'; 216 $matches = null; 217 preg_match($pattern, $url, $matches); 218 if (empty($matches[1])) { 219 return $url; 220 } else { 221 return $matches[1]; 222 } 223 } 224 225 /** 226 * Escapes a url by replacing spaces with %20. 227 * 228 * Note: In general moodle does not automatically escape urls, but for the purposes of making this plugin more user friendly 229 * and make it consistent with some other areas in moodle (such as mod_url), urls will automatically be escaped. 230 * 231 * If moodle_url or PARAM_URL is changed to clean characters that need to be escaped, then this function can be removed 232 * 233 * @param string $url An unescaped url. 234 * @return string The escaped url 235 */ 236 protected function escape_url($url) { 237 $url = str_replace('"', '%22', $url); 238 $url = str_replace('\'', '%27', $url); 239 $url = str_replace(' ', '%20', $url); 240 $url = str_replace('<', '%3C', $url); 241 $url = str_replace('>', '%3E', $url); 242 return $url; 243 } 244 245 public function supported_returntypes() { 246 return (FILE_INTERNAL | FILE_EXTERNAL); 247 } 248 249 /** 250 * Return the source information 251 * 252 * @param stdClass $url 253 * @return string|null 254 */ 255 public function get_file_source_info($url) { 256 return $url; 257 } 258 259 /** 260 * file types supported by url downloader plugin 261 * 262 * @return array 263 */ 264 public function supported_filetypes() { 265 return array('web_image'); 266 } 267 268 /** 269 * Is this repository accessing private data? 270 * 271 * @return bool 272 */ 273 public function contains_private_data() { 274 return false; 275 } 276 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |