<?php

include_once('connect.php');
include_once('functions.php');
 error_reporting(E_ALL);
 ini_set("display_errors", true);
 ini_set("memory_limit", "512M");



if(strtolower($_REQUEST['sql']) == "true" && $_REQUEST['key'] == "e3x5Ym93Pdv"){
	include("Mysqldump.php");
  require_once './assets/PHPMailerAutoload.php';
  global $host, $config;
  global $user, $pass, $db;
	$data = array('compress' => 'Gzip');
    //place user and pass
	$dump = new Mysqldump("mysql:host=$host;dbname=$db", $user, $pass, $data);
    $datestamp = date("Y-m-d_Hi");
    $filepath = './downloads/' . $datestamp . "_mysqldump.gzip";
    //$filename
    $dump->start($filepath);
    echo $filepath;
		if($_REQUEST['ondemand'] == "true"){
			$title = "Database Backup ";
		}else{
			$title = "Database Weekly Backup ";
		}
  //   use PHPMailer\PHPMailer\PHPMailer;
  //   use PHPMailer\PHPMailer\Exception;

    $email = new PHPMailer();
    $email->isMail();
    $email->SetFrom($config['FromEmail'], $config['FromEmailName']); //Name is optional
    $email->Subject   = $title . $datestamp ;
    $email->Body      = "Attached $title";
    $email->AddAddress( $config['OrderEmail'] );

    $email->AddAttachment( $filepath  );
     echo "<pre>";
    // var_dump($email);
     echo "</pre>";
		if( $email->Send()){
			echo "true";
			unlink($filepath) or die("Couldn't delete file");
		}else{
			echo "error:email";
		}


}else{
  //echo "ERROR: CHECK PARAMETERS";
	$filename= '../BackupNoImages' . date('Y-m-d_H:i:s') . '.zip';
	Zip('./',$filename);
    // require_once("./assets/dropphp/DropboxClient.php");
    // $dropbox = new DropboxClient(array(
		// 	'app_key' => "4wtyclmgjqdjp5o",
		// 	'app_secret' => "czi3nf0u1wic4ck",
		// 	'app_full_access' => false,
		// ),'en');
		//
    // $dropbox->SetAccessToken(unserialize('a:2:{s:1:"t";s:16:"ihlzevewlcekxu6b";s:1:"s";s:15:"7f0amps2skco8dt";}'));
    // $result = $dropbox->UploadFile($filename,'/Backup/' . basename($filename));
    // var_dump($result);
    // if($result->revision > 1){
    //     unlink($filename);
    //     event_log("AUTOMATIC_SYSTEM", 'FILE BACKUP', print_r($result, true));
    // }

}


 function Zip($source, $destination)
 	{
 		if (!extension_loaded('zip') || !file_exists($source)) {
 			return false;
 		}

 		$zip = new ZipArchive();
 		if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
 			return false;
 		}

 		$source = str_replace('\\', '/', realpath($source));

 		if (is_dir($source) === true)
 		{
 			$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

 			foreach ($files as $file)
 			{
 				$file = str_replace('\\', '/', $file);
 				if (stripos($file, "public_html/img/catalog/") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/admin/csv/") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/admin/uploads/") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/admin/downloads/") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/development") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/assets/PHPExcel") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/Classes/PHPExcel") > 0) {
 					continue;
 				}
                 if (stripos($file, "public_html/Classes/PHPExcel") > 0) {
 					continue;
 				}
 //				echo $file . "<br>";

 	//			 Ignore "." and ".." folders
 				if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
 					continue;

 				$file = realpath($file);

 				if (is_dir($file) === true)
 				{
 					$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
 				}
 				else if (is_file($file) === true)
 				{
 					$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
 				}
 			}
 		}
 		else if (is_file($source) === true)
 		{
 			$zip->addFromString(basename($source), file_get_contents($source));
 		}
         echo "Finish";
 		return $zip->close();
 	}
 ?>
