MDL-28557 Calendar filters: show all group events in course to teacher
[moodle.git] / repository / webdav / lib.php
blobea4444d2fc54b0cf55a61feb4e98f1df62450326
1 <?php
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.
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/>.
18 /**
19 * repository_webdav class
21 * @since 2.0
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
29 require_once($CFG->libdir.'/webdavlib.php');
31 class repository_webdav extends repository {
32 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
33 parent::__construct($repositoryid, $context, $options);
34 // set up webdav client
35 if (empty($this->options['webdav_server'])) {
36 return;
38 if ($this->options['webdav_auth'] == 'none') {
39 $this->options['webdav_auth'] = false;
41 $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], $this->options['webdav_password'], $this->options['webdav_auth']);
42 if (empty($this->options['webdav_type'])) {
43 $this->webdav_type = '';
44 } else {
45 $this->webdav_type = 'ssl://';
47 if (empty($this->options['webdav_port'])) {
48 if (empty($this->webdav_type)) {
49 $this->dav->port = 80;
50 } else {
51 $this->dav->port = 443;
53 $port = '';
54 } else {
55 $this->dav->port = $this->options['webdav_port'];
56 $port = ':'.$this->options['webdav_port'];
58 $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
59 $this->dav->debug = false;
61 public function check_login() {
62 return true;
64 public function get_file($url, $title = '') {
65 global $CFG;
66 $url = urldecode($url);
67 $path = $this->prepare_file($title);
68 $buffer = '';
69 if (!$this->dav->open()) {
70 return false;
72 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
73 $this->dav->get($webdavpath. $url, $buffer);
74 $fp = fopen($path, 'wb');
75 fwrite($fp, $buffer);
76 return array('path'=>$path);
78 public function global_search() {
79 return false;
81 public function get_listing($path='', $page = '') {
82 global $CFG, $OUTPUT;
83 $list = array();
84 $ret = array();
85 $ret['dynload'] = true;
86 $ret['nosearch'] = true;
87 $ret['nologin'] = true;
88 $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
89 $ret['list'] = array();
90 if (!$this->dav->open()) {
91 return $ret;
93 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
94 if (empty($path) || $path =='/') {
95 $path = '/';
96 } else {
97 $chunks = preg_split('|/|', trim($path, '/'));
98 for ($i = 0; $i < count($chunks); $i++) {
99 $ret['path'][] = array(
100 'name' => urldecode($chunks[$i]),
101 'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
105 $dir = $this->dav->ls($webdavpath. urldecode($path));
106 if (!is_array($dir)) {
107 return $ret;
109 $folders = array();
110 $files = array();
111 foreach ($dir as $v) {
112 if (!empty($v['lastmodified'])) {
113 $v['lastmodified'] = strtotime($v['lastmodified']);
114 } else {
115 $v['lastmodified'] = null;
117 $v['href'] = substr($v['href'], strlen(urlencode($webdavpath)));
118 $title = urldecode(substr($v['href'], strlen($path)));
119 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
120 // a folder
121 if ($path != $v['href']) {
122 $folders[] = array(
123 'title'=>$title,
124 'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false),
125 'children'=>array(),
126 'datemodified'=>$v['lastmodified'],
127 'path'=>$v['href']
130 }else{
131 // a file
132 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
133 $files[] = array(
134 'title'=>$title,
135 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32))->out(false),
136 'size'=>$size,
137 'datemodified'=>$v['lastmodified'],
138 'source'=>$v['href']
142 $ret['list'] = array_merge($folders, $files);
143 return $ret;
145 public static function get_instance_option_names() {
146 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
149 public function instance_config_form($mform) {
150 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
151 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
152 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
154 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
155 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
157 $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
158 $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
160 $choices = array();
161 $choices['none'] = get_string('none');
162 $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
163 //$choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
164 $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
165 $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
168 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
169 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
170 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
172 public function supported_returntypes() {
173 return (FILE_INTERNAL | FILE_EXTERNAL);