<?php
/**
* XCloner
* Oficial website: http://www.joomlaplug.com/
* -------------------------------------------
* Creator: Liuta Romulus Ovidiu
* License: GNU/GPL
* Email: admin@joomlaplug.com
* Revision: 1.0
* Date: July 2007
**/

/*processing the Logout  task*/
function doLogout(){

	if(function_exists('session_unregister'))	{
			@session_unregister('clone');
		}
	
	unset($_SESSION['clone']);
	
	mosRedirect("index.php?option=com_cloner","");
	
}
/*Error printing function*/
function E_print($message){
	
	print "<center><font color='red' size='4'>$message</font></center>";
	
}

/*Simple redirect function*/
function mosRedirect($url, $msg=""){
	
	
	echo "<script type=\"text/javascript\">
	
	window.location = \"$url&mosmsg=".urlencode($msg)."\";
	
	</script>";
	
	exit;
	
}

function fdefault(){

  HTML_cloner::_FDefault();

}

function config($option){
    global $mosConfig_absolute_path, $_CONFIG, $config_file;


     if(@$_REQUEST['action']=='save'){
          #print_r($_REQUEST);exit;
          $databases_incl_list = "";
		  if(is_array($_REQUEST['databases_incl']))
		  foreach($_REQUEST['databases_incl'] as $database){
			
		  $databases_incl_list .= $database. ",";
		 	
		  }
		  if($fp = @fopen($config_file,'w')){

          $cfg = '<?'.'php'."\n";
          
          $cfg .= '$_CONFIG[\'license_code\']="'.$_REQUEST[license_code].'";'."\n";

          $cfg .= '$_CONFIG[\'backup_path\']="'.$_REQUEST[backup_path].'";'."\n";

          $cfg .= '$_CONFIG[\'clonerPath\']="'.$_REQUEST[clonerPath].'";'."\n";
          
          $cfg .= '$_CONFIG[\'jcuser\']="'.$_REQUEST[jcuser].'";'."\n";
          
          if($_REQUEST['jcpass'] == ''){
			
			$jcpass = $_CONFIG['jcpass'];		
			
		  }else{
			
			$jcpass = md5($_REQUEST['jcpass']);
			
		  }

          $cfg .= '$_CONFIG[\'jcpass\']=\''.$jcpass.'\';'."\n";
          
          $cfg .= '$_CONFIG[\'mysql_host\']="'.$_REQUEST[mysql_host].'";'."\n";

          $cfg .= '$_CONFIG[\'mysql_user\']="'.$_REQUEST[mysql_user].'";'."\n";

          $cfg .= '$_CONFIG[\'mysql_pass\']=\''.$_REQUEST[mysql_pass].'\';'."\n";

          $cfg .= '$_CONFIG[\'mysql_database\']="'.$_REQUEST[mysql_database].'";'."\n";
          
          $cfg .= '$_CONFIG[\'select_folders\']="'.$_REQUEST[select_folders].'";'."\n";
          
          $cfg .= '$_CONFIG[\'select_lang\']="'.$_REQUEST[select_lang].'";'."\n";
          
          $cfg .= '$_CONFIG[\'secure_ftp\']="'.$_REQUEST[secure_ftp].'";'."\n";

          $cfg .= '$_CONFIG[\'backup_compress\']="'.$_REQUEST[backup_compress].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_logemail\']="'.$_REQUEST[cron_logemail].'";'."\n";

          $cfg .= '$_CONFIG[\'cron_exclude\']="'.$_REQUEST[cron_exclude].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_send\']="'.$_REQUEST[cron_send].'";'."\n";

          $cfg .= '$_CONFIG[\'cron_btype\']="'.$_REQUEST[cron_btype].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_bname\']="'.$_REQUEST[cron_bname].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_ip\']="'.$_REQUEST[cron_ip].'";'."\n";

          $cfg .= '$_CONFIG[\'cron_ftp_server\']="'.$_REQUEST[cron_ftp_server].'";'."\n";

          $cfg .= '$_CONFIG[\'cron_ftp_user\']="'.$_REQUEST[cron_ftp_user].'";'."\n";

          $cfg .= '$_CONFIG[\'cron_ftp_pass\']=\''.$_REQUEST[cron_ftp_pass].'\';'."\n";

          $cfg .= '$_CONFIG[\'cron_ftp_path\']="'.$_REQUEST[cron_ftp_path].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_ftp_delb\']="'.$_REQUEST[cron_ftp_delb].'";'."\n";
          
          $cfg .= '$_CONFIG[\'databases_incl_list\']="'.$databases_incl_list.'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_sql_drop\']="'.$_REQUEST[cron_sql_drop].'";'."\n";

          $cfg .= '$_CONFIG[\'cron_email_address\']="'.$_REQUEST[cron_email_address].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_file_delete\']="'.$_REQUEST[cron_file_delete].'";'."\n";
          
          $cfg .= '$_CONFIG[\'cron_file_delete_act\']="'.$_REQUEST[cron_file_delete_act].'";'."\n";
          
          $cfg .= '$_CONFIG[\'mem\']="'.$_REQUEST[mem].'";'."\n";
          
          $cfg .= '$_CONFIG[\'backup_refresh\']="'.$_REQUEST[backup_refresh].'";'."\n";
          
          $cfg .= '$_CONFIG[\'refresh_time\']="'.$_REQUEST[refresh_time].'";'."\n";
          
          $cfg .= '$_CONFIG[\'backup_refresh_number\']="'.$_REQUEST[backup_refresh_number].'";'."\n";
          
          $cfg .= '$_CONFIG[\'sql_mem\']="'.$_REQUEST[sql_mem].'";'."\n";
          
          $cfg .= '$_CONFIG[\'enable_db_backup\']="'.$_REQUEST[enable_db_backup].'";'."\n";

          $cfg .= '$_CONFIG[\'zippath\']="'.$_REQUEST[zippath].'";'."\n";
          
          $cfg .= '$_CONFIG[\'tarpath\']="'.$_REQUEST[tarpath].'";'."\n";
  
          $cfg .= '$_CONFIG[\'sqldump\']="'.$_REQUEST[sqldump].'";'."\n"; 
          
          $cfg .= '$_CONFIG[\'system_dlink\']="'.$_REQUEST[system_dlink].'";'."\n";

	  $cfg .= '$_CONFIG[\'mosConfig_live_site\']="'.$_SERVER['HTTP_HOST'].'";'."\n";
          
          $cfg .= '$_CONFIG[\'system_ftptransfer\']="'.$_REQUEST[system_ftptransfer].'";'."\n";
          
          $cfg .= '$_CONFIG[\'system_mdatabases\']="'.$_REQUEST[system_mdatabases].'";'."\n";

	  $cfg .= '$_CONFIG[\'add_backups_dir\']="'.$_REQUEST[add_backups_dir].'";'."\n";

	
	  $cfg .= '$_CONFIG[\'cron_amazon_active\']="'.$_REQUEST[cron_amazon_active].'";'."\n";

	  $cfg .= '$_CONFIG[\'cron_amazon_awsAccessKey\']="'.$_REQUEST[cron_amazon_awsAccessKey].'";'."\n";

	  $cfg .= '$_CONFIG[\'cron_amazon_awsSecretKey\']="'.$_REQUEST[cron_amazon_awsSecretKey].'";'."\n";

	  $cfg .= '$_CONFIG[\'cron_amazon_bucket\']="'.$_REQUEST[cron_amazon_bucket].'";'."\n";	

	  $cfg .= '$_CONFIG[\'cron_amazon_dirname\']="'.$_REQUEST[cron_amazon_dirname].'";'."\n";		


          $cfg .= '?'.'>';

          fwrite($fp, $cfg);

          fclose($fp);
          
          $msg = LM_MSG_BACK_1;
          
          if($_REQUEST['cron_save_as'] != ""){
          
          $fcron = $_CONFIG['multiple_config_dir']."/".$_REQUEST['cron_save_as'].".php";
          
          if($fp = @fopen($fcron, "w")){
          	
          	fwrite($fp, $cfg);
          	
          	fclose($fp);
          	
          }
          else{
          	
          	$msg = "Unable to save  $fcron file, please make sure the folder is writeable!";
          	
          }
          
          }

        
          mosRedirect( 'index2.php?option='.$option."&task=config" , $msg);#exit;
          }
          else
          {
            $msg ="<font color='red'>ERROR... Unable to write to $config_file, please change the permissions!</font>";
            E_print($msg);
          }

      }
 HTML_cloner::Config($option);
}

