"webdav plugin/MDL-16908, fix header check code, reformat code"

This commit is contained in:
dongsheng 2009-04-23 09:50:30 +00:00
parent 577d5f9e32
commit 7b58fb4179
5 changed files with 1610 additions and 1559 deletions

View File

@ -15,6 +15,7 @@ $string['back'] = '< Back';
$string['cacheexpire'] = 'Cache expire';
$string['cachecleared'] = 'Cached files are removed';
$string['cannotinitplugin'] = 'Call plugin_init failed';
$string['cannotdownload'] = 'Cannot download this file';
$string['clicktohide'] = 'Click here to hide';
$string['clicktoshow'] = 'Click here to show';
$string['close'] = 'Close';

View File

@ -1,6 +1,10 @@
<?php
$string['repositoryname'] = 'WebDAV repository';
$string['configplugin'] = 'WebDav configuration';
$string['webdav_type'] = 'WebDav type';
$string['webdav_server'] = 'WebDAV server';
$string['webdav_port'] = 'WebDAV server port';
$string['webdav_user'] = 'WebDAV server user';
$string['webdav_password'] = 'WebDAV server password';
$string['http'] = 'HTTP';
$string['https'] = 'HTTPS';

File diff suppressed because it is too large Load Diff

View File

@ -12,16 +12,29 @@ require_once($CFG->libdir.'/webdavlib.php');
class repository_webdav extends repository {
public function __construct($repositoryid, $context = SITEID, $options = array()) {
parent::__construct($repositoryid, $context, $options);
// set up webdav client
$this->wd = new webdav_client();
if (empty($this->webdav_server)) {
return;
}
if (empty($this->webdav_type)) {
$this->webdav_type = '';
} else {
$this->webdav_type = 'ssl://';
}
$this->wd->set_server($this->webdav_server);
if (empty($this->webdav_port)) {
$this->wd->set_port(80);
if (empty($this->webdav_type)) {
$this->wd->set_port(80);
} else {
$this->wd->set_port(443);
}
$port = '';
} else {
$this->wd->set_port($this->webdav_port);
$port = ':'.$this->webdav_port;
}
$this->webdav_host = $this->webdav_type.$this->webdav_server.$port;
if(!empty($this->webdav_user)){
$this->wd->set_user($this->webdav_user);
}
@ -32,14 +45,15 @@ class repository_webdav extends repository {
$this->wd->set_debug(false);
}
public function check_login() {
global $SESSION;
return true;
}
public function get_file($url, $title) {
global $CFG;
$path = $this->prepare_file($title);
$buffer = '';
$this->wd->open();
if (!$this->wd->open()) {
return false;
}
$this->wd->get($url, $buffer);
$fp = fopen($path, 'wb');
fwrite($fp, $buffer);
@ -53,29 +67,48 @@ class repository_webdav extends repository {
$list = array();
$ret = array();
$ret['dynload'] = true;
$ret['list'] = array();
$ret['nosearch'] = true;
$ret['nologin'] = true;
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
$this->wd->open();
$ret['list'] = array();
if (!$this->wd->open()) {
return $ret;
}
if (empty($path)) {
$dir = $this->wd->ls('/');
$path = '/';
$dir = $this->wd->ls($path);
} else {
if (empty($this->webdav_type)) {
$partern = '#http://'.$this->webdav_host.'/#';
} else {
$partern = '#http://'.$this->webdav_type.$this->webdav_host.'/#';
}
$path = '/'.preg_replace($partern, '', $path);
$dir = $this->wd->ls($path);
}
if (!is_array($dir)) {
return $ret;
}
foreach ($dir as $v) {
$ts = $this->wd->iso8601totime($v['creationdate']);
$filedate = userdate($ts);
if (!empty($v['creationdate'])) {
$ts = $this->wd->iso8601totime($v['creationdate']);
$filedate = userdate($ts);
} else {
$filedate = '';
}
if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
// a folder
if ($path != $v['href']) {
$title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
$matches = array();
preg_match('#(\w+)$#i', $v['href'], $matches);
if (!empty($matches[1])) {
$title = urldecode($matches[1]);
} else {
$title = urldecode($v['href']);
}
$ret['list'][] = array(
'title'=>$title,
'thumbnail'=>$CFG->pixpath.'/f/folder.gif',
'thumbnail'=>$CFG->pixpath.'/f/folder-32.png',
'children'=>array(),
'date'=>$filedate,
'size'=>0,
@ -83,11 +116,13 @@ class repository_webdav extends repository {
);
}
}else{
// a file
$title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
$size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
$ret['list'][] = array(
'title'=>$title,
'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $title),
'size'=>$v['getcontentlength'],
'size'=>$size,
'date'=>$filedate,
'source'=>$v['href']
);
@ -96,10 +131,14 @@ class repository_webdav extends repository {
return $ret;
}
public static function get_instance_option_names() {
return array('webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
}
public function instance_config_form(&$mform) {
$choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
$mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
$mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
$mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
$mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
$mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));

View File

@ -176,11 +176,15 @@ EOD;
}
break;
case 'download':
$path = $repo->get_file($file, $title);
if (empty($itemid)) {
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
}
try {
$path = $repo->get_file($file, $title);
if ($path === false) {
$err->e = get_string('cannotdownload', 'repository');
die(json_encode($err));
}
if (empty($itemid)) {
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
}
if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
echo json_encode(array('client_id'=>$client_id, 'url'=>$path, 'id'=>$path, 'file'=>$path));
} else {