Subversion Repositories oidplus

Rev

Rev 350 | 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
/*
4
 * This file includes:
5
 *
6
 * 1. "PHP SVN CLIENT" class
7
 *    Copyright (C) 2007-2008 by Sixdegrees <cesar@sixdegrees.com.br>
8
 *    Cesar D. Rodas
9
 *    https://code.google.com/archive/p/phpsvnclient/
10
 *    License: BSD License
11
 *    CHANGES by Daniel Marschall, ViaThinkSoft in 2019:
12
 *    - The class has been customized and contains specific changes for the software "OIDplus"
13
 *    - Functions which are not used in the "SVN checkout" were removed.
172 daniel-mar 14
 *      The only important functions are getVersion() and updateWorkingCopy()
171 daniel-mar 15
 *    - The dependency class xml2array was converted from a class into a function and
16
 *      included into this class
172 daniel-mar 17
 *    - Added "revision log/comment" functionality
171 daniel-mar 18
 *
19
 * 2. "xml2array" class
20
 *    Taken from http://www.php.net/manual/en/function.xml-parse.php#52567
21
 *    Modified by Martin Guppy <http://www.deadpan110.com/>
22
 *    CHANGES by Daniel Marschall, ViaThinkSoft in 2019:
23
 *    - Converted class into a single function and added that function into the phpsvnclient class
24
 */
25
 
360 daniel-mar 26
// TODO: Translate (OIDplus _L function)
27
 
171 daniel-mar 28
/**
29
 *  PHP SVN CLIENT
30
 *
360 daniel-mar 31
 *  This class is an SVN client. It can perform read operations
32
 *  to an SVN server (over Web-DAV).
171 daniel-mar 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
 
269 daniel-mar 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>';
293 daniel-mar 42
 
269 daniel-mar 43
        /*protected*/ const PHPSVN_VERSION_REQUEST = '<?xml version="1.0" encoding="utf-8"?><propfind xmlns="DAV:"><prop><checked-in xmlns="DAV:"/></prop></propfind>';
293 daniel-mar 44
 
269 daniel-mar 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>';
293 daniel-mar 46
 
269 daniel-mar 47
        /*protected*/ const NO_ERROR = 1;
48
        /*protected*/ const NOT_FOUND = 2;
49
        /*protected*/ const AUTH_REQUIRED = 3;
50
        /*protected*/ const UNKNOWN_ERROR = 4;
251 daniel-mar 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
 
303 daniel-mar 168
                $webbrowser_update = !is_numeric($from_revision);
169
 
172 daniel-mar 170
                if (!is_numeric($from_revision)) {
171
                        $version_file = $from_revision;
172
                        $from_revision = -1;
173
 
174
                        if (!file_exists($version_file)) {
175
                                echo "ERROR: $version_file missing\n";
171 daniel-mar 176
                                flush();
177
                                return false;
172 daniel-mar 178
                        } else {
179
                                //Obtain the number of current version number of the local copy.
180
                                $cont = file_get_contents($version_file);
181
                                if (!preg_match('@Revision (\d+)@', $cont, $m)) {
182
                                        echo "ERROR: $version_file unknown format\n";
183
                                        flush();
184
                                        return false;
185
                                }
186
                                $from_revision = $m[1];
187
 
188
                                echo "Found $version_file with revision information $from_revision\n";
189
                                flush();
171 daniel-mar 190
                        }
172 daniel-mar 191
                } else {
192
                        $version_file = '';
193
                }
171 daniel-mar 194
 
172 daniel-mar 195
                $errors_happened = false;
196
 
303 daniel-mar 197
                if ($webbrowser_update) {
198
                        // First, do some read/write test (even if we are in preview mode, because we want to detect errors before it is too late)
199
                        $file = $outPath . '/dummy_'.uniqid().'.tmp';
200
                        $file = str_replace("///", "/", $file);
201
                        if (@file_put_contents($file, 'Write Test') === false) {
202
                                echo (!$preview ? "ERROR" : "WARNING").": Cannot write test file $file ! An update through the web browser will NOT be possible.\n";
203
                                flush();
204
                                if (!$preview) return false;
205
                        }
206
                        @unlink($file);
207
                        if (file_exists($file)) {
208
                                echo (!$preview ? "ERROR" : "WARNING").": Cannot delete test file $file ! An update through the web browser will NOT be possible.\n";
209
                                flush();
210
                                if (!$preview) return false;
211
                        }
172 daniel-mar 212
                }
171 daniel-mar 213
 
172 daniel-mar 214
                //Get a list of objects to be updated.
