Merge branch 'w33_MDL-28709_m21_h304' of git://github.com/skodak/moodle into MOODLE_2...
[moodle.git] / repository / url / lib.php
blobf763b39208b68bfd72d0fbf2489205140d2e4af4
1 <?php
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.
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/>.
18 /**
19 * repository_url class
20 * A subclass of repository, which is used to download a file from a specific url
22 * @since 2.0
23 * @package repository
24 * @subpackage 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 {
34 /**
35 * @param int $repositoryid
36 * @param object $context
37 * @param array $options
39 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()){
40 global $CFG;
41 parent::__construct($repositoryid, $context, $options);
42 $this->file_url = optional_param('file', '', PARAM_RAW);
45 public function get_file($url, $file = '') {
46 global $CFG;
47 //$CFG->repository_no_delete = true;
48 $path = $this->prepare_file($file);
49 $fp = fopen($path, 'w');
50 $c = new curl;
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)) {
57 return true;
58 } else {
59 return false;
62 /**
63 * @return mixed
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.': ';
72 $url->id = 'fileurl';
73 $url->type = 'text';
74 $url->name = 'file';
76 $ret['login'] = array($url);
77 $ret['login_btn_label'] = get_string('download', 'repository_url');
78 return $ret;
79 } else {
80 echo <<<EOD
81 <table>
82 <tr>
83 <td>{$strurl}: </td><td><input name="file" type="text" /></td>
84 </tr>
85 </table>
86 <input type="submit" value="{$strdownload}" />
87 EOD;
92 /**
93 * @param mixed $path
94 * @param string $search
95 * @return array
97 public function get_listing($path='', $page='') {
98 global $CFG, $OUTPUT;
99 $ret = array();
100 $curl = new curl;
101 $msg = $curl->head($this->file_url);
102 $info = $curl->get_info();
103 if ($info['http_code'] != 200) {
104 $ret['e'] = $msg;
105 } else {
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);
115 } else {
116 // download this file
117 $ret['list'][] = array(
118 'title'=>$filename,
119 'source'=>$this->file_url,
120 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($filename, 32))->out(false)
124 return $ret;
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_\?\-.]+)$#';
145 $matches = null;
146 preg_match($pattern, $url, $matches);
147 if (empty($matches[1])) {
148 return $url;
149 } else {
150 return $matches[1];
154 public function supported_returntypes() {
155 return (FILE_INTERNAL | FILE_EXTERNAL);