<?php


require_once('scripts/sb_functions.php');

if (!signed_in()){
  global $logged_in;
  $logged_in = logged_in( true, true );
}
else{
    global $blog_permissions;
    if (! $blog_permissions['setup']==1 ){ 
	redirect_to_url('index.php');
    }
}

//global $logged_in;
//$logged_in = logged_in( true, true );
	
read_config();	
sb_language( 'backup' );
	

html_header('',true);



if (!function_exists('scandir')) {
// if < php5
	function scandir($dir = './', $sort = 0)
	{
	   $dir_open = @ opendir($dir);
	   if (! $dir_open)
	       return false;
	   while (($dir_content = readdir($dir_open)) !== false)
	           $files[] = $dir_content;
	   if ($sort == 1)
 	      rsort($files, SORT_STRING);
 	  else
 	      sort($files, SORT_STRING);
 	  return $files;
	}
}


function page_content() {
		
    global $blog_config, $lang_string, $user_colors;
    global $theme_vars; 
				
    $entry_array = array();
    $entry_array['subject'] = $lang_string['title'];
			
    if ($_POST['submit']) {
//------- start
   #################################
  #       SPB Backup by Hydra       #
  #  http://samyweb.altervista.org  #
  #     samyweb<at>gmail<dot>com    #
   #################################
   
   // This file require a folder called '/class/' containing pclzip.lib.php and mail.lib.php.

  include_once('class/pclzip.lib.php');
  include_once('class/mail.lib.php');
  
  // archive's name and position.
  // You can change it.

  if (isset($_POST['archive_name']))
 	$archive_name = $_POST['archive_name']; 
  else $archive_name = 'backup';
  
  if ( $_POST['send_archive'] ) {

	  if ( isset($_POST['temp_folder']) )
	  	$temp_folder = $_POST['temp_folder'].'/';
 	  else $temp_folder = 'temp/';
  
	  // The directory doesn't exist, so that it gets created.
 	 if (!file_exists($temp_folder)) my_mkdir($temp_folder, 0777);
  
	  $archive_path = $temp_folder.$archive_name.'.zip'; 
  }

  if ( $_POST['save_archive'] ) {
	  if (isset($_POST['archive_folder']))
	  	$archive_folder = $_POST['archive_folder'].'/';
 	  else $archive_folder = 'backup/';
  
	  // The directory doesn't exist, so that it gets created.
 	 if (!file_exists($archive_folder)) my_mkdir($archive_folder, 0777);
  
	  $archive_path = $archive_folder.$archive_name.'.zip'; 
	  $timestamp = date (_Ymd_His);
	  if ( file_exists($archive_path) ) $archive_path = $archive_folder.$archive_name.$timestamp.'.zip';
  }

  // create array of folders to zip. The archive will contain also subdirectories.
  // You can change it (i.e. adding other folders).
  // WARNING!! Folders too big will cause function's timeout. It's better to make backup of little groups of folders...
  // For more details on pclzip's class http://www.phpconcept.net/pclzip/man/en/index.php?toc
  
  if ($_POST['dir_all']=='all') $array = './';
  else $array = $_POST['dir_array'];
  
  if ( $_POST['send_archive'] ) {
  
	  // the PKZIP archive $archive_name is created.
 	 $archive = new PclZip($archive_path);
 	 $v_list = $archive->create($array);
 	 if ($v_list == 0) {
 	   die("Error : ".$archive->errorInfo(true));
	  }
  
 	   // email the archive.
		$site = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);

		$blog_title = $blog_config[ 'blog_title' ];
		$from = $blog_config[ 'blog_email' ];
		$to = $from;
		$subject = $lang_string['email_subject'].': '.$blog_title; 
		$body = $lang_string['email_body']."\n".$site; 

		$attachment = fread(fopen($archive_path, "r"), filesize($archive_path));
		$mail = new mime_mail();
		$mail->from = $from;
		$mail->headers = "Errors-To: [EMAIL=".$from."]".$from."[/EMAIL]";
		$mail->to = $to;
		$mail->subject = $subject;
		$mail->body = $body;
		$mail->add_attachment("$attachment", $archive_name.'.zip', "Content-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAgEASABIAAD/7QT+UGhvdG9zaG");
		$mail->send();

		/* error
		if(!(@mail($to,$subject,$body, $headers))){ 
		print "<H5>E-mail failed.</H5>";}  
		else 	print "<H5>E-mail sent.</H5>";  
		*/
 
 	 //  all the files contained in the archive are removed (for security reasons).
 	 $v_list = $archive->delete();
 	 if ($v_list == 0) {
 	   die("Error : ".$archive->errorInfo(true));
 	 }
	//------- ending			

	 $table .= $lang_string['success_send']."<em>".$blog_config[ 'blog_email' ]."</em><br /><br /><a href=\"admin_backup.php\">".$lang_string['back']."</a>";
  }

  if ( $_POST['save_archive'] ) {
  // create array of folders to zip. The archive will contain also subdirectories.
  // You can change it (i.e. adding other folders).
  // WARNING!! Folders too big will cause function's timeout. It's better to make backup of little groups of folders...
  // For more details on pclzip's class http://www.phpconcept.net/pclzip/man/en/index.php?toc
  
  // the PKZIP archive $archive_name is created.
  $archive = new PclZip($archive_path);
  $v_list = $archive->create($array);
  if ($v_list == 0) {
    die("Error : ".$archive->errorInfo(true));
  }
//------- ending			

  $table .= $lang_string['success_save']."<em>".$archive_folder."</em>: <strong><a href=\"".$archive_path."\">".$archive_name.".zip</a></strong><br /><br /><a href=\"admin_backup.php\">".$lang_string['back']."</a>";
  }

			$entry_array['entry'] = $table; 
			}
			
