2008-10-21 06:30:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* repository_webdav class
|
|
|
|
*
|
|
|
|
* @author Dongsheng Cai
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once($CFG->libdir.'/webdavlib.php');
|
|
|
|
|
|
|
|
class repository_webdav extends repository {
|
|
|
|
public function __construct($repositoryid, $context = SITEID, $options = array()) {
|
|
|
|
parent::__construct($repositoryid, $context, $options);
|
2009-04-23 09:50:30 +00:00
|
|
|
// set up webdav client
|
2008-10-21 06:30:50 +00:00
|
|
|
$this->wd = new webdav_client();
|
2009-06-25 04:58:58 +00:00
|
|
|
if (empty($this->options['webdav_server'])) {
|
2009-04-03 02:16:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-06-25 04:58:58 +00:00
|
|
|
if (empty($this->options['webdav_type'])) {
|
|
|
|
$this->options['webdav_type'] = '';
|
2009-04-23 09:50:30 +00:00
|
|
|
} else {
|
2009-06-25 04:58:58 +00:00
|
|
|
$this->options['webdav_type'] = 'ssl://';
|
2009-04-23 09:50:30 +00:00
|
|
|
}
|
2009-06-25 04:58:58 +00:00
|
|
|
$this->wd->set_server($this->options['webdav_server']);
|
|
|
|
if (empty($this->options['webdav_port'])) {
|
|
|
|
if (empty($this->options['webdav_type'])) {
|
2009-04-23 09:50:30 +00:00
|
|
|
$this->wd->set_port(80);
|
|
|
|
} else {
|
|
|
|
$this->wd->set_port(443);
|
|
|
|
}
|
|
|
|
$port = '';
|
2008-10-21 06:30:50 +00:00
|
|
|
} else {
|
2009-06-25 04:58:58 +00:00
|
|
|
$this->wd->set_port($this->options['webdav_port']);
|
|
|
|
$port = ':'.$this->options['webdav_port'];
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
2009-06-25 04:58:58 +00:00
|
|
|
$this->webdav_host = $this->options['webdav_type'].$this->options['webdav_server'].$port;
|
|
|
|
if(!empty($this->options['webdav_user'])){
|
|
|
|
$this->wd->set_user($this->options['webdav_user']);
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
2009-06-25 04:58:58 +00:00
|
|
|
if(!empty($this->options['webdav_password'])) {
|
|
|
|
$this->wd->set_pass($this->options['webdav_password']);
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
|
|
|
$this->wd->set_protocol(1);
|
|
|
|
$this->wd->set_debug(false);
|
|
|
|
}
|
|
|
|
public function check_login() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public function get_file($url, $title) {
|
|
|
|
global $CFG;
|
2009-03-16 02:16:50 +00:00
|
|
|
$path = $this->prepare_file($title);
|
2008-10-21 06:30:50 +00:00
|
|
|
$buffer = '';
|
2009-04-23 09:50:30 +00:00
|
|
|
if (!$this->wd->open()) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
$this->wd->get($url, $buffer);
|
2009-03-16 02:16:50 +00:00
|
|
|
$fp = fopen($path, 'wb');
|
2008-10-21 06:30:50 +00:00
|
|
|
fwrite($fp, $buffer);
|
2009-03-16 02:16:50 +00:00
|
|
|
return $path;
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
|
|
|
public function global_search() {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-18 06:52:54 +00:00
|
|
|
public function get_listing($path='', $page = '') {
|
2009-06-30 02:12:16 +00:00
|
|
|
global $CFG, $OUTPUT;
|
2008-10-21 06:30:50 +00:00
|
|
|
$list = array();
|
|
|
|
$ret = array();
|
|
|
|
$ret['dynload'] = true;
|
2008-10-22 02:50:32 +00:00
|
|
|
$ret['nosearch'] = true;
|
|
|
|
$ret['nologin'] = true;
|
2008-10-21 06:30:50 +00:00
|
|
|
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
|
2009-04-23 09:50:30 +00:00
|
|
|
$ret['list'] = array();
|
|
|
|
if (!$this->wd->open()) {
|
|
|
|
return $ret;
|
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
$path = '/';
|
2009-04-23 09:50:30 +00:00
|
|
|
$dir = $this->wd->ls($path);
|
2008-10-21 06:30:50 +00:00
|
|
|
} else {
|
2009-06-25 04:58:58 +00:00
|
|
|
if (empty($this->options['webdav_type'])) {
|
2009-04-23 09:50:30 +00:00
|
|
|
$partern = '#http://'.$this->webdav_host.'/#';
|
|
|
|
} else {
|
|
|
|
$partern = '#http://'.$this->webdav_type.$this->webdav_host.'/#';
|
|
|
|
}
|
|
|
|
$path = '/'.preg_replace($partern, '', $path);
|
2008-10-21 06:30:50 +00:00
|
|
|
$dir = $this->wd->ls($path);
|
|
|
|
}
|
|
|
|
if (!is_array($dir)) {
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
foreach ($dir as $v) {
|
2009-04-23 09:50:30 +00:00
|
|
|
if (!empty($v['creationdate'])) {
|
|
|
|
$ts = $this->wd->iso8601totime($v['creationdate']);
|
|
|
|
$filedate = userdate($ts);
|
|
|
|
} else {
|
|
|
|
$filedate = '';
|
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
|
2009-04-23 09:50:30 +00:00
|
|
|
// a folder
|
2008-10-21 06:30:50 +00:00
|
|
|
if ($path != $v['href']) {
|
2009-04-23 09:50:30 +00:00
|
|
|
$matches = array();
|
|
|
|
preg_match('#(\w+)$#i', $v['href'], $matches);
|
|
|
|
if (!empty($matches[1])) {
|
|
|
|
$title = urldecode($matches[1]);
|
|
|
|
} else {
|
|
|
|
$title = urldecode($v['href']);
|
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
$ret['list'][] = array(
|
|
|
|
'title'=>$title,
|
2009-12-16 21:50:45 +00:00
|
|
|
'thumbnail'=>$OUTPUT->pix_url('f/folder-32'),
|
2008-10-21 06:30:50 +00:00
|
|
|
'children'=>array(),
|
|
|
|
'date'=>$filedate,
|
|
|
|
'size'=>0,
|
|
|
|
'path'=>$v['href']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}else{
|
2009-04-23 09:50:30 +00:00
|
|
|
// a file
|
2008-10-21 06:30:50 +00:00
|
|
|
$title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
|
2009-04-23 09:50:30 +00:00
|
|
|
$size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
|
2008-10-21 06:30:50 +00:00
|
|
|
$ret['list'][] = array(
|
|
|
|
'title'=>$title,
|
2009-12-16 21:50:45 +00:00
|
|
|
'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32)),
|
2009-04-23 09:50:30 +00:00
|
|
|
'size'=>$size,
|
2008-10-21 06:30:50 +00:00
|
|
|
'date'=>$filedate,
|
|
|
|
'source'=>$v['href']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
public static function get_instance_option_names() {
|
2009-04-23 09:50:30 +00:00
|
|
|
return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function instance_config_form(&$mform) {
|
2009-04-23 09:50:30 +00:00
|
|
|
$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');
|
|
|
|
|
2008-10-21 06:30:50 +00:00
|
|
|
$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'));
|
|
|
|
$mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
|
|
|
|
$mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
|
|
|
|
}
|
2009-11-02 06:45:12 +00:00
|
|
|
public function supported_returntypes() {
|
|
|
|
return (FILE_INTERNAL | FILE_EXTERNAL);
|
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|