215
                $objects_list = $this->getLogsForUpdate($folder, $from_revision + 1);
216
                if (!is_null($objects_list)) {
217
                        // Output version information
218
                        foreach ($objects_list['revisions'] as $revision) {
216 daniel-mar 219
                                $comment = empty($revision['comment']) ? 'No comment' : $revision['comment'];
186 daniel-mar 220
                                $tex = "New revision ".$revision['versionName']." by ".$revision['creator']." (".date('Y-m-d H:i:s', strtotime($revision['date'])).") ";
216 daniel-mar 221
                                echo trim($tex . str_replace("\n", "\n".str_repeat(' ', strlen($tex)), $comment));
186 daniel-mar 222
                                echo "\n";
171 daniel-mar 223
                        }
224
 
172 daniel-mar 225
                        // Add dirs
200 daniel-mar 226
                        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 227
                        foreach ($objects_list['dirs'] as $file) {
228
                                if ($file != '') {
229
                                        $localPath = str_replace($folder, "", $file);
230
                                        $localPath = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localPath,DIRECTORY_SEPARATOR);
171 daniel-mar 231
 
172 daniel-mar 232
                                        echo "Added or modified directory: $file\n";
233
                                        flush();
234
                                        if (!$preview) {
235
                                                $this->createDirs($localPath);
236
                                                if (!is_dir($localPath)) {
237
                                                        $errors_happened = true;
238
                                                        echo "=> FAILED\n";
239
                                                        flush();
171 daniel-mar 240
                                                }
241
                                        }
242
                                }
172 daniel-mar 243
                        }
171 daniel-mar 244
 
172 daniel-mar 245
                        // Add files
200 daniel-mar 246
                        sort($objects_list['files']); // <-- added by Daniel Marschall: Sort list, just for cosmetic improvement
172 daniel-mar 247
                        foreach ($objects_list['files'] as $file) {
248
                                if ($file != '') {
249
                                        $localFile = str_replace($folder, "", $file);
250
                                        $localFile = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localFile,DIRECTORY_SEPARATOR);
171 daniel-mar 251
 
172 daniel-mar 252
                                        echo "Added or modified file: $file\n";
253
                                        flush();
254
                                        if (!$preview) {
255
                                                $contents = $this->getFile($file);
256
                                                if (@file_put_contents($localFile, $contents) === false) {
257
                                                        $errors_happened = true;
258
                                                        echo "=> FAILED\n";
259
                                                        flush();
171 daniel-mar 260
                                                }
261
                                        }
262
                                }
172 daniel-mar 263
                        }
216 daniel-mar 264
 
265
                        // Remove files
200 daniel-mar 266
                        sort($objects_list['filesDelete']); // <-- added by Daniel Marschall: Sort list, just for cosmetic improvement
172 daniel-mar 267
                        foreach ($objects_list['filesDelete'] as $file) {
268
                                if ($file != '') {
269
                                        $localFile = str_replace($folder, "", $file);
270
                                        $localFile = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localFile,DIRECTORY_SEPARATOR);
171 daniel-mar 271
 
172 daniel-mar 272
                                        echo "Removed file: $file\n";
273
                                        flush();
171 daniel-mar 274
 
172 daniel-mar 275
                                        if (!$preview) {
276
                                                @unlink($localFile);
277
                                                if (file_exists($localFile)) {
278
                                                        $errors_happened = true;
279
                                                        echo "=> FAILED\n";
280
                                                        flush();
171 daniel-mar 281
                                                }
282
                                        }
283
                                }
172 daniel-mar 284
                        }
171 daniel-mar 285
 
172 daniel-mar 286
                        // Remove dirs
