Subversion Repositories oidplus

Rev

Rev 200 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
171 daniel-mar 1
<?php
2
 
3
if (!defined('IN_OIDPLUS')) die();
4
 
5
/*
6
 * This file includes:
7
 *
8
 * 1. "PHP SVN CLIENT" class
9
 *    Copyright (C) 2007-2008 by Sixdegrees <cesar@sixdegrees.com.br>
10
 *    Cesar D. Rodas
11
 *    https://code.google.com/archive/p/phpsvnclient/
12
 *    License: BSD License
13
 *    CHANGES by Daniel Marschall, ViaThinkSoft in 2019:
14
 *    - The class has been customized and contains specific changes for the software "OIDplus"
15
 *    - Functions which are not used in the "SVN checkout" were removed.
172 daniel-mar 16
 *      The only important functions are getVersion() and updateWorkingCopy()
171 daniel-mar 17
 *    - The dependency class xml2array was converted from a class into a function and
18
 *      included into this class
172 daniel-mar 19
 *    - Added "revision log/comment" functionality
171 daniel-mar 20
 *
21
 * 2. "xml2array" class
22
 *    Taken from http://www.php.net/manual/en/function.xml-parse.php#52567
23
 *    Modified by Martin Guppy <http://www.deadpan110.com/>
24
 *    CHANGES by Daniel Marschall, ViaThinkSoft in 2019:
25
 *    - Converted class into a single function and added that function into the phpsvnclient class
26
 */
27
 
