dolibarr/htdocs/admin/tools/export.php

238 lines
7.1 KiB
PHP
Raw Normal View History

2006-08-20 21:15:47 +02:00
<?php
/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
2006-08-20 21:15:47 +02:00
*
2011-10-15 20:45:16 +02:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2011-10-15 20:45:16 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2006-08-20 21:15:47 +02:00
/**
2009-03-09 13:38:22 +01:00
* \file htdocs/admin/tools/export.php
2009-09-29 19:14:52 +02:00
* \brief Page to export a database into a dump file
2009-03-09 13:38:22 +01:00
*/
2006-08-20 21:15:47 +02:00
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
2006-08-20 21:15:47 +02:00
$langs->load("admin");
2012-04-10 08:44:43 +02:00
$action=GETPOST('action','alpha');
$what=GETPOST('what','alpha');
$export_type=GETPOST('export_type','alpha');
$file=GETPOST('filename_template','alpha');
2006-08-20 21:15:47 +02:00
2012-04-10 08:44:43 +02:00
$sortfield = GETPOST('sortfield','alpha');
$sortorder = GETPOST('sortorder','alpha');
$page = GETPOST("page",'int');
if (! $sortorder) $sortorder="DESC";
if (! $sortfield) $sortfield="date";
if ($page < 0) { $page = 0; }
2018-04-21 12:16:12 +02:00
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
$offset = $limit * $page;
if (! $user->admin) accessforbidden();
2006-08-20 21:15:47 +02:00
2009-03-09 13:38:22 +01:00
if ($file && ! $what)
2006-08-20 21:15:47 +02:00
{
//print DOL_URL_ROOT.'/dolibarr_export.php';
header("Location: ".DOL_URL_ROOT.'/admin/tools/dolibarr_export.php?msg='.urlencode($langs->trans("ErrorFieldRequired",$langs->transnoentities("ExportMethod"))));
exit;
2006-08-20 21:15:47 +02:00
}
2016-06-25 15:16:32 +02:00
$errormsg='';
2006-08-20 21:15:47 +02:00
2008-08-03 13:34:47 +02:00
/*
* Actions
*/
2008-08-03 13:34:47 +02:00
if ($action == 'delete')
{
$file=$conf->admin->dir_output.'/'.GETPOST('urlfile');
$ret=dol_delete_file($file, 1);
2015-10-23 12:58:30 +02:00
if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
2012-07-29 15:47:24 +02:00
$action='';
}
2006-08-24 20:51:59 +02:00
/*
* View
*/
2006-08-24 20:51:59 +02:00
$_SESSION["commandbackuplastdone"]='';
$_SESSION["commandbackuptorun"]='';
$_SESSION["commandbackupresult"]='';
// Increase limit of time. Works only if we are not in safe mode
2011-03-05 02:53:20 +01:00
$ExecTimeLimit=600;
if (!empty($ExecTimeLimit))
{
2011-03-05 02:53:20 +01:00
$err=error_reporting();
error_reporting(0); // Disable all errors
//error_reporting(E_ALL);
@set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
error_reporting($err);
2010-08-21 22:16:52 +02:00
}
2017-06-26 09:20:16 +02:00
$MemoryLimit=0;
if (!empty($MemoryLimit))
{
2011-03-05 02:53:20 +01:00
@ini_set('memory_limit', $MemoryLimit);
2006-08-20 21:15:47 +02:00
}
$form=new Form($db);
$formfile = new FormFile($db);
//$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
//llxHeader('','',$help_url);
//print load_fiche_titre($langs->trans("Backup"),'','title_setup');
2006-08-20 21:15:47 +02:00
// Start with empty buffer
$dump_buffer = '';
$dump_buffer_len = 0;
// We will send fake headers to avoid browser timeout when buffering
2006-08-20 21:15:47 +02:00
$time_start = time();
$outputdir = $conf->admin->dir_output.'/backup';
$result=dol_mkdir($outputdir);
$utils = new Utils($db);
2009-10-26 04:10:12 +01:00
// MYSQL
2006-08-20 21:15:47 +02:00
if ($what == 'mysql')
{
2017-06-26 09:20:16 +02:00
2016-09-08 11:05:47 +02:00
$cmddump=GETPOST("mysqldump"); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
$cmddump=dol_sanitizePathName($cmddump);
2017-06-26 09:20:16 +02:00
2016-06-25 15:16:32 +02:00
if (! empty($dolibarr_main_restrict_os_commands))
{
$arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
$ok=0;
2018-10-07 21:45:20 +02:00
dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
2016-06-25 15:16:32 +02:00
foreach($arrayofallowedcommand as $allowedcommand)
{
if (preg_match('/'.preg_quote($allowedcommand,'/').'/', $cmddump))
{
$ok=1;
break;
}
}
if (! $ok)
{
$errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
}
}
2017-06-26 09:20:16 +02:00
2016-06-25 15:16:32 +02:00
if (! $errormsg && $cmddump)
{
dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
}
2017-06-26 09:20:16 +02:00
if (! $errormsg)
2016-06-25 15:16:32 +02:00
{
$utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
$errormsg=$utils->error;
$_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
$_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
}
}
// MYSQL NO BIN
if ($what == 'mysqlnobin')
{
$utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
$errormsg=$utils->error;
$_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
$_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
2009-10-26 04:10:12 +01:00
}
// POSTGRESQL
if ($what == 'postgresql')
{
2016-09-08 11:05:47 +02:00
$cmddump=GETPOST("postgresqldump"); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
$cmddump=dol_sanitizePathName($cmddump);
2017-06-26 09:20:16 +02:00
2016-06-25 15:16:32 +02:00
if (! $errormsg && $cmddump)
{
dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
}
2017-06-26 09:20:16 +02:00
if (! $errormsg)
2016-06-25 15:16:32 +02:00
{
$utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
$errormsg=$utils->error;
$_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
$_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
}
$what=''; // Clear to show message to run command
2006-08-20 21:15:47 +02:00
}
2009-10-26 04:10:12 +01:00
if ($errormsg)
{
setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
2009-10-26 04:10:12 +01:00
$resultstring='';
$resultstring.='<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
$_SESSION["commandbackupresult"]=$resultstring;
}
else
{
if ($what)
{
setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
$resultstring='<div class="ok">';
$resultstring.=$langs->trans("BackupFileSuccessfullyCreated").'.<br>';
$resultstring.=$langs->trans("YouCanDownloadBackupFile");
$resultstring.='<div>';
$_SESSION["commandbackupresult"]=$resultstring;
}
2017-11-25 01:58:11 +01:00
/*else
{
2017-11-25 01:58:11 +01:00
setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
}*/
}
2006-08-20 21:15:47 +02:00
/*
$filearray=dol_dir_list($conf->admin->dir_output.'/backup','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);
$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"));
2009-03-09 13:38:22 +01:00
2006-08-20 21:15:47 +02:00
print '<br>';
*/
2006-08-20 21:15:47 +02:00
// Redirect t backup page
header("Location: dolibarr_export.php");
2006-08-20 21:15:47 +02:00
$time_end = time();
$db->close();