287
                        // 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 288
                        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 289
                        foreach ($objects_list['dirsDelete'] as $file) {
290
                                if ($file != '') {
291
                                        $localPath = str_replace($folder, "", $file);
292
                                        $localPath = rtrim($outPath,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($localPath,DIRECTORY_SEPARATOR);
171 daniel-mar 293
 
172 daniel-mar 294
                                        echo "Removed directory: $file\n";
295
                                        flush();
171 daniel-mar 296
 
172 daniel-mar 297
                                        if (!$preview) {
298
                                                $this->removeDirs($localPath);
299
                                                if (is_dir($localPath)) {
300
                                                        $errors_happened = true;
301
                                                        echo "=> FAILED\n";
302
                                                        flush();
171 daniel-mar 303
                                                }
304
                                        }
305
                                }
172 daniel-mar 306
                        }
171 daniel-mar 307
 
216 daniel-mar 308
                        // Update version file
172 daniel-mar 309
                        // Changed by Daniel Marschall: Added $errors_happened
310
                        if (!$preview && !empty($version_file)) {
311
                                if (!$errors_happened) {
312
                                        if (@file_put_contents($version_file, "Revision " . $this->actVersion . "\n") === false) {
313
                                                echo "ERROR: Could not set the revision\n";
314
                                                flush();
315
                                                return false;
171 daniel-mar 316
                                        } else {
172 daniel-mar 317
                                                echo "Set revision to " . $this->actVersion . "\n";
171 daniel-mar 318
                                                flush();
172 daniel-mar 319
                                                return true;
171 daniel-mar 320
                                        }
172 daniel-mar 321
                                } else {
322
                                        echo "Revision NOT set to " . $this->actVersion . " because some files/dirs could not be updated. Please try again.\n";
323
                                        flush();
324
                                        return false;
171 daniel-mar 325
                                }
172 daniel-mar 326
                        } else {
327
                                return true;
171 daniel-mar 328
                        }
329
                }
330
        }
331
 
332
        /**
333
         *  rawDirectoryDump
334
         *
335
         * Dumps SVN data for $folder in the version $version of the repository.
336
         *
337
         *  @param string  $folder Folder to get data
338
         *  @param integer $version Repository version, -1 means actual
339
         *  @return array SVN data dump.
340
         */
341
        private function rawDirectoryDump($folder = '/trunk/', $version = -1)
342
        {
343
                if ($version == -1 || $version > $this->actVersion) {
344
                        $version = $this->actVersion;
345
                }
346
                $url = $this->cleanURL($this->_url . "/!svn/bc/" . $version . "/" . $folder . "/");
347
                $this->initQuery($args, "PROPFIND", $url);
251 daniel-mar 348
                $args['Body']                      = self::PHPSVN_NORMAL_REQUEST;
349
                $args['Headers']['Content-Length'] = strlen(self::PHPSVN_NORMAL_REQUEST);
171 daniel-mar 350
 
351
                if (!$this->Request($args, $headers, $body))
250 daniel-mar 352
                        throw new OIDplusException("Cannot get rawDirectoryDump (Request failed)");
171 daniel-mar 353
 
216 daniel-mar 354
                return self::xmlParse($body);
171 daniel-mar 355
        }
356
 
357
        /**
358
         *  getDirectoryFiles
359
         *
360
         *  Returns all the files in $folder in the version $version of
361
         *  the repository.
362
         *
363
         *  @param string  $folder Folder to get files
364
         *  @param integer $version Repository version, -1 means actual
365
         *  @return array List of files.
366
         */
367
        private function getDirectoryFiles($folder = '/trunk/', $version = -1)
368
        {
369
                if ($arrOutput = $this->rawDirectoryDump($folder, $version)) {
370
                        $files = array();
371
                        foreach ($arrOutput['children'] as $key => $value) {
172 daniel-mar 372
                                array_walk_recursive($value,
373
                                        function ($item, $key) {
374
                                                if ($key == 'name') {
375
                                                        if (($item == 'D:HREF') || ($item == 'LP1:GETLASTMODIFIED') || ($item == 'LP1:VERSION-NAME') || ($item == 'LP2:BASELINE-RELATIVE-PATH') || ($item == 'LP3:BASELINE-RELATIVE-PATH') || ($item == 'D:STATUS')) {
376
                                                                $this->lastDirectoryFiles = $item;
377
                                                        }
378
                                                } elseif (($key == 'tagData') && ($this->lastDirectoryFiles != '')) {
379
 
380
                                                        // Unsure if the 1st of two D:HREF's always returns the result we want, but for now...
381
                                                        if (($this->lastDirectoryFiles == 'D:HREF') && (isset($this->storeDirectoryFiles['type'])))
382
                                                                return;
383
 
384
                                                        // Dump into the array
385
                                                        switch ($this->lastDirectoryFiles) {
386
                                                                case 'D:HREF':
387
                                                                        $var = 'type';
388
                                                                        break;
389
                                                                case 'LP1:VERSION-NAME':
390
                                                                        $var = 'version';
391
                                                                        break;
392
                                                                case 'LP1:GETLASTMODIFIED':
393
                                                                        $var = 'last-mod';
394
                                                                        break;
395
                                                                case 'LP2:BASELINE-RELATIVE-PATH':
396
                                                                case 'LP3:BASELINE-RELATIVE-PATH':
397
                                                                        $var = 'path';
398
                                                                        break;
399
                                                                case 'D:STATUS':
400
                                                                        $var = 'status';
401
                                                                        break;
402
                                                        }
403
                                                        $this->storeDirectoryFiles[$var] = $item;
404
                                                        $this->lastDirectoryFiles        = '';
405
 
406
                                                        // Detect 'type' as either a 'directory' or 'file'
407
                                                        if ((isset($this->storeDirectoryFiles['type'])) && (isset($this->storeDirectoryFiles['last-mod'])) && (isset($this->storeDirectoryFiles['path'])) && (isset($this->storeDirectoryFiles['status']))) {
408
                                                                $this->storeDirectoryFiles['path'] = str_replace(' ', '%20', $this->storeDirectoryFiles['path']); //Hack to make filenames with spaces work.
409
                                                                $len                               = strlen($this->storeDirectoryFiles['path']);
410
                                                                if (substr($this->storeDirectoryFiles['type'], strlen($this->storeDirectoryFiles['type']) - $len) == $this->storeDirectoryFiles['path']) {
411
                                                                        $this->storeDirectoryFiles['type'] = 'file';
412
                                                                } else {
413
                                                                        $this->storeDirectoryFiles['type'] = 'directory';
414
                                                                }
415
                                                        }
416
                                                } else {
417
                                                        $this->lastDirectoryFiles = '';
418
                                                }
419
                                        }
420
                                );
171 daniel-mar 421
                                array_push($files, $this->storeDirectoryFiles);
422
                                unset($this->storeDirectoryFiles);
423
                        }
424
                        return $files;
425
                }
426
                return false;
427
        }
