3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This plugin is used to access files by providing an url
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
26 require_once($CFG->dirroot
. '/repository/lib.php');
27 require_once(dirname(__FILE__
).'/locallib.php');
30 * repository_url class
31 * A subclass of repository, which is used to download a file from a specific url
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
38 class repository_url
extends repository
{
39 var $processedfiles = array();
42 * @param int $repositoryid
43 * @param object $context
44 * @param array $options
46 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()){
48 parent
::__construct($repositoryid, $context, $options);
49 $this->file_url
= optional_param('file', '', PARAM_RAW
);
52 public function check_login() {
53 if (!empty($this->file_url
)) {
62 public function print_login() {
63 $strdownload = get_string('download', 'repository');
64 $strname = get_string('rename', 'repository_url');
65 $strurl = get_string('url', 'repository_url');
66 if ($this->options
['ajax']) {
67 $url = new stdClass();
68 $url->label
= $strurl.': ';
73 $ret['login'] = array($url);
74 $ret['login_btn_label'] = get_string('download', 'repository_url');
75 $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
81 <td>{$strurl}: </td><td><input name="file" type="text" /></td>
84 <input type="submit" value="{$strdownload}" />
92 * @param string $search
95 public function get_listing($path='', $page='') {
98 $ret['list'] = array();
99 $ret['nosearch'] = true;
100 $ret['norefresh'] = true;
101 $ret['nologin'] = true;
103 $this->parse_file(null, $this->file_url
, $ret, true);
108 * Parses one file (either html or css)
110 * @param string $baseurl (optional) URL of the file where link to this file was found
111 * @param string $relativeurl relative or absolute link to the file
113 * @param bool $mainfile true only for main HTML false and false for all embedded/linked files
115 protected function parse_file($baseurl, $relativeurl, &$list, $mainfile = false) {
116 if (preg_match('/([\'"])(.*)\1/', $relativeurl, $matches)) {
117 $relativeurl = $matches[2];
119 if (empty($baseurl)) {
122 $url = htmlspecialchars_decode(url_to_absolute($baseurl, $relativeurl));
124 if (in_array($url, $this->processedfiles
)) {
125 // avoid endless recursion
128 $this->processedfiles
[] = $url;
130 $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 3));
131 $msg = $curl->head($url);
132 $info = $curl->get_info();
133 if ($info['http_code'] != 200) {
135 $list['error'] = $msg;
139 if ($mainfile && (strstr($info['content_type'], 'text/html') ||
empty($info['content_type']))) {
141 $htmlcontent = $curl->get($info['url']);
142 $ddoc = new DOMDocument();
143 @$ddoc->loadHTML($htmlcontent);
145 $tags = $ddoc->getElementsByTagName('img');
146 foreach ($tags as $tag) {
147 $url = $tag->getAttribute('src');
148 $this->add_image_to_list($info['url'], $url, $list);
150 // analyse embedded css (<style>)
151 $tags = $ddoc->getElementsByTagName('style');
152 foreach ($tags as $tag) {
153 if ($tag->getAttribute('type') == 'text/css') {
154 $csstoanalyze .= $tag->textContent
."\n";
157 // analyse links to css (<link type='text/css' href='...'>)
158 $tags = $ddoc->getElementsByTagName('link');
159 foreach ($tags as $tag) {
160 if ($tag->getAttribute('type') == 'text/css' && strlen($tag->getAttribute('href'))) {
161 $this->parse_file($info['url'], $tag->getAttribute('href'), $list);
164 } else if (strstr($info['content_type'], 'css')) {
166 $csscontent = $curl->get($info['url']);
167 $csstoanalyze .= $csscontent."\n";
168 } else if (strstr($info['content_type'], 'image/')) {
169 // download this file
170 $this->add_image_to_list($info['url'], $info['url'], $list);
173 // parse all found css styles
174 if (strlen($csstoanalyze)) {
175 $urls = extract_css_urls($csstoanalyze);
176 if (!empty($urls['property'])) {
177 foreach ($urls['property'] as $url) {
178 $this->add_image_to_list($info['url'], $url, $list);
181 if (!empty($urls['import'])) {
182 foreach ($urls['import'] as $cssurl) {
183 $this->parse_file($info['url'], $cssurl, $list);
189 protected function add_image_to_list($baseurl, $url, &$list) {
190 if (empty($list['list'])) {
191 $list['list'] = array();
193 $src = url_to_absolute($baseurl, htmlspecialchars_decode($url));
194 foreach ($list['list'] as $image) {
195 if ($image['source'] == $src) {
199 $list['list'][] = array(
200 'title'=>$this->guess_filename($url, ''),
203 'thumbnail_height'=>84,
204 'thumbnail_width'=>84
207 public function guess_filename($url, $type) {
208 $pattern = '#\/([\w_\?\-.]+)$#';
210 preg_match($pattern, $url, $matches);
211 if (empty($matches[1])) {
218 public function supported_returntypes() {
219 return (FILE_INTERNAL | FILE_EXTERNAL
);
223 * Return the source information
225 * @param stdClass $url
226 * @return string|null
228 public function get_file_source_info($url) {
233 * file types supported by url downloader plugin
237 public function supported_filetypes() {
238 return array('web_image');