Merge branch 'MDL-22663-master' of git://github.com/FMCorz/moodle

This commit is contained in:
Aparup Banerjee 2012-08-07 10:00:26 +08:00
commit 99f868c541
2 changed files with 67 additions and 43 deletions

View File

@ -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;
@ -57,6 +58,7 @@ class webdav_client {
private $_req;
private $_resp_status;
private $_parser;
private $_parserid;
private $_xmltree;
private $_tree;
private $_ls = array();
@ -79,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;
}
@ -88,6 +90,7 @@ class webdav_client {
$this->pass = $pass;
}
$this->_auth = $auth;
$this->_socket = $socket;
}
public function __set($key, $value) {
$property = '_' . $key;
@ -154,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);
@ -612,9 +615,10 @@ class webdav_client {
if (strcmp($response['header']['Content-Type'], 'text/xml; charset="utf-8"') == 0) {
// ok let's get the content of the xml stuff
$this->_parser = xml_parser_create_ns();
$this->_parserid = (int) $this->_parser;
// forget old data...
unset($this->_lock[$this->_parser]);
unset($this->_xmltree[$this->_parser]);
unset($this->_lock[$this->_parserid]);
unset($this->_xmltree[$this->_parserid]);
xml_parser_set_option($this->_parser,XML_OPTION_SKIP_WHITE,0);
xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
xml_set_object($this->_parser, $this);
@ -630,8 +634,8 @@ class webdav_client {
// Free resources
xml_parser_free($this->_parser);
// add status code to array
$this->_lock[$this->_parser]['status'] = 200;
return $this->_lock[$this->_parser];
$this->_lock[$this->_parserid]['status'] = 200;
return $this->_lock[$this->_parserid];
} else {
print 'Missing Content-Type: text/xml header in response.<br>';
@ -709,9 +713,10 @@ class webdav_client {
if (strcmp($response['header']['Content-Type'], 'text/xml; charset="utf-8"') == 0) {
// ok let's get the content of the xml stuff
$this->_parser = xml_parser_create_ns();
$this->_parserid = (int) $this->_parser;
// forget old data...
unset($this->_delete[$this->_parser]);
unset($this->_xmltree[$this->_parser]);
unset($this->_delete[$this->_parserid]);
unset($this->_xmltree[$this->_parserid]);
xml_parser_set_option($this->_parser,XML_OPTION_SKIP_WHITE,0);
xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
xml_set_object($this->_parser, $this);
@ -728,8 +733,8 @@ class webdav_client {
// Free resources
xml_parser_free($this->_parser);
$this->_delete[$this->_parser]['status'] = $response['status']['status-code'];
return $this->_delete[$this->_parser];
$this->_delete[$this->_parserid]['status'] = $response['status']['status-code'];
return $this->_delete[$this->_parserid];
} else {
print 'Missing Content-Type: text/xml header in response.<br>';
@ -800,9 +805,10 @@ EOD;
if (preg_match('#(application|text)/xml;\s?charset=[\'\"]?utf-8[\'\"]?#i', $response['header']['Content-Type'])) {
// ok let's get the content of the xml stuff
$this->_parser = xml_parser_create_ns('UTF-8');
$this->_parserid = (int) $this->_parser;
// forget old data...
unset($this->_ls[$this->_parser]);
unset($this->_xmltree[$this->_parser]);
unset($this->_ls[$this->_parserid]);
unset($this->_xmltree[$this->_parserid]);
xml_parser_set_option($this->_parser,XML_OPTION_SKIP_WHITE,0);
xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
// xml_parser_set_option($this->_parser,XML_OPTION_TARGET_ENCODING,'UTF-8');
@ -819,7 +825,7 @@ EOD;
// Free resources
xml_parser_free($this->_parser);
$arr = $this->_ls[$this->_parser];
$arr = $this->_ls[$this->_parserid];
return $arr;
} else {
$this->_error_log('Missing Content-Type: text/xml header in response!!');
@ -1038,7 +1044,8 @@ EOD;
private function _endElement($parser, $name) {
// end tag was found...
$this->_xmltree[$parser] = substr($this->_xmltree[$parser],0, strlen($this->_xmltree[$parser]) - (strlen($name) + 1));
$parserid = (int) $parser;
$this->_xmltree[$parserid] = substr($this->_xmltree[$parserid],0, strlen($this->_xmltree[$parserid]) - (strlen($name) + 1));
}
/**
@ -1053,19 +1060,20 @@ EOD;
*/
private function _propfind_startElement($parser, $name, $attrs) {
// lower XML Names... maybe break a RFC, don't know ...
$parserid = (int) $parser;
$propname = strtolower($name);
if (!empty($this->_xmltree[$parser])) {
$this->_xmltree[$parser] .= $propname . '_';
if (!empty($this->_xmltree[$parserid])) {
$this->_xmltree[$parserid] .= $propname . '_';
} else {
$this->_xmltree[$parser] = $propname . '_';
$this->_xmltree[$parserid] = $propname . '_';
}
// translate xml tree to a flat array ...
switch($this->_xmltree[$parser]) {
switch($this->_xmltree[$parserid]) {
case 'dav::multistatus_dav::response_':
// new element in mu
$this->_ls_ref =& $this->_ls[$parser][];
$this->_ls_ref =& $this->_ls[$parserid][];
break;
case 'dav::multistatus_dav::response_dav::href_':
$this->_ls_ref_cdata = &$this->_ls_ref['href'];
@ -1113,7 +1121,7 @@ EOD;
default:
// handle unknown xml elements...
$this->_ls_ref_cdata = &$this->_ls_ref[$this->_xmltree[$parser]];
$this->_ls_ref_cdata = &$this->_ls_ref[$this->_xmltree[$parserid]];
}
}
@ -1148,14 +1156,15 @@ EOD;
*/
private function _delete_startElement($parser, $name, $attrs) {
// lower XML Names... maybe break a RFC, don't know ...
$parserid = (int) $parser;
$propname = strtolower($name);
$this->_xmltree[$parser] .= $propname . '_';
$this->_xmltree[$parserid] .= $propname . '_';
// translate xml tree to a flat array ...
switch($this->_xmltree[$parser]) {
switch($this->_xmltree[$parserid]) {
case 'dav::multistatus_dav::response_':
// new element in mu
$this->_delete_ref =& $this->_delete[$parser][];
$this->_delete_ref =& $this->_delete[$parserid][];
break;
case 'dav::multistatus_dav::response_dav::href_':
$this->_delete_ref_cdata = &$this->_ls_ref['href'];
@ -1163,7 +1172,7 @@ EOD;
default:
// handle unknown xml elements...
$this->_delete_cdata = &$this->_delete_ref[$this->_xmltree[$parser]];
$this->_delete_cdata = &$this->_delete_ref[$this->_xmltree[$parserid]];
}
}
@ -1199,8 +1208,9 @@ EOD;
*/
private function _lock_startElement($parser, $name, $attrs) {
// lower XML Names... maybe break a RFC, don't know ...
$parserid = (int) $parser;
$propname = strtolower($name);
$this->_xmltree[$parser] .= $propname . '_';
$this->_xmltree[$parserid] .= $propname . '_';
// translate xml tree to a flat array ...
/*
@ -1209,10 +1219,10 @@ EOD;
dav::prop_dav::lockdiscovery_dav::activelock_dav::timeout_=
dav::prop_dav::lockdiscovery_dav::activelock_dav::locktoken_dav::href_=
*/
switch($this->_xmltree[$parser]) {
switch($this->_xmltree[$parserid]) {
case 'dav::prop_dav::lockdiscovery_dav::activelock_':
// new element
$this->_lock_ref =& $this->_lock[$parser][];
$this->_lock_ref =& $this->_lock[$parserid][];
break;
case 'dav::prop_dav::lockdiscovery_dav::activelock_dav::locktype_dav::write_':
$this->_lock_ref_cdata = &$this->_lock_ref['locktype'];
@ -1238,7 +1248,7 @@ EOD;
break;
default:
// handle unknown xml elements...
$this->_lock_cdata = &$this->_lock_ref[$this->_xmltree[$parser]];
$this->_lock_cdata = &$this->_lock_ref[$this->_xmltree[$parserid]];
}
}
@ -1254,8 +1264,9 @@ EOD;
* @access private
*/
private function _lock_cData($parser, $cdata) {
$parserid = (int) $parser;
if (trim($cdata) <> '') {
// $this->_error_log(($this->_xmltree[$parser]) . '='. htmlentities($cdata));
// $this->_error_log(($this->_xmltree[$parserid]) . '='. htmlentities($cdata));
$this->_lock_ref_cdata .= $cdata;
} else {
// do nothing
@ -1395,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)

View File

@ -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;
}