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 * repository_url class
20 * A subclass of repository, which is used to download a file from a specific url
25 * @copyright 2009 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once(dirname(__FILE__
).'/locallib.php');
32 class repository_url
extends repository
{
35 * @param int $repositoryid
36 * @param object $context
37 * @param array $options
39 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()){
41 parent
::__construct($repositoryid, $context, $options);
42 $this->file_url
= optional_param('file', '', PARAM_RAW
);
45 public function get_file($url, $file = '') {
47 //$CFG->repository_no_delete = true;
48 $path = $this->prepare_file($file);
49 $fp = fopen($path, 'w');
51 $c->download(array(array('url'=>$url, 'file'=>$fp)));
52 return array('path'=>$path, 'url'=>$url);
55 public function check_login() {
56 if (!empty($this->file_url
)) {
65 public function print_login() {
66 $strdownload = get_string('download', 'repository');
67 $strname = get_string('rename', 'repository_url');
68 $strurl = get_string('url', 'repository_url');
69 if ($this->options
['ajax']) {
70 $url = new stdClass();
71 $url->label
= $strurl.': ';
76 $ret['login'] = array($url);
77 $ret['login_btn_label'] = get_string('download', 'repository_url');
83 <td>{$strurl}: </td><td><input name="file" type="text" /></td>
86 <input type="submit" value="{$strdownload}" />
94 * @param string $search
97 public function get_listing($path='', $page='') {
101 $msg = $curl->head($this->file_url
);
102 $info = $curl->get_info();
103 if ($info['http_code'] != 200) {
106 $ret['list'] = array();
107 $ret['nosearch'] = true;
108 $ret['nologin'] = true;
109 $filename = $this->guess_filename($info['url'], $info['content_type']);
110 if (strstr($info['content_type'], 'text/html') ||
empty($info['content_type'])) {
111 // analysis this web page, general file list
112 $ret['list'] = array();
113 $content = $curl->get($info['url']);
114 $this->analyse_page($info['url'], $content, $ret);
116 // download this file
117 $ret['list'][] = array(
119 'source'=>$this->file_url
,
120 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($filename, 32))->out(false)
126 public function analyse_page($baseurl, $content, &$list) {
127 global $CFG, $OUTPUT;
128 $urls = extract_html_urls($content);
129 $images = $urls['img']['src'];
130 $pattern = '#img(.+)src="?\'?([[:alnum:]:?=&@/._+-]+)"?\'?#i';
131 if (!empty($images)) {
132 foreach($images as $url) {
133 $list['list'][] = array(
134 'title'=>$this->guess_filename($url, ''),
135 'source'=>url_to_absolute($baseurl, $url),
136 'thumbnail'=>url_to_absolute($baseurl, $url),
137 'thumbnail_height'=>84,
138 'thumbnail_width'=>84
143 public function guess_filename($url, $type) {
144 $pattern = '#\/([\w_\?\-.]+)$#';
146 preg_match($pattern, $url, $matches);
147 if (empty($matches[1])) {
154 public function supported_returntypes() {
155 return (FILE_INTERNAL | FILE_EXTERNAL
);