1818namespace repository_owncloud ;
1919use core_php_time_limit ;
2020
21+ defined ('MOODLE_INTERNAL ' ) || die ();
22+
23+ if ($ CFG ->branch >= 34 ) {
24+ require_once ($ CFG ->libdir . '/webdavlib.php ' );
25+
26+ /**
27+ * Class owncloud_client; aliases the webdav_client class.
28+ * It has the necessary modifications since MDL-59844; i.e. Moodle 3.4.
29+
30+ * @package repository_owncloud
31+ * @copyright 2017 Jan Dageförde (Learnweb, University of Münster)
32+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33+ */
34+ class owncloud_client extends \webdav_client {
35+ // TODO remove as soon as M3.3 is not supported anymore.
36+ }
37+ } else {
38+
2139/**
40+ * Moodle 3.3 polyfill for:
41+ * A Moodle-modified WebDAV client, based on
2242 * webdav_client v0.1.5, a php based webdav client class.
2343 * class webdav client. a php based nearly RFC 2518 conforming client.
2444 *
25- *
2645 * This class implements methods to get access to an webdav server.
2746 * Most of the methods are returning boolean false on error, an integer status (http response status) on success
2847 * or an array in case of a multistatus response (207) from the webdav server. Look at the code which keys are used in arrays.
2948 * It's your responsibility to handle the webdav server responses in an proper manner.
3049 * Please notice that all Filenames coming from or going to the webdav server should be UTF-8 encoded (see RFC 2518).
3150 * This class tries to convert all you filenames into utf-8 when it's needed.
3251 *
33- * This class was augmented in 2017 to account for OAuth 2-compatible bearer authentication.
52+ * Moodle modifications:
53+ * * Moodle 3.4: Add support for OAuth 2 bearer token-based authentication
3454 *
3555 * @package moodlecore
3656 * @author Christian Juerges <[email protected] >, Xwave GmbH, Josefstr. 92, 8005 Zuerich - Switzerland 3757 * @copyright (C) 2003/2004, Christian Juerges
3858 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
39- * @version 0.1.5
4059 */
4160
4261class owncloud_client {
@@ -59,7 +78,7 @@ class owncloud_client {
5978 private $ _socket_timeout = 5 ;
6079 private $ _errno ;
6180 private $ _errstr ;
62- private $ _user_agent = 'Moodle WebDav Client for ownCloud ' ;
81+ private $ _user_agent = 'Moodle WebDav Client (3.3 oauth polyfill) ' ;
6382 private $ _crlf = "\r\n" ;
6483 private $ _req ;
6584 private $ _resp_status ;
@@ -86,23 +105,17 @@ class owncloud_client {
86105 private $ _nc = 0 ;
87106
88107 /**
89- * OAuth 2 client; is expected to hold the token for authenticated accesses.
90- * @var \core\oauth2\client
91- */
92- private $ oauthclient ;
93-
94- /**
95- * Prefix to the WebDAV server on host, e.g. /remote.php/webdav/ on most ownCloud installations.
108+ * OAuth token used for bearer auth.
96109 * @var string
97110 */
98- private $ pathprefix ;
111+ private $ oauthtoken ;
99112
100113 /**#@-*/
101114
102115 /**
103116 * Constructor - Initialise class variables
104117 */
105- public function __construct ($ server = '' , $ user = '' , $ pass = '' , $ auth = false , $ socket = '' , $ oauthclient = null , $ pathprefix = ' / ' ) {
118+ public function __construct ($ server = '' , $ user = '' , $ pass = '' , $ auth = false , $ socket = '' , $ oauthtoken = ' ' ) {
106119 if (!empty ($ server )) {
107120 $ this ->_server = $ server ;
108121 }
@@ -112,13 +125,9 @@ public function __construct($server = '', $user = '', $pass = '', $auth = false,
112125 }
113126 $ this ->_auth = $ auth ;
114127 $ this ->_socket = $ socket ;
115- // If provided, add OAuth client and path prefix.
116- $ this ->oauthclient = $ oauthclient ;
117- // Remove trailing slash, because future uses will come with a leading slash.
118- if (strlen ($ pathprefix ) > 0 && substr ($ pathprefix , -1 ) === '/ ' ) {
119- $ pathprefix = substr ($ pathprefix , 0 , -1 );
128+ if ($ auth == 'bearer ' ) {
129+ $ this ->oauthtoken = $ oauthtoken ;
120130 }
121- $ this ->pathprefix = $ pathprefix ;
122131 }
123132 public function __set ($ key , $ value ) {
124133 $ property = '_ ' . $ key ;
@@ -242,8 +251,8 @@ function options() {
242251 // check http-version
243252 if ($ response ['status ' ]['http-version ' ] == 'HTTP/1.1 ' ||
244253 $ response ['status ' ]['http-version ' ] == 'HTTP/1.0 ' ) {
245- return $ response ;
246- }
254+ return $ response ;
255+ }
247256 $ this ->_error_log ('Response was not even http ' );
248257 return false ;
249258
@@ -307,19 +316,19 @@ function get($path, &$buffer, $fp = null) {
307316 // validate the response
308317 // check http-version
309318 if ($ http_version == 'HTTP/1.1 ' || $ http_version == 'HTTP/1.0 ' ) {
310- // seems to be http ... proceed
311- // We expect a 200 code
312- if ($ response ['status ' ]['status-code ' ] == 200 ) {
313- if (!is_null ($ fp )) {
314- $ stat = fstat ($ fp );
315- $ this ->_error_log ('file created with ' . $ stat ['size ' ] . ' bytes. ' );
316- } else {
317- $ this ->_error_log ('returning buffer with ' . strlen ($ response ['body ' ]) . ' bytes. ' );
318- $ buffer = $ response ['body ' ];
319+ // seems to be http ... proceed
320+ // We expect a 200 code
321+ if ($ response ['status ' ]['status-code ' ] == 200 ) {
322+ if (!is_null ($ fp )) {
323+ $ stat = fstat ($ fp );
324+ $ this ->_error_log ('file created with ' . $ stat ['size ' ] . ' bytes. ' );
325+ } else {
326+ $ this ->_error_log ('returning buffer with ' . strlen ($ response ['body ' ]) . ' bytes. ' );
327+ $ buffer = $ response ['body ' ];
328+ }
319329 }
330+ return $ response ['status ' ]['status-code ' ];
320331 }
321- return $ response ['status ' ]['status-code ' ];
322- }
323332 // ups: no http status was returned ?
324333 return false ;
325334 }
@@ -350,12 +359,12 @@ function put($path, $data ) {
350359 // check http-version
351360 if ($ response ['status ' ]['http-version ' ] == 'HTTP/1.1 ' ||
352361 $ response ['status ' ]['http-version ' ] == 'HTTP/1.0 ' ) {
353- // seems to be http ... proceed
354- // We expect a 200 or 204 status code
355- // see rfc 2068 - 9.6 PUT...
356- // print 'http ok<br>';
357- return $ response ['status ' ]['status-code ' ];
358- }
362+ // seems to be http ... proceed
363+ // We expect a 200 or 204 status code
364+ // see rfc 2068 - 9.6 PUT...
365+ // print 'http ok<br>';
366+ return $ response ['status ' ]['status-code ' ];
367+ }
359368 // ups: no http status was returned ?
360369 return false ;
361370 }
@@ -398,12 +407,12 @@ function put_file($path, $filename) {
398407 // check http-version
399408 if ($ response ['status ' ]['http-version ' ] == 'HTTP/1.1 ' ||
400409 $ response ['status ' ]['http-version ' ] == 'HTTP/1.0 ' ) {
401- // seems to be http ... proceed
402- // We expect a 200 or 204 status code
403- // see rfc 2068 - 9.6 PUT...
404- // print 'http ok<br>';
405- return $ response ['status ' ]['status-code ' ];
406- }
410+ // seems to be http ... proceed
411+ // We expect a 200 or 204 status code
412+ // see rfc 2068 - 9.6 PUT...
413+ // print 'http ok<br>';
414+ return $ response ['status ' ]['status-code ' ];
415+ }
407416 // ups: no http status was returned ?
408417 return false ;
409418 } else {
@@ -424,8 +433,7 @@ function put_file($path, $filename) {
424433 * @return bool true on success. false on error.
425434 */
426435 function get_file ($ srcpath , $ localpath ) {
427- // Prepend with WebDAV root.
428- $ srcpath = $ this ->pathprefix . $ srcpath ;
436+
429437 $ localpath = $ this ->utf_decode_path ($ localpath );
430438
431439 $ handle = fopen ($ localpath , 'wb ' );
@@ -470,21 +478,21 @@ function copy_file($src_path, $dst_path, $overwrite) {
470478 // check http-version
471479 if ($ response ['status ' ]['http-version ' ] == 'HTTP/1.1 ' ||
472480 $ response ['status ' ]['http-version ' ] == 'HTTP/1.0 ' ) {
473- /* seems to be http ... proceed
474- just return what server gave us (as defined in rfc 2518) :
475- 201 (Created) - The source resource was successfully copied. The copy operation resulted in the creation of a new resource.
476- 204 (No Content) - The source resource was successfully copied to a pre-existing destination resource.
477- 403 (Forbidden) - The source and destination URIs are the same.
478- 409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
479- 412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
480- or the Overwrite header is "F" and the state of the destination resource is non-null.
481- 423 (Locked) - The destination resource was locked.
482- 502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
483- 507 (Insufficient Storage) - The destination resource does not have sufficient space to record the state of the resource after the
484- execution of this method.
485- */
486- return $ response ['status ' ]['status-code ' ];
487- }
481+ /* seems to be http ... proceed
482+ just return what server gave us (as defined in rfc 2518) :
483+ 201 (Created) - The source resource was successfully copied. The copy operation resulted in the creation of a new resource.
484+ 204 (No Content) - The source resource was successfully copied to a pre-existing destination resource.
485+ 403 (Forbidden) - The source and destination URIs are the same.
486+ 409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
487+ 412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
488+ or the Overwrite header is "F" and the state of the destination resource is non-null.
489+ 423 (Locked) - The destination resource was locked.
490+ 502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
491+ 507 (Insufficient Storage) - The destination resource does not have sufficient space to record the state of the resource after the
492+ execution of this method.
493+ */
494+ return $ response ['status ' ]['status-code ' ];
495+ }
488496 return false ;
489497 }
490498
@@ -523,21 +531,21 @@ function copy_coll($src_path, $dst_path, $overwrite) {
523531 // check http-version
524532 if ($ response ['status ' ]['http-version ' ] == 'HTTP/1.1 ' ||
525533 $ response ['status ' ]['http-version ' ] == 'HTTP/1.0 ' ) {
526- /* seems to be http ... proceed
527- just return what server gave us (as defined in rfc 2518) :
528- 201 (Created) - The source resource was successfully copied. The copy operation resulted in the creation of a new resource.
529- 204 (No Content) - The source resource was successfully copied to a pre-existing destination resource.
530- 403 (Forbidden) - The source and destination URIs are the same.
531- 409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
532- 412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
533- or the Overwrite header is "F" and the state of the destination resource is non-null.
534- 423 (Locked) - The destination resource was locked.
535- 502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
536- 507 (Insufficient Storage) - The destination resource does not have sufficient space to record the state of the resource after the
537- execution of this method.
538- */
539- return $ response ['status ' ]['status-code ' ];
540- }
534+ /* seems to be http ... proceed
535+ just return what server gave us (as defined in rfc 2518) :
536+ 201 (Created) - The source resource was successfully copied. The copy operation resulted in the creation of a new resource.
537+ 204 (No Content) - The source resource was successfully copied to a pre-existing destination resource.
538+ 403 (Forbidden) - The source and destination URIs are the same.
539+ 409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
540+ 412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
541+ or the Overwrite header is "F" and the state of the destination resource is non-null.
542+ 423 (Locked) - The destination resource was locked.
543+ 502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
544+ 507 (Insufficient Storage) - The destination resource does not have sufficient space to record the state of the resource after the
545+ execution of this method.
546+ */
547+ return $ response ['status ' ]['status-code ' ];
548+ }
541549 return false ;
542550 }
543551
@@ -594,8 +602,8 @@ function move($src_path,$dst_path, $overwrite) {
594602 415 (Unsupported Media Type)- The server does not support the request type of the body.
595603 507 (Insufficient Storage) - The resource does not have sufficient space to record the state of the resource after the execution of this method.
596604 */
597- return $ response ['status ' ]['status-code ' ];
598- }
605+ return $ response ['status ' ]['status-code ' ];
606+ }
599607 return false ;
600608 }
601609
@@ -642,7 +650,7 @@ function lock($path) {
642650 423 (Locked) - The resource is locked, so the method has been rejected.
643651 */
644652
645- switch ($ response ['status ' ]['status-code ' ]) {
653+ switch ($ response ['status ' ]['status-code ' ]) {
646654 case 200 :
647655 // collection was successfully locked... see xml response to get lock token...
648656 if (strcmp ($ response ['header ' ]['Content-Type ' ], 'text/xml; charset="utf-8" ' ) == 0 ) {
@@ -680,8 +688,8 @@ function lock($path) {
680688 // someone else has to handle it.
681689 $ this ->_lock ['status ' ] = $ response ['status ' ]['status-code ' ];
682690 return $ this ->_lock ;
691+ }
683692 }
684- }
685693
686694
687695 }
@@ -709,8 +717,8 @@ function unlock($path, $locktoken) {
709717 rfc 2518 says:
710718 204 (OK) - The 204 (No Content) status code is used instead of 200 (OK) because there is no response entity body.
711719 */
712- return $ response ['status ' ]['status-code ' ];
713- }
720+ return $ response ['status ' ]['status-code ' ];
721+ }
714722 return false ;
715723 }
716724
@@ -735,11 +743,11 @@ function delete($path) {
735743 // check http-version
736744 if ($ response ['status ' ]['http-version ' ] == 'HTTP/1.1 ' ||
737745 $ response ['status ' ]['http-version ' ] == 'HTTP/1.0 ' ) {
738- // seems to be http ... proceed
739- // We expect a 207 Multi-Status status code
740- // print 'http ok<br>';
746+ // seems to be http ... proceed
747+ // We expect a 207 Multi-Status status code
748+ // print 'http ok<br>';
741749
742- switch ($ response ['status ' ]['status-code ' ]) {
750+ switch ($ response ['status ' ]['status-code ' ]) {
743751 case 207 :
744752 // collection was NOT deleted... see xml response for reason...
745753 // next there should be a Content-Type: text/xml; charset="utf-8" header line
@@ -780,8 +788,8 @@ function delete($path) {
780788 return $ this ->_delete ;
781789
782790
791+ }
783792 }
784- }
785793
786794 }
787795
@@ -801,7 +809,7 @@ function ls($path) {
801809 $ this ->_error_log ('Missing a path in method ls ' );
802810 return false ;
803811 }
804- $ this ->_path = $ this ->translate_uri ($ this -> pathprefix . $ path );
812+ $ this ->_path = $ this ->translate_uri ($ path );
805813
806814 $ this ->header_unset ();
807815 $ this ->create_basic_request ('PROPFIND ' );
@@ -1351,7 +1359,7 @@ private function create_basic_request($method) {
13511359 }
13521360 } else if ($ this ->_auth == 'bearer ' ) {
13531361 // Send a bearer token if OAuth 2 is active.
1354- $ this ->header_add (sprintf ('Authorization: Bearer %s ' , $ this ->oauthclient -> get_accesstoken ()-> token ));
1362+ $ this ->header_add (sprintf ('Authorization: Bearer %s ' , $ this ->oauthtoken ));
13551363 }
13561364 }
13571365
@@ -1761,3 +1769,4 @@ private function _error_log($err_string) {
17611769 }
17621770 }
17631771}
1772+ }
0 commit comments