28
define("PHPSVN_NORMAL_REQUEST", '<?xml version="1.0" encoding="utf-8"?><propfind xmlns="DAV:"><prop>
29
<getlastmodified xmlns="DAV:"/> <checked-in xmlns="DAV:"/><version-name xmlns="DAV:"/><version-controlled-configuration xmlns="DAV:"/><resourcetype xmlns="DAV:"/><baseline-relative-path xmlns="http://subversion.tigris.org/xmlns/dav/"/><repository-uuid xmlns="http://subversion.tigris.org/xmlns/dav/"/></prop></propfind>');
30
define("PHPSVN_VERSION_REQUEST", '<?xml version="1.0" encoding="utf-8"?><propfind xmlns="DAV:"><prop><checked-in xmlns="DAV:"/></prop></propfind>');
31
define("PHPSVN_LOGS_REQUEST", '<?xml version="1.0" encoding="utf-8"?> <S:log-report xmlns:S="svn:"> <S:start-revision>%d</S:start-revision><S:end-revision>%d</S:end-revision><S:path></S:path><S:discover-changed-paths/></S:log-report>');
32
 
33
define("NOT_FOUND", 2);
34
define("AUTH_REQUIRED", 3);
35
define("UNKNOWN_ERROR", 4);
36
define("NO_ERROR", 1);
37
 
38
/**
39
 *  PHP SVN CLIENT
40
 *
41
 *  This class is a SVN client. It can perform read operations
42
 *  to a SVN server (over Web-DAV).
43
 *  It can get directory files, file contents, logs. All the operaration
44
 *  could be done for a specific version or for the last version.
45
 *
46
 *  @author Cesar D. Rodas <cesar@sixdegrees.com.br>
47
 *  @license BSD License
48
 */
49
class phpsvnclient
50
{
51
        /**
52
         *  SVN Repository URL
53
         *
54
         *  @var string
55
         *  @access private
56
         */
57
        private $_url;
58
 
59
        /**
60
         *  HTTP Client object
61
         *
62
         *  @var object
63
         *  @access private
64
         */
65
        private $_http;
66
 
67
        /**
68
         *  Respository Version.
69
         *
70
         *  @access private
71
         *  @var interger
72
         */
73
        private $_repVersion;
74
 
75
        /**
76
         *  Last error number
77
         *
78
         *  Possible values are NOT_ERROR, NOT_FOUND, AUTH_REQUIRED, UNKOWN_ERROR
79
         *
80
         *  @access public
81
         *  @var integer
82
         */
83
        public $errNro;
84
 
85
        /**
86
         * Number of actual revision local repository.
87
         * @var Integer, Long
88
         */
89
        private $actVersion;
90
        private $storeDirectoryFiles = array();
91
        private $lastDirectoryFiles;
92
        private $file_size;
93
        private $file_size_founded = false;
94
 
95
        public function __construct($url)
96
        {
97
                $http =& $this->_http;
98
                $http             = new http_class;
99
                $http->user_agent = "phpsvnclient (https://code.google.com/archive/p/phpsvnclient/)";
100
 
101
                $this->_url = $url;
102
 
103
                $this->actVersion = $this->getVersion();
104
        }
105
 
106
        /**
107
         * Function for creating directories.
108
         * @param type $path The path to the directory that will be created.
109
         */
110
        private function createDirs($path)
111
        {
112
                $dirs = explode("/", $path);
113
 
114
                foreach ($dirs as $dir) {
115
                        if ($dir != "") {
116
                                $createDir = substr($path, 0, strpos($path, $dir) + strlen($dir));
117
                                @mkdir($createDir);
118
                        }
119
                }
120
        }
121
 
122
        /**
123
         * Function for the recursive removal of directories.
124
         * @param type $path The path to the directory to be deleted.
125
         * @return type Returns the status of a function or function rmdir unlink.
126
         */
127
        private function removeDirs($path)
128
        {
129
                if (is_dir($path)) {
130
                        $entries = scandir($path);
131
                        if ($entries === false) {
132
                                $entries = array();
133
                        }
134
                        foreach ($entries as $entry) {
135
                                if ($entry != '.' && $entry != '..') {
136
                                        $this->removeDirs($path . '/' . $entry);
137
                                }
138
                        }
139
                        return @rmdir($path);
140
                } else {
141
                        return @unlink($path);
142
                }
143
        }
144
 
145
        /**
146
         *  Public Functions
147
         */
148
 
149
        /**
172 daniel-mar 150
        * Updates a working copy
151
        * @param $from_revision Either a revision number or a text file with the
152
        *                       contents "Revision ..." (if it is a file,
153
        *                       the file revision will be updated if everything
154
        *                       was successful)
155
        * @param $folder        SVN remote folder
156
        * @param $outpath       Local path of the working copy
157
        * @param $preview       Only simulate, do not write to files
158
        **/
159
        public function updateWorkingCopy($from_revision='version.txt', $folder = '/trunk/', $outPath = '.', $preview = false)
171 daniel-mar 160
        {
161
                if (!is_dir($outPath)) {
162
                        echo "ERROR: Local path $outPath not existing\n";
163
                        flush();
164
                        return false;
165
                }
166
 
172 daniel-mar 167
                if (!is_numeric($from_revision)) {
168
                        $version_file = $from_revision;
169
                        $from_revision = -1;
170
 
171
                        if (!file_exists($version_file)) {
172
                                echo "ERROR: $version_file missing\n";
171 daniel-mar 173
                                flush();
174
                                return false;
172 daniel-mar 175
                        } else {
176
                                //Obtain the number of current version number of the local copy.
177
                                $cont = file_get_contents($version_file);
178
                                if (!preg_match('@Revision (\d+)@', $cont, $m)) {
179
                                        echo "ERROR: $version_file unknown format\n";
180
                                        flush();
181
                                        return false;
182
                                }
183
                                $from_revision = $m[1];
184
 
185
                                echo "Found $version_file with revision information $from_revision\n";
186
                                flush();
171 daniel-mar 187
                        }
172 daniel-mar 188
                } else {
189
                        $version_file = '';
190
                }
171 daniel-mar 191
 
172 daniel-mar 192
                $errors_happened = false;
193
 
194
                // First, do some read/write test (even if we are in preview mode, because we want to detect errors before it is too late)
195
                $file = $outPath . '/dummy_'.uniqid().'.tmp';
196
                $file = str_replace("///", "/", $file);
197
                if (@file_put_contents($file, 'Write Test') === false) {
198
                        echo "Cannot write test file $file\n";
171 daniel-mar 199
                        flush();
172 daniel-mar 200
                        return false;
201
                }
202
                @unlink($file);
203
                if (file_exists($file)) {
204
                        echo "Cannot delete test file $file\n";
205
                        flush();
206
                        return false;
207
                }
171 daniel-mar 208
 
172 daniel-mar 209
                //Get a list of objects to be updated.
210
                $objects_list = $this->getLogsForUpdate($folder, $from_revision + 1);
211
                if (!is_null($objects_list)) {
212
                        // Output version information
213
                        foreach ($objects_list['revisions'] as $revision) {
216 daniel-mar 214
                                $comment = empty($revision['comment']) ? 'No comment' : $revision['comment'];
186 daniel-mar 215
                                $tex = "New revision ".$revision['versionName']." by ".$revision['creator']." (".date('Y-m-d H:i:s', strtotime($revision['date'])).") ";
216 daniel-mar 216
                                echo trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment));
186 daniel-mar 217
                                echo "\n";
171 daniel-mar 218
                        }
219
 
172 daniel-mar 220
                        // Add dirs
200 daniel-mar 221
                        sort($objects_list['dirs']); // <-- added by Daniel Marschall: Sort folder list, so that directories will be created in the correct hierarchical order
172 daniel-mar 222
                        foreach ($objects_list['dirs'] as $file) {
223
                                if ($file != '') {
224
                                        $localPath = str_replace($folder, "", $file);
225
                                        $localPath = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localPath,DIRECTORY_SEPARATOR);
171 daniel-mar 226
 
172 daniel-mar 227
                                        echo "Added or modified directory: $file\n";
228
                                        flush();
229
                                        if (!$preview) {
230
                                                $this->createDirs($localPath);
231
                                                if (!is_dir($localPath)) {
232
                                                        $errors_happened = true;
233
                                                        echo "=> FAILED\n";
234
                                                        flush();
171 daniel-mar 235
                                                }
236
                                        }
237
                                }
172 daniel-mar 238
                        }
171 daniel-mar 239
 
172 daniel-mar 240
                        // Add files
200 daniel-mar 241
                        sort($objects_list['files']); // <-- added by Daniel Marschall: Sort list, just for cosmetic improvement
172 daniel-mar 242
                        foreach ($objects_list['files'] as $file) {
243
                                if ($file != '') {
244
                                        $localFile = str_replace($folder, "", $file);
245
                                        $localFile = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localFile,DIRECTORY_SEPARATOR);
171 daniel-mar 246
 
172 daniel-mar 247
                                        echo "Added or modified file: $file\n";
248
                                        flush();
249
                                        if (!$preview) {
250
                                                $contents = $this->getFile($file);
251
                                                if (@file_put_contents($localFile, $contents) === false) {
252
                                                        $errors_happened = true;
253
                                                        echo "=> FAILED\n";
254
                                                        flush();
171 daniel-mar 255
                                                }
256
                                        }
257
                                }
172 daniel-mar 258
                        }
