Subversion Repositories oidplus

Rev

Rev 227 | 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))
250 daniel-mar 347
                        throw new OIDplusException("Cannot get rawDirectoryDump (Request failed)");
171 daniel-mar 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))
250 daniel-mar 493
                        throw new OIDplusException("Cannot call getFile (Request failed)");
171 daniel-mar 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))
250 daniel-mar 523
                        throw new OIDplusException("Cannot call getLogsForUpdate (Request failed)");
171 daniel-mar 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();
227 daniel-mar 530
                if (!isset($arrOutput['children'])) $arrOutput['children'] = array();
171 daniel-mar 531
                foreach ($arrOutput['children'] as $value) {
172 daniel-mar 532
                        /*
533
                        <S:log-item>
534
                        <D:version-name>164</D:version-name>
535
                        <S:date>2019-08-13T13:12:13.915920Z</S:date>
536
                        <D:comment>Update assistant bugfix</D:comment>
537
                        <D:creator-displayname>daniel-marschall</D:creator-displayname>
538
                        <S:modified-path node-kind="file" text-mods="true" prop-mods="false">/trunk/update/index.php</S:modified-path>
539
                        <S:modified-path node-kind="file" text-mods="true" prop-mods="false">/trunk/update/phpsvnclient.inc.php</S:modified-path>
540
                        </S:log-item>
541
                        */
542
 
543
                        $versionName = '';
544
                        $date = '';
545
                        $comment = '';
171 daniel-mar 546
                        foreach ($value['children'] as $entry) {
547
                                if (($entry['name'] == 'S:ADDED-PATH') || ($entry['name'] == 'S:MODIFIED-PATH') || ($entry['name'] == 'S:DELETED-PATH')) {
548
                                        if ($entry['attrs']['NODE-KIND'] == "file") {
549
                                                $array['objects'][] = array(
550
                                                        'object_name' => $entry['tagData'],
551
                                                        'action' => $entry['name'],
552
                                                        'type' => 'file'
553
                                                );
554
                                        } else if ($entry['attrs']['NODE-KIND'] == "dir") {
555
                                                $array['objects'][] = array(
556
                                                        'object_name' => $entry['tagData'],
557
                                                        'action' => $entry['name'],
558
                                                        'type' => 'dir'
559
                                                );
560
                                        }
172 daniel-mar 561
                                } else if ($entry['name'] == 'D:VERSION-NAME') {
562
                                        $versionName = isset($entry['tagData']) ? $entry['tagData'] : '';
563
                                } else if ($entry['name'] == 'S:DATE') {
564
                                        $date = isset($entry['tagData']) ? $entry['tagData'] : '';
565
                                } else if ($entry['name'] == 'D:COMMENT') {
566
                                        $comment = isset($entry['tagData']) ? $entry['tagData'] : '';
567
                                } else if ($entry['name'] == 'D:CREATOR-DISPLAYNAME') {
568
                                        $creator = isset($entry['tagData']) ? $entry['tagData'] : '';
171 daniel-mar 569
                                }
570
                        }
172 daniel-mar 571
                        $revlogs[] = array('versionName' => $versionName,
572
                                           'date' => $date,
573
                                           'comment' => $comment,
574
                                           'creator' => $creator);
171 daniel-mar 575
                }
216 daniel-mar 576
                $files       = array();
577
                $filesDelete = array();
578
                $dirs        = array();
579
                $dirsDelete  = array();
171 daniel-mar 580
 
227 daniel-mar 581
                if (!isset($array['objects'])) $array['objects'] = array();
171 daniel-mar 582
                foreach ($array['objects'] as $objects) {
216 daniel-mar 583
                        // This section was completely changed by Daniel Marschall
171 daniel-mar 584
                        if ($objects['type'] == "file") {
585
                                if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
216 daniel-mar 586
                                        self::xarray_add($objects['object_name'], $files);
587
                                        self::xarray_remove($objects['object_name'], $filesDelete);
171 daniel-mar 588
                                }
589
                                if ($objects['action'] == "S:DELETED-PATH") {
216 daniel-mar 590
                                        self::xarray_add($objects['object_name'], $filesDelete);
591
                                        self::xarray_remove($objects['object_name'], $files);
171 daniel-mar 592
                                }
593
                        }
