<?php
//ini_set("display_errors", 1);
error_reporting(0);
include("../includes/site_config.php");
include('../session_handler.inc');
include('../error.php');

include('verTable.php');
$PATH = $_SESSION['SITE_ROOT'];
unlink("../admin/ver_check.php");
unlink("../admin/ver_check.zip");
####################################
if (!is_dir('upgrades')) 
{ 
mkdir("upgrades", 777, true);
chown("upgrades", "wazzu1"); 
}

$query=mysql_query("SELECT * from versions");
$vers=mysql_fetch_object($query);
copy("http://www.wazzum.com/upgrades/ver_check.zip", "../admin/ver_check.zip");
chmod ("../admin/ver_check.php", 777);

$path1 = '../admin/';
$file1 = '../admin/ver_check.zip';
 
$path_parts1 = pathinfo($file1);
$ext1 = $path_parts1[extension];

if($ext == 'gz')
{
	$execute = "gunzip -".$path." $file";
	`$execute`;
}
if($ext1 == 'zip')
{
	$execute = "unzip -u $file1 -d $path1";
	//chmod("$path", 777) or die('cant change permission');
	`$execute`;
	unlink($file1);
}
include('../admin/ver_check.php');

 $count=count($ver);

for($i=0; $i<=$count; $i++)
	{
		if($ver[$i]==$vers->version){
		$j=$i+1;
		$upg= $ver[$j];
		}
	}
$i=$count-1;

###########################

###########################




	
if(isset($_REQUEST['upgrade'])){

/*$ftp_server='wazzumcom.com';
$ftp_user_name='wazzu1';
$ftp_user_pass='ultrax0102x#'; 
   
    $conn_id = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
   
    // login into FTP server
    if (@ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
        //echo "Connected as $ftp_user_name@$ftp_server\n";
        ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 1000);  // Set the network timeout to 10 seconds
   
//    $copy_result = ftp_copyAll($conn_id, 'upgrades', 'copied');
//
//    echo $copy_result;

    }else{
    echo "Couldn't connect as $ftp_user\n";
    }
   
   $upload = ftp_put($conn_id, "/var/www/vhosts/wazzumcom.com/httpdocs/dev/admin/upgrades/2.5.zip", "ftp://wazzum.com/upgrades/2.5.zip", FTP_BINARY);  // upload the file
      if (!$upload) {  // check upload status
         echo "<span style='color:#FF0000'><h2>FTP upload of has failed!</h2></span> <br />";
      } else {
         echo "<span style='color:#339900'><h2>Uploading Completed Successfully!</h2></span><br /><br />";
      }
   */
   
//    @ftp_close($conn_id);    // colse of ftp_connect
    // @ftp_quit($conn*/




chmod ("".$_SESSION['SITE_ROOT']."admin/upgrades/", 777);
chmod ("http://www.wazzum.com/upgrades/$upg/test.zip", 777);
if(!file_exists("upgrades/$upg.zip")){
if(!copy("ftp://wazzum.com/upgrades/$upg.zip", "upgrades/$upg.zip" ))
{
echo "failed to copy file";
}
else{
//chmod ("upgrades/$upg.zip", 777) or die("Cant change Permission");
echo "copying done";
}
}else{
echo "File already exists!";
}

$path = 'upgrades/';
$file = "upgrades/$upg.zip";
 
$path_parts = pathinfo($file);
$ext = $path_parts[extension];
 
if($ext == 'gz')
{
	$execute = "gunzip -".$path." $file";
	`$execute`;
}
if($ext == 'zip')
{
	$execute = "unzip -u $file -d $path";
	//chmod("".$_SESSION['SITE_ROOT']."$file", 777) or die('cant change permission');
	`$execute`;
}
unlink($file);
if(isset($upg)){
$query=mysql_query("update versions set version='$upg', date_updated=CURDATE()");
	}
	
$source =$PATH."admin/upgrades/";
$destination = $PATH;
full_copy($source, $destination);
deleteDir($source);


################

 /*function ftp_copyAll($conn_id, $src_dir, $dst_dir) {
   
      $d = dir($src_dir);
     // ftp_mkdir($conn_id, $dst_dir);   echo "creat dir <b><u> $dst_dir </u></b><br>";
       while($file = $d->read()) { // do this for each file in the directory
          if ($file != "." && $file != "..") { // to prevent an infinite loop
              if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
			  
			  chmod($src_dir."/".$file, 777);
                  ftp_copyAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
              } else {
                $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
                echo "creat files::: <b><u>".$dst_dir."/".$file ." </u></b><br>";
              }
          }
        ob_flush() ;
        sleep(1); 
      }
      $d->close();
    
    return "<br><br><font size=3><b>All Copied  ok </b></font>";
    } // end fn function ftp_copyAll
   
   
   
    //$ftp_user_name = 'asamirftp@phpeg.net';
    //$ftp_user_pass = 'asamirftp123';
    //$ftp_server = 'phpeg.net';
   
    // connect to FTP server
    //$ftp_user_name = 'FTP_User_name';
//    $ftp_user_pass = 'FTP_Password';
//    $ftp_server = 'FTP_Server';
     
	  $ftp_server='wazzumcom.com';
$ftp_user_name='wazzu1';
$ftp_user_pass='ultrax0102x#'; 
   
    $conn_id = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
   
    // login into FTP server
    if (@ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
        //echo "Connected as $ftp_user_name@$ftp_server\n";
        ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 1000);  // Set the network timeout to 10 seconds
   
    $copy_result = ftp_copyAll($conn_id, 'upgrades', 'copied');

    echo $copy_result;

    }else{
    echo "Couldn't connect as $ftp_user\n";
    }
   
    @ftp_close($conn_id);    // colse of ftp_connect
    // @ftp_quit($conn*/

#################
@ftp_close($conn_id);

#############



function full_copySmall( $source, $target ) {
	
	if ( is_dir( $source ) ) {
	
		//@mkdir( $target, 0777 );
		$d = dir( $source );
		while ( FALSE !== ( $entry = $d->read() ) ) {
		//chmod($entry, 0777);
			if ( $entry == '.' || $entry == '..' ) {
				continue;
			}
			$Entry = $source . '/' . $entry; 
			
			$b=substr($entry,0,-4);
	
			
			
			$word =str_replace($entry,".jpg","_small.jpg");
			
			$string=$b.$word;

			if ( is_dir( $Entry ) ) {
				full_copySmall( $Entry, $target . '/' . $entry );
				continue;
			}
			copy( $Entry, $target . '/' . $string );
		}
 
		$d->close();
	}else {
		copy( $source, $target );
	}
}

$sourceP =$_SESSION['SITE_ROOT']."includes/members/";
$destinationP = $_SESSION['SITE_ROOT']."includes/members/";
full_copySmall($sourceP, $destinationP);

############

include_once($_SESSION['SITE_ROOT']."admin/db/$upg.php");



}
//deleteDir('/var/www/vhosts/wazzumcom.com/httpdocs/dev/admin/upgrades/admin');
//deleteDir('/var/www/vhosts/wazzumcom.com/httpdocs/dev/admin/upgrades/lib');
//deleteDir('/var/www/vhosts/wazzumcom.com/httpdocs/dev/admin/upgrades/classes');
//deleteDir('/var/www/vhosts/wazzumcom.com/httpdocs/dev/admin/upgrades/includes');

