forked from KhArtNJava/phpYandexDisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_webdav_client.php
1847 lines (1662 loc) · 67.1 KB
/
class_webdav_client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
CVS:
$Id: class_webdav_client.php,v 1.7 2004/08/18 14:12:40 chris Exp $
$Author: chris $
$Date: 2004/08/18 14:12:40 $
$Revision: 1.7 $
*/
/**
* webdav_client v0.1.3, a php based webdav client class.
*
* Copyright (C) 2003 Christian Juerges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/**
* class webdav client. a php based nearly rfc 2518 conforming client.
*
* This class implements methods to get access to an webdav server.
* Most of the methods return false on error, an passtrough integer (http response status) on success
* or an array in case of a multistatus response (207) from the webdav server.
* It's your responsibility to handle the webdav server responses in an proper manner.
*
* @author Christian Juerges <[email protected]>, Xwave GmbH, Josefstr. 92, 8005 Zuerich - Switzerland.
* @version 0.1.3
* @copyright (C) 2003/2004, Christian Juerges
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @package webdav_client
*/
class webdav_client
{
var $_debug = false;
var $_fp;
var $_server;
var $_port = 80;
var $_path = '/';
var $_user;
var $_protocol = 'HTTP/1.0';
var $_pass;
var $_socket_timeout = 5;
var $_errno;
var $_errstr;
var $_user_agent = 'php class webdav_client $Revision: 1.7 $';
var $_crlf = "\r\n";
var $_req;
var $_resp_status;
var $_parser;
var $_xmltree;
var $_tree;
var $_ls = array( );
var $_ls_ref;
var $_ls_ref_cdata;
var $_delete = array( );
var $_delete_ref;
var $_delete_ref_cdata;
var $_lock = array( );
var $_lock_ref;
var $_lock_rec_cdata;
var $_null = NULL;
var $_header = '';
var $_body = '';
var $_connection_closed = false;
var $_maxheaderlenth = 1000;
/* * #@- */
/**
* Constructor - does nothing...
*/
function webdav_client()
{
// do nothing here
}
/**
* Set webdav server. FQN or IP address.
* @param string server
*/
function set_server( $server )
{
$this->_server = $server;
}
/**
* Set tcp port of webdav server. Default is 80.
* @param int port
*/
function set_port( $port )
{
$this->_port = $port;
}
/**
* set user name for authentification
* @param string user
*/
function set_user( $user )
{
$this->_user = $user;
}
/**
* Set password for authentification
* @param string pass
*/
function set_pass( $pass )
{
$this->_pass = $pass;
}
/**
* set debug on (1) or off (0).
* produces a lot of debug messages in webservers error log if set to on (1).
* @param bool debug
*/
function set_debug( $debug )
{
$this->_debug = $debug;
}
/**
* Set which HTTP protocol will be used.
* Value 1 defines that HTTP/1.1 should be used (Keeps Connection to webdav server alive).
* Otherwise HTTP/1.0 will be used.
* @param int version
*/
function set_protocol( $version )
{
if ( $version == 1 )
{
$this->_protocol = 'HTTP/1.1';
} else
{
$this->_protocol = 'HTTP/1.0';
}
$this->_error_log( 'HTTP Protocol was set to ' . $this->_protocol );
}
/**
* Convert ISO 8601 Date and Time Profile used in RFC 2518 to an unix timestamp.
* @access private
* @param string iso8601
* @return unixtimestamp on sucess. Otherwise false.
*/
function iso8601totime( $iso8601 )
{
/*
date-time = full-date "T" full-time
full-date = date-fullyear "-" date-month "-" date-mday
full-time = partial-time time-offset
date-fullyear = 4DIGIT
date-month = 2DIGIT ; 01-12
date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
month/year
time-hour = 2DIGIT ; 00-23
time-minute = 2DIGIT ; 00-59
time-second = 2DIGIT ; 00-59, 00-60 based on leap second rules
time-secfrac = "." 1*DIGIT
time-numoffset = ("+" / "-") time-hour ":" time-minute
time-offset = "Z" / time-numoffset
partial-time = time-hour ":" time-minute ":" time-second
[time-secfrac]
*/
$regs = array( );
/* [1] [2] [3] [4] [5] [6] */
if ( ereg( '^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z$',
$iso8601, $regs ) )
{
return mktime( $regs[ 4 ], $regs[ 5 ], $regs[ 6 ], $regs[ 2 ],
$regs[ 3 ], $regs[ 1 ] );
}
// to be done: regex for partial-time...apache webdav mod never returns partial-time
return false;
}
/**
* Open's a socket to a webdav server
* @return bool true on success. Otherwise false.
*/
function open()
{
// let's try to open a socket
$this->_error_log( 'open a socket connection' );
$this->_fp = fsockopen( $this->_server, $this->_port, $this->_errno,
$this->_errstr, $this->_socket_timeout );
// set_time_limit(30);
socket_set_blocking( $this->_fp, true );
if ( !$this->_fp )
{
$this->_error_log( "$this->_errstr ($this->_errno)\n" );
return false;
} else
{
$this->_connection_closed = false;
$this->_error_log( 'socket is open: ' . $this->_fp );
return true;
}
}
/**
* Closes an open socket.
*/
function close()
{
$this->_error_log( 'closing socket ' . $this->_fp );
$this->_connection_closed = true;
fclose( $this->_fp );
}
/**
* Check's if server is a webdav compliant server.
* True if server returns a DAV Element in Header and when
* schema 1,2 is supported.
* @return bool true if server is webdav server. Otherwise false.
*/
function check_webdav()
{
$resp = $this->options();
if ( !$resp )
{
return false;
}
$this->_error_log( $resp[ 'header' ][ 'DAV' ] );
// check schema
if ( preg_match( '/1,2/', $resp[ 'header' ][ 'DAV' ] ) )
{
return true;
}
// otherwise return false
return false;
}
/**
* Get options from webdav server.
* @return array with all header fields returned from webdav server. false if server does not speak http.
*/
function options()
{
$this->_header_unset();
$this->_create_basic_request( 'OPTIONS' );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ...
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
return $response;
}
$this->_error_log( 'Response was not even http' );
return false;
}
/**
* Public method mkcol
*
* Creates a new collection/directory on a webdav server
* @param string path
* @return int status code received as reponse from webdav server (see rfc 2518)
*/
function mkcol( $path )
{
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'MKCOL' );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ...
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
/* seems to be http ... proceed
just return what server gave us
rfc 2518 says:
201 (Created) - The collection or structured resource was created in its entirety.
403 (Forbidden) - This indicates at least one of two conditions: 1) the server does not allow the creation of collections at the given
location in its namespace, or 2) the parent collection of the Request-URI exists but cannot accept members.
405 (Method Not Allowed) - MKCOL can only be executed on a deleted/non-existent resource.
409 (Conflict) - A collection cannot be made at the Request-URI until one or more intermediate collections have been created.
415 (Unsupported Media Type)- The server does not support the request type of the body.
507 (Insufficient Storage) - The resource does not have sufficient space to record the state of the resource after the execution of this method.
*/
return $response[ 'status' ][ 'status-code' ];
}
}
/**
* Public method get
*
* Gets a file from a webdav collection.
* @param string path, string &buffer
* @return status code and &$buffer (by reference) with response data from server on success. False on error.
*/
function get( $path, &$buffer )
{
// reopen it
// be sure to close the open socket.
$this->close();
$this->_reopen();
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'GET' );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
// print_r($response);
// validate the response
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
// seems to be http ... proceed
// We expect a 200 code
if ( $response[ 'status' ][ 'status-code' ] == 200 )
{
$this->_error_log( 'returning buffer with ' . strlen( $response[ 'body' ] ) . ' bytes.' );
$buffer = $response[ 'body' ];
// echo "+++!!!!!!!!!!!" . " " . mb_strlen( $response[ 'body' ],
// "windows-1251" ) . "+++" . $response[ 'header' ][ 'Content-Length' ]."\r\n \r\n";
//
// echo "+++!!!!!!!!!!!" . " " . mb_strlen( $response[ 'body' ],
// "cp1251" ) . "+++" . $response[ 'header' ][ 'Content-Length' ]."\r\n \r\n";
if ( mb_strlen( $response[ 'body' ], "cp1251" ) < $response[ 'header' ][ 'Content-Length' ] )
{
$buffer = null;
// print_r($response[ 'body' ]);
// mb_internal_encoding( "UTF-8" );
return false;
}
}
return $response[ 'status' ][ 'status-code' ];
}
// ups: no http status was returned ?
return false;
}
/**
* Public method put
*
* Puts a file into a collection.
* Data is putted as one chunk!
* @param string path, string data
* @return int status-code read from webdavserver. False on error.
*/
function put( $path, $data )
{
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'PUT' );
// add more needed header information ...
$this->_header_add( 'Content-length: ' . strlen( $data ) );
$this->_header_add( 'Content-type: application/octet-stream' );
// send header
$this->_send_request();
// send the rest (data)
fputs( $this->_fp, $data );
$this->_get_respond();
$response = $this->_process_respond();
// validate the response
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
// seems to be http ... proceed
// We expect a 200 or 204 status code
// see rfc 2068 - 9.6 PUT...
// print 'http ok<br>';
return $response[ 'status' ][ 'status-code' ];
}
// ups: no http status was returned ?
return false;
}
/**
* Public method put_file
*
* Read a file as stream and puts it chunk by chunk into webdav server collection.
* Look at php documenation for legal filenames with fopen();
*
* @param string targetpath, string filename
* @return int status code. False on error.
*/
function put_file( $path, $filename )
{
// try to open the file ...
$handle = @fopen( $filename, 'r' );
if ( $handle )
{
// $this->_fp = pfsockopen ($this->_server, $this->_port, $this->_errno, $this->_errstr, $this->_socket_timeout);
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'PUT' );
// add more needed header information ...
$this->_header_add( 'Content-length: ' . filesize( $filename ) );
$this->_header_add( 'Content-type: application/octet-stream' );
// send header
$this->_send_request();
while ( !feof( $handle ) )
{
fputs( $this->_fp, fgets( $handle, 4096 ) );
}
fclose( $handle );
$this->_get_respond();
$response = $this->_process_respond();
// validate the response
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
// seems to be http ... proceed
// We expect a 200 or 204 status code
// see rfc 2068 - 9.6 PUT...
// print 'http ok<br>';
return $response[ 'status' ][ 'status-code' ];
}
// ups: no http status was returned ?
return false;
} else
{
$this->_error_log( 'could not open ' . $filename );
return false;
}
}
/**
* Public method get_file
*
* Gets a file from a collection into local filesystem.
* fopen() is used.
* @param string srcpath, string localpath
* @return true on success. false on error.
*/
function get_file( $srcpath, $localpath )
{
if ( $this->get( $srcpath, $buffer ) )
{
// echo "----------------------" . " " . mb_strlen( $buffer,
// "cp1251" ) ;
$handle = fopen( $localpath, 'w+' );
if ( $handle )
{
fwrite( $handle, $buffer );
fclose( $handle );
return true;
} else
{
return false;
}
} else
{
return false;
}
}
/**
* Public method copy_file
*
* Copy a file on webdav server
* Duplicates a file on the webdav server (serverside).
* All work is done on the webdav server. If you set param overwrite as true,
* the target will be overwritten.
*
* @param string src_path, string dest_path, bool overwrite
* @return int status code (look at rfc 2518). false on error.
*/
function copy_file( $src_path, $dst_path, $overwrite )
{
$this->_path = $this->_translate_uri( $src_path );
$this->_header_unset();
$this->_create_basic_request( 'COPY' );
$this->_header_add( sprintf( 'Destination: http://%s%s', $this->_server,
$this->_translate_uri( $dst_path ) ) );
if ( $overwrite )
{
$this->_header_add( 'Overwrite: T' );
} else
{
$this->_header_add( 'Overwrite: F' );
}
$this->_header_add( '' );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ...
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
/* seems to be http ... proceed
just return what server gave us (as defined in rfc 2518) :
201 (Created) - The source resource was successfully copied. The copy operation resulted in the creation of a new resource.
204 (No Content) - The source resource was successfully copied to a pre-existing destination resource.
403 (Forbidden) - The source and destination URIs are the same.
409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
or the Overwrite header is "F" and the state of the destination resource is non-null.
423 (Locked) - The destination resource was locked.
502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
507 (Insufficient Storage) - The destination resource does not have sufficient space to record the state of the resource after the
execution of this method.
*/
return $response[ 'status' ][ 'status-code' ];
}
return false;
}
/**
* Public method copy_coll
*
* Copy a collection on webdav server
* Duplicates a collection on the webdav server (serverside).
* All work is done on the webdav server. If you set param overwrite as true,
* the target will be overwritten.
*
* @param string src_path, string dest_path, bool overwrite
* @return int status code (look at rfc 2518). false on error.
*/
function copy_coll( $src_path, $dst_path, $overwrite )
{
$this->_path = $this->_translate_uri( $src_path );
$this->_header_unset();
$this->_create_basic_request( 'COPY' );
$this->_header_add( sprintf( 'Destination: http://%s%s', $this->_server,
$this->_translate_uri( $dst_path ) ) );
$this->_header_add( 'Depth: Infinity' );
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
$xml .= "<d:propertybehavior xmlns:d=\"DAV:\">\r\n";
$xml .= " <d:keepalive>*</d:keepalive>\r\n";
$xml .= "</d:propertybehavior>\r\n";
$this->_header_add( 'Content-length: ' . strlen( $xml ) );
$this->_header_add( 'Content-type: text/xml' );
$this->_send_request();
// send also xml
fputs( $this->_fp, $xml );
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ...
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
/* seems to be http ... proceed
just return what server gave us (as defined in rfc 2518) :
201 (Created) - The source resource was successfully copied. The copy operation resulted in the creation of a new resource.
204 (No Content) - The source resource was successfully copied to a pre-existing destination resource.
403 (Forbidden) - The source and destination URIs are the same.
409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
or the Overwrite header is "F" and the state of the destination resource is non-null.
423 (Locked) - The destination resource was locked.
502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
507 (Insufficient Storage) - The destination resource does not have sufficient space to record the state of the resource after the
execution of this method.
*/
return $response[ 'status' ][ 'status-code' ];
}
return false;
}
/**
* Public method move
*
* Move a file or collection on webdav server (serverside)
* If you set param overwrite as true, the target will be overwritten.
*
* @param string src_path, string dest_path, bool overwrite
* @return int status code (look at rfc 2518). false on error.
*/
// --------------------------------------------------------------------------
// public method move
// move/rename a file/collection on webdav server
function move( $src_path, $dst_path, $overwrite )
{
$this->_path = $this->_translate_uri( $src_path );
$this->_header_unset();
$this->_create_basic_request( 'MOVE' );
// dst_path should not be uri translated....
$this->_header_add( sprintf( 'Destination: http://%s%s', $this->_server,
$dst_path ) );
if ( $overwrite )
{
$this->_header_add( 'Overwrite: T' );
} else
{
$this->_header_add( 'Overwrite: F' );
}
$this->_header_add( '' );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ...
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
/* seems to be http ... proceed
just return what server gave us (as defined in rfc 2518) :
201 (Created) - The source resource was successfully moved, and a new resource was created at the destination.
204 (No Content) - The source resource was successfully moved to a pre-existing destination resource.
403 (Forbidden) - The source and destination URIs are the same.
409 (Conflict) - A resource cannot be created at the destination until one or more intermediate collections have been created.
412 (Precondition Failed) - The server was unable to maintain the liveness of the properties listed in the propertybehavior XML element
or the Overwrite header is "F" and the state of the destination resource is non-null.
423 (Locked) - The source or the destination resource was locked.
502 (Bad Gateway) - This may occur when the destination is on another server and the destination server refuses to accept the resource.
201 (Created) - The collection or structured resource was created in its entirety.
403 (Forbidden) - This indicates at least one of two conditions: 1) the server does not allow the creation of collections at the given
location in its namespace, or 2) the parent collection of the Request-URI exists but cannot accept members.
405 (Method Not Allowed) - MKCOL can only be executed on a deleted/non-existent resource.
409 (Conflict) - A collection cannot be made at the Request-URI until one or more intermediate collections have been created.
415 (Unsupported Media Type)- The server does not support the request type of the body.
507 (Insufficient Storage) - The resource does not have sufficient space to record the state of the resource after the execution of this method.
*/
return $response[ 'status' ][ 'status-code' ];
}
return false;
}
/**
* Public method lock
*
* Lock a file or collection.
*
* Lock uses this->_user as lock owner.
*
* @param string path
* @return int status code (look at rfc 2518). false on error.
*/
function lock( $path )
{
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'LOCK' );
$this->_header_add( 'Timeout: Infinite' );
$this->_header_add( 'Content-type: text/xml' );
// create the xml request ...
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
$xml .= "<D:lockinfo xmlns:D='DAV:'\r\n>";
$xml .= " <D:lockscope><D:exclusive/></D:lockscope>\r\n";
$xml .= " <D:locktype><D:write/></D:locktype>\r\n";
$xml .= " <D:owner>\r\n";
$xml .= " <D:href>" . ($this->_user) . "</D:href>\r\n";
$xml .= " </D:owner>\r\n";
$xml .= "</D:lockinfo>\r\n";
$this->_header_add( 'Content-length: ' . strlen( $xml ) );
$this->_send_request();
// send also xml
fputs( $this->_fp, $xml );
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ... (only basic validation)
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
/* seems to be http ... proceed
rfc 2518 says:
200 (OK) - The lock request succeeded and the value of the lockdiscovery property is included in the body.
412 (Precondition Failed) - The included lock token was not enforceable on this resource or the server could not satisfy the
request in the lockinfo XML element.
423 (Locked) - The resource is locked, so the method has been rejected.
*/
switch ( $response[ 'status' ][ 'status-code' ] )
{
case 200:
// collection was successfully locked... see xml response to get lock token...
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();
// forget old data...
unset( $this->_lock[ $this->_parser ] );
unset( $this->_xmltree[ $this->_parser ] );
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 );
xml_set_element_handler( $this->_parser,
"_lock_startElement", "_endElement" );
xml_set_character_data_handler( $this->_parser,
"_lock_cdata" );
if ( !xml_parse( $this->_parser, $response[ 'body' ] ) )
{
die( sprintf( "XML error: %s at line %d",
xml_error_string( xml_get_error_code( $this->_parser ) ),
xml_get_current_line_number( $this->_parser ) ) );
}
// Free resources
xml_parser_free( $this->_parser );
// add status code to array
$this->_lock[ $this->_parser ][ 'status' ] = 200;
return $this->_lock[ $this->_parser ];
} else
{
print 'Missing Content-Type: text/xml header in response.<br>';
}
return false;
default:
// hmm. not what we expected. Just return what we got from webdav server
// someone else has to handle it.
$this->_lock[ 'status' ] = $response[ 'status' ][ 'status-code' ];
return $this->_lock;
}
}
}
/**
* Public method unlock
*
* Unlock a file or collection.
*
* @param string path, string locktoken
* @return int status code (look at rfc 2518). false on error.
*/
function unlock( $path, $locktoken )
{
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'UNLOCK' );
$this->_header_add( sprintf( 'Lock-Token: <%s>', $locktoken ) );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
/* seems to be http ... proceed
rfc 2518 says:
204 (OK) - The 204 (No Content) status code is used instead of 200 (OK) because there is no response entity body.
*/
return $response[ 'status' ][ 'status-code' ];
}
return false;
}
/** --------------------------------------------------------------------------
* Public method delete
*
* deletes a collection/directory on a webdav server
* @param string path
* @return int status code (look at rfc 2518). false on error.
*/
function delete( $path )
{
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'DELETE' );
/* $this->_header_add('Content-Length: 0'); */
$this->_header_add( '' );
$this->_send_request();
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ...
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
// seems to be http ... proceed
// We expect a 207 Multi-Status status code
// print 'http ok<br>';
switch ( $response[ 'status' ][ 'status-code' ] )
{
case 207:
// collection was NOT deleted... see xml response for reason...
// next there should be a Content-Type: text/xml; charset="utf-8" header line
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();
// forget old data...
unset( $this->_delete[ $this->_parser ] );
unset( $this->_xmltree[ $this->_parser ] );
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 );
xml_set_element_handler( $this->_parser,
"_delete_startElement", "_endElement" );
xml_set_character_data_handler( $this->_parser,
"_delete_cdata" );
if ( !xml_parse( $this->_parser, $response[ 'body' ] ) )
{
die( sprintf( "XML error: %s at line %d",
xml_error_string( xml_get_error_code( $this->_parser ) ),
xml_get_current_line_number( $this->_parser ) ) );
}
print_r( $this->_delete[ $this->_parser ] );
print "<br>";
// Free resources
xml_parser_free( $this->_parser );
$this->_delete[ $this->_parser ][ 'status' ] = $response[ 'status' ][ 'status-code' ];
return $this->_delete[ $this->_parser ];
} else
{
print 'Missing Content-Type: text/xml header in response.<br>';
}
return false;
default:
// collection or file was successfully deleted
$this->_delete[ 'status' ] = $response[ 'status' ][ 'status-code' ];
return $this->_delete;
}
}
}
/**
* Public method ls
*
* Get's directory information from webdav server into flat a array using PROPFIND
* @param string path
* @return array dirinfo, false on error
*/
function ls( $path )
{
if ( trim( $path ) == '' )
{
$this->_error_log( 'Missing a path in method ls' );
return false;
}
$this->_path = $this->_translate_uri( $path );
$this->_header_unset();
$this->_create_basic_request( 'PROPFIND' );
$this->_header_add( 'Depth: 1' );
$this->_header_add( 'Content-type: text/xml' );
// create profind xml request...
$xml = "<?xml version=\"1.0\"?>\r\n";
$xml .= "<A:propfind xmlns:A=\"DAV:\">\r\n";
// shall we get all properties ?
$xml .= " <A:allprop/>\r\n";
// or should we better get only wanted props ?
$xml .= "</A:propfind>\r\n";
$this->_header_add( 'Content-length: ' . strlen( $xml ) );
$this->_send_request();
$this->_error_log( $xml );
fputs( $this->_fp, $xml );
$this->_get_respond();
$response = $this->_process_respond();
// validate the response ... (only basic validation)
// check http-version
if ( $response[ 'status' ][ 'http-version' ] == 'HTTP/1.1' ||
$response[ 'status' ][ 'http-version' ] == 'HTTP/1.0' )
{
// seems to be http ... proceed
// We expect a 207 Multi-Status status code
// print 'http ok<br>';
if ( strcmp( $response[ 'status' ][ 'status-code' ], '207' ) == 0 )
{
// ok so far
// next there should be a Content-Type: text/xml; charset="utf-8" header line
if ( strcmp( $response[ 'header' ][ 'Content-Type' ],
'text/xml; charset="utf-8"' ) == 0 ||
strcmp( $response[ 'header' ][ 'Content-Type' ],
'application/xml; charset="utf-8"' ) == 0 )
{
// ok let's get the content of the xml stuff
$this->_parser = xml_parser_create_ns();
// forget old data...
unset( $this->_ls[ $this->_parser ] );
unset( $this->_xmltree[ $this->_parser ] );
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 );
xml_set_element_handler( $this->_parser,
"_propfind_startElement", "_endElement" );
xml_set_character_data_handler( $this->_parser,
"_propfind_cdata" );
if ( !xml_parse( $this->_parser, $response[ 'body' ] ) )
{
die( sprintf( "XML error: %s at line %d",
xml_error_string( xml_get_error_code( $this->_parser ) ),
xml_get_current_line_number( $this->_parser ) ) );
}
// Free resources
xml_parser_free( $this->_parser );
return $this->_ls[ $this->_parser ];
} else
{
$this->_error_log( 'Missing Content-Type: text/xml header in response!!' );
return false;
}
}
}
// response was not http
$this->_error_log( 'Ups in method ls: error in response from server' );
return false;
}
/**
* Public method gpi
*
* Get's path information from webdav server for one element
* @param string path
* @return array dirinfo. false on error
*/
function gpi( $path )
{
// split path by last "/"
$path = rtrim( $path, "/" );
$item = basename( $path );
$dir = dirname( $path );
$list = $this->ls( $dir );
// be sure it is an array
if ( is_array( $list ) )
{
foreach ( $list as $e )
{
$fullpath = urldecode( $e[ 'href' ] );
$filename = basename( $fullpath );