594
                        if ($objects['type'] == "dir") {
595
                                if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
216 daniel-mar 596
                                        self::xarray_add($objects['object_name'], $dirs);
597
                                        self::xarray_remove($objects['object_name'], $dirsDelete);
171 daniel-mar 598
                                }
599
                                if ($objects['action'] == "S:DELETED-PATH") {
600
                                        // Delete files from filelist
216 daniel-mar 601
                                        $files_copy = $files;
602
                                        foreach ($files_copy as $file) {
603
                                                if (strpos($file, $objects['object_name'].'/') === 0) self::xarray_remove($file, $files);
171 daniel-mar 604
                                        }
605
                                        // END OF Delete files from filelist
606
                                        // Delete dirs from dirslist
216 daniel-mar 607
                                        self::xarray_add($objects['object_name'], $dirsDelete);
608
                                        self::xarray_remove($objects['object_name'], $dirs);
171 daniel-mar 609
                                        // END OF Delete dirs from dirslist
610
                                }
611
                        }
612
                }
613
                $out                = array();
614
                $out['files']       = $files;
615
                $out['filesDelete'] = $filesDelete;
616
                $out['dirs']        = $dirs;
617
                $out['dirsDelete']  = $dirsDelete;
172 daniel-mar 618
                $out['revisions']   = $revlogs;
171 daniel-mar 619
                return $out;
620
        }
621
 
622
        /**
623
         *  Returns the repository version
624
         *
625
         *  @return integer Repository version
626
         *  @access public
627
         */
628
        public function getVersion()
629
        {
630
                if ($this->_repVersion > 0)
631
                        return $this->_repVersion;
632
 
633
                $this->_repVersion = -1;
634
                $this->initQuery($args, "PROPFIND", $this->cleanURL($this->_url . "/!svn/vcc/default"));
635
                $args['Body']                      = PHPSVN_VERSION_REQUEST;
636
                $args['Headers']['Content-Length'] = strlen(PHPSVN_NORMAL_REQUEST);
637
                $args['Headers']['Depth']          = 0;
638
 
639
                if (!$this->Request($args, $tmp, $body))
250 daniel-mar 640
                        throw new OIDplusException("Cannot get repository revision (Request failed)");
171 daniel-mar 641
 
642
                $this->_repVersion = null;
643
                if (preg_match('@/(\d+)\s*</D:href>@ismU', $body, $m)) {
644
                        $this->_repVersion = $m[1];
645
                } else {
250 daniel-mar 646
                        throw new OIDplusException("Cannot get repository revision (RegEx failed)");
171 daniel-mar 647
                }
648
 
649
                return $this->_repVersion;
650
        }
651
 
652
        /**
653
         *  Private Functions
654
         */
655
 
656
        /**
657
         *  Prepare HTTP CLIENT object
658
         *
659
         *  @param array &$arguments Byreferences variable.
660
         *  @param string $method Method for the request (GET,POST,PROPFIND, REPORT,ETC).
661
         *  @param string $url URL for the action.
662
         *  @access private
663
         */
664
        private function initQuery(&$arguments, $method, $url)
665
        {
666
                $http =& $this->_http;
667
                $http->GetRequestArguments($url, $arguments);
668
                $arguments["RequestMethod"]           = $method;
669
                $arguments["Headers"]["Content-Type"] = "text/xml";
670
                $arguments["Headers"]["Depth"]        = 1;
671
        }
672
 
673
        /**
674
         *  Open a connection, send request, read header
675
         *  and body.
676
         *
677
         *  @param Array $args Connetion's argument
678
         *  @param Array &$headers Array with the header response.
679
         *  @param string &$body Body response.
680
         *  @return boolean True is query success
681
         *  @access private
682
         */
683
        private function Request($args, &$headers, &$body)