216 daniel-mar 259
 
260
                        // Remove files
200 daniel-mar 261
                        sort($objects_list['filesDelete']); // <-- added by Daniel Marschall: Sort list, just for cosmetic improvement
172 daniel-mar 262
                        foreach ($objects_list['filesDelete'] as $file) {
263
                                if ($file != '') {
264
                                        $localFile = str_replace($folder, "", $file);
265
                                        $localFile = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localFile,DIRECTORY_SEPARATOR);
171 daniel-mar 266
 
172 daniel-mar 267
                                        echo "Removed file: $file\n";
268
                                        flush();
171 daniel-mar 269
 
172 daniel-mar 270
                                        if (!$preview) {
271
                                                @unlink($localFile);
272
                                                if (file_exists($localFile)) {
273
                                                        $errors_happened = true;
274
                                                        echo "=> FAILED\n";
275
                                                        flush();
171 daniel-mar 276
                                                }
277
                                        }
278
                                }
172 daniel-mar 279
                        }
171 daniel-mar 280
 
172 daniel-mar 281
                        // Remove dirs
282
                        // Changed by Daniel Marschall: moved this to the end, because "add/update" requests for this directory might happen before the directory gets removed
200 daniel-mar 283
                        rsort($objects_list['dirsDelete']); // <-- added by Daniel Marschall: Sort list in reverse order, so that directories get deleted in the correct hierarchical order
172 daniel-mar 284
                        foreach ($objects_list['dirsDelete'] as $file) {
285
                                if ($file != '') {
286
                                        $localPath = str_replace($folder, "", $file);
287
                                        $localPath = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localPath,DIRECTORY_SEPARATOR);
171 daniel-mar 288
 
172 daniel-mar 289
                                        echo "Removed directory: $file\n";
290
                                        flush();
171 daniel-mar 291
 
172 daniel-mar 292
                                        if (!$preview) {
293
                                                $this->removeDirs($localPath);
294
                                                if (is_dir($localPath)) {
295
                                                        $errors_happened = true;
296
                                                        echo "=> FAILED\n";
297
                                                        flush();
171 daniel-mar 298
                                                }
299
                                        }
300
                                }
