moodle/repository/url/repository.class.php

142 lines
4.3 KiB
PHP
Executable File

<?php
/**
* repository_url class
* A subclass of repository, which is used to download a file from a specific url
*
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class repository_url extends repository {
/**
* @param int $repositoryid
* @param object $context
* @param array $options
*/
public function __construct($repositoryid, $context = SITEID, $options = array()){
global $CFG;
parent::__construct($repositoryid, $context, $options);
if (!empty($options['client_id'])) {
// will be used to construct download form
$this->client_id = $options['client_id'];
}
$this->file_url = optional_param('file', '', PARAM_RAW);
}
public function get_file($url, $file = '') {
global $CFG;
//$CFG->repository_no_delete = true;
$path = $this->prepare_file($file);
$fp = fopen($path, 'w');
$c = new curl;
$c->download(array(array('url'=>$url, 'file'=>$fp)));
return $path;
}
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-'.$this->client_id;
$url->type = 'text';
$url->name = 'file';
$ret['login'] = array($url);
$ret['login_btn_label'] = get_string('download', 'repository_url');
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;
set_time_limit(0);
$ret = array();
$curl = new curl;
$msg = $curl->head($this->file_url);
$info = $curl->get_info();
if ($info['http_code'] != 200) {
$ret['e'] = $msg;
} else {
$ret['list'] = array();
$ret['nosearch'] = true;
$ret['nologin'] = true;
$filename = $this->guess_filename($info['url'], $info['content_type']);
if (strstr($info['content_type'], 'text/html') || empty($info['content_type'])) {
// analysis this web page, general file list
$ret['list'] = array();
$a = $curl->get($info['url']);
$this->analyse_page($a, $ret);
} else {
// download this file
$ret['list'][] = array(
'title'=>$filename,
'source'=>$this->file_url,
'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $filename)
);
}
}
return $ret;
}
public function analyse_page($content, &$list) {
global $CFG;
$pattern = '#src="?\'?([[:alnum:]:?=&@/._+-]+)"?\'?#i';
$matches = null;
preg_match_all($pattern, $content, $matches);
$matches = array_unique($matches[1]);
if (!empty($matches)) {
foreach($matches as $url) {
$list['list'][] = array(
'title'=>$this->guess_filename($url, ''),
// XXX: need to convert relative url to absolute url
'source'=>$url,
'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $url)
);
}
}
}
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 get_name(){
return get_string('repositoryname', 'repository_url');;
}
}
?>