PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/repository/webdav/lib.php

https://bitbucket.org/moodle/moodle
PHP | 203 lines | 150 code | 12 blank | 41 comment | 23 complexity | 4845fe6f7b494cdc1c1e511af112afd2 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This plugin is used to access webdav files
  18. *
  19. * @since Moodle 2.0
  20. * @package repository_webdav
  21. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot . '/repository/lib.php');
  25. require_once($CFG->libdir.'/webdavlib.php');
  26. /**
  27. * repository_webdav class
  28. *
  29. * @since Moodle 2.0
  30. * @package repository_webdav
  31. * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class repository_webdav extends repository {
  35. public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  36. parent::__construct($repositoryid, $context, $options);
  37. // set up webdav client
  38. if (empty($this->options['webdav_server'])) {
  39. return;
  40. }
  41. if ($this->options['webdav_auth'] == 'none') {
  42. $this->options['webdav_auth'] = false;
  43. }
  44. if (empty($this->options['webdav_type'])) {
  45. $this->webdav_type = '';
  46. } else {
  47. $this->webdav_type = 'ssl://';
  48. }
  49. if (empty($this->options['webdav_port'])) {
  50. $port = '';
  51. if (empty($this->webdav_type)) {
  52. $this->webdav_port = 80;
  53. } else {
  54. $this->webdav_port = 443;
  55. $port = ':443';
  56. }
  57. } else {
  58. $this->webdav_port = $this->options['webdav_port'];
  59. $port = ':' . $this->webdav_port;
  60. }
  61. $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
  62. $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'],
  63. $this->options['webdav_password'], $this->options['webdav_auth'], $this->webdav_type);
  64. $this->dav->port = $this->webdav_port;
  65. $this->dav->debug = false;
  66. }
  67. public function check_login() {
  68. return true;
  69. }
  70. public function get_file($url, $title = '') {
  71. $url = urldecode($url);
  72. // Prepare a file with an arbitrary name - cannot be $title because of special chars (cf. MDL-57002).
  73. $path = $this->prepare_file(uniqid());
  74. if (!$this->dav->open()) {
  75. return false;
  76. }
  77. $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
  78. $this->dav->get_file($webdavpath. $url, $path);
  79. return array('path'=>$path);
  80. }
  81. public function global_search() {
  82. return false;
  83. }
  84. public function get_listing($path='', $page = '') {
  85. global $CFG, $OUTPUT;
  86. $list = array();
  87. $ret = array();
  88. $ret['dynload'] = true;
  89. $ret['nosearch'] = true;
  90. $ret['nologin'] = true;
  91. $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
  92. $ret['list'] = array();
  93. if (!$this->dav->open()) {
  94. return $ret;
  95. }
  96. $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
  97. if (empty($path) || $path =='/') {
  98. $path = '/';
  99. } else {
  100. $chunks = preg_split('|/|', trim($path, '/'));
  101. for ($i = 0; $i < count($chunks); $i++) {
  102. $ret['path'][] = array(
  103. 'name' => urldecode($chunks[$i]),
  104. 'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
  105. );
  106. }
  107. }
  108. $dir = $this->dav->ls($webdavpath. urldecode($path));
  109. if (!is_array($dir)) {
  110. return $ret;
  111. }
  112. $folders = array();
  113. $files = array();
  114. foreach ($dir as $v) {
  115. if (!empty($v['lastmodified'])) {
  116. $v['lastmodified'] = strtotime($v['lastmodified']);
  117. } else {
  118. $v['lastmodified'] = null;
  119. }
  120. // Remove the server URL from the path (if present), otherwise links will not work - MDL-37014
  121. $server = preg_quote($this->options['webdav_server']);
  122. $v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
  123. // Extracting object title from absolute path
  124. $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
  125. $title = substr($v['href'], strlen($path));
  126. if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
  127. // a folder
  128. if ($path != $v['href']) {
  129. $folders[strtoupper($title)] = array(
  130. 'title'=>rtrim($title, '/'),
  131. 'thumbnail'=>$OUTPUT->image_url(file_folder_icon(90))->out(false),
  132. 'children'=>array(),
  133. 'datemodified'=>$v['lastmodified'],
  134. 'path'=>$v['href']
  135. );
  136. }
  137. }else{
  138. // a file
  139. $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
  140. $files[strtoupper($title)] = array(
  141. 'title'=>$title,
  142. 'thumbnail' => $OUTPUT->image_url(file_extension_icon($title, 90))->out(false),
  143. 'size'=>$size,
  144. 'datemodified'=>$v['lastmodified'],
  145. 'source'=>$v['href']
  146. );
  147. }
  148. }
  149. ksort($files);
  150. ksort($folders);
  151. $ret['list'] = array_merge($folders, $files);
  152. return $ret;
  153. }
  154. public static function get_instance_option_names() {
  155. return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
  156. }
  157. public static function instance_config_form($mform) {
  158. $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
  159. $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
  160. $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
  161. $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
  162. $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
  163. $mform->setType('webdav_server', PARAM_HOST);
  164. $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
  165. $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
  166. $mform->setType('webdav_path', PARAM_PATH);
  167. $choices = array();
  168. $choices['none'] = get_string('none');
  169. $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
  170. $choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
  171. $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
  172. $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
  173. $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
  174. $mform->setType('webdav_port', PARAM_INT);
  175. $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
  176. $mform->setType('webdav_user', PARAM_RAW_TRIMMED); // Not for us to clean.
  177. $mform->addElement('passwordunmask', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
  178. array('size' => '40'));
  179. }
  180. public function supported_returntypes() {
  181. return (FILE_INTERNAL | FILE_EXTERNAL);
  182. }
  183. /**
  184. * Is this repository accessing private data?
  185. *
  186. * @return bool
  187. */
  188. public function contains_private_data() {
  189. return false;
  190. }
  191. }