Merge branch 'MDL-31557_master' of git://github.com/markn86/moodle
[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'])) {
d09c62c4 53 $port = '';
3942626d 54 if (empty($this->webdav_type)) {
d09c62c4 55 $this->webdav_port = 80;
7b58fb41 56 } else {
d09c62c4
FM
57 $this->webdav_port = 443;
58 $port = ':443';
7b58fb41 59 }
934ff62c 60 } else {
d09c62c4
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;
d09c62c4
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 = '') {
a193a0cf 74 $url = urldecode($url);
a53d4f45 75 $path = $this->prepare_file($title);
3ced70fa 76 if (!$this->dav->open()) {
7b58fb41 77 return false;
78 }
dc571f30 79 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
e38f6992 80 $this->dav->get_file($webdavpath. $url, $path);
1dce6261 81 return array('path'=>$path);
934ff62c 82 }
83 public function global_search() {
84 return false;
85 }
5fa358d1 86 public function get_listing($path='', $page = '') {
390baf46 87 global $CFG, $OUTPUT;
934ff62c 88 $list = array();
89 $ret = array();
90 $ret['dynload'] = true;
b2b3fd82 91 $ret['nosearch'] = true;
92 $ret['nologin'] = true;
dc571f30 93 $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
7b58fb41 94 $ret['list'] = array();
3ced70fa 95 if (!$this->dav->open()) {
7b58fb41 96 return $ret;
97 }
dc571f30
MG
98 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
99 if (empty($path) || $path =='/') {
100 $path = '/';
934ff62c 101 } else {
dc571f30
MG
102 $chunks = preg_split('|/|', trim($path, '/'));
103 for ($i = 0; $i < count($chunks); $i++) {
104 $ret['path'][] = array(
105 'name' => urldecode($chunks[$i]),
106 'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
107 );
7b58fb41 108 }
934ff62c 109 }
dc571f30 110 $dir = $this->dav->ls($webdavpath. urldecode($path));
934ff62c 111 if (!is_array($dir)) {
112 return $ret;
113 }
dc571f30
MG
114 $folders = array();
115 $files = array();
934ff62c 116 foreach ($dir as $v) {
dc571f30
MG
117 if (!empty($v['lastmodified'])) {
118 $v['lastmodified'] = strtotime($v['lastmodified']);
7b58fb41 119 } else {
dc571f30 120 $v['lastmodified'] = null;
7b58fb41 121 }
d09c62c4 122
8427de68
DS
123 // Remove the server URL from the path (if present), otherwise links will not work - MDL-37014
124 $server = preg_quote($this->options['webdav_server']);
125 $v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
d09c62c4
FM
126 // Extracting object title from absolute path
127 $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
128 $title = substr($v['href'], strlen($path));
129
934ff62c 130 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
7b58fb41 131 // a folder
dc571f30 132 if ($path != $v['href']) {
d09c62c4
FM
133 $folders[strtoupper($title)] = array(
134 'title'=>rtrim($title, '/'),
559276b1 135 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
934ff62c 136 'children'=>array(),
dc571f30 137 'datemodified'=>$v['lastmodified'],
934ff62c 138 'path'=>$v['href']
139 );
140 }
141 }else{
7b58fb41 142 // a file
7b58fb41 143 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
d09c62c4 144 $files[strtoupper($title)] = array(
934ff62c 145 'title'=>$title,
559276b1 146 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false),
7b58fb41 147 'size'=>$size,
dc571f30 148 'datemodified'=>$v['lastmodified'],
934ff62c 149 'source'=>$v['href']
150 );
151 }
152 }
d09c62c4
FM
153 ksort($files);
154 ksort($folders);
dc571f30 155 $ret['list'] = array_merge($folders, $files);
934ff62c 156 return $ret;
157 }
158 public static function get_instance_option_names() {
3ced70fa 159 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
934ff62c 160 }
161
4a126f17 162 public static function instance_config_form($mform) {
7b58fb41 163 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
164 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
165 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
166
934ff62c 167 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
168 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
3ced70fa
DC
169
170 $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
171 $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
172
173 $choices = array();
174 $choices['none'] = get_string('none');
175 $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
3e512abb 176 $choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
3ced70fa
DC
177 $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
178 $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
179
934ff62c 180 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
181 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
ce96f23f
FM
182 $mform->addElement('password', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
183 array('size' => '40'));
934ff62c 184 }
41076c58
DC
185 public function supported_returntypes() {
186 return (FILE_INTERNAL | FILE_EXTERNAL);
187 }
934ff62c 188}