"MDL-15350, fixed boxnet plugin authkey"
[moodle.git] / repository / webdav / repository.class.php
CommitLineData
934ff62c 1<?php
2/**
3 * repository_webdav class
4 *
5 * @author Dongsheng Cai
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 */
9
10require_once($CFG->libdir.'/webdavlib.php');
11
12class repository_webdav extends repository {
13 public function __construct($repositoryid, $context = SITEID, $options = array()) {
14 parent::__construct($repositoryid, $context, $options);
7b58fb41 15 // set up webdav client
934ff62c 16 $this->wd = new webdav_client();
7f728551 17 if (empty($this->webdav_server)) {
18 return;
19 }
7b58fb41 20 if (empty($this->webdav_type)) {
21 $this->webdav_type = '';
22 } else {
23 $this->webdav_type = 'ssl://';
24 }
934ff62c 25 $this->wd->set_server($this->webdav_server);
26 if (empty($this->webdav_port)) {
7b58fb41 27 if (empty($this->webdav_type)) {
28 $this->wd->set_port(80);
29 } else {
30 $this->wd->set_port(443);
31 }
32 $port = '';
934ff62c 33 } else {
34 $this->wd->set_port($this->webdav_port);
7b58fb41 35 $port = ':'.$this->webdav_port;
934ff62c 36 }
7b58fb41 37 $this->webdav_host = $this->webdav_type.$this->webdav_server.$port;
934ff62c 38 if(!empty($this->webdav_user)){
39 $this->wd->set_user($this->webdav_user);
40 }
41 if(!empty($this->webdav_password)) {
42 $this->wd->set_pass($this->webdav_password);
43 }
44 $this->wd->set_protocol(1);
45 $this->wd->set_debug(false);
46 }
47 public function check_login() {
934ff62c 48 return true;
49 }
50 public function get_file($url, $title) {
51 global $CFG;
a53d4f45 52 $path = $this->prepare_file($title);
934ff62c 53 $buffer = '';
7b58fb41 54 if (!$this->wd->open()) {
55 return false;
56 }
934ff62c 57 $this->wd->get($url, $buffer);
a53d4f45 58 $fp = fopen($path, 'wb');
934ff62c 59 fwrite($fp, $buffer);
a53d4f45 60 return $path;
934ff62c 61 }
62 public function global_search() {
63 return false;
64 }
5fa358d1 65 public function get_listing($path='', $page = '') {
934ff62c 66 global $CFG;
67 $list = array();
68 $ret = array();
69 $ret['dynload'] = true;
b2b3fd82 70 $ret['nosearch'] = true;
71 $ret['nologin'] = true;
934ff62c 72 $ret['path'] = array(array('name'=>'Root', 'path'=>0));
7b58fb41 73 $ret['list'] = array();
74 if (!$this->wd->open()) {
75 return $ret;
76 }
934ff62c 77 if (empty($path)) {
934ff62c 78 $path = '/';
7b58fb41 79 $dir = $this->wd->ls($path);
934ff62c 80 } else {
7b58fb41 81 if (empty($this->webdav_type)) {
82 $partern = '#http://'.$this->webdav_host.'/#';
83 } else {
84 $partern = '#http://'.$this->webdav_type.$this->webdav_host.'/#';
85 }
86 $path = '/'.preg_replace($partern, '', $path);
934ff62c 87 $dir = $this->wd->ls($path);
88 }
89 if (!is_array($dir)) {
90 return $ret;
91 }
92 foreach ($dir as $v) {
7b58fb41 93 if (!empty($v['creationdate'])) {
94 $ts = $this->wd->iso8601totime($v['creationdate']);
95 $filedate = userdate($ts);
96 } else {
97 $filedate = '';
98 }
934ff62c 99 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
7b58fb41 100 // a folder
934ff62c 101 if ($path != $v['href']) {
7b58fb41 102 $matches = array();
103 preg_match('#(\w+)$#i', $v['href'], $matches);
104 if (!empty($matches[1])) {
105 $title = urldecode($matches[1]);
106 } else {
107 $title = urldecode($v['href']);
108 }
934ff62c 109 $ret['list'][] = array(
110 'title'=>$title,
7b58fb41 111 'thumbnail'=>$CFG->pixpath.'/f/folder-32.png',
934ff62c 112 'children'=>array(),
113 'date'=>$filedate,
114 'size'=>0,
115 'path'=>$v['href']
116 );
117 }
118 }else{
7b58fb41 119 // a file
934ff62c 120 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
7b58fb41 121 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
934ff62c 122 $ret['list'][] = array(
123 'title'=>$title,
0ef98843 124 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $title),
7b58fb41 125 'size'=>$size,
934ff62c 126 'date'=>$filedate,
127 'source'=>$v['href']
128 );
129 }
130 }
131 return $ret;
132 }
133 public static function get_instance_option_names() {
7b58fb41 134 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
934ff62c 135 }
136
137 public function instance_config_form(&$mform) {
7b58fb41 138 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
139 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
140 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
141
934ff62c 142 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
143 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
144 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
145 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
146 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
147 }
148}