2006-08-20 21:15:47 +02:00
< ? php
2015-12-17 21:10:14 +01:00
/* Copyright ( C ) 2006 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2011 Juanjo Menent < jmenent @ 2 byte . 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
2013-01-16 15:36:08 +01:00
* 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2011-10-15 20:45:16 +02:00
*/
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
2012-08-22 23:24:21 +02:00
require '../../main.inc.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2016-03-29 14:52:27 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/utils.class.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php' ;
2006-08-20 21:15:47 +02:00
$langs -> load ( " admin " );
2020-09-16 19:39:50 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2020-04-10 10:59:32 +02:00
$what = GETPOST ( 'what' , 'alpha' );
$export_type = GETPOST ( 'export_type' , 'alpha' );
$file = GETPOST ( 'filename_template' , 'alpha' );
2019-01-27 11:55:16 +01:00
2019-02-05 18:54:38 +01:00
// Load variable for pagination
2020-04-10 10:59:32 +02:00
$limit = GETPOST ( 'limit' , 'int' ) ? GETPOST ( 'limit' , 'int' ) : $conf -> liste_limit ;
2020-09-18 17:13:01 +02:00
$sortfield = GETPOST ( 'sortfield' , 'aZ09comma' );
2020-09-17 14:31:25 +02:00
$sortorder = GETPOST ( 'sortorder' , 'aZ09comma' );
2020-03-13 13:07:11 +01:00
$page = GETPOSTISSET ( 'pageplusone' ) ? ( GETPOST ( 'pageplusone' ) - 1 ) : GETPOST ( " page " , 'int' );
2019-02-06 15:56:13 +01:00
if ( empty ( $page ) || $page == - 1 || GETPOST ( 'button_search' , 'alpha' ) || GETPOST ( 'button_removefilter' , 'alpha' ) || ( empty ( $toselect ) && $massaction === '0' )) { $page = 0 ; } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
2019-02-05 18:54:38 +01:00
$offset = $limit * $page ;
2020-04-10 10:59:32 +02:00
if ( ! $sortorder ) $sortorder = " DESC " ;
if ( ! $sortfield ) $sortfield = " date " ;
2012-02-10 10:53:11 +01:00
2020-04-10 10:59:32 +02:00
if ( ! $user -> admin ) accessforbidden ();
2006-08-20 21:15:47 +02:00
2020-04-10 10:59:32 +02:00
if ( $file && ! $what )
2006-08-20 21:15:47 +02:00
{
2011-10-14 16:02:18 +02:00
//print DOL_URL_ROOT.'/dolibarr_export.php';
2020-04-10 10:59:32 +02:00
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' ) : '' ));
2011-10-14 16:02:18 +02:00
exit ;
2006-08-20 21:15:47 +02:00
}
2020-04-10 10:59:32 +02:00
$errormsg = '' ;
2016-06-25 15:16:32 +02:00
2006-08-20 21:15:47 +02:00
2008-08-03 13:34:47 +02:00
/*
2012-02-10 10:53:11 +01:00
* Actions
*/
2008-08-03 13:34:47 +02:00
2012-02-10 11:09:34 +01:00
if ( $action == 'delete' )
{
2020-04-10 10:59:32 +02:00
$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' );
2020-04-10 10:59:32 +02:00
$action = '' ;
2012-02-10 11:09:34 +01:00
}
2006-08-24 20:51:59 +02:00
2012-02-10 10:53:11 +01:00
/*
* View
*/
2006-08-24 20:51:59 +02:00
2020-04-10 10:59:32 +02:00
$_SESSION [ " commandbackuplastdone " ] = '' ;
$_SESSION [ " commandbackuptorun " ] = '' ;
$_SESSION [ " commandbackupresult " ] = '' ;
2015-05-18 20:33:02 +02:00
2012-02-10 10:53:11 +01:00
// Increase limit of time. Works only if we are not in safe mode
2020-04-10 10:59:32 +02:00
$ExecTimeLimit = 600 ;
2012-02-10 10:53:11 +01:00
if ( ! empty ( $ExecTimeLimit ))
{
2020-04-10 10:59:32 +02:00
$err = error_reporting ();
error_reporting ( 0 ); // Disable all errors
2011-03-05 02:53:20 +01:00
//error_reporting(E_ALL);
2020-04-10 10:59:32 +02:00
@ set_time_limit ( $ExecTimeLimit ); // Need more than 240 on Windows 7/64
2011-03-05 02:53:20 +01:00
error_reporting ( $err );
2010-08-21 22:16:52 +02:00
}
2020-04-10 10:59:32 +02:00
$MemoryLimit = 0 ;
2012-02-10 10:53:11 +01:00
if ( ! empty ( $MemoryLimit ))
{
2011-03-05 02:53:20 +01:00
@ ini_set ( 'memory_limit' , $MemoryLimit );
2006-08-20 21:15:47 +02:00
}
2020-04-10 10:59:32 +02:00
$form = new Form ( $db );
2012-02-10 10:53:11 +01:00
$formfile = new FormFile ( $db );
2015-04-03 17:17:02 +02:00
//$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
//llxHeader('','',$help_url);
2012-02-10 10:53:11 +01:00
2015-09-24 18:33:48 +02:00
//print load_fiche_titre($langs->trans("Backup"),'','title_setup');
2012-02-10 10:53:11 +01:00
2006-08-20 21:15:47 +02:00
// Start with empty buffer
$dump_buffer = '' ;
$dump_buffer_len = 0 ;
2012-02-10 10:53:11 +01:00
// We will send fake headers to avoid browser timeout when buffering
2006-08-20 21:15:47 +02:00
$time_start = time ();
2016-01-31 14:58:04 +01:00
$outputdir = $conf -> admin -> dir_output . '/backup' ;
2020-04-10 10:59:32 +02:00
$result = dol_mkdir ( $outputdir );
2016-01-31 14:58:04 +01:00
2016-03-29 14:52:27 +02:00
$utils = new Utils ( $db );
2009-10-26 04:10:12 +01:00
// MYSQL
2006-08-20 21:15:47 +02:00
if ( $what == 'mysql' )
{
2020-04-10 10:59:32 +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
2020-04-10 10:59:32 +02:00
if ( ! empty ( $dolibarr_main_restrict_os_commands ))
2016-06-25 15:16:32 +02:00
{
2020-04-10 10:59:32 +02:00
$arrayofallowedcommand = explode ( ',' , $dolibarr_main_restrict_os_commands );
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 );
2020-04-10 10:59:32 +02:00
$basenamecmddump = basename ( $cmddump );
if ( ! in_array ( $basenamecmddump , $arrayofallowedcommand )) // the provided command $cmddump must be an allowed command
2016-06-25 15:16:32 +02:00
{
2020-04-10 10:59:32 +02:00
$errormsg = $langs -> trans ( 'CommandIsNotInsideAllowedCommands' );
2016-06-25 15:16:32 +02:00
}
}
2017-06-26 09:20:16 +02:00
2020-04-10 10:59:32 +02:00
if ( ! $errormsg && $cmddump )
2011-10-14 16:02:18 +02:00
{
2019-01-27 11:55:16 +01:00
dolibarr_set_const ( $db , 'SYSTEMTOOLS_MYSQLDUMP' , $cmddump , 'chaine' , 0 , '' , $conf -> entity );
2011-10-14 16:02:18 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! $errormsg )
2016-06-25 15:16:32 +02:00
{
2019-01-27 11:55:16 +01:00
$utils -> dumpDatabase ( GETPOST ( 'compression' , 'alpha' ), $what , 0 , $file );
2020-04-10 10:59:32 +02:00
$errormsg = $utils -> error ;
$_SESSION [ " commandbackuplastdone " ] = $utils -> result [ 'commandbackuplastdone' ];
$_SESSION [ " commandbackuptorun " ] = $utils -> result [ 'commandbackuptorun' ];
2016-06-25 15:16:32 +02:00
}
2011-10-14 16:02:18 +02:00
}
2016-03-29 14:52:27 +02:00
// MYSQL NO BIN
2011-10-14 16:02:18 +02:00
if ( $what == 'mysqlnobin' )
{
2019-01-27 11:55:16 +01:00
$utils -> dumpDatabase ( GETPOST ( 'compression' , 'alpha' ), $what , 0 , $file );
2015-04-03 17:17:02 +02:00
2020-04-10 10:59:32 +02:00
$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' )
{
2020-04-10 10:59:32 +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
2019-08-01 15:42:44 +02:00
/* Not required , the command is output on screen but not ran for pgsql
if ( ! empty ( $dolibarr_main_restrict_os_commands ))
{
$arrayofallowedcommand = explode ( ',' , $dolibarr_main_restrict_os_commands );
dol_syslog ( " Command are restricted to " . $dolibarr_main_restrict_os_commands . " . We check that one of this command is inside " . $cmddump );
$basenamecmddump = basename ( $cmddump );
if ( ! in_array ( $basenamecmddump , $arrayofallowedcommand )) // the provided command $cmddump must be an allowed command
{
$errormsg = $langs -> trans ( 'CommandIsNotInsideAllowedCommands' );
}
} */
2020-04-10 10:59:32 +02:00
if ( ! $errormsg && $cmddump )
2011-10-14 16:02:18 +02:00
{
2019-01-27 11:55:16 +01:00
dolibarr_set_const ( $db , 'SYSTEMTOOLS_POSTGRESQLDUMP' , $cmddump , 'chaine' , 0 , '' , $conf -> entity );
2011-10-14 16:02:18 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! $errormsg )
2016-06-25 15:16:32 +02:00
{
2019-01-27 11:55:16 +01:00
$utils -> dumpDatabase ( GETPOST ( 'compression' , 'alpha' ), $what , 0 , $file );
2020-04-10 10:59:32 +02:00
$errormsg = $utils -> error ;
$_SESSION [ " commandbackuplastdone " ] = $utils -> result [ 'commandbackuplastdone' ];
$_SESSION [ " commandbackuptorun " ] = $utils -> result [ 'commandbackuptorun' ];
2016-06-25 15:16:32 +02:00
}
2016-03-29 14:52:27 +02:00
2020-04-10 10:59:32 +02:00
$what = '' ; // Clear to show message to run command
2006-08-20 21:15:47 +02:00
}
2009-10-26 04:10:12 +01:00
2016-01-31 14:58:04 +01:00
if ( $errormsg )
{
setEventMessages ( $langs -> trans ( " Error " ) . " : " . $errormsg , null , 'errors' );
2009-10-26 04:10:12 +01:00
2020-04-10 10:59:32 +02:00
$resultstring = '' ;
$resultstring .= '<div class="error">' . $langs -> trans ( " Error " ) . " : " . $errormsg . '</div>' ;
2015-05-18 20:33:02 +02:00
2020-04-10 10:59:32 +02:00
$_SESSION [ " commandbackupresult " ] = $resultstring ;
2020-05-21 09:35:30 +02:00
} else {
2016-01-31 14:58:04 +01:00
if ( $what )
{
setEventMessages ( $langs -> trans ( " BackupFileSuccessfullyCreated " ) . '.<br>' . $langs -> trans ( " YouCanDownloadBackupFile " ), null , 'mesgs' );
2020-04-10 10:59:32 +02:00
$resultstring = '<div class="ok">' ;
$resultstring .= $langs -> trans ( " BackupFileSuccessfullyCreated " ) . '.<br>' ;
$resultstring .= $langs -> trans ( " YouCanDownloadBackupFile " );
$resultstring .= '<div>' ;
2015-05-18 20:33:02 +02:00
2020-04-10 10:59:32 +02:00
$_SESSION [ " commandbackupresult " ] = $resultstring ;
2016-01-31 14:58:04 +01:00
}
2017-11-25 01:58:11 +01:00
/* else
2015-04-03 17:17:02 +02:00
{
2017-11-25 01:58:11 +01:00
setEventMessages ( $langs -> trans ( " YouMustRunCommandFromCommandLineAfterLoginToUser " , $dolibarr_main_db_user , $dolibarr_main_db_user ), null , 'warnings' );
} */
2016-01-31 14:58:04 +01:00
}
2006-08-20 21:15:47 +02:00
2015-04-03 17:17:02 +02:00
/*
2012-02-10 10:53:11 +01:00
$filearray = dol_dir_list ( $conf -> admin -> dir_output . '/backup' , 'files' , 0 , '' , '' , $sortfield ,( strtolower ( $sortorder ) == 'asc' ? SORT_ASC : SORT_DESC ), 1 );
2012-02-10 11:09:34 +01:00
$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>' ;
2015-04-03 17:17:02 +02:00
*/
2006-08-20 21:15:47 +02:00
2019-08-01 15:42:44 +02:00
// Redirect to backup page
2020-04-10 10:59:32 +02:00
header ( " Location: dolibarr_export.php " . ( GETPOST ( 'page_y' , 'int' ) ? '?page_y=' . GETPOST ( 'page_y' , 'int' ) : '' ));
2006-08-20 21:15:47 +02:00
2015-04-03 17:17:02 +02:00
$time_end = time ();
2011-10-14 16:02:18 +02:00
$db -> close ();