Subversion Repositories oidplus

Rev

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