PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/webdav/lib.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 202 lines | 150 code | 12 blank | 40 comment | 23 complexity | 608715b7778774475cbe90775d45fd8f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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 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 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. $path = $this->prepare_file($title);
  73. if (!$this->dav->open()) {
  74. return false;
  75. }
  76. $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
  77. $this->dav->get_file($webdavpath. $url, $path);
  78. return array('path'=>$path);
  79. }
  80. public function global_search() {
  81. return false;
  82. }
  83. public function get_listing($path='', $page = '') {
  84. global $CFG, $OUTPUT;
  85. $list = array();
  86. $ret = array();
  87. $ret['dynload'] = true;
  88. $ret['nosearch'] = true;
  89. $ret['nologin'] = true;
  90. $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
  91. $ret['list'] = array();
  92. if (!$this->dav->open()) {
  93. return $ret;
  94. }
  95. $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
  96. if (empty($path) || $path =='/') {
  97. $path = '/';
  98. } else {
  99. $chunks = preg_split('|/|', trim($path, '/'));
  100. for ($i = 0; $i < count($chunks); $i++) {
  101. $ret['path'][] = array(
  102. 'name' => urldecode($chunks[$i]),
  103. 'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
  104. );
  105. }
  106. }
  107. $dir = $this->dav->ls($webdavpath. urldecode($path));
  108. if (!is_array($dir)) {
  109. return $ret;
  110. }
  111. $folders = array();
  112. $files = array();
  113. foreach ($dir as $v) {
  114. if (!empty($v['lastmodified'])) {
  115. $v['lastmodified'] = strtotime($v['lastmodified']);
  116. } else {
  117. $v['lastmodified'] = null;
  118. }
  119. // Remove the server URL from the path (if present), otherwise links will not work - MDL-37014
  120. $server = preg_quote($this->options['webdav_server']);
  121. $v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
  122. // Extracting object title from absolute path
  123. $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
  124. $title = substr($v['href'], strlen($path));
  125. if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
  126. // a folder
  127. if ($path != $v['href']) {
  128. $folders[strtoupper($title)] = array(
  129. 'title'=>rtrim($title, '/'),
  130. 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
  131. 'children'=>array(),
  132. 'datemodified'=>$v['lastmodified'],
  133. 'path'=>$v['href']
  134. );
  135. }
  136. }else{
  137. // a file
  138. $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
  139. $files[strtoupper($title)] = array(
  140. 'title'=>$title,
  141. 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false),
  142. 'size'=>$size,
  143. 'datemodified'=>$v['lastmodified'],
  144. 'source'=>$v['href']
  145. );
  146. }
  147. }
  148. ksort($files);
  149. ksort($folders);
  150. $ret['list'] = array_merge($folders, $files);
  151. return $ret;
  152. }
  153. public static function get_instance_option_names() {
  154. return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
  155. }
  156. public static function instance_config_form($mform) {
  157. $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
  158. $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
  159. $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
  160. $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
  161. $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
  162. $mform->setType('webdav_server', PARAM_HOST);
  163. $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
  164. $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
  165. $mform->setType('webdav_path', PARAM_PATH);
  166. $choices = array();
  167. $choices['none'] = get_string('none');
  168. $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
  169. $choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
  170. $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
  171. $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
  172. $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
  173. $mform->setType('webdav_port', PARAM_INT);
  174. $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
  175. $mform->setType('webdav_user', PARAM_RAW_TRIMMED); // Not for us to clean.
  176. $mform->addElement('password', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
  177. array('size' => '40'));
  178. }
  179. public function supported_returntypes() {
  180. return (FILE_INTERNAL | FILE_EXTERNAL);
  181. }
  182. /**
  183. * Is this repository accessing private data?
  184. *
  185. * @return bool
  186. */
  187. public function contains_private_data() {
  188. return false;
  189. }
  190. }