MDL-15931 More unit tests of file_info_* classes
[moodle.git] / repository / webdav / repository.class.php
CommitLineData
934ff62c 1<?php
2/**
3 * repository_webdav class
4 *
5 * @author Dongsheng Cai
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 */
9
10require_once($CFG->libdir.'/webdavlib.php');
11
12class repository_webdav extends repository {
13 public function __construct($repositoryid, $context = SITEID, $options = array()) {
14 parent::__construct($repositoryid, $context, $options);
15 $this->wd = new webdav_client();
7f728551 16 if (empty($this->webdav_server)) {
17 return;
18 }
934ff62c 19 $this->wd->set_server($this->webdav_server);
20 if (empty($this->webdav_port)) {
21 $this->wd->set_port(80);
22 } else {
23 $this->wd->set_port($this->webdav_port);
24 }
25 if(!empty($this->webdav_user)){
26 $this->wd->set_user($this->webdav_user);
27 }
28 if(!empty($this->webdav_password)) {
29 $this->wd->set_pass($this->webdav_password);
30 }
31 $this->wd->set_protocol(1);
32 $this->wd->set_debug(false);
33 }
34 public function check_login() {
35 global $SESSION;
36 return true;
37 }
38 public function get_file($url, $title) {
39 global $CFG;
a53d4f45 40 $path = $this->prepare_file($title);
934ff62c 41 $buffer = '';
42 $this->wd->open();
43 $this->wd->get($url, $buffer);
a53d4f45 44 $fp = fopen($path, 'wb');
934ff62c 45 fwrite($fp, $buffer);
a53d4f45 46 return $path;
934ff62c 47 }
48 public function global_search() {
49 return false;
50 }
5fa358d1 51 public function get_listing($path='', $page = '') {
934ff62c 52 global $CFG;
53 $list = array();
54 $ret = array();
55 $ret['dynload'] = true;
56 $ret['list'] = array();
b2b3fd82 57 $ret['nosearch'] = true;
58 $ret['nologin'] = true;
934ff62c 59 $ret['path'] = array(array('name'=>'Root', 'path'=>0));
60 $this->wd->open();
61 if (empty($path)) {
62 $dir = $this->wd->ls('/');
63 $path = '/';
64 } else {
65 $dir = $this->wd->ls($path);
66 }
67 if (!is_array($dir)) {
68 return $ret;
69 }
70 foreach ($dir as $v) {
71 $ts = $this->wd->iso8601totime($v['creationdate']);
72 $filedate = userdate($ts);
73 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
74 if ($path != $v['href']) {
75 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
76 $ret['list'][] = array(
77 'title'=>$title,
78 'thumbnail'=>$CFG->pixpath.'/f/folder.gif',
79 'children'=>array(),
80 'date'=>$filedate,
81 'size'=>0,
82 'path'=>$v['href']
83 );
84 }
85 }else{
86 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
87 $ret['list'][] = array(
88 'title'=>$title,
0ef98843 89 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $title),
934ff62c 90 'size'=>$v['getcontentlength'],
91 'date'=>$filedate,
92 'source'=>$v['href']
93 );
94 }
95 }
96 return $ret;
97 }
98 public static function get_instance_option_names() {
99 return array('webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
100 }
101
102 public function instance_config_form(&$mform) {
103 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
104 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
105 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
106 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
107 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
108 }
109}