export.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/admin/tools/export.php
  21. * \brief Page to export a database into a dump file
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  28. $langs->load("admin");
  29. $action=GETPOST('action','alpha');
  30. $what=GETPOST('what','alpha');
  31. $export_type=GETPOST('export_type','alpha');
  32. $file=GETPOST('filename_template','alpha');
  33. $sortfield = GETPOST('sortfield','alpha');
  34. $sortorder = GETPOST('sortorder','alpha');
  35. $page = GETPOST("page",'int');
  36. if (! $sortorder) $sortorder="DESC";
  37. if (! $sortfield) $sortfield="date";
  38. if ($page < 0) { $page = 0; }
  39. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  40. $offset = $limit * $page;
  41. if (! $user->admin) accessforbidden();
  42. if ($file && ! $what)
  43. {
  44. //print DOL_URL_ROOT.'/dolibarr_export.php';
  45. header("Location: ".DOL_URL_ROOT.'/admin/tools/dolibarr_export.php?msg='.urlencode($langs->trans("ErrorFieldRequired",$langs->transnoentities("ExportMethod"))));
  46. exit;
  47. }
  48. $errormsg='';
  49. /*
  50. * Actions
  51. */
  52. if ($action == 'delete')
  53. {
  54. $file=$conf->admin->dir_output.'/'.GETPOST('urlfile');
  55. $ret=dol_delete_file($file, 1);
  56. if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  57. else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  58. $action='';
  59. }
  60. /*
  61. * View
  62. */
  63. $_SESSION["commandbackuplastdone"]='';
  64. $_SESSION["commandbackuptorun"]='';
  65. $_SESSION["commandbackupresult"]='';
  66. // Increase limit of time. Works only if we are not in safe mode
  67. $ExecTimeLimit=600;
  68. if (!empty($ExecTimeLimit))
  69. {
  70. $err=error_reporting();
  71. error_reporting(0); // Disable all errors
  72. //error_reporting(E_ALL);
  73. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  74. error_reporting($err);
  75. }
  76. $MemoryLimit=0;
  77. if (!empty($MemoryLimit))
  78. {
  79. @ini_set('memory_limit', $MemoryLimit);
  80. }
  81. $form=new Form($db);
  82. $formfile = new FormFile($db);
  83. //$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
  84. //llxHeader('','',$help_url);
  85. //print load_fiche_titre($langs->trans("Backup"),'','title_setup');
  86. // Start with empty buffer
  87. $dump_buffer = '';
  88. $dump_buffer_len = 0;
  89. // We will send fake headers to avoid browser timeout when buffering
  90. $time_start = time();
  91. $outputdir = $conf->admin->dir_output.'/backup';
  92. $result=dol_mkdir($outputdir);
  93. $utils = new Utils($db);
  94. // MYSQL
  95. if ($what == 'mysql')
  96. {
  97. $cmddump=GETPOST("mysqldump"); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
  98. $cmddump=dol_sanitizePathName($cmddump);
  99. if (! empty($dolibarr_main_restrict_os_commands))
  100. {
  101. $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
  102. $ok=0;
  103. dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
  104. foreach($arrayofallowedcommand as $allowedcommand)
  105. {
  106. if (preg_match('/'.preg_quote($allowedcommand,'/').'/', $cmddump))
  107. {
  108. $ok=1;
  109. break;
  110. }
  111. }
  112. if (! $ok)
  113. {
  114. $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
  115. }
  116. }
  117. if (! $errormsg && $cmddump)
  118. {
  119. dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
  120. }
  121. if (! $errormsg)
  122. {
  123. $utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
  124. $errormsg=$utils->error;
  125. $_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
  126. $_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
  127. }
  128. }
  129. // MYSQL NO BIN
  130. if ($what == 'mysqlnobin')
  131. {
  132. $utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
  133. $errormsg=$utils->error;
  134. $_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
  135. $_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
  136. }
  137. // POSTGRESQL
  138. if ($what == 'postgresql')
  139. {
  140. $cmddump=GETPOST("postgresqldump"); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
  141. $cmddump=dol_sanitizePathName($cmddump);
  142. if (! $errormsg && $cmddump)
  143. {
  144. dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
  145. }
  146. if (! $errormsg)
  147. {
  148. $utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
  149. $errormsg=$utils->error;
  150. $_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
  151. $_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
  152. }
  153. $what=''; // Clear to show message to run command
  154. }
  155. if ($errormsg)
  156. {
  157. setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
  158. $resultstring='';
  159. $resultstring.='<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
  160. $_SESSION["commandbackupresult"]=$resultstring;
  161. }
  162. else
  163. {
  164. if ($what)
  165. {
  166. setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
  167. $resultstring='<div class="ok">';
  168. $resultstring.=$langs->trans("BackupFileSuccessfullyCreated").'.<br>';
  169. $resultstring.=$langs->trans("YouCanDownloadBackupFile");
  170. $resultstring.='<div>';
  171. $_SESSION["commandbackupresult"]=$resultstring;
  172. }
  173. /*else
  174. {
  175. setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
  176. }*/
  177. }
  178. /*
  179. $filearray=dol_dir_list($conf->admin->dir_output.'/backup','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);
  180. $result=$formfile->list_of_documents($filearray,null,'systemtools','',1,'backup/',1,0,($langs->trans("NoBackupFileAvailable").'<br>'.$langs->trans("ToBuildBackupFileClickHere",DOL_URL_ROOT.'/admin/tools/dolibarr_export.php')),0,$langs->trans("PreviousDumpFiles"));
  181. print '<br>';
  182. */
  183. // Redirect t backup page
  184. header("Location: dolibarr_export.php");
  185. $time_end = time();
  186. $db->close();