ajax MDL-23694 added missing page context which fixes groups ui ajax search
[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
934ff62c 35 $this->wd = new webdav_client();
ecf4785c 36 if (empty($this->options['webdav_server'])) {
7f728551 37 return;
38 }
ecf4785c 39 if (empty($this->options['webdav_type'])) {
40 $this->options['webdav_type'] = '';
7b58fb41 41 } else {
ecf4785c 42 $this->options['webdav_type'] = 'ssl://';
7b58fb41 43 }
ecf4785c 44 $this->wd->set_server($this->options['webdav_server']);
45 if (empty($this->options['webdav_port'])) {
46 if (empty($this->options['webdav_type'])) {
7b58fb41 47 $this->wd->set_port(80);
48 } else {
49 $this->wd->set_port(443);
50 }
51 $port = '';
934ff62c 52 } else {
ecf4785c 53 $this->wd->set_port($this->options['webdav_port']);
54 $port = ':'.$this->options['webdav_port'];
934ff62c 55 }
ecf4785c 56 $this->webdav_host = $this->options['webdav_type'].$this->options['webdav_server'].$port;
57 if(!empty($this->options['webdav_user'])){
58 $this->wd->set_user($this->options['webdav_user']);
934ff62c 59 }
ecf4785c 60 if(!empty($this->options['webdav_password'])) {
61 $this->wd->set_pass($this->options['webdav_password']);
934ff62c 62 }
63 $this->wd->set_protocol(1);
64 $this->wd->set_debug(false);
65 }
66 public function check_login() {
934ff62c 67 return true;
68 }
69 public function get_file($url, $title) {
70 global $CFG;
a53d4f45 71 $path = $this->prepare_file($title);
934ff62c 72 $buffer = '';
7b58fb41 73 if (!$this->wd->open()) {
74 return false;
75 }
934ff62c 76 $this->wd->get($url, $buffer);
a53d4f45 77 $fp = fopen($path, 'wb');
934ff62c 78 fwrite($fp, $buffer);
1dce6261 79 return array('path'=>$path);
934ff62c 80 }
81 public function global_search() {
82 return false;
83 }
5fa358d1 84 public function get_listing($path='', $page = '') {
390baf46 85 global $CFG, $OUTPUT;
934ff62c 86 $list = array();
87 $ret = array();
88 $ret['dynload'] = true;
b2b3fd82 89 $ret['nosearch'] = true;
90 $ret['nologin'] = true;
934ff62c 91 $ret['path'] = array(array('name'=>'Root', 'path'=>0));
7b58fb41 92 $ret['list'] = array();
93 if (!$this->wd->open()) {
94 return $ret;
95 }
934ff62c 96 if (empty($path)) {
934ff62c 97 $path = '/';
7b58fb41 98 $dir = $this->wd->ls($path);
934ff62c 99 } else {
ecf4785c 100 if (empty($this->options['webdav_type'])) {
7b58fb41 101 $partern = '#http://'.$this->webdav_host.'/#';
102 } else {
103 $partern = '#http://'.$this->webdav_type.$this->webdav_host.'/#';
104 }
105 $path = '/'.preg_replace($partern, '', $path);
934ff62c 106 $dir = $this->wd->ls($path);
107 }
108 if (!is_array($dir)) {
109 return $ret;
110 }
111 foreach ($dir as $v) {
7b58fb41 112 if (!empty($v['creationdate'])) {
113 $ts = $this->wd->iso8601totime($v['creationdate']);
114 $filedate = userdate($ts);
115 } else {
116 $filedate = '';
117 }
934ff62c 118 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
7b58fb41 119 // a folder
934ff62c 120 if ($path != $v['href']) {
7b58fb41 121 $matches = array();
122 preg_match('#(\w+)$#i', $v['href'], $matches);
123 if (!empty($matches[1])) {
124 $title = urldecode($matches[1]);
125 } else {
126 $title = urldecode($v['href']);
127 }
934ff62c 128 $ret['list'][] = array(
129 'title'=>$title,
b5d0cafc 130 'thumbnail'=>$OUTPUT->pix_url('f/folder-32'),
934ff62c 131 'children'=>array(),
132 'date'=>$filedate,
133 'size'=>0,
134 'path'=>$v['href']
135 );
136 }
137 }else{
7b58fb41 138 // a file
934ff62c 139 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
7b58fb41 140 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
934ff62c 141 $ret['list'][] = array(
142 'title'=>$title,
b5d0cafc 143 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32)),
7b58fb41 144 'size'=>$size,
934ff62c 145 'date'=>$filedate,
146 'source'=>$v['href']
147 );
148 }
149 }
150 return $ret;
151 }
152 public static function get_instance_option_names() {
7b58fb41 153 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
934ff62c 154 }
155
aea5595c 156 public function instance_config_form($mform) {
7b58fb41 157 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
158 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
159 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
160
934ff62c 161 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
162 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
163 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
164 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
165 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
166 }
41076c58
DC
167 public function supported_returntypes() {
168 return (FILE_INTERNAL | FILE_EXTERNAL);
169 }
934ff62c 170}