428
 
306 daniel-mar 429
        private static function dirToArray($dir, &$result) {
430
                $cdir = scandir($dir);
431
                foreach ($cdir as $key => $value) {
432
                        if (!in_array($value,array('.','..'))) {
433
                                if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
434
                                        $result[] = $dir.DIRECTORY_SEPARATOR.$value.DIRECTORY_SEPARATOR;
435
                                        self::dirToArray($dir.DIRECTORY_SEPARATOR.$value, $result);
436
                                } else {
437
                                        $result[] = $dir.DIRECTORY_SEPARATOR.$value;
438
                                }
439
                        }
440
                }
441
        }
442
 
443
        public function compareToDirectory($local_folder, $svn_folder='/trunk/', $version=-1) {
444
                $local_cont = array();
445
                self::dirToArray($local_folder, $local_cont);
446
                foreach ($local_cont as $key => &$c) {
350 daniel-mar 447
                        $c = str_replace('\\', '/', $c);
306 daniel-mar 448
                        $c = substr($c, strlen($local_folder));
449
                        if (substr($c,0,1) === '/') $c = substr($c, 1);
450
                        if ($c === '') unset($local_cont[$key]);
451
                        if (strpos($c,'.svn/') === 0) unset($local_cont[$key]);
452
                        if ((strpos($c,'userdata/') === 0) && ($c !== 'userdata/info.txt') && ($c !== 'userdata/.htaccess') && ($c !== 'userdata/index.html') && (substr($c,-1) !== '/')) unset($local_cont[$key]);
453
                }
454
                unset($key);
455
                unset($c);
456
                natsort($local_cont);
457
 
458
                $svn_cont = array();
459
                $contents = $this->getDirectoryTree($svn_folder, $version, true);
460
                foreach ($contents as $cont) {
461
                        if ($cont['type'] == 'directory') {
316 daniel-mar 462
                                $svn_cont[] = '/'.urldecode($cont['path']).'/';
306 daniel-mar 463
                        } else if ($cont['type'] == 'file') {
316 daniel-mar 464
                                $svn_cont[] = '/'.urldecode($cont['path']);
306 daniel-mar 465
                        }
466
                }
467
                foreach ($svn_cont as $key => &$c) {
350 daniel-mar 468
                        $c = str_replace('\\', '/', $c);
306 daniel-mar 469
                        $c = substr($c, strlen($svn_folder));
470
                        if (substr($c,0,1) === '/') $c = substr($c, 1);
471
                        if ($c === '') unset($svn_cont[$key]);
472
                        if ((strpos($c,'userdata/') === 0) && ($c !== 'userdata/info.txt') && ($c !== 'userdata/.htaccess') && ($c !== 'userdata/index.html') && (substr($c,-1) !== '/')) unset($svn_cont[$key]);
473
                }
474
                unset($key);
475
                unset($c);
476
                unset($contents);
477
                unset($cont);
478
                natsort($svn_cont);
479
 
480
                $only_svn = array_diff($svn_cont, $local_cont);
481
                $only_local = array_diff($local_cont, $svn_cont);
482
                return array($svn_cont, $local_cont);
483
        }
