export_files.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_files.php
  22. * \brief Page to export documents into a compressed file
  23. */
  24. if (! defined('CSRFCHECK_WITH_TOKEN')) {
  25. define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
  26. }
  27. // Load Dolibarr environment
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  33. $langs->load("admin");
  34. $action = GETPOST('action', 'aZ09');
  35. $what = GETPOST('what', 'alpha');
  36. $export_type = GETPOST('export_type', 'alpha');
  37. $file = trim(GETPOST('zipfilename_template', 'alpha'));
  38. $compression = GETPOST('compression', 'aZ09');
  39. $file = dol_sanitizeFileName($file);
  40. $file = preg_replace('/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i', '', $file);
  41. $sortfield = GETPOST('sortfield', 'aZ09comma');
  42. $sortorder = GETPOST('sortorder', 'aZ09comma');
  43. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  44. if (!$sortorder) {
  45. $sortorder = "DESC";
  46. }
  47. if (!$sortfield) {
  48. $sortfield = "date";
  49. }
  50. if ($page < 0) {
  51. $page = 0;
  52. } elseif (empty($page)) {
  53. $page = 0;
  54. }
  55. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  56. $offset = $limit * $page;
  57. if (!$user->admin) {
  58. accessforbidden();
  59. }
  60. $errormsg = '';
  61. /*
  62. * Actions
  63. */
  64. if ($action == 'delete') {
  65. $filerelative = dol_sanitizeFileName(GETPOST('urlfile', 'alpha'));
  66. $filepath = $conf->admin->dir_output.'/'.$filerelative;
  67. $ret = dol_delete_file($filepath, 1);
  68. if ($ret) {
  69. setEventMessages($langs->trans("FileWasRemoved", $filerelative), null, 'mesgs');
  70. } else {
  71. setEventMessages($langs->trans("ErrorFailToDeleteFile", $filerelative), null, 'errors');
  72. }
  73. $action = '';
  74. }
  75. /*
  76. * View
  77. */
  78. // Increase limit of time. Works only if we are not in safe mode
  79. $ExecTimeLimit = 1800; // 30mn
  80. if (!empty($ExecTimeLimit)) {
  81. $err = error_reporting();
  82. error_reporting(0); // Disable all errors
  83. //error_reporting(E_ALL);
  84. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  85. error_reporting($err);
  86. }
  87. /* If value has been forced with a php_admin_value, this has no effect. Example of value: '512M' */
  88. $MemoryLimit = getDolGlobalString('MAIN_MEMORY_LIMIT_ARCHIVE_DATAROOT');
  89. if (!empty($MemoryLimit)) {
  90. @ini_set('memory_limit', $MemoryLimit);
  91. }
  92. $form = new Form($db);
  93. $formfile = new FormFile($db);
  94. //$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
  95. //llxHeader('','',$help_url);
  96. //print load_fiche_titre($langs->trans("Backup"),'','title_setup');
  97. // Start with empty buffer
  98. $dump_buffer = '';
  99. $dump_buffer_len = 0;
  100. // We will send fake headers to avoid browser timeout when buffering
  101. $time_start = time();
  102. $outputdir = $conf->admin->dir_output.'/documents';
  103. $result = dol_mkdir($outputdir);
  104. $utils = new Utils($db);
  105. if ($export_type == 'externalmodule' && !empty($what)) {
  106. $fulldirtocompress = DOL_DOCUMENT_ROOT.'/custom/'.dol_sanitizeFileName($what);
  107. } else {
  108. $fulldirtocompress = DOL_DATA_ROOT;
  109. }
  110. $dirtoswitch = dirname($fulldirtocompress);
  111. $dirtocompress = basename($fulldirtocompress);
  112. if ($compression == 'zip') {
  113. $file .= '.zip';
  114. $excludefiles = '/(\.back|\.old|\.log|\.pdf_preview-.*\.png|[\/\\\]temp[\/\\\]|[\/\\\]admin[\/\\\]documents[\/\\\])/i';
  115. //var_dump($fulldirtocompress);
  116. //var_dump($outputdir."/".$file);exit;
  117. $rootdirinzip = '';
  118. if ($export_type == 'externalmodule' && !empty($what)) {
  119. $rootdirinzip = $what;
  120. global $dolibarr_allow_download_external_modules;
  121. if (empty($dolibarr_allow_download_external_modules)) {
  122. print 'Download of external modules is not allowed by $dolibarr_allow_download_external_modules in conf.php file';
  123. $db->close();
  124. exit();
  125. }
  126. }
  127. $ret = dol_compress_dir($fulldirtocompress, $outputdir."/".$file, $compression, $excludefiles, $rootdirinzip);
  128. if ($ret < 0) {
  129. if ($ret == -2) {
  130. $langs->load("errors");
  131. $errormsg = $langs->trans("ErrNoZipEngine");
  132. } else {
  133. $langs->load("errors");
  134. $errormsg = $langs->trans("ErrorFailedToWriteInDir", $outputdir);
  135. }
  136. }
  137. } elseif (in_array($compression, array('gz', 'bz', 'zstd'))) {
  138. $userlogin = ($user->login ? $user->login : 'unknown');
  139. $outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method
  140. $file .= '.tar';
  141. // We also exclude '/temp/' dir and 'documents/admin/documents'
  142. // We make escapement here and call executeCLI without escapement because we don't want to have the '*.log' escaped.
  143. $cmd = "tar -cf '".escapeshellcmd($outputdir."/".$file)."' --exclude-vcs --exclude-caches-all --exclude='temp' --exclude='*.log' --exclude='*.pdf_preview-*.png' --exclude='documents/admin/documents' -C '".escapeshellcmd(dol_sanitizePathName($dirtoswitch))."' '".escapeshellcmd(dol_sanitizeFileName($dirtocompress))."'";
  144. $result = $utils->executeCLI($cmd, $outputfile, 0, null, 1);
  145. $retval = $result['error'];
  146. if ($result['result'] || !empty($retval)) {
  147. $langs->load("errors");
  148. dol_syslog("Documents tar retval after exec=".$retval, LOG_ERR);
  149. $errormsg = 'Error tar generation return '.$retval;
  150. } else {
  151. if ($compression == 'gz') {
  152. $cmd = "gzip -f ".$outputdir."/".$file;
  153. } elseif ($compression == 'bz') {
  154. $cmd = "bzip2 -f ".$outputdir."/".$file;
  155. } elseif ($compression == 'zstd') {
  156. $cmd = "zstd -z -9 -q --rm ".$outputdir."/".$file;
  157. }
  158. $result = $utils->executeCLI($cmd, $outputfile);
  159. $retval = $result['error'];
  160. if ($result['result'] || !empty($retval)) {
  161. $errormsg = 'Error '.$compression.' generation return '.$retval;
  162. unlink($outputdir."/".$file);
  163. }
  164. }
  165. } else {
  166. $errormsg = 'Bad value for compression method';
  167. print $errormsg;
  168. }
  169. // Output export
  170. if ($export_type != 'externalmodule' || empty($what)) {
  171. top_httphead();
  172. if ($errormsg) {
  173. setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
  174. } else {
  175. setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
  176. }
  177. $db->close();
  178. // Redirect to calling page
  179. $returnto = 'dolibarr_export.php';
  180. header("Location: ".$returnto);
  181. exit();
  182. } else {
  183. top_httphead('application/zip');
  184. $zipname = $outputdir."/".$file;
  185. // Then download the zipped file.
  186. header('Content-disposition: attachment; filename='.basename($zipname));
  187. header('Content-Length: '.filesize($zipname));
  188. readfile($zipname);
  189. dol_delete_file($zipname);
  190. $db->close();
  191. exit();
  192. }