### JoomlaCloner Language Manager
function translator($option){
     global  $mosConfig_absolute_path, $lang_array, $lang_dir;
     
    $langs = array();
	$cid = $_REQUEST['cid'];
    $files = $_REQUEST['files'];
    
    if(is_array($cid))
	foreach($cid as $value){
		
		$langs[] = $files[$value];
	
	}
         
	if($_REQUEST['task'] == 'del_lang'){
		
		foreach($langs as $lang){
			
			@unlink ($lang_dir."/".$lang.".php");
			
		}
		
		$msg = LM_LANG_MSG_DEL;
		mosRedirect( 'index2.php?option='.$option."&task=lang" , $msg);
		
	} 
	
	HTML_cloner::Translator($option, $lang_array);
}

function translator_add($option, $task){
     global  $_CONFIG, $lang_array, $lang_dir;
     
     if($task == 'add_lang_new') {
		
	 $lfile = $lang_dir."/".strtolower($_REQUEST['lname']).".php";
	 
	 if(file_exists($lfile)){
		
		$msg = "Language ".$_REQUEST['lname']." already exists!";
	}
	 else
	 if($fp = @fopen($lfile, 'w')){
			
         fwrite($fp, "");

		 fclose($fp);
		 
		 $msg = "Language ".ucfirst($_REQUEST['language'])." successfully added!";
			
		}
	    else{
			
		 $msg = "File $lfile could not be updated, please check it's permissions'!";	
		
		}	
	 
	 mosRedirect( 'index2.php?option='.$option."&task=lang" , $msg);
	 
	 }
    

     HTML_cloner::Translator_Add($option);
}     
function translator_edit($option, $task){
     global  $_CONFIG, $lang_array, $lang_dir;

   if(($task == 'save_lang')||($task == 'save_lang_apply')){
	
   $lfile = $lang_dir."/".$_REQUEST['language'].".php";
    
	if($_REQUEST['language'] == 'english'){
		
		if($fp = @fopen($lfile, 'w')){
			
         fwrite($fp, stripslashes($_REQUEST['def_content']));

		 fclose($fp);
		 
		 $msg = "Language ".ucfirst($_REQUEST['language'])." successfully updated!";
			
		}
	    else{
			
		 $msg = "File $lfile could not be created, please check it's permissions'!";	
		
		}
	if($task == 'save_lang_apply')
	mosRedirect( 'index2.php?option='.$option."&task=edit_lang&langx=".$_REQUEST['language'] , $msg);
	else
	mosRedirect( 'index2.php?option='.$option."&task=lang" , $msg);
		
	}
   else{

   $lfile = $_REQUEST['lfile'];
   $ldata = $_REQUEST['lang'];
   
   if($fp = @fopen($lfile, 'w')){
			
         fwrite($fp, "<"."?php\n");
         foreach($ldata as $key=>$value){
		
		  fwrite($fp, "define(\"".$key."\",\"".stripslashes($value)."\");\n");	 
		  	
		 }
         
         fwrite($fp, "?".">");

		 fclose($fp);
		 
		 $msg = "Language ".ucfirst($_REQUEST['language'])." successfully updated!";
			
		}
   else{
			
		 $msg = "File $lfile could not be updated, please check it's permissions'!";	
		
		}
		
		
   }		
   if($task == 'save_lang_apply')
	mosRedirect( 'index2.php?option='.$option."&task=edit_lang&langx=".$_REQUEST['language'] , $msg);
   else	
   mosRedirect( 'index2.php?option='.$option."&task=lang" , $msg);
   
   }
   
   $content = "";
   $lang = "";
   $cid = $_REQUEST['cid'];
   $files = $_REQUEST['files'];
    
   if(is_array($cid))
	foreach($cid as $value){
		
		$lang = $files[$value];
	
	}
   
   if($_REQUEST['langx']){
	
   $lang = $_REQUEST['langx'];
  	
   }
   
   if($fp = fopen($lang_dir."/english.php", 'r')){
	
    while(!feof($fp)){
		
		$content .= fread($fp, 1024);

	}
	
    fclose($fp);	
   
   }

   $file = $lang_dir."/".$lang.".php";
   $dfile = $lang_dir."/english.php";
 
   
   
   if($lang == 'english')  {
   
    HTML_cloner::Translator_Edit_DEFAULT($option, $content, $file, $lang);
   
   }
   else {
    
	$def_data = get_lang_data($dfile);
    $cur_data = get_lang_data($file);
	
	$data = array_merge($def_data, $cur_data);
	
	HTML_cloner::Translator_Edit($option, $data, $def_data, $file, $lang);
   
   } 

}

function get_lang_data($file){
	global $_CONFIG;
	
    if($fp = fopen($file, 'r')){
	
    while(!feof($fp)){
		
		$content .= fread($fp, 1024);

	}
	
    fclose($fp);	
   
   }	

   $data = array();
	
   $tmp = explode("define(\"", $content);
   foreach($tmp as $key=>$value){
	
	$temp = explode("\",", $value);
	
	$tst = explode("\");", $temp[1]);
	$data[$temp[0]] = substr(trim($tst[0]), 1, strlen($tst[0])); 

  }
   
 return $data;

}

function get_avalaible_langs(){
     global  $_CONFIG, $lang_dir;

  $lang_arr = array();
  
  if ($handle = @opendir($lang_dir)) {

   /* This is the correct way to loop over the directory. */
   while (false !== ($file = readdir($handle))) 
   if(($file != 'index.html') &&($file != '..')&&($file != '.'))
   {
       $lang_arr[] = str_replace(".php", "", $file);
   }

   closedir($handle);
  
  }
	
return $lang_arr;	
}

function showBackups( $option ) {
    // ----------------------------------------------------------
    // Generate a selectable list of the files in Backup Folder
    // ----------------------------------------------------------
    global  $_CONFIG;

    if(!is_dir($_CONFIG['clonerPath'])){
        E_print( "Your backup directory ".$_CONFIG['clonerPath']." is not correct! Unable to continue...");
        return ;
        }

    # initialise list arrays, directories and files separately and array counters for them
    $d_arr = array(); $d = 0;
    $f_arr = array(); $f = 0;
    $s_arr = array(); $s = 0;

    # obtain the list of backup archive files
    getBackupFiles($d_arr, $f_arr, $s_arr, $d, $f);

    # load presentation layer
    HTML_cloner::showBackups( $f_arr, $s_arr, $_CONFIG['clonerPath'], $option );
}

