generate_filelist_xml.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2015-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file build/generate_filelist_xml.php
  20. * \ingroup dev
  21. * \brief This script create a xml checksum file
  22. */
  23. if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Do not create database handler $db
  24. $sapi_type = php_sapi_name();
  25. $script_file = basename(__FILE__);
  26. $path=dirname(__FILE__).'/';
  27. // Test if batch mode
  28. if (substr($sapi_type, 0, 3) == 'cgi') {
  29. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  30. exit;
  31. }
  32. require_once $path."../htdocs/master.inc.php";
  33. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  34. /*
  35. * Main
  36. */
  37. $includecustom=0;
  38. $includeconstants=array();
  39. if (empty($argv[1])) {
  40. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  41. print "Example: ".$script_file." release=6.0.0 includecustom=1 includeconstant=FR:INVOICE_CAN_ALWAYS_BE_REMOVED:0 includeconstant=all:MAILING_NO_USING_PHPMAIL:1\n";
  42. exit -1;
  43. }
  44. parse_str($argv[1]);
  45. $i=0;
  46. while ($i < $argc) {
  47. if (! empty($argv[$i])) parse_str($argv[$i]);
  48. if (preg_match('/includeconstant=/', $argv[$i])) {
  49. $tmp=explode(':', $includeconstant, 3);
  50. if (count($tmp) != 3) {
  51. print "Error: Bad parameter includeconstant ".$includeconstant."\n";
  52. exit -1;
  53. }
  54. $includeconstants[$tmp[0]][$tmp[1]] = $tmp[2];
  55. }
  56. $i++;
  57. }
  58. if (empty($release)) {
  59. print "Error: Missing release paramater\n";
  60. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  61. exit -1;
  62. }
  63. $savrelease = $release;
  64. // If release is auto, we take current version
  65. $tmpver=explode('-', $release, 2);
  66. if ($tmpver[0] == 'auto' || $tmpver[0] == 'autostable') {
  67. $release=DOL_VERSION;
  68. if ($tmpver[1] && $tmpver[0] == 'auto') $release.='-'.$tmpver[1];
  69. }
  70. if (empty($includecustom)) {
  71. $tmpverbis=explode('-', $release, 2);
  72. if (empty($tmpverbis[1]) || $tmpver[0] == 'autostable') {
  73. if (DOL_VERSION != $tmpverbis[0] && $savrelease != 'auto') {
  74. print 'Error: When parameter "includecustom" is not set and there is no suffix in release parameter, version declared into filefunc.in.php ('.DOL_VERSION.') must be exact same value than "release" parameter ('.$tmpverbis[0].')'."\n";
  75. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  76. exit -1;
  77. }
  78. } else {
  79. $tmpverter=explode('-', DOL_VERSION, 2);
  80. if ($tmpverter[0] != $tmpverbis[0]) {
  81. print 'Error: When parameter "includecustom" is not set, version declared into filefunc.in.php ('.DOL_VERSION.') must have value without prefix ('.$tmpverter[0].') that is exact same value than "release" parameter ('.$tmpverbis[0].')'."\n";
  82. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  83. exit -1;
  84. }
  85. }
  86. } else {
  87. if (! preg_match('/'.preg_quote(DOL_VERSION, '/').'-/', $release)) {
  88. print 'Error: When parameter "includecustom" is set, version declared into filefunc.inc.php ('.DOL_VERSION.') must be used with a suffix into "release" parameter (ex: '.DOL_VERSION.'-mydistrib).'."\n";
  89. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  90. exit -1;
  91. }
  92. }
  93. print "Release : ".$release."\n";
  94. print "Working on files into : ".DOL_DOCUMENT_ROOT."\n";
  95. print "Include custom in signature : ".$includecustom."\n";
  96. print "Include constants in signature : ";
  97. foreach ($includeconstants as $countrycode => $tmp) {
  98. foreach ($tmp as $constname => $constvalue) {
  99. print $constname.'='.$constvalue." ";
  100. }
  101. }
  102. print "\n";
  103. //$outputfile=dirname(__FILE__).'/../htdocs/install/filelist-'.$release.'.xml';
  104. $outputdir=dirname(dirname(__FILE__)).'/htdocs/install';
  105. print 'Delete current files '.$outputdir.'/filelist*.xml'."\n";
  106. dol_delete_file($outputdir.'/filelist*.xml', 0, 1, 1);
  107. $checksumconcat=array();
  108. $outputfile=$outputdir.'/filelist-'.$release.'.xml';
  109. $fp = fopen($outputfile, 'w');
  110. if (empty($fp)) {
  111. print 'Failed to open file '.$outputfile."\n";
  112. exit(-1);
  113. }
  114. fputs($fp, '<?xml version="1.0" encoding="UTF-8" ?>'."\n");
  115. fputs($fp, '<checksum_list version="'.$release.'" date="'.dol_print_date(dol_now(), 'dayhourrfc').'" generator="'.$script_file.'">'."\n");
  116. foreach ($includeconstants as $countrycode => $tmp) {
  117. fputs($fp, '<dolibarr_constants country="'.$countrycode.'">'."\n");
  118. foreach ($tmp as $constname => $constvalue) {
  119. $valueforchecksum=(empty($constvalue)?'0':$constvalue);
  120. $checksumconcat[]=$valueforchecksum;
  121. fputs($fp, ' <constant name="'.$constname.'">'.$valueforchecksum.'</constant>'."\n");
  122. }
  123. fputs($fp, '</dolibarr_constants>'."\n");
  124. }
  125. fputs($fp, '<dolibarr_htdocs_dir includecustom="'.$includecustom.'">'."\n");
  126. /*$dir_iterator1 = new RecursiveDirectoryIterator(dirname(__FILE__).'/../htdocs/');
  127. $iterator1 = new RecursiveIteratorIterator($dir_iterator1);
  128. // Need to ignore document custom etc. Note: this also ignore natively symbolic links.
  129. $files = new RegexIterator($iterator1, '#^(?:[A-Z]:)?(?:/(?!(?:'.($includecustom?'':'custom\/|').'documents\/|conf\/|install\/))[^/]+)+/[^/]+\.(?:php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$#i');
  130. */
  131. $regextoinclude='\.(php|php3|php4|php5|phtml|phps|phar|inc|css|scss|html|xml|js|json|tpl|jpg|jpeg|png|gif|ico|sql|lang|txt|yml|md|mp3|mp4|wav|mkv|z|gz|zip|rar|tar|less|svg|eot|woff|woff2|ttf|manifest)$';
  132. $regextoexclude='('.($includecustom?'':'custom|').'documents|conf|install|public\/test|sabre\/sabre\/.*\/tests|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
  133. $files = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude, 'fullname');
  134. $dir='';
  135. $needtoclose=0;
  136. foreach ($files as $filetmp) {
  137. $file = $filetmp['fullname'];
  138. //$newdir = str_replace(dirname(__FILE__).'/../htdocs', '', dirname($file));
  139. $newdir = str_replace(DOL_DOCUMENT_ROOT, '', dirname($file));
  140. if ($newdir!=$dir) {
  141. if ($needtoclose)
  142. fputs($fp, ' </dir>'."\n");
  143. fputs($fp, ' <dir name="'.$newdir.'" >'."\n");
  144. $dir = $newdir;
  145. $needtoclose=1;
  146. }
  147. if (filetype($file)=="file") {
  148. $md5=md5_file($file);
  149. $checksumconcat[]=$md5;
  150. fputs($fp, ' <md5file name="'.basename($file).'" size="'.filesize($file).'">'.$md5.'</md5file>'."\n");
  151. }
  152. }
  153. fputs($fp, ' </dir>'."\n");
  154. fputs($fp, '</dolibarr_htdocs_dir>'."\n");
  155. asort($checksumconcat); // Sort list of checksum
  156. //var_dump($checksumconcat);
  157. fputs($fp, '<dolibarr_htdocs_dir_checksum>'."\n");
  158. fputs($fp, md5(join(',', $checksumconcat))."\n");
  159. fputs($fp, '</dolibarr_htdocs_dir_checksum>'."\n");
  160. $checksumconcat=array();
  161. fputs($fp, '<dolibarr_script_dir version="'.$release.'">'."\n");
  162. // TODO Replace RecursiveDirectoryIterator with dol_dir_list
  163. /*$dir_iterator2 = new RecursiveDirectoryIterator(dirname(__FILE__).'/../scripts/');
  164. $iterator2 = new RecursiveIteratorIterator($dir_iterator2);
  165. // Need to ignore document custom etc. Note: this also ignore natively symbolic links.
  166. $files = new RegexIterator($iterator2, '#^(?:[A-Z]:)?(?:/(?!(?:custom|documents|conf|install))[^/]+)+/[^/]+\.(?:php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$#i');
  167. */
  168. $regextoinclude='\.(php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$';
  169. $regextoexclude='(custom|documents|conf|install)$'; // Exclude dirs
  170. $files = dol_dir_list(dirname(__FILE__).'/../scripts/', 'files', 1, $regextoinclude, $regextoexclude, 'fullname');
  171. $dir='';
  172. $needtoclose=0;
  173. foreach ($files as $filetmp) {
  174. $file = $filetmp['fullname'];
  175. //$newdir = str_replace(dirname(__FILE__).'/../scripts', '', dirname($file));
  176. $newdir = str_replace(DOL_DOCUMENT_ROOT, '', dirname($file));
  177. $newdir = str_replace(dirname(__FILE__).'/../scripts', '', dirname($file));
  178. if ($newdir!=$dir) {
  179. if ($needtoclose)
  180. fputs($fp, ' </dir>'."\n");
  181. fputs($fp, ' <dir name="'.$newdir.'" >'."\n");
  182. $dir = $newdir;
  183. $needtoclose=1;
  184. }
  185. if (filetype($file)=="file") {
  186. $md5=md5_file($file);
  187. $checksumconcat[]=$md5;
  188. fputs($fp, ' <md5file name="'.basename($file).'" size="'.filesize($file).'">'.$md5.'</md5file>'."\n");
  189. }
  190. }
  191. fputs($fp, ' </dir>'."\n");
  192. fputs($fp, '</dolibarr_script_dir>'."\n");
  193. asort($checksumconcat); // Sort list of checksum
  194. fputs($fp, '<dolibarr_script_dir_checksum>'."\n");
  195. fputs($fp, md5(join(',', $checksumconcat))."\n");
  196. fputs($fp, '</dolibarr_script_dir_checksum>'."\n");
  197. fputs($fp, '</checksum_list>'."\n");
  198. fclose($fp);
  199. print "File ".$outputfile." generated\n";
  200. exit(0);