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