PageRenderTime 33ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/webdav/lib.php

https://github.com/200896596/moodle
PHP | 184 lines | 146 code | 10 blank | 28 comment | 28 complexity | 35c1ed3f3073b9506d8b2b710dc67467 MD5 | raw file
  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. * repository_webdav class
  18. *
  19. * @since 2.0
  20. * @package repository
  21. * @subpackage webdav
  22. * @copyright 2009 Dongsheng Cai
  23. * @author Dongsheng Cai <dongsheng@moodle.com>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. require_once($CFG->libdir.'/webdavlib.php');
  27. class repository_webdav extends repository {
  28. public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  29. parent::__construct($repositoryid, $context, $options);
  30. // set up webdav client
  31. if (empty($this->options['webdav_server'])) {
  32. return;
  33. }
  34. if ($this->options['webdav_auth'] == 'none') {
  35. $this->options['webdav_auth'] = false;
  36. }
  37. $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], $this->options['webdav_password'], $this->options['webdav_auth']);
  38. if (empty($this->options['webdav_type'])) {
  39. $this->webdav_type = '';
  40. } else {
  41. $this->webdav_type = 'ssl://';
  42. }
  43. if (empty($this->options['webdav_port'])) {
  44. if (empty($this->webdav_type)) {
  45. $this->dav->port = 80;
  46. } else {
  47. $this->dav->port = 443;
  48. }
  49. $port = '';
  50. } else {
  51. $this->dav->port = $this->options['webdav_port'];
  52. $port = ':'.$this->options['webdav_port'];
  53. }
  54. $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
  55. $this->dav->debug = false;
  56. }
  57. public function check_login() {
  58. return true;
  59. }
  60. public function get_file($url, $title) {
  61. global $CFG;
  62. $url = urldecode($url);
  63. $path = $this->prepare_file($title);
  64. $buffer = '';
  65. if (!$this->dav->open()) {
  66. return false;
  67. }
  68. $this->dav->get($url, $buffer);
  69. $fp = fopen($path, 'wb');
  70. fwrite($fp, $buffer);
  71. return array('path'=>$path);
  72. }
  73. public function global_search() {
  74. return false;
  75. }
  76. public function get_listing($path='', $page = '') {
  77. global $CFG, $OUTPUT;
  78. $list = array();
  79. $ret = array();
  80. $ret['dynload'] = true;
  81. $ret['nosearch'] = true;
  82. $ret['nologin'] = true;
  83. $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>0));
  84. $ret['list'] = array();
  85. if (!$this->dav->open()) {
  86. return $ret;
  87. }
  88. if (empty($path)) {
  89. if ($this->options['webdav_path'] == '/') {
  90. $path = '/';
  91. } else {
  92. $path = '/' . trim($this->options['webdav_path'], './@#$ ') . '/';
  93. }
  94. $dir = $this->dav->ls($path);
  95. } else {
  96. $path = urldecode($path);
  97. if (empty($this->webdav_type)) {
  98. $partern = '#http://'.$this->webdav_host.'/#';
  99. } else {
  100. $partern = '#https://'.$this->webdav_host.'/#';
  101. }
  102. $path = '/'.preg_replace($partern, '', $path);
  103. $dir = $this->dav->ls($path);
  104. }
  105. if (!is_array($dir)) {
  106. return $ret;
  107. }
  108. foreach ($dir as $v) {
  109. if (!empty($v['creationdate'])) {
  110. $ts = $this->dav->iso8601totime($v['creationdate']);
  111. $filedate = userdate($ts);
  112. } else {
  113. $filedate = '';
  114. }
  115. if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
  116. // a folder
  117. if ($path != $v['href']) {
  118. $matches = array();
  119. preg_match('#(\w+)$#i', $v['href'], $matches);
  120. if (!empty($matches[1])) {
  121. $title = urldecode($matches[1]);
  122. } else {
  123. $title = urldecode($v['href']);
  124. }
  125. $ret['list'][] = array(
  126. 'title'=>urldecode(basename($title)),
  127. 'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false),
  128. 'children'=>array(),
  129. 'date'=>$filedate,
  130. 'size'=>0,
  131. 'path'=>$v['href']
  132. );
  133. }
  134. }else{
  135. // a file
  136. $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path)));
  137. $title = basename($title);
  138. $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
  139. $ret['list'][] = array(
  140. 'title'=>$title,
  141. 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32))->out(false),
  142. 'size'=>$size,
  143. 'date'=>$filedate,
  144. 'source'=>$v['href']
  145. );
  146. }
  147. }
  148. return $ret;
  149. }
  150. public static function get_instance_option_names() {
  151. return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
  152. }
  153. public function instance_config_form($mform) {
  154. $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
  155. $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
  156. $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
  157. $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
  158. $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
  159. $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
  160. $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
  161. $choices = array();
  162. $choices['none'] = get_string('none');
  163. $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
  164. //$choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
  165. $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
  166. $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
  167. $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
  168. $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
  169. $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40'));
  170. }
  171. public function supported_returntypes() {
  172. return (FILE_INTERNAL | FILE_EXTERNAL);
  173. }
  174. }