function moveBackup($option){
         global $task;
    $files_out = array();
    if(is_array($_REQUEST[files]))
    {

    foreach($_REQUEST[files] as $key=>$value)
    if($_REQUEST[cid][$key]!="")
      $files_out[] = $value;
    }

    if($_REQUEST['action']=="connect"){

      $ret = start_connect($_REQUEST[files]);

    }
    if(!$ret)
    HTML_cloner::TransferForm($option, $files_out);
}

function start_connect($files){
    global $task, $_CONFIG;


if(($_REQUEST[task]=='move')||($_REQUEST[task2]=='move')){
    }
else{
$source_file[0] = "restore/XCloner.php";
$destination_file[0] = $_REQUEST[ftp_dir]."/XCloner.php";

$source_file[1] = "restore/TAR.php";
$destination_file[1] = $_REQUEST[ftp_dir]."/TAR.php";

}

foreach($files as $file){
 $source_file[] = $_CONFIG['clonerPath']."/".$file;
 $destination_file[] = $_REQUEST[ftp_dir]."/".$file;
 }



list($fhost, $fport) = explode(":",$_REQUEST[ftp_server]);
if($fport == "")
 $fport = '21';
 
$ftp_timeout = '3600';
// set up basic connection
if(!$_CONFIG[secure_ftp]){
$conn_id = ftp_connect($fhost, (int)$fport, (int)$ftp_timeout);
$connect = "Normal";
}
else{
$conn_id = ftp_ssl_connect($fhost, (int)$fport, (int)$ftp_timeout);
$connect = "Secure";
}

// login with username and password
$login_result = @ftp_login($conn_id, $_REQUEST[ftp_user], $_REQUEST[ftp_pass]);

// check connection
if ((!$conn_id) || (!$login_result)) {
       echo "<b  style='color:red'>".LM_MSG_BACK_2."</b>";
       echo "<b  style='color:red'>Attempted to connect to ".$_REQUEST[ftp_server]." for user ".$_REQUEST[ftp_user]."</b>";
       return;
   } else {
       #echo "Connected to $_REQUEST[ftp_server], for user $_REQUEST[ftp_user]";
   }

if($_CONFIG[system_ftptransfer]==1)
{
// turn passive mode on
@ftp_pasv($conn_id, true);
$mode = "Passive";
}
else
{
// turn passive mode off
@ftp_pasv($conn_id, false);
$mode = "Active";
}

echo "Connected to $connect ftp server <b>$_REQUEST[ftp_server] - $mode Mode</b><br />";
for($i=0;$i<sizeof($source_file);$i++)
{
echo "<br />Moving source file <b>".$source_file[$i]."</b>";
// upload the file
if(!$_REQUEST['ftp_inct']){

 $ret = ftp_put($conn_id, $destination_file[$i], $source_file[$i], FTP_BINARY);

 if ($ret)  echo "<br /><b>Upload success to <i>$destination_file[$i]</i> ...<br /></b>";
 else {echo "<b style='color:red'>FTP upload has failed for file $destination_file[$i] ! Stopping ....<br /></b>";return;}
	    

}

if($_REQUEST['ftp_inct']){
 
 $size = filesize($source_file[$i]);
 $dsize = ftp_size($conn_id, $destination_file[$i]);
 $perc = sprintf("%.2f", ($dsize*100)/$size);

 echo " - uploaded $perc% from $size bytes <br>";
 $ret = ftp_nb_put($conn_id, $destination_file[$i], $source_file[$i], FTP_BINARY , FTP_AUTORESUME);



// check upload status
if ($ret == FTP_FAILED ) {
       echo "<b style='color:red'>FTP upload has failed for file $destination_file[$i] ! Stopping ....<br /></b>";return;
       
   } else {
       
	   $j = 1;
       
	   while ($ret == FTP_MOREDATA) {
  
       // Do whatever you want
       #echo ". ";

   
       // Continue uploading...
       $ret = ftp_nb_continue($conn_id);
   
       if($j++ % 500 == 0){

        @ftp_close($conn_id);

		echo "<script>
		var sURL = unescape('".$_SERVER[REQUEST_URI]."');

	    function refresh()
	    {
	    //  This version of the refresh function will cause a new
	    //  entry in the visitor's history.  It is provided for
	    //  those browsers that only support JavaScript 1.0.
	    //
	    window.location.href = sURL;
	    }

		setTimeout( \"refresh()\", 2*1000 );
		
		</script>";
        return 1;
	    
		break;

	   }
     
	 }
	   if ($ret == FTP_FINISHED) {
	    echo "<b>Upload success to <i>$destination_file[$i]</i> ...<br /></b>";
   }

 }
 
}

}
// close the FTP stream
@ftp_close($conn_id);

$redurl = $_REQUEST[ftp_url]."/XCloner.php";

if((substr($redurl, 0, 7)!= "http://") && (substr($redurl, 0, 8)!="https://") )
  $redurl = "http://".trim($redurl);
  
if($_REQUEST['ftp_inct']){

 if($_REQUEST['refresh_done'] != 1){
 echo "<script>
		var sURL = unescape('".$_SERVER[REQUEST_URI]."&refresh_done=1');

	    function refresh()
	    {
	    //  This version of the refresh function will cause a new
	    //  entry in the visitor's history.  It is provided for
	    //  those browsers that only support JavaScript 1.0.
	    //
	    window.location.href = sURL;
	    }

		setTimeout( \"refresh()\", 2*1000 );
		
		</script>";
		return 1;
 }

}
else{
	
  $_REQUEST['refresh_done'] = 1;
	
}
if($_REQUEST['refresh_done'] == 1)

 if(($_REQUEST[task]=='move')||($_REQUEST[task2]=='move')){
    echo "<br><br><h2>".LM_MSG_BACK_3."</h2>"; return 1;
    }
 else{
    echo "<br><br><h2>".LM_MSG_BACK_4." <br /><a href='".$redurl."'>click here to continue...</a></h2>";  return 1;
 }


 return 0;

}
function copyls($source, $dest){

    $handle = fopen($dest, "a");
    if($handle){
    fclose($handle);
    return 1;
    }
    else{
        return 0;
        }

    }

function action($option){
  global $_CONFIG;
  
  $userfile = $_CONFIG['baDownloadPath']."/".$_REQUEST[file];
  $localfile = $_CONFIG['clonerPath']."/".$_REQUEST[file];
          

  if($_REQUEST[action]=='delete'){

    if(@unlink($userfile))
     $msg = LM_MSG_BACK_5;
    else
      $msg = LM_MSG_BACK_6;
    }
  elseif($_REQUEST[action]=='copy'){

    if(@copyls($localfile, $userfile))
     $msg = LM_MSG_BACK_7;
    else
     $msg = LM_MSG_BACK_8;
    }
   mosRedirect( 'index2.php?option='.$option."&task=view" , $msg);

}

function clone_rename($option){
     global $_CONFIG;
     
     if($_REQUEST[task] == 'rename_save'){
         $files = $_REQUEST['cfile'];
         foreach($files as $key=>$value){
             @rename($_CONFIG[clonerPath]."/".$_REQUEST[cfile][$key],$_CONFIG[clonerPath]."/".$_REQUEST[dfile][$key]);
             @rename($_CONFIG[baDownloadPath]."/".$_REQUEST[cfile][$key],$_CONFIG[baDownloadPath]."/".$_REQUEST[dfile][$key]);
             }

         $msg = LM_MSG_BACK_9;
         mosRedirect( 'index2.php?option='.$option."&task=view" , $msg);
         }

     $files = array();

     foreach($_REQUEST[cid] as $key=>$value){
         $files[] = $_REQUEST[files][$key];
         }



    HTML_Cloner::Rename($files, $option);

    }
function downloadBackup($file){

    global $_CONFIG;
    
    $file = $_CONFIG['clonerPath']."/$file";

   //First, see if the file exists
   if (!is_file($file)) { die("<b>404 File $file was not found!</b>"); }

   //File Info
   $len = filesize($file);
   $filename = basename($file);
   $file_extension = strtolower(substr(strrchr($filename,"."),1));

   //Setam Content-Type-urile pentru  fisierul in cauza
   switch( $file_extension ) {
     case "pdf": $ctype="application/pdf"; break;
     case "exe": $ctype="application/octet-stream"; break;
     case "zip": $ctype="application/zip"; break;
     case "doc": $ctype="application/msword"; break;
     case "xls": $ctype="application/vnd.ms-excel"; break;
     case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
     case "gif": $ctype="image/gif"; break;
     case "png": $ctype="image/png"; break;
     case "jpeg":
     case "jpg": $ctype="image/jpg"; break;
     case "mp3": $ctype="audio/mpeg"; break;
     case "wav": $ctype="audio/x-wav"; break;
     case "mpeg":
     case "mpg":
     case "mpe": $ctype="video/mpeg"; break;
     case "mov": $ctype="video/quicktime"; break;
     case "avi": $ctype="video/x-msvideo"; break;

     default: $ctype="application/force-download";
   }

   //Writing Headers
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: public");
   header("Content-Description: File Transfer");
 
   //Content-Type-ul
   header("Content-Type: $ctype");

   //Force Download
   $header="Content-Disposition: attachment; filename=".$filename.";";
   header($header );
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$len);
   @readfile($file);
   exit;
}
    

function confirmBackup( $option ) {
    // ----------------------------------------------------------
    // Routine to display a confirmation screen prior to backup
    // containing the selectable folders and a confirmation for
    // backing up the database
    // ----------------------------------------------------------
    global $_CONFIG;

    if(!is_writeable($_CONFIG['clonerPath'])){
        E_print( "Your backup directory $_CONFIG[clonerPath] is not writeable or does not exists(Backup Store Path:)! <br />Please note, the ./administrator/backups/ folder needs to be created and made writeable!");
        return;
        }

    # Initialise list arrays, directories and files separately and array counters for them
    $excludedFolders = array();
    $d_arr = array(); $d = 0;
    $ds_arr = array();
    $f_arr = array(); $f = 0;
    $s_arr = array(); $s = 0;
    $d_arr[$d] = $_CONFIG['backup_path'];

    #if($_CONFIG['select_folders']==0){
    # obtain the list of folders by recursing the mambo file store
    #recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $excludedFolders, '');
    #}

    if($_CONFIG['select_folders'] == 2)
	if($fp = @fopen($_CONFIG['exfile'], 'w')){

        $excl = explode("\r\n", $_CONFIG['cron_exclude']);
        foreach($excl as $value)
		if($value != ""){
		
        $value = str_replace($_CONFIG['backup_path'], "", $value);
        if($value [0] !='/')
	     $value = '/'.$value;

 	    if((substr($value, strlen($value)-1, 1) == '/')&&(strlen($value)!=1))  
   	     $value = substr($value, 0 , strlen($value)-1); 
		
		fwrite($fp, $value."\r\n");
		}
        fclose($fp);
        }

    # load presentation layer
    if($option != 'nohtml')
    HTML_cloner::confirmBackups( $d_arr, $ds_arr, $_CONFIG['clonerPath'], $option );
    else
    return $d_arr;
}


function deleteBackups( $cid, $option ) {
    // ----------------------------------------------------------
    // Routine to delete the Backup Sets selected in the list
    // backup sets screen
    // ----------------------------------------------------------
    global $_CONFIG;


    # Cycle through all the selected Backups and Deleted them
    foreach ($cid as $key=>$value) {

        $file = $_CONFIG['clonerPath'].'/'. $_REQUEST['f'.$value];


        if ( unlink( $file ) ) {
            $msg .= $_REQUEST['f'.$value].' - '.LM_DELETE_FILE_SUCCESS;
        } else {
            $msg .= $_REQUEST['f'.$value].' - '.LM_DELETE_FILE_FAILED    ;
        }
    }

    # redirect to list screen
    #$msg = _DELETE_FILE_SUCCESS;
    mosRedirect( 'index2.php?option='.$option."&task=view" , $msg );
}

function generateBackuprefresh($cid, $option, $backup_file){
	
	global $_CONFIG;
	global $databases_incl, $back_path, $sql_file, $perm_file, $htaccess;

	$perm_file = $_CONFIG['backups_dir']."/perm.txt";
    

	$lines = $_REQUEST['lines'];
	
	$fperm = fopen($perm_file,"r");
    
    	
	$startf = (int)$_REQUEST['startf'];
	
	$endf = $startf + $_CONFIG['backup_refresh_number'];
    
	$excluded_cmd = "";
	
	if($fp = @fopen($_REQUEST['excl_manual'], "r")){
       	
       	while(!feof($fp))
		  $excluded_cmd .= fread($fp, 1024);
       	
       	fclose($fp);
       }
	
  
    $url = "index2.php?option=com_cloner&task=refresh&startf=$endf&lines=$lines&backup=$backup_file&excl_manual=".$_REQUEST['excl_manual'];
	
	
	if($endf>=$lines)
	 $endf = $lines;
	else 
	 echo "Continue link: <a href='$url'>click here&gt;&gt;</a><br />";
	
	 if((int)$lines!=0)
	$percent = sprintf("%d",($endf*100)/(int)$lines);
	
	
	echo "Total process: $percent% out of $lines files<br />";
	echo "Processing files $startf to $endf for backup file $backup_file!<br />";
	echo "Current backup size: ".getFileSizeText(filesize($backup_file))."<br /><br />";
	
	chdir($_CONFIG['backup_path']);
    
	if($fperm){
	while ((!feof($fperm))&&($line < $endf)) {
      
	  $line++;
       
      $buffer = explode("|", fgets($fperm, 4096));
       
      if(($line < $endf) && ($line >= $startf)){
       
       
       echo $inc_message = "<br />$line. ";
           
       if(($buffer[0]!="")&&(is_file($buffer[0]))){
       
        $file = $buffer[0];
       
        ### appending files
        echo $inc_message = "file - $file";
              
        
		if(!$_CONFIG['mem']){
        #### CREATE BACKUP USING TAR LIBRARIES
        $file_path_full = $buffer[0];
        
		{

           #$return = PclTarAddList($backup_file, array($file_path_full), '', $_CONFIG['backup_path'], "tar");
           $tar_object = new Archive_Tar($backup_file);
		   $return = $tar_object->addModify(array($file_path_full), '', $_CONFIG['backup_path']);

        }
		
		#### END TAR #########################
		}		
        else{
        #### CREATE BACKUP USING SERVER UTILITIES
        
		  $file = str_replace(" ","\ ", $file);
		  
	      exec($_CONFIG[tarpath]." $excluded_cmd -".$_CONFIG['tarcompress']."vf $backup_file --update $file");
        
        
		#### END SERVER UTILITIES
		}
		
		}
        else{
        	
        	echo $inc_message = "directory - <b>".$buffer[0]."</b>";
        	
        }
              
      }
    
	}
   

   #exit; 
    
    echo "<br />New backup size: ".getFileSizeText(filesize($backup_file))."<br />";
    
    
    if($endf>=$lines){
    	
    	## ALL DONE
    	echo LM_MSG_BACK_11;
    	@fclose($fperm);
    	
    	
    }
    else{
    	## REDIRECTING
    	@fclose($fperm);  
    	    	
    	echo "<br>All done, redirecting in ".$_CONFIG['refresh_time']." seconds, or <a href='$url'>click here </a>";
    	
    	echo "
    	<script language='javascript'>
    	function redirect(){
    	window.location = '".$url."';
    	}
    	
    	setTimeout(\"redirect()\",".$_CONFIG['refresh_time']."000);
    	
    	</script>";
    	
    	
    }
	
	}
	else{
		
		echo "Unable to continue, could not open file $perm_file for reading!";
		
	}
    
    if($endf>=$lines){
	$back_path = $_CONFIG['backups_dir'];   
	@unlink($back_path."/database-sql.sql");
    @unlink($back_path."/perm.txt");
    @unlink($back_path."/htaccess.txt");
    @unlink($back_path."/.excl");
    @unlink($back_path."/.excl_tar");
	}
	return; 
   
}


function generateBackup( $cid, $option ) {
    // ----------------------------------------------------------
    // Routine to generate recurse a folder structure and record
    // the files, their sizes and parent folders
    // ----------------------------------------------------------
    global $_CONFIG;


    # generate database backup if required
    
    if(!$_CONFIG['enable_db_backup']){
     
        $backupDatabase = 0;
     
    }else{
		
		$backupDatabase = $_REQUEST['dbbackup'];
		
	}

    if($_REQUEST[cron_access]){

        $_CONFIG['backup_refresh'] = 0;
		
		$_REQUEST['dbbackup_drop'] = $_CONFIG['cron_sql_drop'];

        if($_CONFIG[cron_btype] == '1')
          $backupDatabase = 0;
        }


    if(!is_writeable($_CONFIG['clonerPath'])){
        E_print( "Your backup directory ".$_CONFIG['clonerPath']." is not writeable or does not exists!");
        return;
        }
    if(!is_writeable($_CONFIG['backups_dir'])){
        E_print( "Required sql backup directory ".$_CONFIG['backups_dir']." is not writeable or does not exists!");
        return;
        }


    # generate the backup set filename
    #$urlinfo = parse_url($mosConfig_live_site);
    #$domainname = strtolower(str_replace('.','_',$urlinfo[host]));
    #$domainname= $_SERVER['HTTP_HOST'];
    $domainname= $_CONFIG['mosConfig_live_site'];
    
        
    if(($_CONFIG['mem']) && ($_CONFIG['backup_refresh'])){
		
		$f_ext = '.tar';
		$_CONFIG['tarcompress'] = '';
		
	}elseif($_CONFIG['backup_compress']){
		
		$f_ext = '.tgz';
		$_CONFIG['tarcompress'] = 'z';
		
	}else{
		
		$f_ext = '.tar';
		$_CONFIG['tarcompress'] = '';
	}
	
	/*if($_CONFIG['backup_refresh']){
     $f_ext = ".tar";
    }
    elseif($_REQUEST[cron_dbonly]){
	 $f_ext = ".tar";
	}
	else{
	 $f_ext = ".tgz";
	}*/

    if($_REQUEST['bname']==""){

    if($backupDatabase == 1){
      if($_REQUEST['dbbackup_drop']){
       $filename1  = 'backup_'.date("Y-m-d_H-i").'_'.$domainname.'-sql-drop'.$f_ext;
       }
      else{
       $filename1  = 'backup_'.date("Y-m-d_H-i").'_'.$domainname.'-sql-nodrop'.$f_ext;
       }
      }
    else
      $filename1  = 'backup_'.date("Y-m-d_H-i").'_'.$domainname.'-nosql'.$f_ext;
    }
    else{
      $filename1 = $_REQUEST['bname'].$f_ext;
      }
     
     $sql_file = array();

    if ($backupDatabase == 1) {
        $tables = array(); $tables[0] = 'all';
        $excltables = $_REQUEST['excltables'];

        $sql_file[] = doBackup($tables, 'sql', 'local', 'both', $_SERVER['HTTP_USER_AGENT'], $_CONFIG['backups_dir'], $databaseResult, 'database' , $excltables, $_CONFIG['mysql_database']);
        
        $databaseResult = "<b>".$_CONFIG['mysql_database'].":</b> ".$databaseResult;
		############## ADD multiple databases #######################
		$databases_incl = $_REQUEST['databases_incl'];
        
        if(is_array($databases_incl)){
         foreach($databases_incl as $database_name)
		 if($database_name!=''){
			
			$excltables = ""; 
			mysql_query("USE  $database_name");
		
			$sql_file[] = doBackup($tables, 'sql', 'local', 'both', $_SERVER['HTTP_USER_AGENT'], $_CONFIG['backups_dir'], $databaseResult_incl, $database_name , $excltables, $database_name);
			
			$databaseResult .= "<br /> <b>$database_name:</b> ".$databaseResult_incl;
			
		 }
        
		mysql_query("USE  ".$_CONFIG['mysql_database']);

		}
            

    } else {
        $databaseResult = LM_DATABASE_EXCLUDED;
    }


	$excluded = array();
	
	$data = "";

    $excluded[] = $_CONFIG['backups_dir']."/".$filename1;

    if($_REQUEST['cron_access']){
		
		$data = $_CONFIG['cron_exclude'];
		
	}
	elseif($_CONFIG[select_folders] == 1){
    
	    $data = $_REQUEST['exluded'];
    
	}
	
    ##### START THE EXCLUSION FILTER
    if(($fp = @fopen($_CONFIG['exfile'],"r"))&&(!$_REQUEST['cron_access'])){

     while(!feof($fp)){
      $data .= fread($fp, 1024);
     }
    fclose($fp);
    }
    
	if(sizeof($_REQUEST[cid])>0)
	 $data .=implode("\r\n", $_REQUEST[cid]);
	 
	$_COOKIES = explode("\r\n", $data);
	
	
   if(is_array($_COOKIES))
     foreach($_COOKIES as $key=>$value)
	 if($value != ""){
     
	$value = str_replace($_CONFIG['backup_path'], "", $value);
    if($value [0] !='/')
	  $value = '/'.$value;

	if((substr($value, strlen($value)-1, 1) == '/')&&(strlen($value)!=1))  
	 $value = substr($value, 0 , strlen($value)-1);
	 
	if(($value!="")&&($value!='/administrator/backups')){
      
        if(trim($value) != '/administrator')
             $excluded[] = str_replace("//","/",$_CONFIG['backup_path']."/".$value);
        else{
             if ($handle = opendir($_CONFIG['backup_path'].'/administrator')) {
               while (false !== ($file = readdir($handle))) {
                 if ($file != "." && $file != ".." && $file != "backups") {
                   $excluded[] = str_replace("//","/",$_CONFIG['backup_path']."/administrator/".$file);
                 }
               }
             closedir($handle);
             }

        }

    }

   }
    #### END EXCLUSION FILTER
  
    if(!$_CONFIG['add_backups_dir'])
        {
        # initialise list arrays, directories and files separately and array counters for them
        $d_arr = array(); $d = 0;
        $f_arr = array(); $f = 0;
        $s_arr = array(); $s = 0;

        getBackupFiles($d_arr, $f_arr, $s_arr, $d, $f);;
        $i = 0;
        while($i<sizeof($f_arr)){

         $excluded[] = $_CONFIG['clonerPath']."/".$f_arr[$i];
         $i++;

         }

        }
    # obtain list of folders included in the backup
    $includeFolder = array();

    #print_r($excluded);exit;
    
    #$includeFolder = confirmBackup('nohtml');

    #initialise list arrays, directories and files separately and array counters for them
    $d_arr  = array(); $d = 0;
    $ds_arr = array();
    $f_arr  = array(); $f = 0;
    $s_arr  = array(); $s = 0;$mdir=0;

    $perm_file = $_CONFIG['backups_dir']."/perm.txt";
    @unlink($perm_file);
    $fperm = fopen($perm_file,"w");
    
	for($i=0; $i<sizeof($excluded); $i++ ){
		
		$excluded[$i] = str_replace("//", "/", $excluded[$i]);
	}
    #print_r($excluded);exit;
    # obtain the list of files by recursing the mambo file store
    recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $includeFolder, '', $excluded, $fperm);

    @fclose($fperm);

    @chmod($perm_file, 0777);


    # format total archive size
    $originalSize = getFileSizeText($s);

    # extend the file locations to include the full path

    for( $i=0; $i < count( $f_arr ); $i++ ) {

        if(!file_exists($f_arr[$i])){
         unset($f_arr[$i]);
        }


    }


    
    
    #if((file_exists($perm_file))&&(!in_array($perm_file, $f_arr)))
    # $f_arr[] = $perm_file;


    # add sql file to the backup

    if($_REQUEST[cron_access]){
    
    if(is_array($sql_file))
	 foreach($sql_file as $key=>$sqlfile)
      if(($backupDatabase == 1)&&(!in_array($_CONFIG['backups_dir'].$sqlfile, $f_arr)))
        $f_arr[] = $sqlfile;
    
	} 



    $filename   = $_CONFIG['clonerPath'].'/'.$filename1;

    $f_arr = array_unique($f_arr);

    # create the Zip file from the fileset array
    @unlink($filename);
      
    $perm_lines = 0;
    
	$handle = @fopen($perm_file, "r");
    
    if ($handle) {
    
        while (!feof($handle)) {
          fgets($handle, 4096);
          $perm_lines++;
         }
    
      fclose($handle);	
    
     }      
   
    
    if(!$_CONFIG['mem']){
    #### START TAR MANUAL MODE ############################################################

      if($_CONFIG['backup_refresh']){
       
	  	$f_arr = array($_CONFIG['backups_dir']."/index.html");
      
      }
	  
 
      if($_CONFIG['backup_refresh'])
           $tar_type= "tar";
      else
	       $tar_type= "tgz"; 
		   
	    
	  $tar_object = new Archive_Tar($filename, compression($tar_type));
	  #$tar_object->setErrorHandling(PEAR_ERROR_PRINT);  // Optional error handling
	  $return = $tar_object->addModify($f_arr, '', $_CONFIG['backup_path']);

      if($return != 1){

           echo "Backup failed using tar mode, error code $return!";

      }
       
      
      
 	  if($_CONFIG['backup_refresh']){
       
       echo "Starting the manual backup process!<br />";
       echo "Database backup: ".$databaseResult."<br /><br />";
	   
	   if(file_exists($filename))
        {
    	
		   echo "Backup $filename created, we may continue!<br />";
		   #echo "Database backup: ".$databaseResult ."<br />";
    	   echo "<a href='index2.php?option=com_cloner&lines=".$perm_lines."&task=refresh&backup=$filename&excl_manual=$excl_manual'>Please click here to continue!</a>";
    	   return;
        
		}
        else
        {
    	
		   E_print( "Backup failed, please check your tar server utility support!"); 
		   return ;
        
		}
      
      }	  
	  
	  
    #### END TAR MODE ############################################################
    }
    else{
      #### CREATE BACKUP USING SERVER UTILITIES
        $i=0;
        chdir($_CONFIG['backup_path']);

        $excl_files = "";
        while($i<sizeof($excluded)){
            $file = $excluded[$i];
            $file = str_replace($_CONFIG['backup_path'],"",$file);
            $file = "##".$file;
            $file = str_replace("##//","",$file);
            $file = str_replace("##/","",$file);
            $file = str_replace("##","",$file);
            
            $excl_files .= "/".$file."* ";
            
            $i++;
         }
         
		 
        chdir($_CONFIG['backup_path']);
        
        $excl_cmd = "";
        
        if($fp = fopen($_CONFIG['exfile_tar'], "w")){
         $i = 0;
         while($i<sizeof($excluded)){
            $file = $excluded[$i];
            $file = str_replace($_CONFIG['backup_path'],"",$file);
            $file = "##".$file;
            $file = str_replace("##//","",$file);
            $file = str_replace("##/","",$file);
            $file = str_replace("##","",$file);

            $excl_cmd .= " --exclude=./".$file." ";

            $excl_files = "./".$file."\r\n";
            fwrite($fp, $excl_files);
            $i++;
            }

         fclose($fp);
        }
        
        
        
       if($_CONFIG['backup_refresh']){
       ########## STARTING THE MANUAL BACKUP ######################################################
      
       
       $exclude = $excl_cmd ." -X ".$_CONFIG['exfile_tar'];
       
       $excl_manual = $_CONFIG['exfile_tar']."_manual";
       
       if($fp = fopen($excl_manual, "w")){
       	
       	fwrite($fp, $exclude);
       	
       	fclose($fp);
       }
       
	   	
    	
       echo "Starting the manual backup process!<br />";
       echo "Database backup: ".$databaseResult."<br /><br />";  
    
       chdir($_CONFIG['backup_path']);
    
       #$filename = str_replace(".tgz",".tar",$filename);
    
       exec($_CONFIG[tarpath]." $exclude -c".$_CONFIG['tarcompress']."vf $filename ./administrator/backups/index.html");
       exec($_CONFIG[tarpath]." -".$_CONFIG['tarcompress']."vf $filename --update ./administrator/backups/database-sql.sql");
       exec($_CONFIG[tarpath]." -".$_CONFIG['tarcompress']."vf $filename --update ./administrator/backups/htaccess.txt");
       exec($_CONFIG[tarpath]." -".$_CONFIG['tarcompress']."vf $filename --update ./administrator/backups/perm.txt");
    
       if(file_exists($filename))
       {
    	   echo "Backup $filename created, we may continue!<br />"; 
    	   #echo "Database backup: ".$databaseResult."<br />" ;
    	   echo "<a href='index2.php?option=com_cloner&lines=".$perm_lines."&task=refresh&backup=$filename&excl_manual=$excl_manual'>Please click here to continue!</a>";
    	   return;
       }
       else{
    	   E_print( "Backup failed, please check your tar server utility support!"); return ;
       }
         
        return;	
    	
        }
        
       
        if($_REQUEST[cron_dbonly]!=1){

         exec($_CONFIG[tarpath]." $excl_cmd "." -X ".$_CONFIG['exfile_tar']."  -chv".$_CONFIG['tarcompress']."f $filename ./");

        }else{

		 exec($_CONFIG[tarpath]."  -".$_CONFIG['tarcompress']."cvf $filename ./administrator/backups/database-sql.sql");
         
         if(is_array($databases_incl)){
             foreach($databases_incl as $database_name)
				 if($database_name!="") {
	                exec($_CONFIG[tarpath]."  -".$_CONFIG['tarcompress']."vf $filename --update  ./administrator/backups/".$database_name."-sql.sql");
			     }
         }

        }

        #######END
        
    
	chdir($_CONFIG['script_path']);
    #### END
    }

    if(!file_exists($filename)){
        if($_CONFIG[archive_type]==1)
        echo LM_MSG_BACK_12;
        else
        echo LM_MSG_BACK_13;
        return;
        }
    # format the compressed size of the fileset
    $archiveSize = getFileSizeText(filesize($filename));



    # load presentation layer
    if($option!='nohtml'){
   
     HTML_cloner::generateBackup($filename1, $archiveSize, $originalSize, $mdir, $f, $databaseResult, $option );
   
    }else{
    	
    logxx(HTML_cloner::generateBackup_text($filename1, $archiveSize, $originalSize, $mdir, $f, $databaseResult, $option ));
   	    	
    }
    
	if(is_array($databases_incl)){
         foreach($databases_incl as $database_name){
          @unlink($_CONFIG['backups_dir']."/".$database_name."-sql.sql");
		  }
    }      
	@unlink($_CONFIG['backups_dir']."/database-sql.sql");
    @unlink($sql_file);
    @unlink($perm_file);
    @unlink($htaccess);
    @unlink($_CONFIG['exfile']);
    @unlink($_CONFIG['exfile_tar']);
    return  $filename1;


}

function showHelp( $option ) {
    // ----------------------------------------------------------
    // Display the Help Screen
    // ----------------------------------------------------------

    # load presentation layer
    HTML_cloner::showHelp( $option );
}


function multidimsort(&$a)
{
   sort($a);
   $c = count($a);
   for($i = 0; $i < $c; $i++)
       if (is_array($a[$i]))
           multidimsort($a[$i]);
}

function compression($ext){
	
	switch ($ext){
	   
		case 'tar': $compresion = 'false'; break;

		case 'tgz': $compresion = 'true'; break;
		
		default: $compresion = 'false'; break;
		
	}	
	
	return $compression;
}

function recurseFiles(&$d_arr, &$ds_arr, &$f_arr, &$s_arr, &$d, &$f, &$s, &$includedFolders, $path, $excluded=array(), $fperm = '') {
    // ----------------------------------------------------------
    // Routine to recurse a folder structure and record the files
    // their sizes and parent folders
    // ----------------------------------------------------------
    global $mdir, $_CONFIG;

    $currentfullpath = $_CONFIG['backup_path'].$path;

    # Open possibly available directory
    if((is_dir( $currentfullpath ) )&&(!is_link($currentfullpath))) {
        if( $handle = @opendir( $currentfullpath ) ) {
            while( false !== ( $file = readdir( $handle ) ) ) {
                # Make sure we don't push parental directories or dotfiles (unix) into the arrays
                if( ($file != "." && $file != "..") ) {
                    $exc =0;
                    $cfile = $currentfullpath . "/" . $file;
                    if(sizeof($excluded)!=0)
                            {
                               foreach($excluded as $key=>$value){
                                
								  if(($value!='')&&(!$exc)){
    	                            $cfile = str_replace("//", "/", $cfile);
		                                if((strstr($cfile, $value)!='')||(strstr($cfile."/", $value)!='')){
		                                  $exc=1; break;
                                		}
                              	  }
							   }
							}
                    if(( @is_dir( $cfile ) ) && (!@is_link($currentfullpath))) {

                           # Create array for directories
                           if(($fperm)&&(!$exc)){

                            $perm = substr(sprintf('%o', fileperms($cfile)), -4);

                            fwrite($fperm, str_replace($_CONFIG['backup_path'], "" , $cfile)."|".$perm."\n");

                           }

                           #$d_arr[++$d] = $cfile;

                        if(!$exc)
                        recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $includedFolders, $path . "/" . $file, $excluded, $fperm);
                    } else {
                        #if ( in_array($currentfullpath, $includedFolders) )
                        {

                            # Create array for files

                            if(($fperm)&&(!$exc)){

                             $perm = substr(sprintf('%o', @fileperms($cfile)), -4);
                             
                             #$sfile = sprintf("%.2f",get_filesize($cfile)/1024);

                             fwrite($fperm, str_replace($_CONFIG['backup_path'], "" , $cfile)."|".$perm."\n");

                            }
                            if(!$exc){

                              #$s_arr[$f] = @filesize($cfile);
                                
                              if((!$_CONFIG['mem']) && ($_CONFIG['backup_refresh'] != 1 )){

                              $f_arr[$f++] = $cfile;
                              
                              }else{
								
							  $f++;
								
						  	 }
                              

                              $s += @filesize($cfile);

                            }

                            
                            
                        }
                    }
                }
            }
        }
        # Wrap things up if we're in a directory
        #if( is_dir( $currentfullpath ) )
            @closedir( $handle );
    }
}

