Skip to content

Commit

Permalink
MDL-22663 Repository: WebDAV supports SSL and chunked data
Browse files Browse the repository at this point in the history
Conflicts:

	repository/webdav/lib.php
  • Loading branch information
Frederic Massart committed Aug 3, 2012
1 parent f515f00 commit 9f412a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
13 changes: 10 additions & 3 deletions lib/webdavlib.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -89,6 +90,7 @@ function __construct($server = '', $user = '', $pass = '', $auth = false) {
$this->pass = $pass;
}
$this->_auth = $auth;
$this->_socket = $socket;
}
public function __set($key, $value) {
$property = '_' . $key;
Expand Down Expand Up @@ -155,7 +157,7 @@ function iso8601totime($iso8601) {
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);
Expand Down Expand Up @@ -1401,7 +1403,12 @@ private function get_respond() {
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)
Expand Down
21 changes: 13 additions & 8 deletions repository/webdav/lib.php
Expand Up @@ -38,24 +38,27 @@ public function __construct($repositoryid, $context = SYSCONTEXTID, $options = a
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'])) {
$port = '';
if (empty($this->webdav_type)) {
$this->dav->port = 80;
$this->webdav_port = 80;
} else {
$this->dav->port = 443;
$this->webdav_port = 443;
$port = ':443';
}
$port = '';
} 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() {
Expand Down Expand Up @@ -103,9 +106,10 @@ public function get_listing($path='', $page = '') {
} else {
$partern = '#https://'.$this->webdav_host.'/#';
}
$path = '/'.preg_replace($partern, '', $path);
$path = '/'.preg_replace($partern, '', ltrim($path, '/'));
$dir = $this->dav->ls($path);
}

if (!is_array($dir)) {
return $ret;
}
Expand All @@ -116,9 +120,10 @@ public function get_listing($path='', $page = '') {
} else {
$filedate = '';
}

if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
// a folder
if (ltrim($path, '/') != ltrim($v['href'], '/')) {
if (ltrim($path, '/') != urldecode(ltrim($v['href'], '/'))) {
$matches = array();
preg_match('#(\w+)$#i', $v['href'], $matches);
if (!empty($matches[1])) {
Expand Down

0 comments on commit 9f412a7

Please sign in to comment.