#!/usr/bin/php -q
<?php
if (PHP_VERSION_ID < 60000) {
	mb_internal_encoding("UTF-8");
	set_time_limit(0);
	ob_implicit_flush();
	declare(ticks = 1);
	date_default_timezone_set("Europe/Paris");
}
$config = array();
error_reporting(-1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
ini_set('log_errors', true);
ini_set('error_log', '/var/log/wprotect/wprotect_restore.log');
require_once("/opt/wprotect/class/share_functions.php");
//-----------------------------------------------------------------------------
function myexec($commande, $log_error=TRUE) {
	exec ($commande, $result, $return);
	if (($return > 0) && $log_error) logme ("EXEC : ".$commande." -> ".$return);
	return $result;
}
//-----------------------------------------------------------------------------
function logme ($data) {
	syslog ( LOG_DEBUG , "[wprotect_restore] ".$data );
}
//-----------------------------------------------------------------------------
function go_uncompress($target) {
	clean_tmp();
	myexec("cp /backup/".$target." /backup/tmp/");
	myexec("cd /backup/tmp/; tar xvf /backup/tmp/".$target);
	myexec("rm /backup/tmp/".$target);
}
//-----------------------------------------------------------------------------
function wprotect_stop() {
	logme("Stopping Wprotect...");
	echo("Stopping Wprotect...\n");
	myexec("/etc/init.d/wprotect stop");
}
//-----------------------------------------------------------------------------
function wprotect_start() {
	echo("Starting Wprotect...\n");
	logme("Starting Wprotect...");
	myexec("/etc/init.d/wprotect start");
}
//-----------------------------------------------------------------------------
function bdd_import() {
	$cdir = scandir("/backup/tmp");
   foreach ($cdir as $k => $v) {
		if ('.' !== $v && '..' !== $v){
			$file_info = pathinfo("/backup/tmp/".$v);
			if (isset($file_info['extension']) AND $file_info['extension']=="sql") {
				echo("BDD import -> ".$v."\n");
				logme("BDD import -> ".$v);
				myexec("cd /backup; mysql -u root -pa8pksduj wprotect < /backup/tmp/".$v);
			}
		}
   }
}
//-----------------------------------------------------------------------------
function clean_tmp() {
	myexec("cd /backup");
	myexec("rm -R /backup/tmp");
	myexec("mkdir -p /backup/tmp/");
}
//-----------------------------------------------------------------------------
function copy_files() {
	global $no_net_config;
	logme("Restore files...");
	echo("Restore files...\n");
	myexec("rm -R /etc/wprotect.old");
	myexec("cp -R /etc/wprotect /etc/wprotect.old; cp -R /backup/tmp/etc/wprotect /etc/");
	if (!$no_net_config) {
		myexec("mv -f /etc/network/interfaces /etc/network/interfaces.old; cp /backup/tmp/etc/network/interfaces /etc/network/interfaces");
	}
	myexec("cp /backup/tmp/log/networ* /var/log/wprotect/");
	myexec("cp /backup/tmp/var/lib/munin/localdomain/* /var/lib/munin/localdomain/");
	myexec("chown -R munin:munin /var/lib/munin/localdomain/*");
	myexec("rm /var/cache/munin/www/localdomain/localhost.localdomain/*.png");
	myexec("su - munin --shell=/bin/bash -c /usr/share/munin/munin-update");
	myexec("su - munin --shell=/bin/bash -c /usr/share/munin/munin-graph");
}
//-----------------------------------------------------------------------------
function restore_portal_files() {	
	myexec("cp -R /backup/tmp/portal_files/* /var/www/admin/fileman/Uploads/");
}
//-----------------------------------------------------------------------------
//§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ MAIN PROGRAM §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
$no_net_config=FALSE;
foreach ($argv AS $k=>$v) {
	if (($v=="-help") OR ($v=="--help") OR ($v=="-h")) {
		echo "Restore backup for WPROTECT, backup must be place into /backup.\n\n";
		echo "Exemple : wp_restore backup_filename";
		echo "Commandline Argument :\n";
		echo "----------------------\n";
		echo "-no-net-config : don't restore /etc/network/interfaces \n\n";
		exit();
	}
	if (($v=="-no-net-config")) {
		$no_net_config=TRUE;
	}
}

logme("start ...");
if (isset($argv[1])) {
	$target=$argv[1];
}
else {
	echo("erreur, le fichier n'est pas valide ou introuvable .\n");
	logme("erreur, le fichier n'est pas valide ou introuvable .");
	exit(0);
}
if (!file_exists("/backup/".$target)) {
	echo("erreur, le fichier n'est pas valide ou introuvable .\n");
	logme("erreur, le fichier n'est pas valide ou introuvable .");
	exit(0);
}
if( ! Thread::available() ) {
	logme("Threads not supported");
    die( 'Threads not supported' );
}
go_uncompress($target);
wprotect_stop();
bdd_import();
copy_files();
restore_portal_files();
clean_tmp();
myexec("chmod +x /opt/wprotect/install; /opt/wprotect/install");
logme("End.");
?>