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