-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpsvnclient.php
1088 lines (969 loc) · 39.7 KB
/
phpsvnclient.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
/*
* **************************************************************************
* Copyright (C) 2007-2008 by Sixdegrees *
* "Working with freedom" *
* http://www.sixdegrees.com.br *
* *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to *
* the following conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR *
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
* OTHER DEALINGS IN THE SOFTWARE. *
* **************************************************************************
*/
define("PHPSVN_DIR", dirname(__FILE__));
define("LOG_FILE", PHPSVN_DIR . time() . ".log.html");
require_once PHPSVN_DIR . "/http.php";
require_once PHPSVN_DIR . "/xml_parser.php"; // to be dropped?
require_once PHPSVN_DIR . "/definitions.php";
require_once PHPSVN_DIR . "/xml2Array.php";
/**
* PHP SVN CLIENT
*
* This class is a SVN client. It can perform read operations
* to a SVN server (over Web-DAV).
* It can get directory files, file contents, logs. All the operaration
* could be done for a specific version or for the last version.
*
* @author Cesar D. Rodas <[email protected]>
* @license BSD License
*/
class phpsvnclient {
/**
* SVN Repository URL
*
* @var string
* @access private
*/
private $_url;
/**
* Cache, for don't request the same thing in a
* short period of time.
*
* @var string
* @access private
*/
private $_cache;
/**
* HTTP Client object
*
* @var object
* @access private
*/
private $_http;
/**
* Respository Version.
*
* @access private
* @var interger
*/
private $_repVersion;
/**
* Password
*
* @access private
* @var string
*/
private $pass;
/**
* Password
*
* @access private
* @var string
*/
private $user;
/**
* Last error number
*
* Possible values are NOT_ERROR, NOT_FOUND, AUTH_REQUIRED, UNKOWN_ERROR
*
* @access public
* @var integer
*/
public $errNro;
/**
* Number of actual revision local repository.
* @var Integer, Long
*/
private $actVersion;
private $storeDirectoryFiles = array();
private $lastDirectoryFiles;
private $file_size;
private $file_size_founded = false;
/**
* The path to the file to perform after update procedure
* or checkout of a local repository.
* @var String
*/
private $path_exec_after_completition = '';
/**
* Array with MIME types.
* @var Array
*/
private $mime_array;
public function phpsvnclient($url = 'http://phpsvnclient.googlecode.com/svn/', $user = false, $pass = false) {
$this->__construct($url, $user, $pass);
register_shutdown_function(array(&$this, '__destruct'));
}
public function __construct($url = 'http://phpsvnclient.googlecode.com/svn/', $user = false, $pass = false) {
$http = & $this->_http;
$http = new http_class;
$http->user_agent = "phpsvnclient (http://phpsvnclient.googlecode.com/)";
$this->_url = $url;
$this->user = $user;
$this->pass = $pass;
$this->actVersion = $this->getVersion();
}
/**
* Function for creating directories.
* @param type $path The path to the directory that will be created.
*/
function createDirs($path) {
$dirs = explode("/", $path);
foreach ($dirs as $dir) {
if ($dir != "") {
$createDir = substr($path, 0, strpos($path, $dir) + strlen($dir));
@mkdir($createDir);
}
}
}
/**
* Function for the recursive removal of directories.
* @param type $path The path to the directory to be deleted.
* @return type Returns the status of a function or function rmdir unlink.
*/
function removeDirs($path) {
if (is_dir($path)) {
$entries = scandir($path);
if ($entries === false) {
$entries = array();
}
foreach ($entries as $entry) {
if ($entry != '.' && $entry != '..') {
$this->removeDirs($path . '/' . $entry);
}
}
return rmdir($path);
} else {
return unlink($path);
}
}
/**
* Function for logging.
* @param type $contents The line for entry in the log file.
*/
function logging($contents) {
$hOut = fopen(LOG_FILE, 'a+');
fwrite($hOut, $contents);
fclose($hOut);
}
/**
* Public Functions
*/
/**
* Performs a checkout and creates files and folders.
*
* @param string $folder Defaults to disk root
* @param string $outPath Defaults to current folder (.)
* @param boolean $checkFiles Whether it is necessary to check the received
* files in the sizes. Can be useful in case often files are accepted
* with an error.
*/
public function checkOut($folder = '/', $outPath = '.', $checkFiles = false) {
while ($outPath[strlen($outPath) - 1] == '/' && strlen($outPath) > 1) {
$outPath = substr($outPath, 0, -1);
}
$tree = $this->getDirectoryTree($folder);
if (!file_exists($outPath)) {
mkdir($outPath, 0777, TRUE);
}
foreach ($tree as $file) {
$path = $file['path'];
$tmp = strstr(trim($path, '/'), trim($folder, '/'));
$createPath = $outPath . '/' . ($tmp ? substr($tmp, strlen(trim($folder, '/'))) : "");
if (trim($path, '/') == trim($folder, '/'))
continue;
if ($file['type'] == 'directory' && !is_dir($createPath)) {
echo "Current status: <font color='blue'>Directory: " . $createPath . "</font><br /> \r\n";
$this->logging("Current status: <font color='blue'>Directory: " . $createPath . "</font><br /> \r\n");
flush();
mkdir($createPath);
} elseif ($file['type'] == 'file') {
for ($x = 0; $x < 2; $x++) {
$contents = $this->getFile($path);
$outText .= "<font color='blue'>Getting file: </font> " . $path;
$outText .= " <br />\r\n";
if ($checkFiles) {
$fileSize = $this->getFileSize($path);
$outText.= " The size of the received file: " . strlen($contents) .
" File size in a repository: " . $fileSize;
$outText.= " <br />\r\n";
if (strlen($contents) != $fileSize) {
$outText.= "<font color='red'> Error receiving file: " . $createPath . "</font> --- " . $x;
} else {
break;
}
$outText.= " <br />\r\n";
} else {
break;
}
}
echo $outText;
$this->logging($outText);
flush();
$hOut = fopen($createPath, 'w');
fwrite($hOut, $contents);
fclose($hOut);
}
}
if ($this->path_exec_after_completition != '') {
$this->exec_after_completition();
}
}
/**
* Function to easily create and update a working copy of the repository.
* @param type $folder Folder in remote repository
* @param type $outPath Folder for storing files
* @param boolean $checkFiles Whether it is necessary to check the received
* files in the sizes. Can be useful in case often files are accepted
* with an error.
*/
public function createOrUpdateWorkingCopy($folder = '/', $outPath = '.', $checkFiles = false) {
if (!file_exists($outPath . '/.svn/entries')) {
//Create a directory for storing system information for further updates.
$this->createDirs($outPath . '/.svn');
//Keeping the current version of the copy.
$hOut = fopen($outPath . '/.svn/entries', 'w');
fwrite($hOut, $this->actVersion);
fclose($hOut);
echo "Current status: <font color='blue'>Starting checkout...</font><br /> \r\n";
$this->logging("Current status: <font color='blue'>Starting checkout...</font><br /> \r\n");
flush();
$this->checkOut($folder, $outPath, $checkFiles);
} else {
//Obtain the number of current version number of the local copy.
$hOut = fopen($outPath . '/.svn/entries', 'r');
while (!feof($hOut)) {
$copy_version = fgets($hOut);
}
fclose($hOut);
echo "Repository exist with version: " . $copy_version . "<br /> \r\n";
$this->logging("Repository exist with version: " . $copy_version . "<br /> \r\n");
flush();
//Get a list of objects to be updated.
$objects_list = $this->getLogsForUpdate($folder, $copy_version + 1);
if (!is_null($objects_list)) {
////Lets update dirs
// Add dirs
foreach ($objects_list['dirs'] as $file) {
if ($file != '') {
$file = str_replace($folder, "", $file);
$file = $outPath . '/' . $file;
$file = str_replace("///", "/", $file);
echo "<font color='blue'>Added or modified directory: </font>" . $file . "<br />\r\n";
$this->logging("<font color='blue'>Added or modified directory: </font>" . $file . "<br />\r\n");
$this->createDirs($file);
}
}
// Remove dirs
// TEST IT!
foreach ($objects_list['dirsDelete'] as $file) {
if ($file != '') {
$file = str_replace($folder, "", $file);
$file = $outPath . '/' . $file;
$file = str_replace("///", "/", $file);
$this->removeDirs($file);
echo "<font color='red'>Removed directory: </font>" . $file . "<br />\r\n";
}
}
echo "<font color='green'>************************</font><br />\r\n";
////Lets update files
// Add files
foreach ($objects_list['files'] as $file) {
if ($file != '') {
$createPath = str_replace($folder, "", $file);
$createPath = $outPath . '/' . $createPath;
$createPath = str_replace("///", "/", $createPath);
$contents = $this->getFile($file);
$hOut = fopen($createPath, 'w');
fwrite($hOut, $contents);
fclose($hOut);
$out = "<font color='blue'>Added or modified file: </font> ";
if (strlen($contents) < 1) {
$out.= "<font color='red'> " . $file . " with 0 size </font> ";
} else {
$out.= $file;
}
$out.= " <br />\r\n";
echo $out;
}
}
//Remove files
foreach ($objects_list['filesDelete'] as $file) {
if ($file != '') {
$file = str_replace($folder, "", $file);
$file = $outPath . '/' . $file;
$file = str_replace("///", "/", $file);
unlink($file);
echo "<font color='red'>Removed file: </font>" . $file . "<br />\r\n";
}
}
$hOut = fopen($outPath . '/.svn/entries', 'w');
fwrite($hOut, $this->actVersion);
fclose($hOut);
}
}
if ($this->path_exec_after_completition != '') {
$this->exec_after_completition();
}
}
/**
* Function to view the changes between revisions of the specified object.
* @param type $path The path to the object (file or directory).
* @param type $revFrom Initial revision.
* @param type $revTo The final revision.
*/
public function diffVersions($path = '', $revFrom = 0, $revTo = 0) {
require_once 'ext/Diff/Diff.php';
require_once 'ext/Diff/Renderer.php';
require_once 'ext/Diff/Renderer/unified.php';
$this->mime_array = $this->get_mime_array();
//Get a list of objects to be updated.
$objects_list = $this->getLogsForUpdate($path, $revFrom, $revTo, false);
if (!is_null($objects_list)) {
// print_r($objects_list);
foreach ($objects_list['files'] as $file) {
if ($file != '') {
$path_info = pathinfo($file);
$mime_type = $this->mime_array[$path_info['extension']];
if (strpos($mime_type, "text") !== false) {
$file_revFrom = $this->getFile($file, $revFrom);
$file_revFrom =
$this->explodeX(array("\r\n", "\r", "\n"), $file_revFrom);
$file_revTo = $this->getFile($file, $revTo);
$file_revTo = $this->explodeX(array("\r\n", "\r", "\n"), $file_revTo);
/* Create the Diff object. */
$diff = new Text_Diff('auto', array($file_revFrom, $file_revTo));
/* Output the diff in unified format. */
$renderer = new Text_Diff_Renderer_unified();
$result = $renderer->render($diff);
if (strlen($result) > 1) {
echo "Index: " . $file . " \r\n";
echo "===================================================================" . " \r\n";
echo "--- " . $file . " (revision " . $revFrom . ")" . " \r\n";
echo "+++ " . $file . " (revision " . $revTo . ")" . " \r\n";
echo $renderer->render($diff) . " \r\n";
}
}
}
}
foreach ($objects_list['filesDelete'] as $file) {
if ($file != '') {
$path_info = pathinfo($file);
$mime_type = $this->mime_array[$path_info['extension']];
if (strpos($mime_type, "text") !== false) {
$file_revFrom = $this->getFile($file, $revFrom);
$file_revFrom =
$this->explodeX(array("\r\n", "\r", "\n"), $file_revFrom);
$file_revTo = $this->getFile($file, $revTo);
$file_revTo = $this->explodeX(array("\r\n", "\r", "\n"), $file_revTo);
/* Create the Diff object. */
$diff = new Text_Diff('auto', array($file_revFrom, $file_revTo));
/* Output the diff in unified format. */
$renderer = new Text_Diff_Renderer_unified();
$result = $renderer->render($diff);
if (strlen($result) > 1) {
echo "Index: " . $file . " \r\n";
echo "===================================================================" . " \r\n";
echo "--- " . $file . " (revision " . $revFrom . ")" . " \r\n";
echo "+++ " . $file . " (revision " . $revTo . ")" . " \r\n";
echo $renderer->render($diff) . " \r\n";
}
}
}
}
}
}
/**
* rawDirectoryDump
*
* Dumps SVN data for $folder in the version $version of the repository.
*
* @param string $folder Folder to get data
* @param integer $version Repository version, -1 means actual
* @return array SVN data dump.
*/
public function rawDirectoryDump($folder = '/', $version = -1) {
if ($version == -1 || $version > $this->actVersion) {
$version = $this->actVersion;
}
$url = $this->cleanURL($this->_url . "/!svn/bc/" . $version . "/" . $folder . "/");
$this->initQuery($args, "PROPFIND", $url);
$args['Body'] = PHPSVN_NORMAL_REQUEST;
$args['Headers']['Content-Length'] = strlen(PHPSVN_NORMAL_REQUEST);
if (!$this->Request($args, $headers, $body)) {
return false;
}
$xml2Array = new xml2Array();
return $xml2Array->xmlParse($body);
}
/**
* getDirectoryFiles
*
* Returns all the files in $folder in the version $version of
* the repository.
*
* @param string $folder Folder to get files
* @param integer $version Repository version, -1 means actual
* @return array List of files.
*/
public function getDirectoryFiles($folder = '/', $version = -1) {
if ($arrOutput = $this->rawDirectoryDump($folder, $version)) {
$files = array();
foreach ($arrOutput['children'] as $key => $value) {
array_walk_recursive($value, array($this, 'storeDirectoryFiles'));
array_push($files, $this->storeDirectoryFiles);
unset($this->storeDirectoryFiles);
}
return $files;
}
return false;
}
/**
* getDirectoryTree
*
* Returns the complete tree of files and directories in $folder from the
* version $version of the repository. Can also be used to get the info
* for a single file or directory.
*
* @param string $folder Folder to get tree
* @param integer $version Repository version, -1 means current
* @param boolean $recursive Whether to get the tree recursively, or just
* the specified directory/file.
*
* @return array List of files and directories.
*/
public function getDirectoryTree($folder = '/', $version = -1, $recursive = true) {
$directoryTree = array();
if (!($arrOutput = $this->getDirectoryFiles($folder, $version)))
return false;
if (!$recursive)
return $arrOutput[0];
while (count($arrOutput) && is_array($arrOutput)) {
$array = array_shift($arrOutput);
array_push($directoryTree, $array);
if (trim($array['path'], '/') == trim($folder, '/'))
continue;
if ($array['type'] == 'directory') {
$walk = $this->getDirectoryFiles($array['path'], $version);
array_shift($walk);
foreach ($walk as $step) {
array_unshift($arrOutput, $step);
}
}
}
return $directoryTree;
}
/**
* Returns file contents
*
* @param string $file File pathname
* @param integer $version File Version
* @return string File content and information, false on error, or if a
* directory is requested
*/
public function getFile($file, $version = -1) {
if ($version == -1 || $version > $this->actVersion) {
$version = $this->actVersion;
}
// check if this is a directory... if so, return false, otherwise we
// get the HTML output of the directory listing from the SVN server.
// This is maybe a bit heavy since it makes another connection to the
// SVN server. Maybe add this as an option/parameter? ES 23/06/08
$fileInfo = $this->getDirectoryTree($file, $version, false);
if ($fileInfo["type"] == "directory")
return false;
$url = $this->cleanURL($this->_url . "/!svn/bc/" . $version . "/" . $file . "/");
$this->initQuery($args, "GET", $url);
if (!$this->Request($args, $headers, $body))
return false;
return $body;
}
/**
* Get changes logs of a file.
*
* Get repository change logs between version
* $vini and $vend.
*
* @param integer $vini Initial Version
* @param integer $vend End Version
* @return Array Respository Logs
*/
public function getRepositoryLogs($path = "/", $vini = 0, $vend = -1) {
return $this->getFileLogs($path, $vini, $vend);
}
/**
* Get changes logs of a file.
*
* Get repository change of a file between version
* $vini and $vend.
*
* @param string $file File for which to get log data
* @param integer $vini Initial Version
* @param integer $vend End Version
* @return array Respository Logs
*/
public function getFileLogs($file, $vini = 0, $vend = -1) {
$fileLogs = array();
if ($vend == -1 || $vend > $this->actVersion)
$vend = $this->actVersion;
if ($vini < 0)
$vini = 0;
if ($vini > $vend)
$vini = $vend;
$url = $this->cleanURL($this->_url . "/!svn/bc/" . $this->actVersion . "/" . $file . "/");
$this->initQuery($args, "REPORT", $url);
$args['Body'] = sprintf(PHPSVN_LOGS_REQUEST, $vini, $vend);
$args['Headers']['Content-Length'] = strlen($args['Body']);
$args['Headers']['Depth'] = 1;
if (!$this->Request($args, $headers, $body))
return false;
$xml2Array = new xml2Array();
$arrOutput = $xml2Array->xmlParse($body);
foreach ($arrOutput['children'] as $value) {
$array = array();
foreach ($value['children'] as $entry) {
if ($entry['name'] == 'D:VERSION-NAME')
$array['version'] = $entry['tagData'];
if ($entry['name'] == 'D:CREATOR-DISPLAYNAME')
$array['author'] = $entry['tagData'];
if ($entry['name'] == 'S:DATE')
$array['date'] = $entry['tagData'];
if ($entry['name'] == 'D:COMMENT')
$array['comment'] = $entry['tagData'];
if (($entry['name'] == 'S:ADDED-PATH') ||
($entry['name'] == 'S:MODIFIED-PATH') ||
($entry['name'] == 'S:DELETED-PATH')) {
// For backward compatability
$array['files'][] = $entry['tagData'];
if ($entry['name'] == 'S:ADDED-PATH')
$array['add_files'][] = $entry['tagData'];
if ($entry['name'] == 'S:MODIFIED-PATH')
$array['mod_files'][] = $entry['tagData'];
if ($entry['name'] == 'S:DELETED-PATH')
$array['del_files'][] = $entry['tagData'];
}
}
array_push($fileLogs, $array);
}
return $fileLogs;
}
public function getLogsForUpdate($file, $vini = 0, $vend = -1, $checkvend = true) {
$fileLogs = array();
if (($vend == -1 || $vend > $this->actVersion) && $checkvend) {
$vend = $this->actVersion;
}
if ($vini < 0)
$vini = 0;
if ($vini > $vend) {
$vini = $vend;
echo "Nothing updated";
$this->logging("Nothing updated");
return null;
}
$url = $this->cleanURL($this->_url . "/!svn/bc/" . $this->actVersion . "/" . $file . "/");
$this->initQuery($args, "REPORT", $url);
$args['Body'] = sprintf(PHPSVN_LOGS_REQUEST, $vini, $vend);
$args['Headers']['Content-Length'] = strlen($args['Body']);
$args['Headers']['Depth'] = 1;
if (!$this->Request($args, $headers, $body)) {
echo "ERROR in request";
return false;
}
$xml2Array = new xml2Array();
$arrOutput = $xml2Array->xmlParse($body);
$array = array();
foreach ($arrOutput['children'] as $value) {
foreach ($value['children'] as $entry) {
if (($entry['name'] == 'S:ADDED-PATH') ||
($entry['name'] == 'S:MODIFIED-PATH') ||
($entry['name'] == 'S:DELETED-PATH')) {
if ($entry['attrs']['NODE-KIND'] == "file") {
$array['objects'][] = array('object_name' => $entry['tagData'], 'action' => $entry['name'], 'type' => 'file');
} else if ($entry['attrs']['NODE-KIND'] == "dir") {
$array['objects'][] = array('object_name' => $entry['tagData'], 'action' => $entry['name'], 'type' => 'dir');
}
}
}
}
$files = "";
$filesDelete = "";
$dirs = "";
$dirsDelete = "";
foreach ($array['objects'] as $objects) {
if ($objects['type'] == "file") {
if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
$file = $objects['object_name'] . "/*+++*/";
$files.=$file;
$filesDelete = str_replace($file, "", $filesDelete, $count);
}
if ($objects['action'] == "S:DELETED-PATH") {
if (strpos($files, $objects['object_name']) !== false) {
$file = $objects['object_name'] . "/*+++*/";
$count = 1;
$files = str_replace($file, "", $files, $count);
} else {
$filesDelete.=$objects['object_name'] . "/*+++*/";
}
}
}
if ($objects['type'] == "dir") {
if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
$dir = $objects['object_name'] . "/*+++*/";
$dirs.=$dir;
$dirsDelete = str_replace($dir, "", $dirsDelete, $count);
}
if ($objects['action'] == "S:DELETED-PATH") {
// Delete files from filelist
$dir = $objects['object_name'] . "/";
$files1 = explode("/*+++*/", $files);
for ($x = 0; $x < count($files1); $x++) {
if (strpos($files1[$x], $dir) !== false) {
unset($files1[$x]);
}
}
$files = implode("/*+++*/", $files1);
// END OF Delete files from filelist
// Delete dirs from dirslist
if (strpos($dirs, $objects['object_name']) !== false) {
$dir = $objects['object_name'] . "/*+++*/";
$count = 1;
$dirs = str_replace($dir, "", $dirs, $count);
} else {
$dirsDelete.=$objects['object_name'] . "/*+++*/";
}
// END OF Delete dirs from dirslist
}
}
}
$files = explode("/*+++*/", $files);
$filesDelete = explode("/*+++*/", $filesDelete);
$dirs = explode("/*+++*/", $dirs);
$dirsDelete = explode("/*+++*/", $dirsDelete);
$out = array();
$out['files'] = $files;
$out['filesDelete'] = $filesDelete;
$out['dirs'] = $dirs;
$out['dirsDelete'] = $dirsDelete;
return $out;
}
/**
* Returns the repository version
*
* @return integer Repository version
* @access public
*/
public function getVersion() {
if ($this->_repVersion > 0)
return $this->_repVersion;
$this->_repVersion = -1;
$this->initQuery($args, "PROPFIND", $this->cleanURL($this->_url . "/!svn/vcc/default"));
$args['Body'] = PHPSVN_VERSION_REQUEST;
$args['Headers']['Content-Length'] = strlen(PHPSVN_NORMAL_REQUEST);
$args['Headers']['Depth'] = 0;
// echo $vini."\r\n";
// echo $vend."\r\n";
echo "Args: \r\n";
print_r($args);
echo "Headers: \r\n";
print_r($tmp);
if (!$this->Request($args, $tmp, $body)) {
return $this->_repVersion;
}
$parser = new xml_parser_class;
$parser->Parse($body, true);
$enable = false;
foreach ($parser->structure as $value) {
if ($enable) {
$t = explode("/", $value);
// start from the end and move backwards until we find a non-blank entry
$index = count($t) - 1;
while ($t[$index] == "") {
$index--;
}
// check the last non-empty element to see if it's numeric. If so, it's the revision number
if (is_numeric($t[$index])) {
$this->_repVersion = $t[$index];
break;
} else {
$enable = false;
continue;
}
}
if (is_array($value) && $value['Tag'] == 'D:href')
$enable = true;
}
return $this->_repVersion;
}
/**
* Deprecated functions for backward comatability
*/
/**
* Set URL
*
* Set the project repository URL.
*
* @param string $url URL of the project.
* @access public
*/
public function setRepository($url) {
$this->_url = $url;
$this->_repVersion = 0;
$this->actVersion = $this->getVersion();
}
/**
* Old method; there's a typo in the name. This is now a wrapper for setRepository
*/
public function setRespository($url) {
return $this->setRepository($url);
}
/**
* Add Authentication settings
*
* @param string $user Username
* @param string $pass Password
*/
public function setAuth($user, $pass) {
$this->user = $user;
$this->pass = $pass;
}
/**
* Private Functions
*/
/**
* Callback for array_walk_recursive in public function getDirectoryFiles
*
* @access private
*/
private function storeDirectoryFiles($item, $key) {
if ($key == 'name') {
if (($item == 'D:HREF') ||
($item == 'LP1:GETLASTMODIFIED') ||
($item == 'LP1:VERSION-NAME') ||
($item == 'LP2:BASELINE-RELATIVE-PATH') ||
($item == 'LP3:BASELINE-RELATIVE-PATH') ||
($item == 'D:STATUS')) {
$this->lastDirectoryFiles = $item;
}
} elseif (($key == 'tagData') && ($this->lastDirectoryFiles != '')) {
// Unsure if the 1st of two D:HREF's always returns the result we want, but for now...
if (($this->lastDirectoryFiles == 'D:HREF') && (isset($this->storeDirectoryFiles['type'])))
return;
// Dump into the array
switch ($this->lastDirectoryFiles) {
case 'D:HREF':
$var = 'type';
break;
case 'LP1:VERSION-NAME':
$var = 'version';
break;
case 'LP1:GETLASTMODIFIED':
$var = 'last-mod';
break;
case 'LP2:BASELINE-RELATIVE-PATH':
case 'LP3:BASELINE-RELATIVE-PATH':
$var = 'path';
break;
case 'D:STATUS':
$var = 'status';
break;
}
$this->storeDirectoryFiles[$var] = $item;
$this->lastDirectoryFiles = '';
// Detect 'type' as either a 'directory' or 'file'
if ((isset($this->storeDirectoryFiles['type'])) &&
(isset($this->storeDirectoryFiles['last-mod'])) &&
(isset($this->storeDirectoryFiles['path'])) &&
(isset($this->storeDirectoryFiles['status']))) {
$this->storeDirectoryFiles['path'] = str_replace(' ', '%20', $this->storeDirectoryFiles['path']); //Hack to make filenames with spaces work.
$len = strlen($this->storeDirectoryFiles['path']);
if (substr($this->storeDirectoryFiles['type'], strlen($this->storeDirectoryFiles['type']) - $len) == $this->storeDirectoryFiles['path']) {
$this->storeDirectoryFiles['type'] = 'file';
} else {
$this->storeDirectoryFiles['type'] = 'directory';
}
}
} else {
$this->lastDirectoryFiles = '';
}
}
/**
* Prepare HTTP CLIENT object
*
* @param array &$arguments Byreferences variable.
* @param string $method Method for the request (GET,POST,PROPFIND, REPORT,ETC).
* @param string $url URL for the action.
* @access private
*/
private function initQuery(&$arguments, $method, $url) {
$http = & $this->_http;
$http->GetRequestArguments($url, $arguments);
if (isset($this->user) && isset($this->pass)) {
$arguments["Headers"]["Authorization"] = " Basic " . base64_encode($this->user . ":" . $this->pass);
}
$arguments["RequestMethod"] = $method;
$arguments["Headers"]["Content-Type"] = "text/xml";
$arguments["Headers"]["Depth"] = 1;
}
/**
* Open a connection, send request, read header
* and body.
*
* @param Array $args Connetion's argument
* @param Array &$headers Array with the header response.
* @param string &$body Body response.
* @return boolean True is query success
* @access private
*/
private function Request($args, &$headers, &$body) {
$args['RequestURI'] = str_replace(' ', '%20', $args['RequestURI']); //Hack to make filenames with spaces work.
$http = & $this->_http;
$http->Open($args);
$http->SendRequest($args);
$http->ReadReplyHeaders($headers);
if ($http->response_status[0] != 2) {
switch ($http->response_status) {
case 404:
$this->errNro = NOT_FOUND;
break;
case 401:
$this->errNro = AUTH_REQUIRED;
break;
default:
$this->errNro = UNKNOWN_ERROR;
break;
}
// trigger_error("request to $args[RequestURI] failed: $http->response_status
//Error: $http->error");
$http->close();
return false;
}
$this->errNro = NO_ERROR;
$body = '';
$tbody = '';
for (;;) {
$error = $http->ReadReplyBody($tbody, 1000);
if ($error != "" || strlen($tbody) == 0) {
break;
}
$body.= ( $tbody);
}
//print_r($tbody);
$http->close();
return true;
}
/**
* Returns $url stripped of '//'
*
* Delete "//" on URL requests.
*
* @param string $url URL
* @return string New cleaned URL.
* @access private
*/
private function cleanURL($url) {
return preg_replace("/((^:)\/\/)/", "//", $url);
}