"MDL-16908, webdav plugin for repository module"
[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();
16 $this->wd->set_server($this->webdav_server);
17 if (empty($this->webdav_port)) {
18 $this->wd->set_port(80);
19 } else {
20 $this->wd->set_port($this->webdav_port);
21 }
22 if(!empty($this->webdav_user)){
23 $this->wd->set_user($this->webdav_user);
24 }
25 if(!empty($this->webdav_password)) {
26 $this->wd->set_pass($this->webdav_password);
27 }
28 $this->wd->set_protocol(1);
29 $this->wd->set_debug(false);
30 }
31 public function check_login() {
32 global $SESSION;
33 return true;
34 }
35 public function get_file($url, $title) {
36 global $CFG;
37 if (!file_exists($CFG->dataroot.'/temp/download')) {
38 mkdir($CFG->dataroot.'/temp/download/', 0777, true);
39 }
40 if (is_dir($CFG->dataroot.'/temp/download')) {
41 $dir = $CFG->dataroot.'/temp/download/';
42 }
43 if (empty($file)) {
44 $file = uniqid('repo').'_'.time().'.tmp';
45 }
46 if (file_exists($dir.$file)) {
47 $file = uniqid('m').$file;
48 }
49
50 $buffer = '';
51 $this->wd->open();
52 $this->wd->get($url, $buffer);
53 $fp = fopen($dir.$file, 'w');
54 fwrite($fp, $buffer);
55 return $dir.$file;
56 }
57 public function global_search() {
58 return false;
59 }
60 public function get_listing($path='') {
61 global $CFG;
62 $list = array();
63 $ret = array();
64 $ret['dynload'] = true;
65 $ret['list'] = array();
66 $ret['path'] = array(array('name'=>'Root', 'path'=>0));
67 $this->wd->open();
68 if (empty($path)) {
69 $dir = $this->wd->ls('/');
70 $path = '/';
71 } else {
72 $dir = $this->wd->ls($path);
73 }
74 if (!is_array($dir)) {
75 return $ret;
76 }
77 foreach ($dir as $v) {
78 $ts = $this->wd->iso8601totime($v['creationdate']);
79 $filedate = userdate($ts);
80 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
81 if ($path != $v['href']) {
82 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
83 $ret['list'][] = array(
84 'title'=>$title,
85 'thumbnail'=>$CFG->pixpath.'/f/folder.gif',
86 'children'=>array(),
87 'date'=>$filedate,
88 'size'=>0,
89 'path'=>$v['href']
90 );
91 }
92 }else{
93 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
94 $ret['list'][] = array(
95 'title'=>$title,
96 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo("icon", $title),
97 'size'=>$v['getcontentlength'],
98 'date'=>$filedate,
99 'source'=>$v['href']
100 );
101 }
102 }
103 return $ret;
104 }
105 public static function get_instance_option_names() {
106 return array('webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
107 }
108
109 public function instance_config_form(&$mform) {
110 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
111 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
112 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
113 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
114 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
115 }
116}