generate_filelist_xml.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <http://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. {
  41. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  42. 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";
  43. exit -1;
  44. }
  45. parse_str($argv[1]);
  46. $i=0;
  47. while ($i < $argc)
  48. {
  49. if (! empty($argv[$i])) parse_str($argv[$i]);
  50. if (preg_match('/includeconstant=/',$argv[$i]))
  51. {
  52. $tmp=explode(':', $includeconstant, 3);
  53. if (count($tmp) != 3)
  54. {
  55. print "Error: Bad parameter includeconstant ".$includeconstant."\n";
  56. exit -1;
  57. }
  58. $includeconstants[$tmp[0]][$tmp[1]] = $tmp[2];
  59. }
  60. $i++;
  61. }
  62. if (empty($release))
  63. {
  64. print "Error: Missing release paramater\n";
  65. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  66. exit -1;
  67. }
  68. $savrelease = $release;
  69. // If release is auto, we take current version
  70. $tmpver=explode('-', $release, 2);
  71. if ($tmpver[0] == 'auto' || $tmpver[0] == 'autostable')
  72. {
  73. $release=DOL_VERSION;
  74. if ($tmpver[1] && $tmpver[0] == 'auto') $release.='-'.$tmpver[1];
  75. }
  76. if (empty($includecustom))
  77. {
  78. $tmpverbis=explode('-', $release, 2);
  79. if (empty($tmpverbis[1]) || $tmpver[0] == 'autostable')
  80. {
  81. if (DOL_VERSION != $tmpverbis[0] && $savrelease != 'auto')
  82. {
  83. 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";
  84. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  85. exit -1;
  86. }
  87. }
  88. else
  89. {
  90. $tmpverter=explode('-', DOL_VERSION, 2);
  91. if ($tmpverter[0] != $tmpverbis[0])
  92. {
  93. 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";
  94. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  95. exit -1;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. if (! preg_match('/'.preg_quote(DOL_VERSION,'/').'-/',$release))
  102. {
  103. 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";
  104. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  105. exit -1;
  106. }
  107. }
  108. print "Release : ".$release."\n";
  109. print "Include custom in signature : ".$includecustom."\n";
  110. print "Include constants in signature : ";
  111. foreach ($includeconstants as $countrycode => $tmp)
  112. {
  113. foreach($tmp as $constname => $constvalue)
  114. {
  115. print $constname.'='.$constvalue." ";
  116. }
  117. }
  118. print "\n";
  119. //$outputfile=dirname(__FILE__).'/../htdocs/install/filelist-'.$release.'.xml';
  120. $outputdir=dirname(dirname(__FILE__)).'/htdocs/install';
  121. print 'Delete current files '.$outputdir.'/filelist*.xml'."\n";
  122. dol_delete_file($outputdir.'/filelist*.xml',0,1,1);
  123. $checksumconcat=array();
  124. $outputfile=$outputdir.'/filelist-'.$release.'.xml';
  125. $fp = fopen($outputfile,'w');
  126. fputs($fp, '<?xml version="1.0" encoding="UTF-8" ?>'."\n");
  127. fputs($fp, '<checksum_list version="'.$release.'" date="'.dol_print_date(dol_now(), 'dayhourrfc').'" generator="'.$script_file.'">'."\n");
  128. foreach ($includeconstants as $countrycode => $tmp)
  129. {
  130. fputs($fp, '<dolibarr_constants country="'.$countrycode.'">'."\n");
  131. foreach($tmp as $constname => $constvalue)
  132. {
  133. $valueforchecksum=(empty($constvalue)?'0':$constvalue);
  134. $checksumconcat[]=$valueforchecksum;
  135. fputs($fp, ' <constant name="'.$constname.'">'.$valueforchecksum.'</constant>'."\n");
  136. }
  137. fputs($fp, '</dolibarr_constants>'."\n");
  138. }
  139. fputs($fp, '<dolibarr_htdocs_dir includecustom="'.$includecustom.'">'."\n");
  140. /*$dir_iterator1 = new RecursiveDirectoryIterator(dirname(__FILE__).'/../htdocs/');
  141. $iterator1 = new RecursiveIteratorIterator($dir_iterator1);
  142. // Need to ignore document custom etc. Note: this also ignore natively symbolic links.
  143. $files = new RegexIterator($iterator1, '#^(?:[A-Z]:)?(?:/(?!(?:'.($includecustom?'':'custom\/|').'documents\/|conf\/|install\/))[^/]+)+/[^/]+\.(?:php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$#i');
  144. */
  145. $regextoinclude='\.(php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$';
  146. $regextoexclude='('.($includecustom?'':'custom|').'documents|conf|install|public\/test|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
  147. $files = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude, 'fullname');
  148. $dir='';
  149. $needtoclose=0;
  150. foreach ($files as $filetmp) {
  151. $file = $filetmp['fullname'];
  152. //$newdir = str_replace(dirname(__FILE__).'/../htdocs', '', dirname($file));
  153. $newdir = str_replace(DOL_DOCUMENT_ROOT, '', dirname($file));
  154. if ($newdir!=$dir) {
  155. if ($needtoclose)
  156. fputs($fp, ' </dir>'."\n");
  157. fputs($fp, ' <dir name="'.$newdir.'" >'."\n");
  158. $dir = $newdir;
  159. $needtoclose=1;
  160. }
  161. if (filetype($file)=="file") {
  162. $md5=md5_file($file);
  163. $checksumconcat[]=$md5;
  164. fputs($fp, ' <md5file name="'.basename($file).'">'.$md5.'</md5file>'."\n");
  165. }
  166. }
  167. fputs($fp, ' </dir>'."\n");
  168. fputs($fp, '</dolibarr_htdocs_dir>'."\n");
  169. asort($checksumconcat); // Sort list of checksum
  170. //var_dump($checksumconcat);
  171. fputs($fp, '<dolibarr_htdocs_dir_checksum>'."\n");
  172. fputs($fp, md5(join(',',$checksumconcat))."\n");
  173. fputs($fp, '</dolibarr_htdocs_dir_checksum>'."\n");
  174. $checksumconcat=array();
  175. fputs($fp, '<dolibarr_script_dir version="'.$release.'">'."\n");
  176. // TODO Replace RecursiveDirectoryIterator with dol_dir_list
  177. /*$dir_iterator2 = new RecursiveDirectoryIterator(dirname(__FILE__).'/../scripts/');
  178. $iterator2 = new RecursiveIteratorIterator($dir_iterator2);
  179. // Need to ignore document custom etc. Note: this also ignore natively symbolic links.
  180. $files = new RegexIterator($iterator2, '#^(?:[A-Z]:)?(?:/(?!(?:custom|documents|conf|install))[^/]+)+/[^/]+\.(?:php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$#i');
  181. */
  182. $regextoinclude='\.(php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$';
  183. $regextoexclude='(custom|documents|conf|install)$'; // Exclude dirs
  184. $files = dol_dir_list(dirname(__FILE__).'/../scripts/', 'files', 1, $regextoinclude, $regextoexclude, 'fullname');
  185. $dir='';
  186. $needtoclose=0;
  187. foreach ($files as $filetmp) {
  188. $file = $filetmp['fullname'];
  189. //$newdir = str_replace(dirname(__FILE__).'/../scripts', '', dirname($file));
  190. $newdir = str_replace(DOL_DOCUMENT_ROOT, '', dirname($file));
  191. $newdir = str_replace(dirname(__FILE__).'/../scripts', '', dirname($file));
  192. if ($newdir!=$dir) {
  193. if ($needtoclose)
  194. fputs($fp, ' </dir>'."\n");
  195. fputs($fp, ' <dir name="'.$newdir.'" >'."\n");
  196. $dir = $newdir;
  197. $needtoclose=1;
  198. }
  199. if (filetype($file)=="file") {
  200. $md5=md5_file($file);
  201. $checksumconcat[]=$md5;
  202. fputs($fp, ' <md5file name="'.basename($file).'">'.$md5.'</md5file>'."\n");
  203. }
  204. }
  205. fputs($fp, ' </dir>'."\n");
  206. fputs($fp, '</dolibarr_script_dir>'."\n");
  207. asort($checksumconcat); // Sort list of checksum
  208. fputs($fp, '<dolibarr_script_dir_checksum>'."\n");
  209. fputs($fp, md5(join(',',$checksumconcat))."\n");
  210. fputs($fp, '</dolibarr_script_dir_checksum>'."\n");
  211. fputs($fp, '</checksum_list>'."\n");
  212. fclose($fp);
  213. print "File ".$outputfile." generated\n";
  214. exit(0);