484
 
171 daniel-mar 485
        /**
486
         *  getDirectoryTree
487
         *
488
         *  Returns the complete tree of files and directories in $folder from the
489
         *  version $version of the repository. Can also be used to get the info
490
         *  for a single file or directory.
491
         *
492
         *  @param string  $folder Folder to get tree
493
         *  @param integer $version Repository version, -1 means current
494
         *  @param boolean $recursive Whether to get the tree recursively, or just
495
         *  the specified directory/file.
496
         *
497
         *  @return array List of files and directories.
498
         */
499
        private function getDirectoryTree($folder = '/trunk/', $version = -1, $recursive = true)
500
        {
501
                $directoryTree = array();
502
 
503
                if (!($arrOutput = $this->getDirectoryFiles($folder, $version)))
504
                        return false;
505
 
506
                if (!$recursive)
507
                        return $arrOutput[0];
508
 
509
                while (count($arrOutput) && is_array($arrOutput)) {
510
                        $array = array_shift($arrOutput);
511
 
512
                        array_push($directoryTree, $array);
513
 
514
                        if (trim($array['path'], '/') == trim($folder, '/'))
515
                                continue;
516
 
517
                        if ($array['type'] == 'directory') {
518
                                $walk = $this->getDirectoryFiles($array['path'], $version);
519
                                array_shift($walk);
520
 
521
                                foreach ($walk as $step) {
522
                                        array_unshift($arrOutput, $step);
523
                                }
524
                        }
525
                }
526
                return $directoryTree;
527
        }
528
 
529
        /**
530
         *  Returns file contents
531
         *
532
         *  @param      string  $file File pathname
533
         *  @param      integer $version File Version
534
         *  @return     string  File content and information, false on error, or if a
535
         *              directory is requested
536
         */
537
        private function getFile($file, $version = -1)
538
        {
539
                if ($version == -1 || $version > $this->actVersion) {
540
                        $version = $this->actVersion;
541
                }
542
 
543
                // check if this is a directory... if so, return false, otherwise we
544
                // get the HTML output of the directory listing from the SVN server.
545
                // This is maybe a bit heavy since it makes another connection to the
546
                // SVN server. Maybe add this as an option/parameter? ES 23/06/08
547
                $fileInfo = $this->getDirectoryTree($file, $version, false);
548
                if ($fileInfo["type"] == "directory")
549
                        return false;
550
 
551
                $url = $this->cleanURL($this->_url . "/!svn/bc/" . $version . "/" . $file . "/");
552
                $this->initQuery($args, "GET", $url);
553
                if (!$this->Request($args, $headers, $body))
250 daniel-mar 554
                        throw new OIDplusException("Cannot call getFile (Request failed)");
171 daniel-mar 555
 
556
                return $body;
557
        }
558
 
172 daniel-mar 559
        private function getLogsForUpdate($file, $vini = 0, $vend = -1)
