MDL-22663 Repository: Fixed strict standards notices in WebDAV lib
[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 }
47 $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], $this->options['webdav_password'], $this->options['webdav_auth']);
ecf4785c 48 if (empty($this->options['webdav_type'])) {
3942626d 49 $this->webdav_type = '';
7b58fb41 50 } else {
3942626d 51 $this->webdav_type = 'ssl://';
7b58fb41 52 }
ecf4785c 53 if (empty($this->options['webdav_port'])) {
3942626d 54 if (empty($this->webdav_type)) {
3ced70fa 55 $this->dav->port = 80;
7b58fb41 56 } else {
3ced70fa 57 $this->dav->port = 443;
7b58fb41 58 }
59 $port = '';
934ff62c 60 } else {
3ced70fa 61 $this->dav->port = $this->options['webdav_port'];
ecf4785c 62 $port = ':'.$this->options['webdav_port'];
934ff62c 63 }
3942626d 64 $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
3ced70fa 65 $this->dav->debug = false;
934ff62c 66 }
67 public function check_login() {
934ff62c 68 return true;
69 }
68a7c9a6 70 public function get_file($url, $title = '') {
934ff62c 71 global $CFG;
a193a0cf 72 $url = urldecode($url);
a53d4f45 73 $path = $this->prepare_file($title);
934ff62c 74 $buffer = '';
3ced70fa 75 if (!$this->dav->open()) {
7b58fb41 76 return false;
77 }
dc571f30
MG
78 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
79 $this->dav->get($webdavpath. $url, $buffer);
a53d4f45 80 $fp = fopen($path, 'wb');
934ff62c 81 fwrite($fp, $buffer);
1dce6261 82 return array('path'=>$path);
934ff62c 83 }
84 public function global_search() {
85 return false;
86 }
5fa358d1 87 public function get_listing($path='', $page = '') {
390baf46 88 global $CFG, $OUTPUT;
934ff62c 89 $list = array();
90 $ret = array();
91 $ret['dynload'] = true;
b2b3fd82 92 $ret['nosearch'] = true;
93 $ret['nologin'] = true;
dc571f30 94 $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
7b58fb41 95 $ret['list'] = array();
3ced70fa 96 if (!$this->dav->open()) {
7b58fb41 97 return $ret;
98 }
dc571f30
MG
99 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
100 if (empty($path) || $path =='/') {
101 $path = '/';
934ff62c 102 } else {
dc571f30
MG
103 $chunks = preg_split('|/|', trim($path, '/'));
104 for ($i = 0; $i < count($chunks); $i++) {
105 $ret['path'][] = array(
106 'name' => urldecode($chunks[$i]),
107 'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
108 );
7b58fb41 109 }
934ff62c 110 }
dc571f30 111 $dir = $this->dav->ls($webdavpath. urldecode($path));
934ff62c 112 if (!is_array($dir)) {
113 return $ret;
114 }
dc571f30
MG
115 $folders = array();
116 $files = array();
934ff62c 117 foreach ($dir as $v) {
dc571f30
MG
118 if (!empty($v['lastmodified'])) {
119 $v['lastmodified'] = strtotime($v['lastmodified']);
7b58fb41 120 } else {
dc571f30 121 $v['lastmodified'] = null;
7b58fb41 122 }
dc571f30
MG
123 $v['href'] = substr($v['href'], strlen(urlencode($webdavpath)));
124 $title = urldecode(substr($v['href'], strlen($path)));
934ff62c 125 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
7b58fb41 126 // a folder
dc571f30
MG
127 if ($path != $v['href']) {
128 $folders[] = array(
129 'title'=>$title,
559276b1 130 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
934ff62c 131 'children'=>array(),
dc571f30 132 'datemodified'=>$v['lastmodified'],
934ff62c 133 'path'=>$v['href']
134 );
135 }
136 }else{
7b58fb41 137 // a file
7b58fb41 138 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
dc571f30 139 $files[] = array(
934ff62c 140 'title'=>$title,
559276b1 141 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false),
7b58fb41 142 'size'=>$size,
dc571f30 143 'datemodified'=>$v['lastmodified'],
934ff62c 144 'source'=>$v['href']
145 );
146 }
147 }
dc571f30 148 $ret['list'] = array_merge($folders, $files);
934ff62c 149 return $ret;
150 }
151 public static function get_instance_option_names() {
3ced70fa 152 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
934ff62c 153 }
154
4a126f17 155 public static function instance_config_form($mform) {
7b58fb41 156 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
157 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
158 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
159
934ff62c 160 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
161 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
3ced70fa
DC
162
163 $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
164 $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
165
166 $choices = array();
167 $choices['none'] = get_string('none');
168 $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
169 //$choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
170 $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
171 $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
172
173
934ff62c 174 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
175 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
176 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
177 }
41076c58
DC
178 public function supported_returntypes() {
179 return (FILE_INTERNAL | FILE_EXTERNAL);
180 }
934ff62c 181}