172 daniel-mar 301
                        }
171 daniel-mar 302
 
216 daniel-mar 303
                        // Update version file
172 daniel-mar 304
                        // Changed by Daniel Marschall: Added $errors_happened
305
                        if (!$preview && !empty($version_file)) {
306
                                if (!$errors_happened) {
307
                                        if (@file_put_contents($version_file, "Revision " . $this->actVersion . "\n") === false) {
308
                                                echo "ERROR: Could not set the revision\n";
309
                                                flush();
310
                                                return false;
171 daniel-mar 311
                                        } else {
172 daniel-mar 312
                                                echo "Set revision to " . $this->actVersion . "\n";
171 daniel-mar 313
                                                flush();
172 daniel-mar 314
                                                return true;
171 daniel-mar 315
                                        }
172 daniel-mar 316
                                } else {
317
                                        echo "Revision NOT set to " . $this->actVersion . " because some files/dirs could not be updated. Please try again.\n";
318
                                        flush();
319
                                        return false;
171 daniel-mar 320
                                }
172 daniel-mar 321
                        } else {
322
                                return true;
171 daniel-mar 323
                        }
324
                }
325
        }
326
 
327
        /**
328
         *  rawDirectoryDump
329
         *
330
         * Dumps SVN data for $folder in the version $version of the repository.
331
         *
332
         *  @param string  $folder Folder to get data
333
         *  @param integer $version Repository version, -1 means actual
334
         *  @return array SVN data dump.
335
         */
336
        private function rawDirectoryDump($folder = '/trunk/', $version = -1)
337
        {
338
                if ($version == -1 || $version > $this->actVersion) {
339
                        $version = $this->actVersion;
340
                }
341
                $url = $this->cleanURL($this->_url . "/!svn/bc/" . $version . "/" . $folder . "/");
342
                $this->initQuery($args, "PROPFIND", $url);
343
                $args['Body']                      = PHPSVN_NORMAL_REQUEST;
344
                $args['Headers']['Content-Length'] = strlen(PHPSVN_NORMAL_REQUEST);
345
 
346
                if (!$this->Request($args, $headers, $body))
347
                        throw new Exception("Cannot get rawDirectoryDump (Request failed)");
348
 
216 daniel-mar 349
                return self::xmlParse($body);
171 daniel-mar 350
        }
351
 
352
        /**
353
         *  getDirectoryFiles
354
         *
355
         *  Returns all the files in $folder in the version $version of
356
         *  the repository.
357
         *
358
         *  @param string  $folder Folder to get files
359
         *  @param integer $version Repository version, -1 means actual
360
         *  @return array List of files.
361
         */
362
        private function getDirectoryFiles($folder = '/trunk/', $version = -1)
