Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
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
 
18
/**
19
 * This plugin is used to access webdav files
20
 *
21
 * @since Moodle 2.0
22
 * @package    repository_webdav
23
 * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
require_once($CFG->dirroot . '/repository/lib.php');
27
require_once($CFG->libdir.'/webdavlib.php');
28
 
29
/**
30
 * repository_webdav class
31
 *
32
 * @since Moodle 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
 */
37
class repository_webdav extends repository {
38
 
39
    /** @var string webdav type. */
40
    protected $webdav_type;
41
 
42
    /** @var mixed webdav port. */
43
    protected $webdav_port;
44
 
45
    /** @var string webdav host. */
46
    protected $webdav_host;
47
 
48
    /** @var webdav_client webdav client. */
49
    protected $dav;
50
 
51
    public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
52
        parent::__construct($repositoryid, $context, $options);
53
        // set up webdav client
54
        if (empty($this->options['webdav_server'])) {
55
            return;
56
        }
57
        if ($this->options['webdav_auth'] == 'none') {
58
            $this->options['webdav_auth'] = false;
59
        }
60
        if (empty($this->options['webdav_type'])) {
61
            $this->webdav_type = '';
62
        } else {
63
            $this->webdav_type = 'ssl://';
64
        }
65
        if (empty($this->options['webdav_port'])) {
66
            $port = '';
67
            if (empty($this->webdav_type)) {
68
                $this->webdav_port = 80;
69
            } else {
70
                $this->webdav_port = 443;
71
                $port = ':443';
72
            }
73
        } else {
74
            $this->webdav_port = $this->options['webdav_port'];
75
            $port = ':' . $this->webdav_port;
76
        }
77
        $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
78
        $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'],
79
                $this->options['webdav_password'], $this->options['webdav_auth'], $this->webdav_type);
80
        $this->dav->port = $this->webdav_port;
81
        $this->dav->debug = false;
82
    }
83
    public function check_login() {
84
        return true;
85
    }
86
    public function get_file($url, $title = '') {
87
        $url = urldecode($url);
88
        // Prepare a file with an arbitrary name - cannot be $title because of special chars (cf. MDL-57002).
89
        $path = $this->prepare_file(uniqid());
90
        if (!$this->dav->open()) {
91
            return false;
92
        }
93
        $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
94
        $this->dav->get_file($webdavpath. $url, $path);
95
        return array('path'=>$path);
96
    }
97
    public function global_search() {
98
        return false;
99
    }
100
    public function get_listing($path='', $page = '') {
101
        global $CFG, $OUTPUT;
102
        $list = array();
103
        $ret  = array();
104
        $ret['dynload'] = true;
105
        $ret['nosearch'] = true;
106
        $ret['nologin'] = true;
107
        $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
108
        $ret['list'] = array();
109
        if (!$this->dav->open()) {
110
            return $ret;
111
        }
112
        $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
113
        if (empty($path) || $path =='/') {
114
            $path = '/';
115
        } else {
116
            $chunks = preg_split('|/|', trim($path, '/'));
117
            for ($i = 0; $i < count($chunks); $i++) {
118
                $ret['path'][] = array(
119
                    'name' => urldecode($chunks[$i]),
120
                    'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
121
                );
122
            }
123
        }
124
        $dir = $this->dav->ls($webdavpath. urldecode($path));
125
        if (!is_array($dir)) {
126
            return $ret;
127
        }
128
        $folders = array();
129
        $files = array();
130
        foreach ($dir as $v) {
131
            if (!empty($v['lastmodified'])) {
132
                $v['lastmodified'] = strtotime($v['lastmodified']);
133
            } else {
134
                $v['lastmodified'] = null;
135
            }
136
 
137
            // Remove the server URL from the path (if present), otherwise links will not work - MDL-37014
138
            $server = preg_quote($this->options['webdav_server']);
139
            $v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
140
            // Extracting object title from absolute path
141
            $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
142
            $title = substr($v['href'], strlen($path));
143
 
144
            if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
145
                // A folder.
146
                if ($path != $v['href']) {
147
                    $folders[strtoupper($title)] = array(
148
                        'title' => rtrim($title, '/'),
149
                        'thumbnail' => $OUTPUT->image_url(file_folder_icon())->out(false),
150
                        'children' => array(),
151
                        'datemodified' => $v['lastmodified'],
152
                        'path' => $v['href'],
153
                    );
154
                }
155
            }else{
156
                // A file.
157
                $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
158
                $files[strtoupper($title)] = array(
159
                    'title' => $title,
160
                    'thumbnail' => $OUTPUT->image_url(file_extension_icon($title))->out(false),
161
                    'size' => $size,
162
                    'datemodified' => $v['lastmodified'],
163
                    'source' => $v['href'],
164
                );
165
            }
166
        }
167
        ksort($files);
168
        ksort($folders);
169
        $ret['list'] = array_merge($folders, $files);
170
        return $ret;
171
    }
172
    public static function get_instance_option_names() {
173
        return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
174
    }
175
 
176
    public static function instance_config_form($mform) {
177
        $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
178
        $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
179
        $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
180
 
181
        $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
182
        $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
183
        $mform->setType('webdav_server', PARAM_HOST);
184
 
185
        $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
186
        $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
187
        $mform->setType('webdav_path', PARAM_PATH);
188
 
189
        $choices = array();
190
        $choices['none'] = get_string('none');
191
        $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
192
        $choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
193
        $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
194
        $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
195
 
196
        $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
197
        $mform->setType('webdav_port', PARAM_INT);
198
        $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
199
        $mform->setType('webdav_user', PARAM_RAW_TRIMMED); // Not for us to clean.
200
        $mform->addElement('passwordunmask', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
201
            array('size' => '40'));
202
    }
203
    public function supported_returntypes() {
204
        return (FILE_INTERNAL | FILE_EXTERNAL);
205
    }
206
 
207
 
208
    /**
209
     * Is this repository accessing private data?
210
     *
211
     * @return bool
212
     */
213
    public function contains_private_data() {
214
        return false;
215
    }
216
}