export.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  5. * Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/tools/export.php
  22. * \brief Page to export a database into a dump file
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  30. $langs->load("admin");
  31. $action = GETPOST('action', 'aZ09');
  32. $what = GETPOST('what', 'alpha');
  33. $export_type = GETPOST('export_type', 'alpha');
  34. $file = dol_sanitizeFileName(GETPOST('filename_template', 'alpha'));
  35. // Load variable for pagination
  36. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  37. $sortfield = GETPOST('sortfield', 'aZ09comma');
  38. $sortorder = GETPOST('sortorder', 'aZ09comma');
  39. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  40. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  41. $page = 0;
  42. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  43. $offset = $limit * $page;
  44. if (!$sortorder) {
  45. $sortorder = "DESC";
  46. }
  47. if (!$sortfield) {
  48. $sortfield = "date";
  49. }
  50. if (!$user->admin) {
  51. accessforbidden();
  52. }
  53. $errormsg = '';
  54. $utils = new Utils($db);
  55. /*
  56. * Actions
  57. */
  58. if ($file && !$what) {
  59. //print DOL_URL_ROOT.'/dolibarr_export.php';
  60. header("Location: ".DOL_URL_ROOT.'/admin/tools/dolibarr_export.php?msg='.urlencode($langs->trans("ErrorFieldRequired", $langs->transnoentities("ExportMethod"))).(GETPOST('page_y', 'int') ? '&page_y='.GETPOST('page_y', 'int') : ''));
  61. exit;
  62. }
  63. if ($action == 'delete') {
  64. $file = $conf->admin->dir_output.'/'.dol_sanitizeFileName(GETPOST('urlfile'));
  65. $ret = dol_delete_file($file, 1);
  66. if ($ret) {
  67. setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  68. } else {
  69. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  70. }
  71. $action = '';
  72. }
  73. $_SESSION["commandbackuplastdone"] = '';
  74. $_SESSION["commandbackuptorun"] = '';
  75. $_SESSION["commandbackupresult"] = '';
  76. // Increase limit of time. Works only if we are not in safe mode
  77. $ExecTimeLimit = 600; // Set it to 0 to not use a forced time limit
  78. if (!empty($ExecTimeLimit)) {
  79. $err = error_reporting();
  80. error_reporting(0); // Disable all errors
  81. //error_reporting(E_ALL);
  82. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  83. error_reporting($err);
  84. }
  85. $MemoryLimit = 0;
  86. if (!empty($MemoryLimit)) {
  87. @ini_set('memory_limit', $MemoryLimit);
  88. }
  89. // Start with empty buffer
  90. $dump_buffer = '';
  91. $dump_buffer_len = 0;
  92. // We will send fake headers to avoid browser timeout when buffering
  93. $time_start = time();
  94. $outputdir = $conf->admin->dir_output.'/backup';
  95. $result = dol_mkdir($outputdir);
  96. $lowmemorydump = GETPOSTISSET("lowmemorydump") ? GETPOST("lowmemorydump") : getDolGlobalString('MAIN_LOW_MEMORY_DUMP');
  97. // MYSQL
  98. if ($what == 'mysql') {
  99. $cmddump = GETPOST("mysqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
  100. $cmddump = dol_sanitizePathName($cmddump);
  101. if (!empty($dolibarr_main_restrict_os_commands)) {
  102. $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
  103. dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
  104. $basenamecmddump = basename($cmddump);
  105. if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
  106. $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
  107. }
  108. }
  109. if (!$errormsg && $cmddump) {
  110. dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
  111. }
  112. if (!$errormsg) {
  113. $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
  114. $errormsg = $utils->error;
  115. $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
  116. $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
  117. }
  118. }
  119. // MYSQL NO BIN
  120. if ($what == 'mysqlnobin') {
  121. $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
  122. $errormsg = $utils->error;
  123. $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
  124. $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
  125. }
  126. // POSTGRESQL
  127. if ($what == 'postgresql') {
  128. $cmddump = GETPOST("postgresqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
  129. $cmddump = dol_sanitizePathName($cmddump);
  130. /* Not required, the command is output on screen but not ran for pgsql
  131. if (!empty($dolibarr_main_restrict_os_commands))
  132. {
  133. $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
  134. dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
  135. $basenamecmddump=basename($cmddump);
  136. if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
  137. {
  138. $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
  139. }
  140. } */
  141. if (!$errormsg && $cmddump) {
  142. dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
  143. }
  144. if (!$errormsg) {
  145. $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
  146. $errormsg = $utils->error;
  147. $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
  148. $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
  149. }
  150. $what = ''; // Clear to show message to run command
  151. }
  152. if ($errormsg) {
  153. setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
  154. $resultstring = '';
  155. $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
  156. $_SESSION["commandbackupresult"] = $resultstring;
  157. } else {
  158. if ($what) {
  159. setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
  160. $resultstring = '<div class="ok">';
  161. $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
  162. $resultstring .= $langs->trans("YouCanDownloadBackupFile");
  163. $resultstring .= '<div>';
  164. $_SESSION["commandbackupresult"] = $resultstring;
  165. }
  166. /*else
  167. {
  168. setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
  169. }*/
  170. }
  171. /*
  172. * View
  173. */
  174. top_httphead();
  175. $db->close();
  176. // Redirect to backup page
  177. header("Location: dolibarr_export.php".(GETPOST('page_y', 'int') ? '?page_y='.GETPOST('page_y', 'int') : ''));
  178. exit();