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