363
        {
364
                if ($arrOutput = $this->rawDirectoryDump($folder, $version)) {
365
                        $files = array();
366
                        foreach ($arrOutput['children'] as $key => $value) {
172 daniel-mar 367
                                array_walk_recursive($value,
368
                                        function ($item, $key) {
369
                                                if ($key == 'name') {
370
                                                        if (($item == 'D:HREF') || ($item == 'LP1:GETLASTMODIFIED') || ($item == 'LP1:VERSION-NAME') || ($item == 'LP2:BASELINE-RELATIVE-PATH') || ($item == 'LP3:BASELINE-RELATIVE-PATH') || ($item == 'D:STATUS')) {
371
                                                                $this->lastDirectoryFiles = $item;
372
                                                        }
373
                                                } elseif (($key == 'tagData') && ($this->lastDirectoryFiles != '')) {
374
 
375
                                                        // Unsure if the 1st of two D:HREF's always returns the result we want, but for now...
376
                                                        if (($this->lastDirectoryFiles == 'D:HREF') && (isset($this->storeDirectoryFiles['type'])))
377
                                                                return;
378
 
379
                                                        // Dump into the array
380
                                                        switch ($this->lastDirectoryFiles) {
381
                                                                case 'D:HREF':
382
                                                                        $var = 'type';
383
                                                                        break;
384
                                                                case 'LP1:VERSION-NAME':
385
                                                                        $var = 'version';
386
                                                                        break;
387
                                                                case 'LP1:GETLASTMODIFIED':
388
                                                                        $var = 'last-mod';
389
                                                                        break;
390
                                                                case 'LP2:BASELINE-RELATIVE-PATH':
391
                                                                case 'LP3:BASELINE-RELATIVE-PATH':
392
                                                                        $var = 'path';
393
                                                                        break;
394
                                                                case 'D:STATUS':
395
                                                                        $var = 'status';
396
                                                                        break;
397
                                                        }
398
                                                        $this->storeDirectoryFiles[$var] = $item;
399
                                                        $this->lastDirectoryFiles        = '';
400
 
401
                                                        // Detect 'type' as either a 'directory' or 'file'
402
                                                        if ((isset($this->storeDirectoryFiles['type'])) && (isset($this->storeDirectoryFiles['last-mod'])) && (isset($this->storeDirectoryFiles['path'])) && (isset($this->storeDirectoryFiles['status']))) {
403
                                                                $this->storeDirectoryFiles['path'] = str_replace(' ', '%20', $this->storeDirectoryFiles['path']); //Hack to make filenames with spaces work.
404
                                                                $len                               = strlen($this->storeDirectoryFiles['path']);
405
                                                                if (substr($this->storeDirectoryFiles['type'], strlen($this->storeDirectoryFiles['type']) - $len) == $this->storeDirectoryFiles['path']) {
406
                                                                        $this->storeDirectoryFiles['type'] = 'file';
407
                                                                } else {
408
                                                                        $this->storeDirectoryFiles['type'] = 'directory';
409
                                                                }
410
                                                        }
411
                                                } else {
412
                                                        $this->lastDirectoryFiles = '';
413
                                                }
414
                                        }
415
                                );
171 daniel-mar 416
                                array_push($files, $this->storeDirectoryFiles);
417
                                unset($this->storeDirectoryFiles);
418
                        }
419
                        return $files;
420
                }
421
                return false;
422
        }
423
 
424
        /**
425
         *  getDirectoryTree
426
         *
427
         *  Returns the complete tree of files and directories in $folder from the
428
         *  version $version of the repository. Can also be used to get the info
429
         *  for a single file or directory.
430
         *
431
         *  @param string  $folder Folder to get tree
432
         *  @param integer $version Repository version, -1 means current
433
         *  @param boolean $recursive Whether to get the tree recursively, or just
434
         *  the specified directory/file.
435
         *
436
         *  @return array List of files and directories.
437
         */
438
        private function getDirectoryTree($folder = '/trunk/', $version = -1, $recursive = true)
439
        {
440
                $directoryTree = array();
441
 
442
                if (!($arrOutput = $this->getDirectoryFiles($folder, $version)))
443
                        return false;
444
 
445
                if (!$recursive)
446
                        return $arrOutput[0];
447
 
448
                while (count($arrOutput) && is_array($arrOutput)) {
449
                        $array = array_shift($arrOutput);
450
 
451
                        array_push($directoryTree, $array);
452
 
453
                        if (trim($array['path'], '/') == trim($folder, '/'))
454
                                continue;
455
 
456
                        if ($array['type'] == 'directory') {
457
                                $walk = $this->getDirectoryFiles($array['path'], $version);
458
                                array_shift($walk);
459
 
460
                                foreach ($walk as $step) {
461
                                        array_unshift($arrOutput, $step);
462
                                }
463
                        }
464
                }
465
                return $directoryTree;
466
        }
467
 
468
        /**
469
         *  Returns file contents
470
         *
471
         *  @param      string  $file File pathname
472
         *  @param      integer $version File Version
473
         *  @return     string  File content and information, false on error, or if a
474
         *              directory is requested
475
         */
476
        private function getFile($file, $version = -1)
477
        {
478
                if ($version == -1 || $version > $this->actVersion) {
479
                        $version = $this->actVersion;
480
                }
481
 
482
                // check if this is a directory... if so, return false, otherwise we
483
                // get the HTML output of the directory listing from the SVN server.
484
                // This is maybe a bit heavy since it makes another connection to the
485
                // SVN server. Maybe add this as an option/parameter? ES 23/06/08
486
                $fileInfo = $this->getDirectoryTree($file, $version, false);
487
                if ($fileInfo["type"] == "directory")
488
                        return false;
489
 
490
                $url = $this->cleanURL($this->_url . "/!svn/bc/" . $version . "/" . $file . "/");
491
                $this->initQuery($args, "GET", $url);
492
                if (!$this->Request($args, $headers, $body))
493
                        throw new Exception("Cannot call getFile (Request failed)");
494
 
495
                return $body;
496
        }
