[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/repository/webdav/ -> lib.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1