Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

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