|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 require_once(dirname(__FILE__).'/locallib.php'); 00031 00032 class repository_url extends repository { 00033 00039 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()){ 00040 global $CFG; 00041 parent::__construct($repositoryid, $context, $options); 00042 $this->file_url = optional_param('file', '', PARAM_RAW); 00043 } 00044 00045 public function get_file($url, $file = '') { 00046 global $CFG; 00047 //$CFG->repository_no_delete = true; 00048 $path = $this->prepare_file($file); 00049 $fp = fopen($path, 'w'); 00050 $c = new curl; 00051 $c->download(array(array('url'=>$url, 'file'=>$fp))); 00052 return array('path'=>$path, 'url'=>$url); 00053 } 00054 00055 public function check_login() { 00056 if (!empty($this->file_url)) { 00057 return true; 00058 } else { 00059 return false; 00060 } 00061 } 00065 public function print_login() { 00066 $strdownload = get_string('download', 'repository'); 00067 $strname = get_string('rename', 'repository_url'); 00068 $strurl = get_string('url', 'repository_url'); 00069 if ($this->options['ajax']) { 00070 $url = new stdClass(); 00071 $url->label = $strurl.': '; 00072 $url->id = 'fileurl'; 00073 $url->type = 'text'; 00074 $url->name = 'file'; 00075 00076 $ret['login'] = array($url); 00077 $ret['login_btn_label'] = get_string('download', 'repository_url'); 00078 return $ret; 00079 } else { 00080 echo <<<EOD 00081 <table> 00082 <tr> 00083 <td>{$strurl}: </td><td><input name="file" type="text" /></td> 00084 </tr> 00085 </table> 00086 <input type="submit" value="{$strdownload}" /> 00087 EOD; 00088 00089 } 00090 } 00091 00097 public function get_listing($path='', $page='') { 00098 global $CFG, $OUTPUT; 00099 $ret = array(); 00100 $curl = new curl; 00101 $msg = $curl->head($this->file_url); 00102 $info = $curl->get_info(); 00103 if ($info['http_code'] != 200) { 00104 $ret['e'] = $msg; 00105 } else { 00106 $ret['list'] = array(); 00107 $ret['nosearch'] = true; 00108 $ret['nologin'] = true; 00109 $filename = $this->guess_filename($info['url'], $info['content_type']); 00110 if (strstr($info['content_type'], 'text/html') || empty($info['content_type'])) { 00111 // analysis this web page, general file list 00112 $ret['list'] = array(); 00113 $content = $curl->get($info['url']); 00114 $this->analyse_page($info['url'], $content, $ret); 00115 } else { 00116 // download this file 00117 $ret['list'][] = array( 00118 'title'=>$filename, 00119 'source'=>$this->file_url, 00120 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($filename, 32))->out(false) 00121 ); 00122 } 00123 } 00124 return $ret; 00125 } 00126 public function analyse_page($baseurl, $content, &$list) { 00127 global $CFG, $OUTPUT; 00128 $urls = extract_html_urls($content); 00129 $images = $urls['img']['src']; 00130 $pattern = '#img(.+)src="?\'?([[:alnum:]:?=&@/._+-]+)"?\'?#i'; 00131 if (!empty($images)) { 00132 foreach($images as $url) { 00133 $list['list'][] = array( 00134 'title'=>$this->guess_filename($url, ''), 00135 'source'=>url_to_absolute($baseurl, $url), 00136 'thumbnail'=>url_to_absolute($baseurl, $url), 00137 'thumbnail_height'=>84, 00138 'thumbnail_width'=>84 00139 ); 00140 } 00141 } 00142 } 00143 public function guess_filename($url, $type) { 00144 $pattern = '#\/([\w_\?\-.]+)$#'; 00145 $matches = null; 00146 preg_match($pattern, $url, $matches); 00147 if (empty($matches[1])) { 00148 return $url; 00149 } else { 00150 return $matches[1]; 00151 } 00152 } 00153 00154 public function supported_returntypes() { 00155 return (FILE_INTERNAL | FILE_EXTERNAL); 00156 } 00157 } 00158