497
 
172 daniel-mar 498
        private function getLogsForUpdate($file, $vini = 0, $vend = -1)
171 daniel-mar 499
        {
500
                $fileLogs = array();
501
 
172 daniel-mar 502
                if ($vend == -1) {
171 daniel-mar 503
                        $vend = $this->actVersion;
504
                }
505
 
506
                if ($vini < 0)
507
                        $vini = 0;
508
 
509
                if ($vini > $vend) {
510
                        $vini = $vend;
511
                        echo "Nothing updated\n";
512
                        flush();
513
                        return null;
514
                }
515
 
516
                $url = $this->cleanURL($this->_url . "/!svn/bc/" . $this->actVersion . "/" . $file . "/");
517
                $this->initQuery($args, "REPORT", $url);
518
                $args['Body']                      = sprintf(PHPSVN_LOGS_REQUEST, $vini, $vend);
519
                $args['Headers']['Content-Length'] = strlen($args['Body']);
520
                $args['Headers']['Depth']          = 1;
521
 
522
                if (!$this->Request($args, $headers, $body))
523
                        throw new Exception("Cannot call getLogsForUpdate (Request failed)");
524
 
216 daniel-mar 525
                $arrOutput = self::xmlParse($body);
171 daniel-mar 526
 
172 daniel-mar 527
                $revlogs = array();
528
 
171 daniel-mar 529
                $array = array();
530
                foreach ($arrOutput['children'] as $value) {
172 daniel-mar 531
                        /*
532
                        <S:log-item>
533
                        <D:version-name>164</D:version-name>
534
                        <S:date>2019-08-13T13:12:13.915920Z</S:date>
535
                        <D:comment>Update assistant bugfix</D:comment>
536
                        <D:creator-displayname>daniel-marschall</D:creator-displayname>
537
                        <S:modified-path node-kind="file" text-mods="true" prop-mods="false">/trunk/update/index.php</S:modified-path>
538
                        <S:modified-path node-kind="file" text-mods="true" prop-mods="false">/trunk/update/phpsvnclient.inc.php</S:modified-path>
539
                        </S:log-item>
540
                        */
541
 
542
                        $versionName = '';
543
                        $date = '';
544
                        $comment = '';
171 daniel-mar 545
                        foreach ($value['children'] as $entry) {
546
                                if (($entry['name'] == 'S:ADDED-PATH') || ($entry['name'] == 'S:MODIFIED-PATH') || ($entry['name'] == 'S:DELETED-PATH')) {
547
                                        if ($entry['attrs']['NODE-KIND'] == "file") {
548
                                                $array['objects'][] = array(
549
                                                        'object_name' => $entry['tagData'],
550
                                                        'action' => $entry['name'],
551
                                                        'type' => 'file'
552
                                                );
553
                                        } else if ($entry['attrs']['NODE-KIND'] == "dir") {
554
                                                $array['objects'][] = array(
555
                                                        'object_name' => $entry['tagData'],
556
                                                        'action' => $entry['name'],
557
                                                        'type' => 'dir'
558
                                                );
559
                                        }
172 daniel-mar 560
                                } else if ($entry['name'] == 'D:VERSION-NAME') {
561
                                        $versionName = isset($entry['tagData']) ? $entry['tagData'] : '';
562
                                } else if ($entry['name'] == 'S:DATE') {
563
                                        $date = isset($entry['tagData']) ? $entry['tagData'] : '';
564
                                } else if ($entry['name'] == 'D:COMMENT') {
565
                                        $comment = isset($entry['tagData']) ? $entry['tagData'] : '';
566
                                } else if ($entry['name'] == 'D:CREATOR-DISPLAYNAME') {
567
                                        $creator = isset($entry['tagData']) ? $entry['tagData'] : '';
171 daniel-mar 568
                                }
569
                        }
172 daniel-mar 570
                        $revlogs[] = array('versionName' => $versionName,
571
                                           'date' => $date,
572
                                           'comment' => $comment,
573
                                           'creator' => $creator);
171 daniel-mar 574
                }
216 daniel-mar 575
                $files       = array();
576
                $filesDelete = array();
577
                $dirs        = array();
578
                $dirsDelete  = array();
171 daniel-mar 579
 
580
                foreach ($array['objects'] as $objects) {
216 daniel-mar 581
                        // This section was completely changed by Daniel Marschall
171 daniel-mar 582
                        if ($objects['type'] == "file") {
583
                                if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
216 daniel-mar 584
                                        self::xarray_add($objects['object_name'], $files);
585
                                        self::xarray_remove($objects['object_name'], $filesDelete);
171 daniel-mar 586
                                }
587
                                if ($objects['action'] == "S:DELETED-PATH") {
216 daniel-mar 588
                                        self::xarray_add($objects['object_name'], $filesDelete);
589
                                        self::xarray_remove($objects['object_name'], $files);
171 daniel-mar 590
                                }
591
                        }
592
                        if ($objects['type'] == "dir") {
593
                                if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
216 daniel-mar 594
                                        self::xarray_add($objects['object_name'], $dirs);
595
                                        self::xarray_remove($objects['object_name'], $dirsDelete);
171 daniel-mar 596
                                }
597
                                if ($objects['action'] == "S:DELETED-PATH") {
598
                                        // Delete files from filelist
216 daniel-mar 599
                                        $files_copy = $files;
600
                                        foreach ($files_copy as $file) {
601
                                                if (strpos($file, $objects['object_name'].'/') === 0) self::xarray_remove($file, $files);
171 daniel-mar 602
                                        }
603
                                        // END OF Delete files from filelist
604
                                        // Delete dirs from dirslist
216 daniel-mar 605
                                        self::xarray_add($objects['object_name'], $dirsDelete);
606
                                        self::xarray_remove($objects['object_name'], $dirs);
171 daniel-mar 607
                                        // END OF Delete dirs from dirslist
608
                                }
609
                        }