function get_filesize($path){
	
   $sizeInBytes = filesize($path);
   if (!$sizeInBytes) {
   $command = "ls -l \"$path\" | cut -d \" \" -f 6";
   $sizeInBytes = @exec($command);       
   }

  return $sizeInBytes; 
	
}

function getBackupFiles(&$d_arr, &$f_arr, &$s_arr, &$d, &$f) {
    // ----------------------------------------------------------
    // Routine to list the existing backup files in the Component
    // Backup folder
    // ----------------------------------------------------------
    global $_CONFIG;
    
    $path = $_CONFIG['clonerPath'];
    # Open possibly available directory
    if( is_dir( $path ) ) {
        if( $handle = opendir( $path ) ) {
            while( false !== ( $file = readdir( $handle ) ) ) {
                # Make sure we don't push parental directories or dotfiles (unix) into the arrays
                if( $file != "." && $file != ".." && $file[0] != "." ) {
                    if( is_dir( $path . "/" . $file ) )
                        # Create array for directories
                        $d_arr[$d++] = $file;
                    else
                        if ((strstr($file, '.zip' ))||(strstr($file, '.tgz' ))||(strstr($file, '.tar' ))) {
                            # Create array for files
                            $f_arr[$f++] = $file;
                        }
                }
            }
        }
    }

    # Wrap things up if we're in a directory
    if( is_dir( $path ) )
        closedir( $handle );
     sort($f_arr);
    # Print file list
    for( $i=0; $i < count( $f_arr ); $i++ ) {
        $s_arr[$i] = getFileSizeText(get_filesize( $path . "/" . $f_arr[$i] ));
    }
}


