weekly release 2.3.3+
[moodle.git] / repository / webdav / lib.php
CommitLineData
934ff62c 1<?php
10d53fd3
DC
2
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.
14//
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/>.
17
934ff62c 18/**
67233725 19 * This plugin is used to access webdav files
934ff62c 20 *
10d53fd3 21 * @since 2.0
67233725
DC
22 * @package repository_webdav
23 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
934ff62c 25 */
67233725 26require_once($CFG->dirroot . '/repository/lib.php');
934ff62c 27require_once($CFG->libdir.'/webdavlib.php');
28
67233725
DC
29/**
30 * repository_webdav class
31 *
32 * @since 2.0
33 * @package repository_webdav
34 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
934ff62c 37class repository_webdav extends repository {
447c7a19 38 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
934ff62c 39 parent::__construct($repositoryid, $context, $options);
7b58fb41 40 // set up webdav client
ecf4785c 41 if (empty($this->options['webdav_server'])) {
7f728551 42 return;
43 }
3ced70fa
DC
44 if ($this->options['webdav_auth'] == 'none') {
45 $this->options['webdav_auth'] = false;
46 }
ecf4785c 47 if (empty($this->options['webdav_type'])) {
3942626d 48 $this->webdav_type = '';
7b58fb41 49 } else {
3942626d 50 $this->webdav_type = 'ssl://';
7b58fb41 51 }
ecf4785c 52 if (empty($this->options['webdav_port'])) {
85a9d23d 53 $port = '';
3942626d 54 if (empty($this->webdav_type)) {
85a9d23d 55 $this->webdav_port = 80;
7b58fb41 56 } else {
85a9d23d
FM
57 $this->webdav_port = 443;
58 $port = ':443';
7b58fb41 59 }
934ff62c 60 } else {
85a9d23d
FM
61 $this->webdav_port = $this->options['webdav_port'];
62 $port = ':' . $this->webdav_port;
934ff62c 63 }
3942626d 64 $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
85a9d23d
FM
65 $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'],
66 $this->options['webdav_password'], $this->options['webdav_auth'], $this->webdav_type);
67 $this->dav->port = $this->webdav_port;
3ced70fa 68 $this->dav->debug = false;
934ff62c 69 }
70 public function check_login() {
934ff62c 71 return true;
72 }
68a7c9a6 73 public function get_file($url, $title = '') {
934ff62c 74 global $CFG;
a193a0cf 75 $url = urldecode($url);
a53d4f45 76 $path = $this->prepare_file($title);
934ff62c 77 $buffer = '';
3ced70fa 78 if (!$this->dav->open()) {
7b58fb41 79 return false;
80 }
dc571f30
MG
81 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
82 $this->dav->get($webdavpath. $url, $buffer);
a53d4f45 83 $fp = fopen($path, 'wb');
934ff62c 84 fwrite($fp, $buffer);
1dce6261 85 return array('path'=>$path);
934ff62c 86 }
87 public function global_search() {
88 return false;
89 }
5fa358d1 90 public function get_listing($path='', $page = '') {
390baf46 91 global $CFG, $OUTPUT;
934ff62c 92 $list = array();
93 $ret = array();
94 $ret['dynload'] = true;
b2b3fd82 95 $ret['nosearch'] = true;
96 $ret['nologin'] = true;
dc571f30 97 $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
7b58fb41 98 $ret['list'] = array();
3ced70fa 99 if (!$this->dav->open()) {
7b58fb41 100 return $ret;
101 }
dc571f30
MG
102 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
103 if (empty($path) || $path =='/') {
104 $path = '/';
934ff62c 105 } else {
dc571f30
MG
106 $chunks = preg_split('|/|', trim($path, '/'));
107 for ($i = 0; $i < count($chunks); $i++) {
108 $ret['path'][] = array(
109 'name' => urldecode($chunks[$i]),
110 'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
111 );
7b58fb41 112 }
934ff62c 113 }
dc571f30 114 $dir = $this->dav->ls($webdavpath. urldecode($path));
934ff62c 115 if (!is_array($dir)) {
116 return $ret;
117 }
dc571f30
MG
118 $folders = array();
119 $files = array();
934ff62c 120 foreach ($dir as $v) {
dc571f30
MG
121 if (!empty($v['lastmodified'])) {
122 $v['lastmodified'] = strtotime($v['lastmodified']);
7b58fb41 123 } else {
dc571f30 124 $v['lastmodified'] = null;
7b58fb41 125 }
85a9d23d
FM
126
127 // Extracting object title from absolute path
128 $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
129 $title = substr($v['href'], strlen($path));
130
934ff62c 131 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
7b58fb41 132 // a folder
dc571f30 133 if ($path != $v['href']) {
85a9d23d
FM
134 $folders[strtoupper($title)] = array(
135 'title'=>rtrim($title, '/'),
559276b1 136 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
934ff62c 137 'children'=>array(),
dc571f30 138 'datemodified'=>$v['lastmodified'],
934ff62c 139 'path'=>$v['href']
140 );
141 }
142 }else{
7b58fb41 143 // a file
7b58fb41 144 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
85a9d23d 145 $files[strtoupper($title)] = array(
934ff62c 146 'title'=>$title,
559276b1 147 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false),
7b58fb41 148 'size'=>$size,
dc571f30 149 'datemodified'=>$v['lastmodified'],
934ff62c 150 'source'=>$v['href']
151 );
152 }
153 }
85a9d23d
FM
154 ksort($files);
155 ksort($folders);
dc571f30 156 $ret['list'] = array_merge($folders, $files);
934ff62c 157 return $ret;
158 }
159 public static function get_instance_option_names() {
3ced70fa 160 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
934ff62c 161 }
162
4a126f17 163 public static function instance_config_form($mform) {
7b58fb41 164 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
165 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
166 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
167
934ff62c 168 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
169 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
3ced70fa
DC
170
171 $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
172 $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
173
174 $choices = array();
175 $choices['none'] = get_string('none');
176 $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
a602649f 177 $choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
3ced70fa
DC
178 $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
179 $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
180
181
934ff62c 182 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
183 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
184 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
185 }
41076c58
DC
186 public function supported_returntypes() {
187 return (FILE_INTERNAL | FILE_EXTERNAL);
188 }
934ff62c 189}