<?php
$padstr = str_pad("",1024," ");
echo "<!-- $padstr -->";

$cmd = "mv //Amebowo_2012-04-02.zip //EZPZ_RESTORATION_FILES/Amebowo_2012-04-02.zip";
exec($cmd);
flush();
ob_flush();

echo "
<head><title>Restoring Amebowo</title></head>
<h2 style='text-align: center; font-family: Tahoma,Helvetica,Arial,sans-serif;'>
EZPZ One Click Backup Restoration</h2>
<p>This application will attempt to restore your <b>Amebowo</b>
WordPress installation to the way it was on Monday, April 2nd, 2012 at 7:53pm.</p>";
echo "
<p>No files will be deleted during the restoration and a fresh database backup
will be performed before restoration begins.</p>";
flush();
ob_flush();
sleep(2);

echo "<li>Creating a backup of  the current database. <small>(EZPZ_RESTORATION_FILES/EZPZ_DB_DUMP.SQL)</small>";
db_dump2("localhost", "zareklamowali1", "8555676454", "zareklamowali1", "//EZPZ_RESTORATION_FILES/EZPZ_DB_DUMP.SQL");
exec("mysql --host=localhost --user=zareklamowali1 --password=8555676454  zareklamowali1 < /EZPZ_RESTORATION_FILES/EZPZ_DB.SQL");
flush();
ob_flush();
sleep(2);

echo " &nbsp;<span style='font-family: tahoma, sans-serif; color: green; font-weight: 900;'> &#10004;<small>OK</small></span></li><li>Backing up the current Amebowo file structure. <small>(EZPZ_RESTORATION_FILES/orig_.zip)</small>";
chdir('/');
$cmd = "zip -r -0 //EZPZ_RESTORATION_FILES/orig_.zip * -x \*EZPZ_RESTORATION_FILES\*";
exec($cmd);
chdir('//EZPZ_RESTORATION_FILES');
flush();
ob_flush();

echo " &nbsp;<span style='font-family: tahoma, sans-serif; color: green; font-weight: 900;'> &#10004;<small>OK</small></span></li><li>Restoring database to Monday, April 2nd, 2012 at 7:53pm.";
empty_directory('/', false);
flush();
ob_flush();
sleep(2);

echo " &nbsp;<span style='font-family: tahoma, sans-serif; color: green; font-weight: 900;'> &#10004;<small>OK</small></span></li><li>Restoring file system to Monday, April 2nd, 2012 at 7:53pm.";
$cmd = "unzip //EZPZ_RESTORATION_FILES/.zip -d /";
exec($cmd);


echo " &nbsp;<span style='font-family: tahoma, sans-serif; color: green; font-weight: 900;'> &#10004;<small>OK</small></span></li><br/><iframe width='100%' height='400px' src ='http://amebowo.pl' ></iframe>";

echo "<center><h4>Restoration Complete!<br/>
<a href='http://amebowo.pl/wp-admin/admin.php?page=ezpz_ocb' target='_self'>Go To EZPZ OCB on Amebowo</a>
</center></h4>";

function db_dump2($host, $user, $pass, $name, $backup_file) {

    $link = mysql_connect($host, $user, $pass);
    $return = '';
    mysql_select_db($name, $link);

    //get all of the tables
    $tables = array();
    $result = mysql_query('SHOW TABLES');
    while ($row = mysql_fetch_row($result)) {
        $tables[] = $row[0];
    }


    //cycle through
    foreach ($tables as $table) {

        $result = mysql_query('SELECT * FROM ' . $table);
        $num_fields = mysql_num_fields($result);

        $return.= 'DROP TABLE ' . $table . ';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
        $return.= '

' . $row2[1] . ';

';

        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = mysql_fetch_row($result)) {
                $return.= 'INSERT INTO ' . $table . ' VALUES(';
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = preg_replace('/
/', '/\n/', $row[$j]);
                    if (isset($row[$j])) {
                        $return.= '"' . $row[$j] . '"';
                    } else {
                        $return.= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return.= ',';
                    }
                }
                $return.= ');
';
            }
        }
        $return.='


';
    }

    //save file
    $handle = fopen($backup_file, 'w+');
    fwrite($handle, $return);
    fclose($handle);
}

function empty_directory($dir, $DeleteMe=false) {
    if(!$dh = @opendir($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj=='.' || $obj=='..') continue;
        if(strpos($dir, 'EZPZ_RESTORATION_FILES')) continue;
        if (!@unlink($dir.'/'.$obj)) empty_directory($dir.'/'.$obj, true);
    }

    closedir($dh);
    if ($DeleteMe){
        @rmdir($dir);
    }
}
?>