#############
function full_copy( $source, $target ) {
	
	if ( is_dir( $source ) ) {
	
		@mkdir( $target, 0777 );
		$d = dir( $source );
		while ( FALSE !== ( $entry = $d->read() ) ) {
		chmod($entry, 0777);
			if ( $entry == '.' || $entry == '..' ) {
				continue;
			}
			$Entry = $source . '/' . $entry; 
			if ( is_dir( $Entry ) ) {
				full_copy( $Entry, $target . '/' . $entry );
				continue;
			}
			copy( $Entry, $target . '/' . $entry );
		}
 
		$d->close();
	}else {
		copy( $source, $target );
	}
}

############

function deleteDir($dir) {
   // open the directory
   $dhandle = opendir($dir);

   if ($dhandle) {
      // loop through it
      while (false !== ($fname = readdir($dhandle))) {
         // if the element is a directory, and 
         // does not start with a '.' or '..'
         // we call deleteDir function recursively 
         // passing this element as a parameter
         if (is_dir( "{$dir}/{$fname}" )) {
            if (($fname != '.') && ($fname != '..')  && ($fname != 'upgrades')) {
               //echo "<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />";
               deleteDir("$dir/$fname");
            }
         // the element is a file, so we delete it
         } else {
            //echo "Deleting File: {$dir}/{$fname} <br />";
            unlink("{$dir}/{$fname}");
         }
      }
      closedir($dhandle);
    }
   // now directory is empty, so we can use
   // the rmdir() function to delete it
   //echo "<u>Deleting Directory</u>: {$dir} <br />";
   //echo $_SESSION['SITE_ROOT'];
   if($dir!=$_SESSION['SITE_ROOT']."admin/upgrades/"){
   rmdir($dir);
   }
}

// call deleteDir function and pass to it 
// as a parameter a directory name


##############


$query=mysql_query("SELECT * from versions");
$versi=mysql_fetch_object($query);

$area = 'member';




?>
<?php include("include/header.php"); ?>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Upgrade Tool</div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="100%" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
       
    <tr>
      <td class="admin-head">&nbsp;</td>
    </tr>
    <tr>
      <td>

	<table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
     
    

        <tr align=left class="normal" >
          <td align="left">Your current version is <?=$versi->version?><br /></td>
          <? if($versi->version!=$ver[$i]){ ?><td align="left" valign="top"><a href="<?=$_SERVER['PHP_SELF']?>?upgrade=yes&ver=<?=$upg?>"><strong>Upgrade</strong></a></td><? }?>
          </tr>
	<tr><td colspan="3"><hr class="line"/></td></tr>
   

  </table>

	  </td>
    </tr>
</table>




</div>
<div class="admin-page-bottom"></div></div>

<?php include("include/footer.php"); ?>