684
        {
685
                $args['RequestURI'] = str_replace(' ', '%20', $args['RequestURI']); //Hack to make filenames with spaces work.
686
                $http =& $this->_http;
687
                $http->Open($args);
688
                $http->SendRequest($args);
689
                $http->ReadReplyHeaders($headers);
690
                if ($http->response_status[0] != 2) {
691
                        switch ($http->response_status) {
692
                                case 404:
693
                                        $this->errNro = NOT_FOUND;
694
                                        break;
695
                                case 401:
696
                                        $this->errNro = AUTH_REQUIRED;
697
                                        break;
698
                                default:
699
                                        $this->errNro = UNKNOWN_ERROR;
700
                                        break;
701
                        }
702
                        //            trigger_error("request to $args[RequestURI] failed: $http->response_status
703
                        //Error: $http->error");
704
                        $http->close();
705
                        return false;
706
                }
707
                $this->errNro = NO_ERROR;
708
                $body         = '';
709
                $tbody        = '';
710
                for (;;) {
711
                        $error = $http->ReadReplyBody($tbody, 1000);
712
                        if ($error != "" || strlen($tbody) == 0) {
713
                                break;
714
                        }
715
                        $body .= ($tbody);
716
                }
717
                $http->close();
718
                return true;
719
        }
720
 
721
        /**
722
         *  Returns $url stripped of '//'
723
         *
724
         *  Delete "//" on URL requests.
725
         *
726
         *  @param string $url URL
727
         *  @return string New cleaned URL.
728
         *  @access private
729
         */
730
        private function cleanURL($url)
731
        {
732
                return preg_replace("/((^:)\/\/)/", "//", $url);
733
        }
734
 
735
        /*
736
          Taken from http://www.php.net/manual/en/function.xml-parse.php#52567
737
          Modified by Martin Guppy <http://www.deadpan110.com/>
738
          Usage
739
          Grab some XML data, either from a file, URL, etc. however you want.
740
          Assume storage in $strYourXML;
741
          Converted "class" into a single function by Daniel Marschall, ViaThinkSoft
742
         */
743
        private static function xmlParse($strInputXML) {
744
                $arrOutput = array();
745
 
746
                $resParser = xml_parser_create();
747
                xml_set_element_handler($resParser,
748
                        function /*tagOpen*/($parser, $name, $attrs) use (&$arrOutput) {
749
                                $tag = array("name" => $name, "attrs" => $attrs);
750
                                array_push($arrOutput, $tag);
751
                        },
752
                        function /*tagClosed*/($parser, $name) use (&$arrOutput) {
753
                                $arrOutput[count($arrOutput) - 2]['children'][] = $arrOutput[count($arrOutput) - 1];
754
                                array_pop($arrOutput);
755
                        }
756
                );
757
                xml_set_character_data_handler($resParser,
758
                        function /*tagData*/($parser, $tagData) use (&$arrOutput) {
759
                                if (trim($tagData)) {
760
                                        if (isset($arrOutput[count($arrOutput) - 1]['tagData'])) {
761
                                                $arrOutput[count($arrOutput) - 1]['tagData'] .= $tagData;
762
                                        } else {
763
                                                $arrOutput[count($arrOutput) - 1]['tagData'] = $tagData;
764
                                        }
765
                                }
766
                        }
767
                );
768
 
769
                if (!xml_parse($resParser, $strInputXML)) {
770
                        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($resParser)), xml_get_current_line_number($resParser)));
771
                }
772
 
773
                xml_parser_free($resParser);
774
 
775
                return $arrOutput[0];
776
        }
216 daniel-mar 777
 
778
        /*
779
          Small helper functions
780
        */
781
 
782
        private static function xarray_add($needle, &$array) {
783
                $key = array_search($needle, $array);
784
                if ($key === false) {
785
                        $array[] = $needle;
786
                }
787
        }
788
 
789
        private static function xarray_remove($needle, &$array) {
790
                while (true) {
791
                        $key = array_search($needle, $array);
792
                        if ($key === false) break;
793
                        unset($array[$key]);
794
                }
795
        }
171 daniel-mar 796
}