utils.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. <?php
  2. /* Copyright (C) 2016 Destailleur Laurent <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/utils.class.php
  19. * \ingroup core
  20. * \brief File for Utils class
  21. */
  22. /**
  23. * Class to manage utility methods
  24. */
  25. class Utils
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. var $output; // Used by Cron method to return message
  32. var $result; // Used by Cron method to return data
  33. /**
  34. * Constructor
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. function __construct($db)
  39. {
  40. $this->db = $db;
  41. }
  42. /**
  43. * Purge files into directory of data files.
  44. * CAN BE A CRON TASK
  45. *
  46. * @param string $choice Choice of purge mode ('tempfiles', '' or 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfile')
  47. * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
  48. */
  49. function purgeFiles($choice='tempfilesold')
  50. {
  51. global $conf, $langs, $dolibarr_main_data_root;
  52. $langs->load("admin");
  53. dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG);
  54. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  55. $filesarray=array();
  56. if (empty($choice)) $choice='tempfilesold';
  57. if ($choice=='tempfiles' || $choice=='tempfilesold')
  58. {
  59. // Delete temporary files
  60. if ($dolibarr_main_data_root)
  61. {
  62. $filesarray=dol_dir_list($dolibarr_main_data_root, "directories", 1, '^temp$', '', 'name', SORT_ASC, 2, 0, '', 1); // Do not follow symlinks
  63. if ($choice == 'tempfilesold')
  64. {
  65. $now = dol_now();
  66. foreach($filesarray as $key => $val)
  67. {
  68. if ($val['date'] > ($now - (24 * 3600))) unset($filesarray[$key]); // Discard files not older than 24h
  69. }
  70. }
  71. }
  72. }
  73. if ($choice=='allfiles')
  74. {
  75. // Delete all files (except install.lock, do not follow symbolic links)
  76. if ($dolibarr_main_data_root)
  77. {
  78. $filesarray=dol_dir_list($dolibarr_main_data_root, "all", 0, '', 'install\.lock$', 'name', SORT_ASC, 0, 0, '', 1);
  79. }
  80. }
  81. if ($choice=='logfile')
  82. {
  83. // Define files log
  84. if ($dolibarr_main_data_root)
  85. {
  86. $filesarray=dol_dir_list($dolibarr_main_data_root, "files", 0, '.*\.log[\.0-9]*(\.gz)?$', 'install\.lock$', 'name', SORT_ASC, 0, 0, '', 1);
  87. }
  88. $filelog='';
  89. if (! empty($conf->syslog->enabled))
  90. {
  91. $filelog=$conf->global->SYSLOG_FILE;
  92. $filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog);
  93. $alreadyincluded=false;
  94. foreach ($filesarray as $tmpcursor)
  95. {
  96. if ($tmpcursor['fullname'] == $filelog) { $alreadyincluded=true; }
  97. }
  98. if (! $alreadyincluded) $filesarray[]=array('fullname'=>$filelog,'type'=>'file');
  99. }
  100. }
  101. $count=0;
  102. $countdeleted=0;
  103. $counterror=0;
  104. if (count($filesarray))
  105. {
  106. foreach($filesarray as $key => $value)
  107. {
  108. //print "x ".$filesarray[$key]['fullname']."-".$filesarray[$key]['type']."<br>\n";
  109. if ($filesarray[$key]['type'] == 'dir')
  110. {
  111. $startcount=0;
  112. $tmpcountdeleted=0;
  113. $result=dol_delete_dir_recursive($filesarray[$key]['fullname'], $startcount, 1, 0, $tmpcountdeleted);
  114. $count+=$result;
  115. $countdeleted+=$tmpcountdeleted;
  116. }
  117. elseif ($filesarray[$key]['type'] == 'file')
  118. {
  119. // If (file that is not logfile) or (if mode is logfile)
  120. if ($filesarray[$key]['fullname'] != $filelog || $choice=='logfile')
  121. {
  122. $result=dol_delete_file($filesarray[$key]['fullname'], 1, 1);
  123. if ($result)
  124. {
  125. $count++;
  126. $countdeleted++;
  127. }
  128. else
  129. {
  130. $counterror++;
  131. }
  132. }
  133. }
  134. }
  135. // Update cachenbofdoc
  136. if (! empty($conf->ecm->enabled) && $choice=='allfiles')
  137. {
  138. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  139. $ecmdirstatic = new EcmDirectory($this->db);
  140. $result = $ecmdirstatic->refreshcachenboffile(1);
  141. }
  142. }
  143. if ($count > 0)
  144. {
  145. $this->output=$langs->trans("PurgeNDirectoriesDeleted", $countdeleted);
  146. if ($count > $countdeleted) $this->output.='<br>'.$langs->trans("PurgeNDirectoriesFailed", ($count - $countdeleted));
  147. }
  148. else $this->output=$langs->trans("PurgeNothingToDelete").($choice == 'tempfilesold' ? ' (older than 24h)':'');
  149. //return $count;
  150. return 0; // This function can be called by cron so must return 0 if OK
  151. }
  152. /**
  153. * Make a backup of database
  154. * CAN BE A CRON TASK
  155. *
  156. * @param string $compression 'gz' or 'bz' or 'none'
  157. * @param string $type 'mysql', 'postgresql', ...
  158. * @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron)
  159. * @param string $file 'auto' or filename to build
  160. * @param int $keeplastnfiles Keep only last n files (not used yet)
  161. * @param int $execmethod 0=Use default method (that is 1 by default), 1=Use the PHP 'exec', 2=Use the 'popen' method
  162. * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
  163. */
  164. function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0, $execmethod=0)
  165. {
  166. global $db, $conf, $langs, $dolibarr_main_data_root;
  167. global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
  168. $langs->load("admin");
  169. dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG);
  170. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  171. // Check compression parameter
  172. if (! in_array($compression, array('none', 'gz', 'bz', 'zip')))
  173. {
  174. $langs->load("errors");
  175. $this->error=$langs->transnoentitiesnoconv("ErrorBadValueForParameter", $compression, "Compression");
  176. return -1;
  177. }
  178. // Check type parameter
  179. if ($type == 'auto') $type = $db->type;
  180. if (! in_array($type, array('postgresql', 'pgsql', 'mysql', 'mysqli', 'mysqlnobin')))
  181. {
  182. $langs->load("errors");
  183. $this->error=$langs->transnoentitiesnoconv("ErrorBadValueForParameter", $type, "Basetype");
  184. return -1;
  185. }
  186. // Check file parameter
  187. if ($file == 'auto')
  188. {
  189. $prefix='dump';
  190. $ext='sql';
  191. if (in_array($type, array('mysql', 'mysqli'))) { $prefix='mysqldump'; $ext='sql'; }
  192. //if ($label == 'PostgreSQL') { $prefix='pg_dump'; $ext='dump'; }
  193. if (in_array($type, array('pgsql'))) { $prefix='pg_dump'; $ext='sql'; }
  194. $file=$prefix.'_'.$dolibarr_main_db_name.'_'.dol_sanitizeFileName(DOL_VERSION).'_'.strftime("%Y%m%d%H%M").'.'.$ext;
  195. }
  196. $outputdir = $conf->admin->dir_output.'/backup';
  197. $result=dol_mkdir($outputdir);
  198. // MYSQL
  199. if ($type == 'mysql' || $type == 'mysqli')
  200. {
  201. $cmddump=$conf->global->SYSTEMTOOLS_MYSQLDUMP;
  202. $outputfile = $outputdir.'/'.$file;
  203. // for compression format, we add extension
  204. $compression=$compression ? $compression : 'none';
  205. if ($compression == 'gz') $outputfile.='.gz';
  206. if ($compression == 'bz') $outputfile.='.bz2';
  207. $outputerror = $outputfile.'.err';
  208. dol_mkdir($conf->admin->dir_output.'/backup');
  209. // Parameteres execution
  210. $command=$cmddump;
  211. if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command
  212. //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
  213. $param=$dolibarr_main_db_name." -h ".$dolibarr_main_db_host;
  214. $param.=" -u ".$dolibarr_main_db_user;
  215. if (! empty($dolibarr_main_db_port)) $param.=" -P ".$dolibarr_main_db_port;
  216. if (! GETPOST("use_transaction")) $param.=" -l --single-transaction";
  217. if (GETPOST("disable_fk") || $usedefault) $param.=" -K";
  218. if (GETPOST("sql_compat") && GETPOST("sql_compat") != 'NONE') $param.=" --compatible=".escapeshellarg(GETPOST("sql_compat","alpha"));
  219. if (GETPOST("drop_database")) $param.=" --add-drop-database";
  220. if (GETPOST("sql_structure") || $usedefault)
  221. {
  222. if (GETPOST("drop") || $usedefault) $param.=" --add-drop-table=TRUE";
  223. else $param.=" --add-drop-table=FALSE";
  224. }
  225. else
  226. {
  227. $param.=" -t";
  228. }
  229. if (GETPOST("disable-add-locks")) $param.=" --add-locks=FALSE";
  230. if (GETPOST("sql_data") || $usedefault)
  231. {
  232. $param.=" --tables";
  233. if (GETPOST("showcolumns") || $usedefault) $param.=" -c";
  234. if (GETPOST("extended_ins") || $usedefault) $param.=" -e";
  235. else $param.=" --skip-extended-insert";
  236. if (GETPOST("delayed")) $param.=" --delayed-insert";
  237. if (GETPOST("sql_ignore")) $param.=" --insert-ignore";
  238. if (GETPOST("hexforbinary") || $usedefault) $param.=" --hex-blob";
  239. }
  240. else
  241. {
  242. $param.=" -d"; // No row information (no data)
  243. }
  244. $param.=" --default-character-set=utf8"; // We always save output into utf8 charset
  245. $paramcrypted=$param;
  246. $paramclear=$param;
  247. if (! empty($dolibarr_main_db_pass))
  248. {
  249. $paramcrypted.=' -p"'.preg_replace('/./i','*',$dolibarr_main_db_pass).'"';
  250. $paramclear.=' -p"'.str_replace(array('"','`'),array('\"','\`'),$dolibarr_main_db_pass).'"';
  251. }
  252. $errormsg='';
  253. $handle = '';
  254. // Start call method to execute dump
  255. $fullcommandcrypted=$command." ".$paramcrypted." 2>&1";
  256. $fullcommandclear=$command." ".$paramclear." 2>&1";
  257. if ($compression == 'none') $handle = fopen($outputfile, 'w');
  258. if ($compression == 'gz') $handle = gzopen($outputfile, 'w');
  259. if ($compression == 'bz') $handle = bzopen($outputfile, 'w');
  260. if ($handle)
  261. {
  262. if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN;
  263. if (empty($execmethod)) $execmethod=1;
  264. $ok=0;
  265. dol_syslog("Utils::dumpDatabase execmethod=".$execmethod." command:".$fullcommandcrypted, LOG_DEBUG);
  266. // TODO Replace with executeCLI function
  267. if ($execmethod == 1)
  268. {
  269. exec($fullcommandclear, $readt, $retval);
  270. $result = $retval;
  271. if ($retval != 0)
  272. {
  273. $langs->load("errors");
  274. dol_syslog("Datadump retval after exec=".$retval, LOG_ERR);
  275. $error = 'Error '.$retval;
  276. $ok=0;
  277. }
  278. else
  279. {
  280. $i=0;
  281. if (!empty($readt))
  282. foreach($readt as $key=>$read)
  283. {
  284. $i++; // output line number
  285. if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue;
  286. fwrite($handle, $read.($execmethod == 2 ? '' : "\n"));
  287. if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1;
  288. elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1;
  289. }
  290. }
  291. }
  292. if ($execmethod == 2) // With this method, there is no way to get the return code, only output
  293. {
  294. $handlein = popen($fullcommandclear, 'r');
  295. $i=0;
  296. while (!feof($handlein))
  297. {
  298. $i++; // output line number
  299. $read = fgets($handlein);
  300. // Exclude warning line we don't want
  301. if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue;
  302. fwrite($handle,$read);
  303. if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1;
  304. elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1;
  305. }
  306. pclose($handlein);
  307. }
  308. if ($compression == 'none') fclose($handle);
  309. if ($compression == 'gz') gzclose($handle);
  310. if ($compression == 'bz') bzclose($handle);
  311. if (! empty($conf->global->MAIN_UMASK))
  312. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  313. }
  314. else
  315. {
  316. $langs->load("errors");
  317. dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
  318. $errormsg=$langs->trans("ErrorFailedToWriteInDir");
  319. }
  320. // Get errorstring
  321. if ($compression == 'none') $handle = fopen($outputfile, 'r');
  322. if ($compression == 'gz') $handle = gzopen($outputfile, 'r');
  323. if ($compression == 'bz') $handle = bzopen($outputfile, 'r');
  324. if ($handle)
  325. {
  326. // Get 2048 first chars of error message.
  327. $errormsg = fgets($handle,2048);
  328. // Close file
  329. if ($compression == 'none') fclose($handle);
  330. if ($compression == 'gz') gzclose($handle);
  331. if ($compression == 'bz') bzclose($handle);
  332. if ($ok && preg_match('/^-- MySql/i',$errormsg)) $errormsg=''; // Pas erreur
  333. else
  334. {
  335. // Renommer fichier sortie en fichier erreur
  336. //print "$outputfile -> $outputerror";
  337. @dol_delete_file($outputerror, 1, 0, 0, null, false, 0);
  338. @rename($outputfile,$outputerror);
  339. // Si safe_mode on et command hors du parametre exec, on a un fichier out vide donc errormsg vide
  340. if (! $errormsg)
  341. {
  342. $langs->load("errors");
  343. $errormsg=$langs->trans("ErrorFailedToRunExternalCommand");
  344. }
  345. }
  346. }
  347. // Fin execution commande
  348. $this->output = $errormsg;
  349. $this->error = $errormsg;
  350. $this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => "");
  351. //if (empty($this->output)) $this->output=$this->result['commandbackuplastdone'];
  352. }
  353. // MYSQL NO BIN
  354. if ($type == 'mysqlnobin')
  355. {
  356. $outputfile = $outputdir.'/'.$file;
  357. $outputfiletemp = $outputfile.'-TMP.sql';
  358. // for compression format, we add extension
  359. $compression=$compression ? $compression : 'none';
  360. if ($compression == 'gz') $outputfile.='.gz';
  361. if ($compression == 'bz') $outputfile.='.bz2';
  362. $outputerror = $outputfile.'.err';
  363. dol_mkdir($conf->admin->dir_output.'/backup');
  364. if ($compression == 'gz' or $compression == 'bz')
  365. {
  366. $this->backupTables($outputfiletemp);
  367. dol_compress_file($outputfiletemp, $outputfile, $compression);
  368. unlink($outputfiletemp);
  369. }
  370. else
  371. {
  372. $this->backupTables($outputfile);
  373. }
  374. $this->output = "";
  375. $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => "");
  376. }
  377. // POSTGRESQL
  378. if ($type == 'postgresql' || $type == 'pgsql')
  379. {
  380. $cmddump=$conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
  381. $outputfile = $outputdir.'/'.$file;
  382. // for compression format, we add extension
  383. $compression=$compression ? $compression : 'none';
  384. if ($compression == 'gz') $outputfile.='.gz';
  385. if ($compression == 'bz') $outputfile.='.bz2';
  386. $outputerror = $outputfile.'.err';
  387. dol_mkdir($conf->admin->dir_output.'/backup');
  388. // Parameteres execution
  389. $command=$cmddump;
  390. if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command
  391. //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
  392. //$param="-F c";
  393. $param="-F p";
  394. $param.=" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
  395. $param.=" -U ".$dolibarr_main_db_user;
  396. if (! empty($dolibarr_main_db_port)) $param.=" -p ".$dolibarr_main_db_port;
  397. if (GETPOST("sql_compat") && GETPOST("sql_compat") == 'ANSI') $param.=" --disable-dollar-quoting";
  398. if (GETPOST("drop_database")) $param.=" -c -C";
  399. if (GETPOST("sql_structure"))
  400. {
  401. if (GETPOST("drop")) $param.=" --add-drop-table";
  402. if (! GETPOST("sql_data")) $param.=" -s";
  403. }
  404. if (GETPOST("sql_data"))
  405. {
  406. if (! GETPOST("sql_structure")) $param.=" -a";
  407. if (GETPOST("showcolumns")) $param.=" -c";
  408. }
  409. $param.=' -f "'.$outputfile.'"';
  410. //if ($compression == 'none')
  411. if ($compression == 'gz') $param.=' -Z 9';
  412. //if ($compression == 'bz')
  413. $paramcrypted=$param;
  414. $paramclear=$param;
  415. /*if (! empty($dolibarr_main_db_pass))
  416. {
  417. $paramcrypted.=" -W".preg_replace('/./i','*',$dolibarr_main_db_pass);
  418. $paramclear.=" -W".$dolibarr_main_db_pass;
  419. }*/
  420. $paramcrypted.=" -w ".$dolibarr_main_db_name;
  421. $paramclear.=" -w ".$dolibarr_main_db_name;
  422. $this->output = "";
  423. $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => $command." ".$paramcrypted);
  424. }
  425. // Clean old files
  426. if ($keeplastnfiles > 0)
  427. {
  428. $tmpfiles = dol_dir_list($conf->admin->dir_output.'/backup', 'files', 0, '', '(\.err|\.old|\.sav)$', 'date', SORT_DESC);
  429. $i=0;
  430. foreach($tmpfiles as $key => $val)
  431. {
  432. $i++;
  433. if ($i <= $keeplastnfiles) continue;
  434. dol_delete_file($val['fullname'], 0, 0, 0, null, false, 0);
  435. }
  436. }
  437. return 0;
  438. }
  439. /**
  440. * Execute a CLI command.
  441. *
  442. * @param string $command Command line to execute.
  443. * @param string $outputfile Output file (used only when method is 2). For exemple $conf->admin->dir_temp.'/out.tmp';
  444. * @param int $execmethod 0=Use default method (that is 1 by default), 1=Use the PHP 'exec', 2=Use the 'popen' method
  445. * @return array array('result'=>...,'output'=>...,'error'=>...). result = 0 means OK.
  446. */
  447. function executeCLI($command, $outputfile, $execmethod=0)
  448. {
  449. global $conf, $langs;
  450. $result = 0;
  451. $output = '';
  452. $error = '';
  453. $command=escapeshellcmd($command);
  454. $command.=" 2>&1";
  455. if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN;
  456. if (empty($execmethod)) $execmethod=1;
  457. //$execmethod=1;
  458. dol_syslog("Utils::executeCLI execmethod=".$execmethod." system:".$command, LOG_DEBUG);
  459. $output_arr=array();
  460. if ($execmethod == 1)
  461. {
  462. exec($command, $output_arr, $retval);
  463. $result = $retval;
  464. if ($retval != 0)
  465. {
  466. $langs->load("errors");
  467. dol_syslog("Utils::executeCLI retval after exec=".$retval, LOG_ERR);
  468. $error = 'Error '.$retval;
  469. }
  470. }
  471. if ($execmethod == 2) // With this method, there is no way to get the return code, only output
  472. {
  473. $ok=0;
  474. $handle = fopen($outputfile, 'w+b');
  475. if ($handle)
  476. {
  477. dol_syslog("Utils::executeCLI run command ".$command);
  478. $handlein = popen($command, 'r');
  479. while (!feof($handlein))
  480. {
  481. $read = fgets($handlein);
  482. fwrite($handle,$read);
  483. $output_arr[]=$read;
  484. }
  485. pclose($handlein);
  486. fclose($handle);
  487. }
  488. if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  489. }
  490. // Update with result
  491. if (is_array($output_arr) && count($output_arr)>0)
  492. {
  493. foreach($output_arr as $val)
  494. {
  495. $output.=$val.($execmethod == 2 ? '' : "\n");
  496. }
  497. }
  498. dol_syslog("Utils::executeCLI result=".$result." output=".$output." error=".$error, LOG_DEBUG);
  499. return array('result'=>$result, 'output'=>$output, 'error'=>$error);
  500. }
  501. /**
  502. * Generate documentation of a Module
  503. *
  504. * @param string $module Module name
  505. * @return int <0 if KO, >0 if OK
  506. */
  507. function generateDoc($module)
  508. {
  509. global $conf, $langs;
  510. global $dirins;
  511. $error = 0;
  512. $modulelowercase=strtolower($module);
  513. // Dir for module
  514. $dir = $dirins.'/'.$modulelowercase;
  515. // Zip file to build
  516. $FILENAMEDOC='';
  517. // Load module
  518. dol_include_once($modulelowercase.'/core/modules/mod'.$module.'.class.php');
  519. $class='mod'.$module;
  520. if (class_exists($class))
  521. {
  522. try {
  523. $moduleobj = new $class($this->db);
  524. }
  525. catch(Exception $e)
  526. {
  527. $error++;
  528. dol_print_error($e->getMessage());
  529. }
  530. }
  531. else
  532. {
  533. $error++;
  534. $langs->load("errors");
  535. dol_print_error($langs->trans("ErrorFailedToLoadModuleDescriptorForXXX", $module));
  536. exit;
  537. }
  538. $arrayversion=explode('.',$moduleobj->version,3);
  539. if (count($arrayversion))
  540. {
  541. $FILENAMEASCII=strtolower($module).'.asciidoc';
  542. $FILENAMEDOC=strtolower($module).'.html'; // TODO Use/text PDF
  543. $dirofmodule = dol_buildpath(strtolower($module), 0).'/doc';
  544. $dirofmoduletmp = dol_buildpath(strtolower($module), 0).'/doc/temp';
  545. $outputfiledoc = $dirofmodule.'/'.$FILENAMEDOC;
  546. if ($dirofmodule)
  547. {
  548. if (! dol_is_dir($dirofmodule)) dol_mkdir($dirofmodule);
  549. if (! dol_is_dir($dirofmoduletmp)) dol_mkdir($dirofmoduletmp);
  550. if (! is_writable($dirofmoduletmp))
  551. {
  552. $this->error = 'Dir '.$dirofmoduletmp.' does not exists or is not writable';
  553. return -1;
  554. }
  555. $destfile=$dirofmoduletmp.'/'.$FILENAMEASCII;
  556. $fhandle = fopen($destfile, 'w+');
  557. if ($fhandle)
  558. {
  559. $specs=dol_dir_list(dol_buildpath(strtolower($module).'/doc', 0), 'files', 1, '(\.md|\.asciidoc)$', array('\/temp\/'));
  560. $i = 0;
  561. foreach ($specs as $spec)
  562. {
  563. if (preg_match('/notindoc/', $spec['relativename'])) continue; // Discard file
  564. if (preg_match('/disabled/', $spec['relativename'])) continue; // Discard file
  565. $pathtofile = strtolower($module).'/doc/'.$spec['relativename'];
  566. $format='asciidoc';
  567. if (preg_match('/\.md$/i', $spec['name'])) $format='markdown';
  568. $filecursor = @file_get_contents($spec['fullname']);
  569. if ($filecursor)
  570. {
  571. fwrite($fhandle, ($i ? "\n<<<\n\n" : "").$filecursor."\n");
  572. }
  573. else
  574. {
  575. $this->error = 'Failed to concat content of file '.$spec['fullname'];
  576. return -1;
  577. }
  578. $i++;
  579. }
  580. fwrite($fhandle, "\n\n\n== DATA SPECIFICATIONS...\n\n");
  581. // TODO
  582. fwrite($fhandle, "TODO...");
  583. fclose($fhandle);
  584. }
  585. $conf->global->MODULEBUILDER_ASCIIDOCTOR='asciidoctor';
  586. if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR))
  587. {
  588. dol_print_error('', 'Module setup not complete');
  589. exit;
  590. }
  591. $command=$conf->global->MODULEBUILDER_ASCIIDOCTOR.' '.$destfile.' -n -o '.$dirofmodule.'/'.$FILENAMEDOC;
  592. $outfile=$dirofmoduletmp.'/out.tmp';
  593. require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  594. $utils = new Utils($db);
  595. $resarray = $utils->executeCLI($command, $outfile);
  596. if ($resarray['result'] != '0')
  597. {
  598. $this->error = $resarray['error'].' '.$resarray['output'];
  599. }
  600. $result = ($resarray['result'] == 0) ? 1 : 0;
  601. }
  602. else
  603. {
  604. $result = 0;
  605. }
  606. if ($result > 0)
  607. {
  608. return 1;
  609. }
  610. else
  611. {
  612. $error++;
  613. $langs->load("errors");
  614. $this->error = $langs->trans("ErrorFailToGenerateFile", $outputfiledoc);
  615. }
  616. }
  617. else
  618. {
  619. $error++;
  620. $langs->load("errors");
  621. $this->error = $langs->trans("ErrorCheckVersionIsDefined");
  622. }
  623. return -1;
  624. }
  625. /**
  626. * This saves syslog files and compresses older ones.
  627. * Nb of archive to keep is defined into $conf->global->SYSLOG_FILE_SAVES
  628. * CAN BE A CRON TASK
  629. *
  630. * @return int 0 if OK, < 0 if KO
  631. */
  632. function compressSyslogs()
  633. {
  634. global $conf;
  635. if(empty($conf->loghandlers['mod_syslog_file'])) { // File Syslog disabled
  636. return 0;
  637. }
  638. if(! function_exists('gzopen')) {
  639. $this->error = 'Support for gzopen not available in this PHP';
  640. return -1;
  641. }
  642. dol_include_once('/core/lib/files.lib.php');
  643. $nbSaves = ! empty($conf->global->SYSLOG_FILE_SAVES) ? intval($conf->global->SYSLOG_FILE_SAVES) : 14;
  644. if (empty($conf->global->SYSLOG_FILE)) {
  645. $mainlogdir = DOL_DATA_ROOT;
  646. $mainlog = 'dolibarr.log';
  647. } else {
  648. $mainlogfull = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
  649. $mainlogdir = dirname($mainlogfull);
  650. $mainlog = basename($mainlogfull);
  651. }
  652. $tabfiles = dol_dir_list(DOL_DATA_ROOT, 'files', 0, '^(dolibarr_.+|odt2pdf)\.log$'); // Also handle other log files like dolibarr_install.log
  653. $tabfiles[] = array('name' => $mainlog, 'path' => $mainlogdir);
  654. foreach($tabfiles as $file) {
  655. $logname = $file['name'];
  656. $logpath = $file['path'];
  657. if (dol_is_file($logpath.'/'.$logname) && dol_filesize($logpath.'/'.$logname) > 0) // If log file exists and is not empty
  658. {
  659. // Handle already compressed files to rename them and add +1
  660. $filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';
  661. $gzfilestmp = dol_dir_list($logpath, 'files', 0, $filter);
  662. $gzfiles = array();
  663. foreach($gzfilestmp as $gzfile) {
  664. $tabmatches = array();
  665. preg_match('/'.$filter.'/i', $gzfile['name'], $tabmatches);
  666. $numsave = intval($tabmatches[1]);
  667. $gzfiles[$numsave] = $gzfile;
  668. }
  669. krsort($gzfiles, SORT_NUMERIC);
  670. foreach($gzfiles as $numsave => $dummy) {
  671. if (dol_is_file($logpath.'/'.$logname.'.'.($numsave+1).'.gz')) {
  672. return -2;
  673. }
  674. if($numsave >= $nbSaves) {
  675. dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz', 0, 0, 0, null, false, 0);
  676. } else {
  677. dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave+1).'.gz', 0, 1, 0, 0);
  678. }
  679. }
  680. // Compress current file and recreate it
  681. if ($nbSaves > 0) { // If $nbSaves is 1, we keep 1 archive .gz file, If 2, we keep 2 .gz files
  682. $gzfilehandle = gzopen($logpath.'/'.$logname.'.1.gz', 'wb9');
  683. if (empty($gzfilehandle)) {
  684. $this->error = 'Failted to open file '.$logpath.'/'.$logname.'.1.gz';
  685. return -3;
  686. }
  687. $sourcehandle = fopen($logpath.'/'.$logname, 'r');
  688. if (empty($sourcehandle)) {
  689. $this->error = 'Failed to open file '.$logpath.'/'.$logname;
  690. return -4;
  691. }
  692. while(! feof($sourcehandle)) {
  693. gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024)); // Read 512 kB at a time
  694. }
  695. fclose($sourcehandle);
  696. gzclose($gzfilehandle);
  697. @chmod($logpath.'/'.$logname.'.1.gz', octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
  698. }
  699. dol_delete_file($logpath.'/'.$logname, 0, 0, 0, null, false, 0);
  700. // Create empty file
  701. $newlog = fopen($logpath.'/'.$logname, 'a+');
  702. fclose($newlog);
  703. //var_dump($logpath.'/'.$logname." - ".octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
  704. @chmod($logpath.'/'.$logname, octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
  705. }
  706. }
  707. $this->output = 'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.' files) done.';
  708. return 0;
  709. }
  710. /** Backup the db OR just a table without mysqldump binary, with PHP only (does not require any exec permission)
  711. * Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php)
  712. * Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog
  713. * Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump.
  714. * Note2: db2SQL by Howard Yeend can be an alternative, by using SHOW FIELDS FROM and SHOW KEYS FROM we could generate a more precise dump (eg: by getting the type of the field and then precisely outputting the right formatting - in quotes, numeric or null - instead of trying to guess like we are doing now).
  715. *
  716. * @param string $outputfile Output file name
  717. * @param string $tables Table name or '*' for all
  718. * @return int <0 if KO, >0 if OK
  719. */
  720. function backupTables($outputfile, $tables='*')
  721. {
  722. global $db, $langs;
  723. global $errormsg;
  724. // Set to UTF-8
  725. if (is_a($db, 'DoliDBMysqli')) {
  726. /** @var DoliDBMysqli $db */
  727. $db->db->set_charset('utf8');
  728. } else {
  729. /** @var DoliDB $db */
  730. $db->query('SET NAMES utf8');
  731. $db->query('SET CHARACTER SET utf8');
  732. }
  733. //get all of the tables
  734. if ($tables == '*')
  735. {
  736. $tables = array();
  737. $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
  738. while($row = $db->fetch_row($result))
  739. {
  740. $tables[] = $row[0];
  741. }
  742. }
  743. else
  744. {
  745. $tables = is_array($tables) ? $tables : explode(',',$tables);
  746. }
  747. //cycle through
  748. $handle = fopen($outputfile, 'w+');
  749. if (fwrite($handle, '') === false)
  750. {
  751. $langs->load("errors");
  752. dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
  753. $errormsg=$langs->trans("ErrorFailedToWriteInDir");
  754. return -1;
  755. }
  756. // Print headers and global mysql config vars
  757. $sqlhead = '';
  758. $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION."
  759. --
  760. -- Host: ".$db->db->host_info." Database: ".$db->database_name."
  761. -- ------------------------------------------------------
  762. -- Server version ".$db->db->server_info."
  763. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  764. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  765. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  766. /*!40101 SET NAMES utf8 */;
  767. /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
  768. /*!40103 SET TIME_ZONE='+00:00' */;
  769. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  770. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  771. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  772. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  773. ";
  774. if (GETPOST("nobin_disable_fk")) $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n";
  775. //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n";
  776. if (GETPOST("nobin_use_transaction")) $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
  777. fwrite($handle, $sqlhead);
  778. $ignore = '';
  779. if (GETPOST("nobin_sql_ignore")) $ignore = 'IGNORE ';
  780. $delayed = '';
  781. if (GETPOST("nobin_delayed")) $delayed = 'DELAYED ';
  782. // Process each table and print their definition + their datas
  783. foreach($tables as $table)
  784. {
  785. // Saving the table structure
  786. fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n");
  787. if (GETPOST("nobin_drop")) fwrite($handle,"DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it
  788. fwrite($handle,"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
  789. fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n");
  790. $resqldrop=$db->query('SHOW CREATE TABLE '.$table);
  791. $row2 = $db->fetch_row($resqldrop);
  792. if (empty($row2[1]))
  793. {
  794. fwrite($handle, "\n-- WARNING: Show create table ".$table." return empy string when it should not.\n");
  795. }
  796. else
  797. {
  798. fwrite($handle,$row2[1].";\n");
  799. //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n");
  800. // Dumping the data (locking the table and disabling the keys check while doing the process)
  801. fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n");
  802. if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back)
  803. if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n");
  804. else fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n");
  805. $sql='SELECT * FROM '.$table;
  806. $result = $db->query($sql);
  807. while($row = $db->fetch_row($result))
  808. {
  809. // For each row of data we print a line of INSERT
  810. fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES (');
  811. $columns = count($row);
  812. for($j=0; $j<$columns; $j++) {
  813. // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier)
  814. if ($row[$j] == null && !is_string($row[$j])) {
  815. // IMPORTANT: if the field is NULL we set it NULL
  816. $row[$j] = 'NULL';
  817. } elseif(is_string($row[$j]) && $row[$j] == '') {
  818. // if it's an empty string, we set it as an empty string
  819. $row[$j] = "''";
  820. } elseif(is_numeric($row[$j]) && !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0)
  821. // if it's a number, we return it as-is
  822. // $row[$j] = $row[$j];
  823. } else { // else for all other cases we escape the value and put quotes around
  824. $row[$j] = addslashes($row[$j]);
  825. $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
  826. $row[$j] = "'".$row[$j]."'";
  827. }
  828. }
  829. fwrite($handle,implode(',', $row).");\n");
  830. }
  831. if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking
  832. if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table
  833. fwrite($handle,"\n\n\n");
  834. }
  835. }
  836. /* Backup Procedure structure*/
  837. /*
  838. $result = $db->query('SHOW PROCEDURE STATUS');
  839. if ($db->num_rows($result) > 0)
  840. {
  841. while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; }
  842. foreach($procedures as $proc)
  843. {
  844. fwrite($handle,"DELIMITER $$\n\n");
  845. fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n");
  846. $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'");
  847. $row2 = $db->fetch_row($resqlcreateproc);
  848. fwrite($handle,"\n".$row2[2]."$$\n\n");
  849. fwrite($handle,"DELIMITER ;\n\n");
  850. }
  851. }
  852. */
  853. /* Backup Procedure structure*/
  854. // Write the footer (restore the previous database settings)
  855. $sqlfooter="\n\n";
  856. if (GETPOST("nobin_use_transaction")) $sqlfooter .= "COMMIT;\n";
  857. if (GETPOST("nobin_disable_fk")) $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n";
  858. $sqlfooter.="\n\n-- Dump completed on ".date('Y-m-d G-i-s');
  859. fwrite($handle, $sqlfooter);
  860. fclose($handle);
  861. return 1;
  862. }
  863. }