|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00029 require_once($CFG->libdir.'/webdavlib.php'); 00030 00031 class repository_webdav extends repository { 00032 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { 00033 parent::__construct($repositoryid, $context, $options); 00034 // set up webdav client 00035 if (empty($this->options['webdav_server'])) { 00036 return; 00037 } 00038 if ($this->options['webdav_auth'] == 'none') { 00039 $this->options['webdav_auth'] = false; 00040 } 00041 $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], $this->options['webdav_password'], $this->options['webdav_auth']); 00042 if (empty($this->options['webdav_type'])) { 00043 $this->webdav_type = ''; 00044 } else { 00045 $this->webdav_type = 'ssl://'; 00046 } 00047 if (empty($this->options['webdav_port'])) { 00048 if (empty($this->webdav_type)) { 00049 $this->dav->port = 80; 00050 } else { 00051 $this->dav->port = 443; 00052 } 00053 $port = ''; 00054 } else { 00055 $this->dav->port = $this->options['webdav_port']; 00056 $port = ':'.$this->options['webdav_port']; 00057 } 00058 $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port; 00059 $this->dav->debug = false; 00060 } 00061 public function check_login() { 00062 return true; 00063 } 00064 public function get_file($url, $title) { 00065 global $CFG; 00066 $url = urldecode($url); 00067 $path = $this->prepare_file($title); 00068 $buffer = ''; 00069 if (!$this->dav->open()) { 00070 return false; 00071 } 00072 $this->dav->get($url, $buffer); 00073 $fp = fopen($path, 'wb'); 00074 fwrite($fp, $buffer); 00075 return array('path'=>$path); 00076 } 00077 public function global_search() { 00078 return false; 00079 } 00080 public function get_listing($path='', $page = '') { 00081 global $CFG, $OUTPUT; 00082 $list = array(); 00083 $ret = array(); 00084 $ret['dynload'] = true; 00085 $ret['nosearch'] = true; 00086 $ret['nologin'] = true; 00087 $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>0)); 00088 $ret['list'] = array(); 00089 if (!$this->dav->open()) { 00090 return $ret; 00091 } 00092 if (empty($path)) { 00093 if ($this->options['webdav_path'] == '/') { 00094 $path = '/'; 00095 } else { 00096 $path = '/' . trim($this->options['webdav_path'], './@#$ ') . '/'; 00097 } 00098 $dir = $this->dav->ls($path); 00099 } else { 00100 $path = urldecode($path); 00101 if (empty($this->webdav_type)) { 00102 $partern = '#http://'.$this->webdav_host.'/#'; 00103 } else { 00104 $partern = '#https://'.$this->webdav_host.'/#'; 00105 } 00106 $path = '/'.preg_replace($partern, '', $path); 00107 $dir = $this->dav->ls($path); 00108 } 00109 if (!is_array($dir)) { 00110 return $ret; 00111 } 00112 foreach ($dir as $v) { 00113 if (!empty($v['creationdate'])) { 00114 $ts = $this->dav->iso8601totime($v['creationdate']); 00115 $filedate = userdate($ts); 00116 } else { 00117 $filedate = ''; 00118 } 00119 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') { 00120 // a folder 00121 if (ltrim($path, '/') != ltrim($v['href'], '/')) { 00122 $matches = array(); 00123 preg_match('#(\w+)$#i', $v['href'], $matches); 00124 if (!empty($matches[1])) { 00125 $title = urldecode($matches[1]); 00126 } else { 00127 $title = urldecode($v['href']); 00128 } 00129 $ret['list'][] = array( 00130 'title'=>urldecode(basename($title)), 00131 'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false), 00132 'children'=>array(), 00133 'date'=>$filedate, 00134 'size'=>0, 00135 'path'=>$v['href'] 00136 ); 00137 } 00138 }else{ 00139 // a file 00140 $path = rtrim($path,'/'); 00141 $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path))); 00142 $title = basename($title); 00143 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:''; 00144 $ret['list'][] = array( 00145 'title'=>$title, 00146 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32))->out(false), 00147 'size'=>$size, 00148 'date'=>$filedate, 00149 'source'=>$v['href'] 00150 ); 00151 } 00152 } 00153 return $ret; 00154 } 00155 public static function get_instance_option_names() { 00156 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth'); 00157 } 00158 00159 public function instance_config_form($mform) { 00160 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav')); 00161 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices); 00162 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client'); 00163 00164 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40')); 00165 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client'); 00166 00167 $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40')); 00168 $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client'); 00169 00170 $choices = array(); 00171 $choices['none'] = get_string('none'); 00172 $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav'); 00173 //$choices['digest'] = get_string('webdavdigestauth', 'repository_webdav'); 00174 $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices); 00175 $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client'); 00176 00177 00178 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40')); 00179 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40')); 00180 $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40')); 00181 } 00182 public function supported_returntypes() { 00183 return (FILE_INTERNAL | FILE_EXTERNAL); 00184 } 00185 }