// form			
			 else {

			$table = '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="backup">';
			$table .= $lang_string['instructions'].'<br /><br />';
			$table .= '<hr noshade size="1" color="#'.$user_colors['inner_border_color'].'">';
			
			$table .= '<input type="checkbox" id="send_archive" name="send_archive" checked="checked"> '.$lang_string['send_archive'].'<br />';
			$table .= '<input type="checkbox" id="save_archive" name="save_archive" checked="checked"> '.$lang_string['save_archive'].'<br /><br />'; 
			
			$table .= '<label for="archive_folder"> '.$lang_string['name_of_dir'].'</label><br />
						<input type="text" name="archive_folder" value="backup" autocomplete="OFF" size="40" style="width:90%;"><br /><br />';
			
			$table .= '<label for="temp_folder"> '.$lang_string['name_of_tempdir'].'</label><br />
						<input type="text" name="temp_folder" value="temp" autocomplete="OFF" size="40" style="width:90%;"><br /><br />';

			$table .= '<label for="archive_name"> '.$lang_string['name_of_archive'].'</label><br />
						<input type="text" name="archive_name" value="backup" autocomplete="OFF" size="40" style="width:90%;"><br /><br />';

			$table .= '<hr noshade size="1" color="#'.$user_colors['inner_border_color'].'">';

				
			$table .= '<label for="dir_array"> '.$lang_string['select_dir'].'</label><br />
						('.$lang_string['warning'].')<br /><br />';
			$table .= '<input type="checkbox" name="dir_all" value="all"> '.$lang_string['select_all'].'<br /><br />';

			$dir = scandir('./', 0);
			
						for ( $i = 0; $i < count( $dir ); $i++ ) {
							if ( is_dir($dir[$i]) && $dir[$i] != '.' && $dir[$i] != '..' ) {
								$table .= '<input type="checkbox" name="dir_array[]" value="'.$dir[$i].'"';
								if ( $dir[$i] == 'content' || $dir[$i] == 'config' || $dir[$i] == 'images' ) { $table .= ' checked="checked"'; }
								$table .= '> [ '.$dir[$i].' ]<br />';
							}						
						}
					
			
			$table .= '<hr noshade size="1" color="#'.$user_colors['inner_border_color'].'">';
			
			$table .= '<br /><input type="submit" name="submit" value="'.$lang_string['submit_btn'].'"></form>';
			
			
			
			$entry_array['entry'] = $table;
			}

			echo( theme_staticentry( $entry_array ) );
			
			
		}
		


theme_pagelayout();
html_footer();

?>
