generate_filelist_xml.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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')) {
  24. define('NOREQUIREDB', '1'); // Do not create database handler $db
  25. }
  26. $sapi_type = php_sapi_name();
  27. $script_file = basename(__FILE__);
  28. $path=dirname(__FILE__).'/';
  29. // Test if batch mode
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  32. exit;
  33. }
  34. require_once $path."../htdocs/master.inc.php";
  35. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  36. /*
  37. * Main
  38. */
  39. $includecustom=0;
  40. $includeconstants=array();
  41. if (empty($argv[1])) {
  42. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value] [buildzip=1]\n";
  43. 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";
  44. exit -1;
  45. }
  46. $i=0;
  47. while ($i < $argc) {
  48. if (! empty($argv[$i])) {
  49. parse_str($argv[$i]); // set all params $release, $includecustom, $includeconstant, $buildzip ...
  50. }
  51. if (preg_match('/includeconstant=/', $argv[$i])) {
  52. $tmp=explode(':', $includeconstant, 3); // $includeconstant has been set with previous parse_str()
  53. if (count($tmp) != 3) {
  54. print "Error: Bad parameter includeconstant=".$includeconstant."\n";
  55. exit -1;
  56. }
  57. $includeconstants[$tmp[0]][$tmp[1]] = $tmp[2];
  58. }
  59. $i++;
  60. }
  61. if (empty($release)) {
  62. print "Error: Missing release paramater\n";
  63. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  64. exit -1;
  65. }
  66. $savrelease = $release;
  67. // If release is auto, we take current version
  68. $tmpver=explode('-', $release, 2);
  69. if ($tmpver[0] == 'auto' || $tmpver[0] == 'autostable') {
  70. $release=DOL_VERSION;
  71. if ($tmpver[1] && $tmpver[0] == 'auto') {
  72. $release.='-'.$tmpver[1];
  73. }
  74. }
  75. if (empty($includecustom)) {
  76. $tmpverbis=explode('-', $release, 2);
  77. if (empty($tmpverbis[1]) || $tmpver[0] == 'autostable') {
  78. if (DOL_VERSION != $tmpverbis[0] && $savrelease != 'auto') {
  79. 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";
  80. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  81. exit -1;
  82. }
  83. } else {
  84. $tmpverter=explode('-', DOL_VERSION, 2);
  85. if ($tmpverter[0] != $tmpverbis[0]) {
  86. 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";
  87. print "Usage: ".$script_file." release=autostable|auto[-mybuild]|x.y.z[-mybuild] [includecustom=1] [includeconstant=CC:MY_CONF_NAME:value]\n";
  88. exit -1;
  89. }
  90. }
  91. } else {
  92. if (! preg_match('/'.preg_quote(DOL_VERSION, '/').'-/', $release)) {
  93. 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";
  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. print "Working on files into : ".DOL_DOCUMENT_ROOT."\n";
  99. print "Release : ".$release."\n";
  100. print "Include custom in signature : ".$includecustom."\n";
  101. print "Include constants in signature : ";
  102. foreach ($includeconstants as $countrycode => $tmp) {
  103. foreach ($tmp as $constname => $constvalue) {
  104. print $constname.'='.$constvalue." ";
  105. }
  106. }
  107. print "\n";
  108. //$outputfile=dirname(__FILE__).'/../htdocs/install/filelist-'.$release.'.xml';
  109. $outputdir=dirname(dirname(__FILE__)).'/htdocs/install';
  110. print 'Delete current files '.$outputdir.'/filelist*.xml*'."\n";
  111. dol_delete_file($outputdir.'/filelist*.xml*', 0, 1, 1);
  112. $checksumconcat=array();
  113. $outputfile=$outputdir.'/filelist-'.$release.'.xml';
  114. $fp = fopen($outputfile, 'w');
  115. if (empty($fp)) {
  116. print 'Failed to open file '.$outputfile."\n";
  117. exit(-1);
  118. }
  119. fputs($fp, '<?xml version="1.0" encoding="UTF-8" ?>'."\n");
  120. fputs($fp, '<checksum_list version="'.$release.'" date="'.dol_print_date(dol_now(), 'dayhourrfc').'" generator="'.$script_file.'">'."\n");
  121. foreach ($includeconstants as $countrycode => $tmp) {
  122. fputs($fp, '<dolibarr_constants country="'.$countrycode.'">'."\n");
  123. foreach ($tmp as $constname => $constvalue) {
  124. $valueforchecksum=(empty($constvalue)?'0':$constvalue);
  125. $checksumconcat[]=$valueforchecksum;
  126. fputs($fp, ' <constant name="'.$constname.'">'.$valueforchecksum.'</constant>'."\n");
  127. }
  128. fputs($fp, '</dolibarr_constants>'."\n");
  129. }
  130. fputs($fp, '<dolibarr_htdocs_dir includecustom="'.$includecustom.'">'."\n");
  131. /*$dir_iterator1 = new RecursiveDirectoryIterator(dirname(__FILE__).'/../htdocs/');
  132. $iterator1 = new RecursiveIteratorIterator($dir_iterator1);
  133. // Need to ignore document custom etc. Note: this also ignore natively symbolic links.
  134. $files = new RegexIterator($iterator1, '#^(?:[A-Z]:)?(?:/(?!(?:'.($includecustom?'':'custom\/|').'documents\/|conf\/|install\/))[^/]+)+/[^/]+\.(?:php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$#i');
  135. */
  136. // Define qualified files (must be same than into generate_filelist_xml.php and in api_setup.class.php)
  137. $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|bak|md|mp3|mp4|wav|mkv|z|gz|zip|rar|tar|less|svg|eot|woff|woff2|ttf|manifest)$';
  138. $regextoexclude = '('.($includecustom?'':'custom|').'documents|conf|install|dejavu-fonts-ttf-.*|public\/test|sabre\/sabre\/.*\/tests|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
  139. $files = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude, 'fullname');
  140. $dir='';
  141. $needtoclose=0;
  142. foreach ($files as $filetmp) {
  143. $file = $filetmp['fullname'];
  144. //$newdir = str_replace(dirname(__FILE__).'/../htdocs', '', dirname($file));
  145. $newdir = str_replace(DOL_DOCUMENT_ROOT, '', dirname($file));
  146. if ($newdir!=$dir) {
  147. if ($needtoclose) {
  148. fputs($fp, ' </dir>'."\n");
  149. }
  150. fputs($fp, ' <dir name="'.$newdir.'">'."\n");
  151. $dir = $newdir;
  152. $needtoclose=1;
  153. }
  154. if (filetype($file)=="file") {
  155. $md5=md5_file($file);
  156. $checksumconcat[]=$md5;
  157. fputs($fp, ' <md5file name="'.basename($file).'" size="'.filesize($file).'">'.$md5.'</md5file>'."\n");
  158. }
  159. }
  160. fputs($fp, ' </dir>'."\n");
  161. fputs($fp, '</dolibarr_htdocs_dir>'."\n");
  162. asort($checksumconcat); // Sort list of checksum
  163. //var_dump($checksumconcat);
  164. fputs($fp, '<dolibarr_htdocs_dir_checksum>'."\n");
  165. fputs($fp, md5(join(',', $checksumconcat))."\n");
  166. fputs($fp, '</dolibarr_htdocs_dir_checksum>'."\n");
  167. $checksumconcat=array();
  168. fputs($fp, '<dolibarr_script_dir version="'.$release.'">'."\n");
  169. // TODO Replace RecursiveDirectoryIterator with dol_dir_list
  170. /*$dir_iterator2 = new RecursiveDirectoryIterator(dirname(__FILE__).'/../scripts/');
  171. $iterator2 = new RecursiveIteratorIterator($dir_iterator2);
  172. // Need to ignore document custom etc. Note: this also ignore natively symbolic links.
  173. $files = new RegexIterator($iterator2, '#^(?:[A-Z]:)?(?:/(?!(?:custom|documents|conf|install))[^/]+)+/[^/]+\.(?:php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$#i');
  174. */
  175. $regextoinclude='\.(php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$';
  176. $regextoexclude='(custom|documents|conf|install)$'; // Exclude dirs
  177. $files = dol_dir_list(dirname(__FILE__).'/../scripts/', 'files', 1, $regextoinclude, $regextoexclude, 'fullname');
  178. $dir='';
  179. $needtoclose=0;
  180. foreach ($files as $filetmp) {
  181. $file = $filetmp['fullname'];
  182. //$newdir = str_replace(dirname(__FILE__).'/../scripts', '', dirname($file));
  183. $newdir = str_replace(DOL_DOCUMENT_ROOT, '', dirname($file));
  184. $newdir = str_replace(dirname(__FILE__).'/../scripts', '', dirname($file));
  185. if ($newdir!=$dir) {
  186. if ($needtoclose) {
  187. fputs($fp, ' </dir>'."\n");
  188. }
  189. fputs($fp, ' <dir name="'.$newdir.'" >'."\n");
  190. $dir = $newdir;
  191. $needtoclose=1;
  192. }
  193. if (filetype($file)=="file") {
  194. $md5=md5_file($file);
  195. $checksumconcat[]=$md5;
  196. fputs($fp, ' <md5file name="'.basename($file).'" size="'.filesize($file).'">'.$md5.'</md5file>'."\n");
  197. }
  198. }
  199. fputs($fp, ' </dir>'."\n");
  200. fputs($fp, '</dolibarr_script_dir>'."\n");
  201. asort($checksumconcat); // Sort list of checksum
  202. fputs($fp, '<dolibarr_script_dir_checksum>'."\n");
  203. fputs($fp, md5(join(',', $checksumconcat))."\n");
  204. fputs($fp, '</dolibarr_script_dir_checksum>'."\n");
  205. fputs($fp, '</checksum_list>'."\n");
  206. fclose($fp);
  207. if (empty($buildzip)) {
  208. print "File ".$outputfile." generated\n";
  209. } else {
  210. if ($buildzip == '1' || $buildzip == 'zip') {
  211. $result = dol_compress_file($outputfile, $outputfile.'.zip', 'zip');
  212. if ($result > 0) {
  213. dol_delete_file($outputfile);
  214. print "File ".$outputfile.".zip generated\n";
  215. }
  216. } elseif ($buildzip == '2' || $buildzip == 'gz') {
  217. $result = dol_compress_file($outputfile, $outputfile.'.gz', 'gz');
  218. if ($result > 0) {
  219. dol_delete_file($outputfile);
  220. print "File ".$outputfile.".gz generated\n";
  221. }
  222. }
  223. }
  224. exit(0);