610
                }
611
                $out                = array();
612
                $out['files']       = $files;
613
                $out['filesDelete'] = $filesDelete;
614
                $out['dirs']        = $dirs;
615
                $out['dirsDelete']  = $dirsDelete;
172 daniel-mar 616
                $out['revisions']   = $revlogs;
171 daniel-mar 617
                return $out;
618
        }
619
 
620
        /**
621
         *  Returns the repository version
622
         *
623
         *  @return integer Repository version
624
         *  @access public
625
         */
626
        public function getVersion()
627
        {
628
                if ($this->_repVersion > 0)
629
                        return $this->_repVersion;
630
 
631
                $this->_repVersion = -1;
632
                $this->initQuery($args, "PROPFIND", $this->cleanURL($this->_url . "/!svn/vcc/default"));
633
                $args['Body']                      = PHPSVN_VERSION_REQUEST;
634
                $args['Headers']['Content-Length'] = strlen(PHPSVN_NORMAL_REQUEST);
635
                $args['Headers']['Depth']          = 0;
636
 
637
                if (!$this->Request($args, $tmp, $body))
638
                        throw new Exception("Cannot get repository revision (Request failed)");
639
 
640
                $this->_repVersion = null;
641
                if (preg_match('@/(\d+)\s*</D:href>@ismU', $body, $m)) {
642
                        $this->_repVersion = $m[1];
643
                } else {
644
                        throw new Exception("Cannot get repository revision (RegEx failed)");
645
                }
646
 
647
                return $this->_repVersion;
648
        }
649
 
650
        /**
651
         *  Private Functions
652
         */
653
 
654
        /**
655
         *  Prepare HTTP CLIENT object
656
         *
657
         *  @param array &$arguments Byreferences variable.
658
         *  @param string $method Method for the request (GET,POST,PROPFIND, REPORT,ETC).
659
         *  @param string $url URL for the action.
660
         *  @access private
661
         */
662
        private function initQuery(&$arguments, $method, $url)
663
        {
664
                $http =& $this->_http;
665
                $http->GetRequestArguments($url, $arguments);
666
                $arguments["RequestMethod"]           = $method;
667
                $arguments["Headers"]["Content-Type"] = "text/xml";
668
                $arguments["Headers"]["Depth"]        = 1;
669
        }
670
 