171 daniel-mar 560
        {
561
                $fileLogs = array();
562
 
172 daniel-mar 563
                if ($vend == -1) {
171 daniel-mar 564
                        $vend = $this->actVersion;
565
                }
566
 
567
                if ($vini < 0)
568
                        $vini = 0;
569
 
570
                if ($vini > $vend) {
571
                        $vini = $vend;
572
                        echo "Nothing updated\n";
573
                        flush();
574
                        return null;
575
                }
576
 
577
                $url = $this->cleanURL($this->_url . "/!svn/bc/" . $this->actVersion . "/" . $file . "/");
578
                $this->initQuery($args, "REPORT", $url);
251 daniel-mar 579
                $args['Body']                      = sprintf(self::PHPSVN_LOGS_REQUEST, $vini, $vend);
171 daniel-mar 580
                $args['Headers']['Content-Length'] = strlen($args['Body']);
581
                $args['Headers']['Depth']          = 1;
582
 
583
                if (!$this->Request($args, $headers, $body))
250 daniel-mar 584
                        throw new OIDplusException("Cannot call getLogsForUpdate (Request failed)");
171 daniel-mar 585
 
216 daniel-mar 586
                $arrOutput = self::xmlParse($body);
171 daniel-mar 587
 
172 daniel-mar 588
                $revlogs = array();
589
 
171 daniel-mar 590
                $array = array();
227 daniel-mar 591
                if (!isset($arrOutput['children'])) $arrOutput['children'] = array();
171 daniel-mar 592
                foreach ($arrOutput['children'] as $value) {
172 daniel-mar 593
                        /*
594
                        <S:log-item>
595
                        <D:version-name>164</D:version-name>
596
                        <S:date>2019-08-13T13:12:13.915920Z</S:date>
597
                        <D:comment>Update assistant bugfix</D:comment>
598
                        <D:creator-displayname>daniel-marschall</D:creator-displayname>
599
                        <S:modified-path node-kind="file" text-mods="true" prop-mods="false">/trunk/update/index.php</S:modified-path>
600
                        <S:modified-path node-kind="file" text-mods="true" prop-mods="false">/trunk/update/phpsvnclient.inc.php</S:modified-path>
601
                        </S:log-item>
602
                        */
603
 
604
                        $versionName = '';
605
                        $date = '';
606
                        $comment = '';
171 daniel-mar 607
                        foreach ($value['children'] as $entry) {
608
                                if (($entry['name'] == 'S:ADDED-PATH') || ($entry['name'] == 'S:MODIFIED-PATH') || ($entry['name'] == 'S:DELETED-PATH')) {
609
                                        if ($entry['attrs']['NODE-KIND'] == "file") {
610
                                                $array['objects'][] = array(
611
                                                        'object_name' => $entry['tagData'],
612
                                                        'action' => $entry['name'],
613
                                                        'type' => 'file'
614
                                                );
615
                                        } else if ($entry['attrs']['NODE-KIND'] == "dir") {
616
                                                $array['objects'][] = array(
617
                                                        'object_name' => $entry['tagData'],
618
                                                        'action' => $entry['name'],
619
                                                        'type' => 'dir'
620
                                                );
621
                                        }
172 daniel-mar 622
                                } else if ($entry['name'] == 'D:VERSION-NAME') {
623
                                        $versionName = isset($entry['tagData']) ? $entry['tagData'] : '';
624
                                } else if ($entry['name'] == 'S:DATE') {
625
                                        $date = isset($entry['tagData']) ? $entry['tagData'] : '';
626
                                } else if ($entry['name'] == 'D:COMMENT') {
627
                                        $comment = isset($entry['tagData']) ? $entry['tagData'] : '';
628
                                } else if ($entry['name'] == 'D:CREATOR-DISPLAYNAME') {
629
                                        $creator = isset($entry['tagData']) ? $entry['tagData'] : '';
171 daniel-mar 630
                                }
631
                        }
172 daniel-mar 632
                        $revlogs[] = array('versionName' => $versionName,
633
                                           'date' => $date,
634
                                           'comment' => $comment,
635
                                           'creator' => $creator);
171 daniel-mar 636
                }
216 daniel-mar 637
                $files       = array();
638
                $filesDelete = array();
639
                $dirs        = array();
315 daniel-mar 640
                $dirsNew     = array();
641
                $dirsMod     = array();
216 daniel-mar 642
                $dirsDelete  = array();
171 daniel-mar 643
 
227 daniel-mar 644
                if (!isset($array['objects'])) $array['objects'] = array();
171 daniel-mar 645
                foreach ($array['objects'] as $objects) {
216 daniel-mar 646
                        // This section was completely changed by Daniel Marschall
171 daniel-mar 647
                        if ($objects['type'] == "file") {
648
                                if ($objects['action'] == "S:ADDED-PATH" || $objects['action'] == "S:MODIFIED-PATH") {
216 daniel-mar 649
                                        self::xarray_add($objects['object_name'], $files);
650
                                        self::xarray_remove($objects['object_name'], $filesDelete);
171 daniel-mar 651
                                }
652
                                if ($objects['action'] == "S:DELETED-PATH") {
216 daniel-mar 653
                                        self::xarray_add($objects['object_name'], $filesDelete);
654
                                        self::xarray_remove($objects['object_name'], $files);
171 daniel-mar 655
                                }
656
                        }
657
                        if ($objects['type'] == "dir") {
315 daniel-mar 658
                                if ($objects['action'] == "S:ADDED-PATH") {
216 daniel-mar 659
                                        self::xarray_add($objects['object_name'], $dirs);
315 daniel-mar 660
                                        self::xarray_add($objects['object_name'], $dirsNew);
216 daniel-mar 661
                                        self::xarray_remove($objects['object_name'], $dirsDelete);
171 daniel-mar 662
                                }
315 daniel-mar 663
                                if ($objects['action'] == "S:MODIFIED-PATH") {
664
                                        self::xarray_add($objects['object_name'], $dirs);
665
                                        self::xarray_add($objects['object_name'], $dirsMod);
666
                                        self::xarray_remove($objects['object_name'], $dirsDelete);
667
                                }
171 daniel-mar 668
                                if ($objects['action'] == "S:DELETED-PATH") {
669
                                        // Delete files from filelist
216 daniel-mar 670
                                        $files_copy = $files;
671
                                        foreach ($files_copy as $file) {
672
                                                if (strpos($file, $objects['object_name'].'/') === 0) self::xarray_remove($file, $files);
171 daniel-mar 673
                                        }
674
                                        // END OF Delete files from filelist
675
                                        // Delete dirs from dirslist
216 daniel-mar 676
                                        self::xarray_add($objects['object_name'], $dirsDelete);
677
                                        self::xarray_remove($objects['object_name'], $dirs);
315 daniel-mar 678
                                        self::xarray_remove($objects['object_name'], $dirsMod);
679
                                        self::xarray_remove($objects['object_name'], $dirsNew);
171 daniel-mar 680
                                        // END OF Delete dirs from dirslist
681
                                }
682
                        }
683
                }
315 daniel-mar 684
                foreach ($dirsNew as $dir) {
685
                        // For new directories, also download all its contents
316 daniel-mar 686
                        try {
687
                                $contents = $this->getDirectoryTree($dir, $vend, true);
688
                        } catch (Exception $e) {
689
                                // This can happen when you update from a very old version and a directory was new which is not existing in the newest ($vend) version
690
                                // In this case, we don't need it and can ignore the error
691
                                $contents = array();
692
                        }
302 daniel-mar 693
                        foreach ($contents as $cont) {
694
                                if ($cont['type'] == 'directory') {
316 daniel-mar 695
                                        $dirname = '/'.urldecode($cont['path']);
302 daniel-mar 696
                                        self::xarray_add($dirname, $dirs);
697
                                        self::xarray_remove($dirname, $dirsDelete);
698
                                } else if ($cont['type'] == 'file') {
316 daniel-mar 699
                                        $filename = '/'.urldecode($cont['path']);
302 daniel-mar 700
                                        self::xarray_add($filename, $files);
701
                                        self::xarray_remove($filename, $filesDelete);
702
                                }
703
                        }
704
                }
171 daniel-mar 705
                $out                = array();
706
                $out['files']       = $files;
707
                $out['filesDelete'] = $filesDelete;
708
                $out['dirs']        = $dirs;
709
                $out['dirsDelete']  = $dirsDelete;
172 daniel-mar 710
                $out['revisions']   = $revlogs;
171 daniel-mar 711
                return $out;
712
        }
