From 85a9d23d916a540331a599acc64a05b28f7bae68 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 2 Aug 2012 12:04:32 +0800 Subject: [PATCH] MDL-22663 Repository: WebDAV supports SSL and chunked data --- lib/webdavlib.php | 13 ++++++++++--- repository/webdav/lib.php | 34 +++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/webdavlib.php b/lib/webdavlib.php index 58d3d8e1d6d..9051a1e168c 100644 --- a/lib/webdavlib.php +++ b/lib/webdavlib.php @@ -44,6 +44,7 @@ class webdav_client { private $_server; private $_protocol = 'HTTP/1.1'; private $_port = 80; + private $_socket = ''; private $_path ='/'; private $_auth = false; private $_user; @@ -80,7 +81,7 @@ class webdav_client { /** * Constructor - Initialise class variables */ - function __construct($server = '', $user = '', $pass = '', $auth = false) { + function __construct($server = '', $user = '', $pass = '', $auth = false, $socket = '') { if (!empty($server)) { $this->_server = $server; } @@ -89,6 +90,7 @@ class webdav_client { $this->pass = $pass; } $this->_auth = $auth; + $this->_socket = $socket; } public function __set($key, $value) { $property = '_' . $key; @@ -155,7 +157,7 @@ class webdav_client { function open() { // let's try to open a socket $this->_error_log('open a socket connection'); - $this->sock = fsockopen($this->_server, $this->_port, $this->_errno, $this->_errstr, $this->_socket_timeout); + $this->sock = fsockopen($this->_socket . $this->_server, $this->_port, $this->_errno, $this->_errstr, $this->_socket_timeout); set_time_limit(30); if (is_resource($this->sock)) { socket_set_blocking($this->sock, true); @@ -1404,7 +1406,12 @@ EOD; fread($this->sock, 1); // also drop off the Line Feed $chunk_size=hexdec($chunk_size); // convert to a number in decimal system if ($chunk_size > 0) { - $buffer .= fread($this->sock,$chunk_size); + $read = 0; + // Reading the chunk in one bite is not secure, we read it byte by byte. + while ($read < $chunk_size) { + $buffer .= fread($this->sock, 1); + $read++; + } } fread($this->sock, 2); // ditch the CRLF that trails the chunk } while ($chunk_size); // till we reach the 0 length chunk (end marker) diff --git a/repository/webdav/lib.php b/repository/webdav/lib.php index 07c66b1eab6..f0aa4145d40 100644 --- a/repository/webdav/lib.php +++ b/repository/webdav/lib.php @@ -44,24 +44,27 @@ class repository_webdav extends repository { if ($this->options['webdav_auth'] == 'none') { $this->options['webdav_auth'] = false; } - $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], $this->options['webdav_password'], $this->options['webdav_auth']); if (empty($this->options['webdav_type'])) { $this->webdav_type = ''; } else { $this->webdav_type = 'ssl://'; } if (empty($this->options['webdav_port'])) { - if (empty($this->webdav_type)) { - $this->dav->port = 80; - } else { - $this->dav->port = 443; - } $port = ''; + if (empty($this->webdav_type)) { + $this->webdav_port = 80; + } else { + $this->webdav_port = 443; + $port = ':443'; + } } else { - $this->dav->port = $this->options['webdav_port']; - $port = ':'.$this->options['webdav_port']; + $this->webdav_port = $this->options['webdav_port']; + $port = ':' . $this->webdav_port; } $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port; + $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], + $this->options['webdav_password'], $this->options['webdav_auth'], $this->webdav_type); + $this->dav->port = $this->webdav_port; $this->dav->debug = false; } public function check_login() { @@ -120,13 +123,16 @@ class repository_webdav extends repository { } else { $v['lastmodified'] = null; } - $v['href'] = substr($v['href'], strlen(urlencode($webdavpath))); - $title = urldecode(substr($v['href'], strlen($path))); + + // Extracting object title from absolute path + $v['href'] = substr(urldecode($v['href']), strlen($webdavpath)); + $title = substr($v['href'], strlen($path)); + if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') { // a folder if ($path != $v['href']) { - $folders[] = array( - 'title'=>$title, + $folders[strtoupper($title)] = array( + 'title'=>rtrim($title, '/'), 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false), 'children'=>array(), 'datemodified'=>$v['lastmodified'], @@ -136,7 +142,7 @@ class repository_webdav extends repository { }else{ // a file $size = !empty($v['getcontentlength'])? $v['getcontentlength']:''; - $files[] = array( + $files[strtoupper($title)] = array( 'title'=>$title, 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false), 'size'=>$size, @@ -145,6 +151,8 @@ class repository_webdav extends repository { ); } } + ksort($files); + ksort($folders); $ret['list'] = array_merge($folders, $files); return $ret; }