function getFileSizeText($filesize) {
    // ----------------------------------------------------------
    // Routine to display a formatted version of a filesize
    // ----------------------------------------------------------

    if( $filesize >= 1024 && $filesize < 1048576) {
        # Size in kilobytes
        return round( $filesize / 1024, 2 ) . " KB";
    } elseif( $filesize >= 1048576 ) {
        # Size in megabytes
        return round( $filesize / 1024 / 1024, 2 ) . " MB";
    } else {
        # Size in bytes
        return $filesize . " bytes";
    }
}

function doBackup( $tables, $OutType, $OutDest, $toBackUp, $UserAgent, $local_backup_path, &$databaseResult, $backupname, $excltables = array(), $dbname) {
    global $database, $mosConfig_db, $mosConfig_sitename, $version,$option,$task, $mosConfig_dbprefix, $_CONFIG,$mosConfig_user,$mosConfig_password,$mosConfig_host;


    if (!$tables[0])
    {
        $databaseResult = LM_DATABASE_MISSING_TABLES;
        return;
    }


    /* Determine the mime type and file extension for the output file */

    if ($OutType == "zip") {
        $filename = $backupname ."-sql". ".zip";

    } elseif ($OutType == "html") {
        $filename = $backupname ."-sql". ".html";

    } else {
        $filename = $backupname ."-sql". ".sql";

    };


     $sqlfile = "$local_backup_path/$filename";
     @unlink($sqlfile);
    /* Store all the tables we want to back-up in variable $tables[] */

    if ($tables[0] == "all") {
        array_pop($tables);
        $query = mysql_query("SHOW tables");
        while($row = mysql_fetch_array($query)){
			
			$tables_list[] = $row[0];
			
		}
        
		
		$tables = array_merge($tables, $tables_list);
    }
    $newtables = array();

    #if(is_array($excltables))
    foreach($tables as $key=>$value){
            if(is_array($excltables)){
             if(!in_array($value, $excltables))
              $newtables[] = $value;
            }
            else
              $newtables[] = $value;

        }
        

     if(sizeof($tables) == sizeof($newtables))
      $ex_dump = "";
     else
      $ex_dump = @implode(" ",$newtables);
     $tables = $newtables;

     if($_REQUEST['dbbackup_drop']){
       $drop = " --add-drop-table";
       }
     else{
       $drop = "";
       }
     
     if($_REQUEST['dbbackup_comp']){
     	
       $drop .= " --compatible=".strtolower($_REQUEST['dbbackup_comp'])." ";      
     	
     }
       
     if($_CONFIG['sql_mem']){
      exec ($_CONFIG[sqldump]." -h ".$_CONFIG['mysql_host']." -u ".$_CONFIG['mysql_user']." -p".$_CONFIG['mysql_pass']." ".$dbname." > ".$sqlfile ." $drop --allow-keywords ". $ex_dump);

      if(filesize($sqlfile))
       $databaseResult = LM_DATABASE_BACKUP_COMPLETED.' ( '.getFileSizeText(filesize($sqlfile)).' )';
      else
       $databaseResult = LM_MSG_BACK_14;
       
      exec("chmod 777 $sqlfile");

     return $sqlfile;
     }

/*Added some default values for quotes and auto_increment problems*/
    mysql_query("SET SQL_QUOTE_SHOW_CREATE=1;");
    mysql_query("SET sql_mode = 0;");

    if($_REQUEST['dbbackup_comp'])
     {
     
      mysql_query("SET sql_mode=".$_REQUEST['dbbackup_comp'].";");
     	
     }


    /* Store the "Create Tables" SQL in variable $CreateTable[$tblval] */
    if ($toBackUp!="data")
    {
        foreach ($tables as $tblval)
        {
            $query = mysql_query("SHOW CREATE table `$tblval`");
            $row = mysql_fetch_array($query);
			$CreateTable[$tblval] = $row[1];
        }
    }

    /* Store all the FIELD TYPES being backed-up (text fields need to be delimited) in variable $FieldType*/
    if ($toBackUp!="structure")
    {
        foreach ($tables as $tblval)
        {
            $query = mysql_query("SHOW FIELDS FROM $tblval");
            while($row = mysql_fetch_row($query)){
				
				$fields[] = $row[0];
				
			}
            foreach($fields as $field)
            {
                $FieldType[$tblval][$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type);
            }
        }
    }


    if ($OutDest == "local") {



                $fp = fopen($sqlfile, "w");
                if (!$fp) {
                    $databaseResult = LM_DATABASE_BACKUP_FAILED;
                    return ;
                }
    }

    /* Build the fancy header on the dump file */
    $OutBuffer = "";
    if ($OutType == 'html') {
    } else {
        $OutBuffer .= "#\n";
        $OutBuffer .= "# Powered by XCloner Site Backup\n";
        $OutBuffer .= "# http://www.joomlaplug.com\n";
        $OutBuffer .= "#\n";
        $OutBuffer .= "# Host: ".$_SERVER['HTTP_HOST']."\n";
        $OutBuffer .= "# Generation Time: " . date("M j, Y \a\\t H:i") . "\n";
        $OutBuffer .= "# Server version: " . getVersion() . "\n";
        $OutBuffer .= "# PHP Version: " . phpversion() . "\n";
        $OutBuffer .= "# Database : `" . $dbname . "`\n# --------------------------------------------------------\n";
    }
    fwrite($fp, $OutBuffer);
    unset($OutBuffer);

    /* Okay, here's the meat & potatoes */
    foreach ($tables as $tblval) {
        if ($toBackUp != "data") {
            if ($OutType == 'html') {
            } else {
                $OutBuffer .= "#\n# Table structure for table `$tblval`\n#\n";
                if($_REQUEST['dbbackup_drop'])
                	$OutBuffer .= "#\nDROP table IF EXISTS $tblval;\n";
                
				$OutBuffer .= $CreateTable[$tblval].";\r\n";
            }
        }
        fwrite($fp, $OutBuffer);
        unset($OutBuffer);
        
        
          if ($toBackUp != "structure") {

                $OutBuffer .= "#\n# Dumping data for table `$tblval`\n#\n";
                $query = @mysql_query("SELECT *  FROM $tblval");
                                
                while($row = @mysql_fetch_array($query, MYSQL_ASSOC))
                {
                     
                    $InsertDump = "INSERT INTO $tblval VALUES (";
                    $arr =$row;
                    foreach($arr as $key => $value)
                    {
                        $value = addslashes( $value );
                        $value = str_replace( "\n", '\r\n', $value );
                        $value = str_replace( "\r", '', $value );
                        #if (@preg_match ("/\b" . $FieldType[$tblval][$key] . "\b/i", "DATE TIME DATETIME CHAR VARCHAR TEXT TINYTEXT MEDIUMTEXT LONGTEXT BLOB TINYBLOB MEDIUMBLOB LONGBLOB ENUM SET"))
                        {
                            $InsertDump .= "'$value',";
                        }
                        /*else
                        {
                            $InsertDump .= "'$value',";
                        } */
                    }
                    $OutBuffer .= rtrim($InsertDump,',') . ");\n";
                    fwrite($fp, $OutBuffer);
                    unset($OutBuffer);
                    $i++;
                }
         }
    }

      if ($OutDest == "local") {


                    #fwrite($fp, $OutBuffer);
                    fclose($fp);

                    @chmod($sqlfile, 0777);

                    $databaseResult = LM_DATABASE_BACKUP_COMPLETED.' ( '.getFileSizeText(filesize($sqlfile)).' )';
                    return $sqlfile;

            }
}
 
 
function getVersion(){
	
	$query = mysql_query("SELECT version()");
	$row = mysql_fetch_array($query);
	return $row[0];
	
}

?>