671
        /**
672
         *  Open a connection, send request, read header
673
         *  and body.
674
         *
675
         *  @param Array $args Connetion's argument
676
         *  @param Array &$headers Array with the header response.
677
         *  @param string &$body Body response.
678
         *  @return boolean True is query success
679
         *  @access private
680
         */
681
        private function Request($args, &$headers, &$body)
682
        {
683
                $args['RequestURI'] = str_replace(' ', '%20', $args['RequestURI']); //Hack to make filenames with spaces work.
684
                $http =& $this->_http;
685
                $http->Open($args);
686
                $http->SendRequest($args);
687
                $http->ReadReplyHeaders($headers);
688
                if ($http->response_status[0] != 2) {
689
                        switch ($http->response_status) {
690
                                case 404:
691
                                        $this->errNro = NOT_FOUND;
692
                                        break;
693
                                case 401:
694
                                        $this->errNro = AUTH_REQUIRED;
695
                                        break;
696
                                default:
697
                                        $this->errNro = UNKNOWN_ERROR;
698
                                        break;
699
                        }
700
                        //            trigger_error("request to $args[RequestURI] failed: $http->response_status
701
                        //Error: $http->error");
702
                        $http->close();
703
                        return false;
704
                }
705
                $this->errNro = NO_ERROR;
706
                $body         = '';
707
                $tbody        = '';
708
                for (;;) {
709
                        $error = $http->ReadReplyBody($tbody, 1000);
710
                        if ($error != "" || strlen($tbody) == 0) {
711
                                break;
712
                        }
713
                        $body .= ($tbody);
714
                }
715
                $http->close();
716
                return true;
717
        }
718
 
719
        /**
720
         *  Returns $url stripped of '//'
721
         *
722
         *  Delete "//" on URL requests.
723
         *
724
         *  @param string $url URL
725
         *  @return string New cleaned URL.
726
         *  @access private
727
         */
728
        private function cleanURL($url)
729
        {
730
                return preg_replace("/((^:)\/\/)/", "//", $url);
731
        }
732
 
733
        /*
734
          Taken from http://www.php.net/manual/en/function.xml-parse.php#52567
735
          Modified by Martin Guppy <http://www.deadpan110.com/>
736
          Usage
737
          Grab some XML data, either from a file, URL, etc. however you want.
738
          Assume storage in $strYourXML;
739
          Converted "class" into a single function by Daniel Marschall, ViaThinkSoft
740
         */
741
        private static function xmlParse($strInputXML) {
742
                $arrOutput = array();
743
 
744
                $resParser = xml_parser_create();
745
                xml_set_element_handler($resParser,
746
                        function /*tagOpen*/($parser, $name, $attrs) use (&$arrOutput) {
747
                                $tag = array("name" => $name, "attrs" => $attrs);
748
                                array_push($arrOutput, $tag);
749
                        },
750
                        function /*tagClosed*/($parser, $name) use (&$arrOutput) {
751
                                $arrOutput[count($arrOutput) - 2]['children'][] = $arrOutput[count($arrOutput) - 1];
752
                                array_pop($arrOutput);
753
                        }
754
                );
755
                xml_set_character_data_handler($resParser,
756
                        function /*tagData*/($parser, $tagData) use (&$arrOutput) {
757
                                if (trim($tagData)) {
758
                                        if (isset($arrOutput[count($arrOutput) - 1]['tagData'])) {
759
                                                $arrOutput[count($arrOutput) - 1]['tagData'] .= $tagData;
760
                                        } else {
761
                                                $arrOutput[count($arrOutput) - 1]['tagData'] = $tagData;
762
                                        }
763
                                }
764
                        }
765
                );
766
 
767
                if (!xml_parse($resParser, $strInputXML)) {
768
                        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($resParser)), xml_get_current_line_number($resParser)));
769
                }
770
 
771
                xml_parser_free($resParser);
772
 
773
                return $arrOutput[0];
774
        }
216 daniel-mar 775
 
776
        /*
777
          Small helper functions
778
        */
779
 
780
        private static function xarray_add($needle, &$array) {
781
                $key = array_search($needle, $array);
782
                if ($key === false) {
783
                        $array[] = $needle;
784
                }
785
        }
786
 
787
        private static function xarray_remove($needle, &$array) {
788
                while (true) {
789
                        $key = array_search($needle, $array);
790
                        if ($key === false) break;
791
                        unset($array[$key]);
792
                }
793
        }
171 daniel-mar 794
}