713
 
714
        /**
715
         *  Returns the repository version
716
         *
717
         *  @return integer Repository version
718
         *  @access public
719
         */
720
        public function getVersion()
721
        {
722
                if ($this->_repVersion > 0)
723
                        return $this->_repVersion;
724
 
725
                $this->_repVersion = -1;
726
                $this->initQuery($args, "PROPFIND", $this->cleanURL($this->_url . "/!svn/vcc/default"));
251 daniel-mar 727
                $args['Body']                      = self::PHPSVN_VERSION_REQUEST;
728
                $args['Headers']['Content-Length'] = strlen(self::PHPSVN_NORMAL_REQUEST);
171 daniel-mar 729
                $args['Headers']['Depth']          = 0;
730
 
731
                if (!$this->Request($args, $tmp, $body))
250 daniel-mar 732
                        throw new OIDplusException("Cannot get repository revision (Request failed)");
171 daniel-mar 733
 
734
                $this->_repVersion = null;
735
                if (preg_match('@/(\d+)\s*</D:href>@ismU', $body, $m)) {
736
                        $this->_repVersion = $m[1];
737
                } else {
250 daniel-mar 738
                        throw new OIDplusException("Cannot get repository revision (RegEx failed)");
171 daniel-mar 739
                }
740
 
741
                return $this->_repVersion;
742
        }
743
 
744
        /**
745
         *  Private Functions
746
         */
747
 
748
        /**
749
         *  Prepare HTTP CLIENT object
750
         *
751
         *  @param array &$arguments Byreferences variable.
752
         *  @param string $method Method for the request (GET,POST,PROPFIND, REPORT,ETC).
753
         *  @param string $url URL for the action.
754
         *  @access private
755
         */
756
        private function initQuery(&$arguments, $method, $url)
757
        {
758
                $http =& $this->_http;
759
                $http->GetRequestArguments($url, $arguments);
760
                $arguments["RequestMethod"]           = $method;
761
                $arguments["Headers"]["Content-Type"] = "text/xml";
762
                $arguments["Headers"]["Depth"]        = 1;
763
        }
764
 
