dolibarr  13.0.2
export_files.php
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/>.
18 */
19 
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 
31 $langs->load("admin");
32 
33 $action = GETPOST('action', 'aZ09');
34 $what = GETPOST('what', 'alpha');
35 $export_type = GETPOST('export_type', 'alpha');
36 $file = trim(GETPOST('zipfilename_template', 'alpha'));
37 $compression = GETPOST('compression', 'aZ09');
38 
39 $file = dol_sanitizeFileName($file);
40 $file = preg_replace('/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2)$/i', '', $file);
41 
42 $sortfield = GETPOST('sortfield', 'aZ09comma');
43 $sortorder = GETPOST('sortorder', 'aZ09comma');
44 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
45 if (!$sortorder) $sortorder = "DESC";
46 if (!$sortfield) $sortfield = "date";
47 if ($page < 0) { $page = 0; } elseif (empty($page)) $page = 0;
48 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
49 $offset = $limit * $page;
50 
51 if (!$user->admin) accessforbidden();
52 
53 $errormsg = '';
54 
55 
56 /*
57  * Actions
58  */
59 
60 if ($action == 'delete')
61 {
62  $filerelative = dol_sanitizeFileName(GETPOST('urlfile', 'alpha'));
63  $filepath = $conf->admin->dir_output.'/'.$filerelative;
64  $ret = dol_delete_file($filepath, 1);
65  if ($ret) setEventMessages($langs->trans("FileWasRemoved", $filerelative), null, 'mesgs');
66  else setEventMessages($langs->trans("ErrorFailToDeleteFile", $filerelative), null, 'errors');
67  $action = '';
68 }
69 
70 
71 /*
72  * View
73  */
74 
75 // Increase limit of time. Works only if we are not in safe mode
76 $ExecTimeLimit = 1800; // 30mn
77 if (!empty($ExecTimeLimit))
78 {
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 {
88  @ini_set('memory_limit', $MemoryLimit);
89 }
90 
91 $form = new Form($db);
92 $formfile = new FormFile($db);
93 
94 //$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
95 //llxHeader('','',$help_url);
96 
97 //print load_fiche_titre($langs->trans("Backup"),'','title_setup');
98 
99 
100 // Start with empty buffer
101 $dump_buffer = '';
102 $dump_buffer_len = 0;
103 
104 // We will send fake headers to avoid browser timeout when buffering
105 $time_start = time();
106 
107 
108 $outputdir = $conf->admin->dir_output.'/documents';
109 $result = dol_mkdir($outputdir);
110 
111 $utils = new Utils($db);
112 
113 if ($compression == 'zip')
114 {
115  $file .= '.zip';
116  $excludefiles = '/(\.back|\.old|\.log|[\/\\\]temp[\/\\\]|documents[\/\\\]admin[\/\\\]documents[\/\\\])/i';
117  $ret = dol_compress_dir(DOL_DATA_ROOT, $outputdir."/".$file, $compression, $excludefiles);
118  if ($ret < 0)
119  {
120  if ($ret == -2) {
121  $langs->load("errors");
122  $errormsg = $langs->trans("ErrNoZipEngine");
123  } else {
124  $langs->load("errors");
125  $errormsg = $langs->trans("ErrorFailedToWriteInDir", $outputdir);
126  }
127  }
128 } elseif (in_array($compression, array('gz', 'bz')))
129 {
130  $userlogin = ($user->login ? $user->login : 'unknown');
131 
132  $outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method
133 
134  $file .= '.tar';
135  // We also exclude '/temp/' dir and 'documents/admin/documents'
136  $cmd = "tar -cf ".$outputdir."/".$file." --exclude-vcs --exclude 'temp' --exclude 'dolibarr.log' --exclude 'dolibarr_*.log' --exclude 'documents/admin/documents' -C ".dirname(DOL_DATA_ROOT)." ".basename(DOL_DATA_ROOT);
137 
138  $result = $utils->executeCLI($cmd, $outputfile);
139 
140  $retval = $result['error'];
141  if ($result['result'] || !empty($retval))
142  {
143  $langs->load("errors");
144  dol_syslog("Documents tar retval after exec=".$retval, LOG_ERR);
145  $errormsg = 'Error tar generation return '.$retval;
146  } else {
147  if ($compression == 'gz')
148  {
149  $cmd = "gzip -f ".$outputdir."/".$file;
150  }
151  if ($compression == 'bz')
152  {
153  $cmd = "bzip2 -f ".$outputdir."/".$file;
154  }
155 
156  $result = $utils->executeCLI($cmd, $outputfile);
157 
158  $retval = $result['error'];
159  if ($result['result'] || !empty($retval))
160  {
161  $errormsg = 'Error '.$compression.' generation return '.$retval;
162  unlink($outputdir."/".$file);
163  }
164  }
165 }
166 
167 if ($errormsg)
168 {
169  setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
170 }
171 
172 // Redirect t backup page
173 header("Location: dolibarr_export.php");
174 
175 $time_end = time();
176 
177 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage utility methods.
Definition: utils.class.php:28
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1144
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
Class to offer components to list and upload files.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)