Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'MDL-22663-22' of git://github.com/FMCorz/moodle into MO…

…ODLE_22_STABLE
  • Loading branch information...
commit 07b64939e8636776aa5bdf2db0991cb4aae6164a 2 parents e8b2b43 + 9f412a7
Aparup Banerjee nebgor authored

Showing 2 changed files with 59 additions and 38 deletions. Show diff stats Hide diff stats

  1. +46 30 lib/webdavlib.php
  2. +13 8 repository/webdav/lib.php
76 lib/webdavlib.php
@@ -44,6 +44,7 @@ class webdav_client {
44 44 private $_server;
45 45 private $_protocol = 'HTTP/1.1';
46 46 private $_port = 80;
  47 + private $_socket = '';
47 48 private $_path ='/';
48 49 private $_auth = false;
49 50 private $_user;
@@ -57,6 +58,7 @@ class webdav_client {
57 58 private $_req;
58 59 private $_resp_status;
59 60 private $_parser;
  61 + private $_parserid;
60 62 private $_xmltree;
61 63 private $_tree;
62 64 private $_ls = array();
@@ -79,7 +81,7 @@ class webdav_client {
79 81 /**
80 82 * Constructor - Initialise class variables
81 83 */
82   - function __construct($server = '', $user = '', $pass = '', $auth = false) {
  84 + function __construct($server = '', $user = '', $pass = '', $auth = false, $socket = '') {
83 85 if (!empty($server)) {
84 86 $this->_server = $server;
85 87 }
@@ -88,6 +90,7 @@ function __construct($server = '', $user = '', $pass = '', $auth = false) {
88 90 $this->pass = $pass;
89 91 }
90 92 $this->_auth = $auth;
  93 + $this->_socket = $socket;
91 94 }
92 95 public function __set($key, $value) {
93 96 $property = '_' . $key;
@@ -154,7 +157,7 @@ function iso8601totime($iso8601) {
154 157 function open() {
155 158 // let's try to open a socket
156 159 $this->_error_log('open a socket connection');
157   - $this->sock = fsockopen($this->_server, $this->_port, $this->_errno, $this->_errstr, $this->_socket_timeout);
  160 + $this->sock = fsockopen($this->_socket . $this->_server, $this->_port, $this->_errno, $this->_errstr, $this->_socket_timeout);
158 161 set_time_limit(30);
159 162 if (is_resource($this->sock)) {
160 163 socket_set_blocking($this->sock, true);
@@ -612,9 +615,10 @@ function lock($path) {
612 615 if (strcmp($response['header']['Content-Type'], 'text/xml; charset="utf-8"') == 0) {
613 616 // ok let's get the content of the xml stuff
614 617 $this->_parser = xml_parser_create_ns();
  618 + $this->_parserid = (int) $this->_parser;
615 619 // forget old data...
616   - unset($this->_lock[$this->_parser]);
617   - unset($this->_xmltree[$this->_parser]);
  620 + unset($this->_lock[$this->_parserid]);
  621 + unset($this->_xmltree[$this->_parserid]);
618 622 xml_parser_set_option($this->_parser,XML_OPTION_SKIP_WHITE,0);
619 623 xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
620 624 xml_set_object($this->_parser, $this);
@@ -630,8 +634,8 @@ function lock($path) {
630 634 // Free resources
631 635 xml_parser_free($this->_parser);
632 636 // add status code to array
633   - $this->_lock[$this->_parser]['status'] = 200;
634   - return $this->_lock[$this->_parser];
  637 + $this->_lock[$this->_parserid]['status'] = 200;
  638 + return $this->_lock[$this->_parserid];
635 639
636 640 } else {
637 641 print 'Missing Content-Type: text/xml header in response.<br>';
@@ -709,9 +713,10 @@ function delete($path) {
709 713 if (strcmp($response['header']['Content-Type'], 'text/xml; charset="utf-8"') == 0) {
710 714 // ok let's get the content of the xml stuff
711 715 $this->_parser = xml_parser_create_ns();
  716 + $this->_parserid = (int) $this->_parser;
712 717 // forget old data...
713   - unset($this->_delete[$this->_parser]);
714   - unset($this->_xmltree[$this->_parser]);
  718 + unset($this->_delete[$this->_parserid]);
  719 + unset($this->_xmltree[$this->_parserid]);
715 720 xml_parser_set_option($this->_parser,XML_OPTION_SKIP_WHITE,0);
716 721 xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
717 722 xml_set_object($this->_parser, $this);
@@ -728,8 +733,8 @@ function delete($path) {
728 733
729 734 // Free resources
730 735 xml_parser_free($this->_parser);
731   - $this->_delete[$this->_parser]['status'] = $response['status']['status-code'];
732   - return $this->_delete[$this->_parser];
  736 + $this->_delete[$this->_parserid]['status'] = $response['status']['status-code'];
  737 + return $this->_delete[$this->_parserid];
733 738
734 739 } else {
735 740 print 'Missing Content-Type: text/xml header in response.<br>';
@@ -800,9 +805,10 @@ function ls($path) {
800 805 if (preg_match('#(application|text)/xml;\s?charset=[\'\"]?utf-8[\'\"]?#i', $response['header']['Content-Type'])) {
801 806 // ok let's get the content of the xml stuff
802 807 $this->_parser = xml_parser_create_ns('UTF-8');
  808 + $this->_parserid = (int) $this->_parser;
803 809 // forget old data...
804   - unset($this->_ls[$this->_parser]);
805   - unset($this->_xmltree[$this->_parser]);
  810 + unset($this->_ls[$this->_parserid]);
  811 + unset($this->_xmltree[$this->_parserid]);
806 812 xml_parser_set_option($this->_parser,XML_OPTION_SKIP_WHITE,0);
807 813 xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
808 814 // xml_parser_set_option($this->_parser,XML_OPTION_TARGET_ENCODING,'UTF-8');
@@ -819,7 +825,7 @@ function ls($path) {
819 825
820 826 // Free resources
821 827 xml_parser_free($this->_parser);
822   - $arr = $this->_ls[$this->_parser];
  828 + $arr = $this->_ls[$this->_parserid];
823 829 return $arr;
824 830 } else {
825 831 $this->_error_log('Missing Content-Type: text/xml header in response!!');
@@ -1035,7 +1041,8 @@ function mget($filelist) {
1035 1041
1036 1042 private function _endElement($parser, $name) {
1037 1043 // end tag was found...
1038   - $this->_xmltree[$parser] = substr($this->_xmltree[$parser],0, strlen($this->_xmltree[$parser]) - (strlen($name) + 1));
  1044 + $parserid = (int) $parser;
  1045 + $this->_xmltree[$parserid] = substr($this->_xmltree[$parserid],0, strlen($this->_xmltree[$parserid]) - (strlen($name) + 1));
1039 1046 }
1040 1047
1041 1048 /**
@@ -1050,19 +1057,20 @@ private function _endElement($parser, $name) {
1050 1057 */
1051 1058 private function _propfind_startElement($parser, $name, $attrs) {
1052 1059 // lower XML Names... maybe break a RFC, don't know ...
  1060 + $parserid = (int) $parser;
1053 1061
1054 1062 $propname = strtolower($name);
1055   - if (!empty($this->_xmltree[$parser])) {
1056   - $this->_xmltree[$parser] .= $propname . '_';
  1063 + if (!empty($this->_xmltree[$parserid])) {
  1064 + $this->_xmltree[$parserid] .= $propname . '_';
1057 1065 } else {
1058   - $this->_xmltree[$parser] = $propname . '_';
  1066 + $this->_xmltree[$parserid] = $propname . '_';
1059 1067 }
1060 1068
1061 1069 // translate xml tree to a flat array ...
1062   - switch($this->_xmltree[$parser]) {
  1070 + switch($this->_xmltree[$parserid]) {
1063 1071 case 'dav::multistatus_dav::response_':
1064 1072 // new element in mu
1065   - $this->_ls_ref =& $this->_ls[$parser][];
  1073 + $this->_ls_ref =& $this->_ls[$parserid][];
1066 1074 break;
1067 1075 case 'dav::multistatus_dav::response_dav::href_':
1068 1076 $this->_ls_ref_cdata = &$this->_ls_ref['href'];
@@ -1110,7 +1118,7 @@ private function _propfind_startElement($parser, $name, $attrs) {
1110 1118
1111 1119 default:
1112 1120 // handle unknown xml elements...
1113   - $this->_ls_ref_cdata = &$this->_ls_ref[$this->_xmltree[$parser]];
  1121 + $this->_ls_ref_cdata = &$this->_ls_ref[$this->_xmltree[$parserid]];
1114 1122 }
1115 1123 }
1116 1124
@@ -1145,14 +1153,15 @@ private function _propfind_cData($parser, $cdata) {
1145 1153 */
1146 1154 private function _delete_startElement($parser, $name, $attrs) {
1147 1155 // lower XML Names... maybe break a RFC, don't know ...
  1156 + $parserid = (int) $parser;
1148 1157 $propname = strtolower($name);
1149   - $this->_xmltree[$parser] .= $propname . '_';
  1158 + $this->_xmltree[$parserid] .= $propname . '_';
1150 1159
1151 1160 // translate xml tree to a flat array ...
1152   - switch($this->_xmltree[$parser]) {
  1161 + switch($this->_xmltree[$parserid]) {
1153 1162 case 'dav::multistatus_dav::response_':
1154 1163 // new element in mu
1155   - $this->_delete_ref =& $this->_delete[$parser][];
  1164 + $this->_delete_ref =& $this->_delete[$parserid][];
1156 1165 break;
1157 1166 case 'dav::multistatus_dav::response_dav::href_':
1158 1167 $this->_delete_ref_cdata = &$this->_ls_ref['href'];
@@ -1160,7 +1169,7 @@ private function _delete_startElement($parser, $name, $attrs) {
1160 1169
1161 1170 default:
1162 1171 // handle unknown xml elements...
1163   - $this->_delete_cdata = &$this->_delete_ref[$this->_xmltree[$parser]];
  1172 + $this->_delete_cdata = &$this->_delete_ref[$this->_xmltree[$parserid]];
1164 1173 }
1165 1174 }
1166 1175
@@ -1196,8 +1205,9 @@ private function _delete_cData($parser, $cdata) {
1196 1205 */
1197 1206 private function _lock_startElement($parser, $name, $attrs) {
1198 1207 // lower XML Names... maybe break a RFC, don't know ...
  1208 + $parserid = (int) $parser;
1199 1209 $propname = strtolower($name);
1200   - $this->_xmltree[$parser] .= $propname . '_';
  1210 + $this->_xmltree[$parserid] .= $propname . '_';
1201 1211
1202 1212 // translate xml tree to a flat array ...
1203 1213 /*
@@ -1206,10 +1216,10 @@ private function _lock_startElement($parser, $name, $attrs) {
1206 1216 dav::prop_dav::lockdiscovery_dav::activelock_dav::timeout_=
1207 1217 dav::prop_dav::lockdiscovery_dav::activelock_dav::locktoken_dav::href_=
1208 1218 */
1209   - switch($this->_xmltree[$parser]) {
  1219 + switch($this->_xmltree[$parserid]) {
1210 1220 case 'dav::prop_dav::lockdiscovery_dav::activelock_':
1211 1221 // new element
1212   - $this->_lock_ref =& $this->_lock[$parser][];
  1222 + $this->_lock_ref =& $this->_lock[$parserid][];
1213 1223 break;
1214 1224 case 'dav::prop_dav::lockdiscovery_dav::activelock_dav::locktype_dav::write_':
1215 1225 $this->_lock_ref_cdata = &$this->_lock_ref['locktype'];
@@ -1235,7 +1245,7 @@ private function _lock_startElement($parser, $name, $attrs) {
1235 1245 break;
1236 1246 default:
1237 1247 // handle unknown xml elements...
1238   - $this->_lock_cdata = &$this->_lock_ref[$this->_xmltree[$parser]];
  1248 + $this->_lock_cdata = &$this->_lock_ref[$this->_xmltree[$parserid]];
1239 1249
1240 1250 }
1241 1251 }
@@ -1251,8 +1261,9 @@ private function _lock_startElement($parser, $name, $attrs) {
1251 1261 * @access private
1252 1262 */
1253 1263 private function _lock_cData($parser, $cdata) {
  1264 + $parserid = (int) $parser;
1254 1265 if (trim($cdata) <> '') {
1255   - // $this->_error_log(($this->_xmltree[$parser]) . '='. htmlentities($cdata));
  1266 + // $this->_error_log(($this->_xmltree[$parserid]) . '='. htmlentities($cdata));
1256 1267 $this->_lock_ref_cdata .= $cdata;
1257 1268 } else {
1258 1269 // do nothing
@@ -1392,7 +1403,12 @@ private function get_respond() {
1392 1403 fread($this->sock, 1); // also drop off the Line Feed
1393 1404 $chunk_size=hexdec($chunk_size); // convert to a number in decimal system
1394 1405 if ($chunk_size > 0) {
1395   - $buffer .= fread($this->sock,$chunk_size);
  1406 + $read = 0;
  1407 + // Reading the chunk in one bite is not secure, we read it byte by byte.
  1408 + while ($read < $chunk_size) {
  1409 + $buffer .= fread($this->sock, 1);
  1410 + $read++;
  1411 + }
1396 1412 }
1397 1413 fread($this->sock, 2); // ditch the CRLF that trails the chunk
1398 1414 } while ($chunk_size); // till we reach the 0 length chunk (end marker)
21 repository/webdav/lib.php
@@ -38,24 +38,27 @@ public function __construct($repositoryid, $context = SYSCONTEXTID, $options = a
38 38 if ($this->options['webdav_auth'] == 'none') {
39 39 $this->options['webdav_auth'] = false;
40 40 }
41   - $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'], $this->options['webdav_password'], $this->options['webdav_auth']);
42 41 if (empty($this->options['webdav_type'])) {
43 42 $this->webdav_type = '';
44 43 } else {
45 44 $this->webdav_type = 'ssl://';
46 45 }
47 46 if (empty($this->options['webdav_port'])) {
  47 + $port = '';
48 48 if (empty($this->webdav_type)) {
49   - $this->dav->port = 80;
  49 + $this->webdav_port = 80;
50 50 } else {
51   - $this->dav->port = 443;
  51 + $this->webdav_port = 443;
  52 + $port = ':443';
52 53 }
53   - $port = '';
54 54 } else {
55   - $this->dav->port = $this->options['webdav_port'];
56   - $port = ':'.$this->options['webdav_port'];
  55 + $this->webdav_port = $this->options['webdav_port'];
  56 + $port = ':' . $this->webdav_port;
57 57 }
58 58 $this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
  59 + $this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'],
  60 + $this->options['webdav_password'], $this->options['webdav_auth'], $this->webdav_type);
  61 + $this->dav->port = $this->webdav_port;
59 62 $this->dav->debug = false;
60 63 }
61 64 public function check_login() {
@@ -103,9 +106,10 @@ public function get_listing($path='', $page = '') {
103 106 } else {
104 107 $partern = '#https://'.$this->webdav_host.'/#';
105 108 }
106   - $path = '/'.preg_replace($partern, '', $path);
  109 + $path = '/'.preg_replace($partern, '', ltrim($path, '/'));
107 110 $dir = $this->dav->ls($path);
108 111 }
  112 +
109 113 if (!is_array($dir)) {
110 114 return $ret;
111 115 }
@@ -116,9 +120,10 @@ public function get_listing($path='', $page = '') {
116 120 } else {
117 121 $filedate = '';
118 122 }
  123 +
119 124 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
120 125 // a folder
121   - if (ltrim($path, '/') != ltrim($v['href'], '/')) {
  126 + if (ltrim($path, '/') != urldecode(ltrim($v['href'], '/'))) {
122 127 $matches = array();
123 128 preg_match('#(\w+)$#i', $v['href'], $matches);
124 129 if (!empty($matches[1])) {

0 comments on commit 07b6493

Please sign in to comment.
Something went wrong with that request. Please try again.