765
        /**
766
         *  Open a connection, send request, read header
767
         *  and body.
768
         *
769
         *  @param Array $args Connetion's argument
770
         *  @param Array &$headers Array with the header response.
771
         *  @param string &$body Body response.
772
         *  @return boolean True is query success
773
         *  @access private
774
         */
775
        private function Request($args, &$headers, &$body)
776
        {
777
                $args['RequestURI'] = str_replace(' ', '%20', $args['RequestURI']); //Hack to make filenames with spaces work.
778
                $http =& $this->_http;
779
                $http->Open($args);
780
                $http->SendRequest($args);
781
                $http->ReadReplyHeaders($headers);
782
                if ($http->response_status[0] != 2) {
783
                        switch ($http->response_status) {
784
                                case 404:
251 daniel-mar 785
                                        $this->errNro = self::NOT_FOUND;
171 daniel-mar 786
                                        break;
787
                                case 401:
251 daniel-mar 788
                                        $this->errNro = self::AUTH_REQUIRED;
171 daniel-mar 789
                                        break;
790
                                default:
251 daniel-mar 791
                                        $this->errNro = self::UNKNOWN_ERROR;
171 daniel-mar 792
                                        break;
793
                        }
794
                        //            trigger_error("request to $args[RequestURI] failed: $http->response_status
795
                        //Error: $http->error");
796
                        $http->close();
797
                        return false;
798
                }
251 daniel-mar 799
                $this->errNro = self::NO_ERROR;
171 daniel-mar 800
                $body         = '';
801
                $tbody        = '';
802
                for (;;) {
803
                        $error = $http->ReadReplyBody($tbody, 1000);
804
                        if ($error != "" || strlen($tbody) == 0) {
805
                                break;
806
                        }
807
                        $body .= ($tbody);
808
                }
809
                $http->close();
810
                return true;
811
        }
812
 
813
        /**
814
         *  Returns $url stripped of '//'
815
         *
816
         *  Delete "//" on URL requests.
817
         *
818
         *  @param string $url URL
819
         *  @return string New cleaned URL.
820
         *  @access private
821
         */
822
        private function cleanURL($url)
823
        {
824
                return preg_replace("/((^:)\/\/)/", "//", $url);
825
        }
826
 
827
        /*
828
          Taken from http://www.php.net/manual/en/function.xml-parse.php#52567
829
          Modified by Martin Guppy <http://www.deadpan110.com/>
830
          Usage
831
          Grab some XML data, either from a file, URL, etc. however you want.
832
          Assume storage in $strYourXML;
833
          Converted "class" into a single function by Daniel Marschall, ViaThinkSoft
834
         */
835
        private static function xmlParse($strInputXML) {
836
                $arrOutput = array();
837
 
838
                $resParser = xml_parser_create();
839
                xml_set_element_handler($resParser,
840
                        function /*tagOpen*/($parser, $name, $attrs) use (&$arrOutput) {
841
                                $tag = array("name" => $name, "attrs" => $attrs);
842
                                array_push($arrOutput, $tag);
843
                        },
844
                        function /*tagClosed*/($parser, $name) use (&$arrOutput) {
845
                                $arrOutput[count($arrOutput) - 2]['children'][] = $arrOutput[count($arrOutput) - 1];
846
                                array_pop($arrOutput);
847
                        }
848
                );
849
                xml_set_character_data_handler($resParser,
850
                        function /*tagData*/($parser, $tagData) use (&$arrOutput) {
851
                                if (trim($tagData)) {
852
                                        if (isset($arrOutput[count($arrOutput) - 1]['tagData'])) {
853
                                                $arrOutput[count($arrOutput) - 1]['tagData'] .= $tagData;
854
                                        } else {
855
                                                $arrOutput[count($arrOutput) - 1]['tagData'] = $tagData;
856
                                        }
857
                                }
858
                        }
859
                );
860
 
861
                if (!xml_parse($resParser, $strInputXML)) {
862
                        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($resParser)), xml_get_current_line_number($resParser)));
863
                }
864
 
865
                xml_parser_free($resParser);
866
 
867
                return $arrOutput[0];
868
        }
216 daniel-mar 869
 
870
        /*
871
          Small helper functions
872
        */
873
 
874
        private static function xarray_add($needle, &$array) {
875
                $key = array_search($needle, $array);
876
                if ($key === false) {
877
                        $array[] = $needle;
878
                }
879
        }
880
 
881
        private static function xarray_remove($needle, &$array) {
882
                while (true) {
883
                        $key = array_search($needle, $array);
884
                        if ($key === false) break;
885
                        unset($array[$key]);
886
                }
887
        }
360 daniel-mar 888
}