portfolio/lib MDL-15995 Added a missing global declaration for $CFG that broke cron
[moodle.git] / repository / webdav / repository.class.php
CommitLineData
934ff62c 1<?php
2/**
3 * repository_webdav class
4 *
5 * @author Dongsheng Cai
934ff62c 6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 */
8
9require_once($CFG->libdir.'/webdavlib.php');
10
11class repository_webdav extends repository {
12 public function __construct($repositoryid, $context = SITEID, $options = array()) {
13 parent::__construct($repositoryid, $context, $options);
7b58fb41 14 // set up webdav client
934ff62c 15 $this->wd = new webdav_client();
ecf4785c 16 if (empty($this->options['webdav_server'])) {
7f728551 17 return;
18 }
ecf4785c 19 if (empty($this->options['webdav_type'])) {
20 $this->options['webdav_type'] = '';
7b58fb41 21 } else {
ecf4785c 22 $this->options['webdav_type'] = 'ssl://';
7b58fb41 23 }
ecf4785c 24 $this->wd->set_server($this->options['webdav_server']);
25 if (empty($this->options['webdav_port'])) {
26 if (empty($this->options['webdav_type'])) {
7b58fb41 27 $this->wd->set_port(80);
28 } else {
29 $this->wd->set_port(443);
30 }
31 $port = '';
934ff62c 32 } else {
ecf4785c 33 $this->wd->set_port($this->options['webdav_port']);
34 $port = ':'.$this->options['webdav_port'];
934ff62c 35 }
ecf4785c 36 $this->webdav_host = $this->options['webdav_type'].$this->options['webdav_server'].$port;
37 if(!empty($this->options['webdav_user'])){
38 $this->wd->set_user($this->options['webdav_user']);
934ff62c 39 }
ecf4785c 40 if(!empty($this->options['webdav_password'])) {
41 $this->wd->set_pass($this->options['webdav_password']);
934ff62c 42 }
43 $this->wd->set_protocol(1);
44 $this->wd->set_debug(false);
45 }
46 public function check_login() {
934ff62c 47 return true;
48 }
49 public function get_file($url, $title) {
50 global $CFG;
a53d4f45 51 $path = $this->prepare_file($title);
934ff62c 52 $buffer = '';
7b58fb41 53 if (!$this->wd->open()) {
54 return false;
55 }
934ff62c 56 $this->wd->get($url, $buffer);
a53d4f45 57 $fp = fopen($path, 'wb');
934ff62c 58 fwrite($fp, $buffer);
a53d4f45 59 return $path;
934ff62c 60 }
61 public function global_search() {
62 return false;
63 }
5fa358d1 64 public function get_listing($path='', $page = '') {
390baf46 65 global $CFG, $OUTPUT;
934ff62c 66 $list = array();
67 $ret = array();
68 $ret['dynload'] = true;
b2b3fd82 69 $ret['nosearch'] = true;
70 $ret['nologin'] = true;
934ff62c 71 $ret['path'] = array(array('name'=>'Root', 'path'=>0));
7b58fb41 72 $ret['list'] = array();
73 if (!$this->wd->open()) {
74 return $ret;
75 }
934ff62c 76 if (empty($path)) {
934ff62c 77 $path = '/';
7b58fb41 78 $dir = $this->wd->ls($path);
934ff62c 79 } else {
ecf4785c 80 if (empty($this->options['webdav_type'])) {
7b58fb41 81 $partern = '#http://'.$this->webdav_host.'/#';
82 } else {
83 $partern = '#http://'.$this->webdav_type.$this->webdav_host.'/#';
84 }
85 $path = '/'.preg_replace($partern, '', $path);
934ff62c 86 $dir = $this->wd->ls($path);
87 }
88 if (!is_array($dir)) {
89 return $ret;
90 }
91 foreach ($dir as $v) {
7b58fb41 92 if (!empty($v['creationdate'])) {
93 $ts = $this->wd->iso8601totime($v['creationdate']);
94 $filedate = userdate($ts);
95 } else {
96 $filedate = '';
97 }
934ff62c 98 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
7b58fb41 99 // a folder
934ff62c 100 if ($path != $v['href']) {
7b58fb41 101 $matches = array();
102 preg_match('#(\w+)$#i', $v['href'], $matches);
103 if (!empty($matches[1])) {
104 $title = urldecode($matches[1]);
105 } else {
106 $title = urldecode($v['href']);
107 }
934ff62c 108 $ret['list'][] = array(
109 'title'=>$title,
b5d0cafc 110 'thumbnail'=>$OUTPUT->pix_url('f/folder-32'),
934ff62c 111 'children'=>array(),
112 'date'=>$filedate,
113 'size'=>0,
114 'path'=>$v['href']
115 );
116 }
117 }else{
7b58fb41 118 // a file
934ff62c 119 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
7b58fb41 120 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
934ff62c 121 $ret['list'][] = array(
122 'title'=>$title,
b5d0cafc 123 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32)),
7b58fb41 124 'size'=>$size,
934ff62c 125 'date'=>$filedate,
126 'source'=>$v['href']
127 );
128 }
129 }
130 return $ret;
131 }
132 public static function get_instance_option_names() {
7b58fb41 133 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_user', 'webdav_password');
934ff62c 134 }
135
136 public function instance_config_form(&$mform) {
7b58fb41 137 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
138 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
139 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
140
934ff62c 141 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
142 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
143 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
144 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
145 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
146 }
41076c58
DC
147 public function supported_returntypes() {
148 return (FILE_INTERNAL | FILE_EXTERNAL);
149 }
934ff62c 150}