Subversion Repositories oidplus

Rev

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