<?php

//////////////////////////////////////////////////////////////
//===========================================================
// upgrade.php(For individual softwares)
//===========================================================
// SOFTACULOUS 
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       10th Jan 2009
// Time:       21:00 hrs
// Site:       http://www.softaculous.com/ (SOFTACULOUS)
// ----------------------------------------------------------
// Please Read the Terms of use at http://www.softaculous.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Inc.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('SOFTACULOUS')){

	die('Hacking Attempt');

}

/////////////////////////////////////////
// All functions in this PAGE must begin
// with TWO UNDERSCORE '__' to avoid 
// clashes with SOFTACULOUS Functions
// e.g. __funcname()
/////////////////////////////////////////

//////////////////////////////////////////
// Note : The path of the upgrade package 
//        is $software['path'].'/' . So to
//        access other files use 
//        $software['path'].'/other_file.ext'
//////////////////////////////////////////

// NOTE: $__settings will contain the installation information like PATH, URL. They are :
//       $__settings['ver'] - The version of the current installation
//		 $__settings['itime'] - When the software was installed
//		 $__settings['softpath'] - The current PATH
//		 $__settings['softurl'] - The URL of the software
//		 IF database was made by Softaclous
//		 $__settings['softdb'] - The Database name
//		 $__settings['softdbuser'] - Database User
//		 $__settings['softdbhost'] - Database Host
//		 $__settings['softdbpass'] - Database Password

//The Upgrade process
function __upgrade($version_from){

global $__settings, $error, $software, $globals, $notes, $setupcontinue;

	
	//CHMOD some files
	@schmod($__settings['softpath'].'/temp/', $globals['odc'], 1);
	@schmod($__settings['softpath'].'/logs/', $globals['odc'], 1);
	
	if(sversion_compare($__settings['ver'], '1.0.0', '<')){
		
		@schmod($__settings['softpath'].'/bin/', $globals['odc'], 1);
		
		exec($__settings['softpath'].'/bin/update.sh << EOF
?
y
EOF', $out, $ret);
		
		if(!empty($_GET['debug'])){
			r_print($out);
			r_print($ret);
		}
		
		$auto_upgrading = sis_autoupgrading();
		
		// If it was sucessful dont give the Setuplocation
		if(preg_match('/This(\s*?)instance(\s*?)of(\s*?)Roundcube(\s*?)is(\s*?)up-to-date/is', $out)){
			$setupcontinue = '';
		}elseif(!empty($auto_upgrading)){
			$error[] = '{{err_auto_upgrade}}';
		}
	}else{
		
		$file = sfile($__settings['softpath'].'/config/config.inc.php');
	
		if(empty($file)){
			return false;
		}
		
		$file .= "\n".'$config[\'enable_installer\'] = true;';
		
		swrite($__settings['softpath'].'/config/config.inc.php',$file,1);

		if(sversion_compare($__settings['ver'], '1.6.1', '<')){
			
			soft_preg_replace('/\$config\[("|\')default_host("|\')\](\s*?)=(\s*?)("|\')(.*?)("|\');/is', $file, $imap_host, 6);
			soft_preg_replace('/\$config\[("|\')smtp_server("|\')\](\s*?)=(\s*?)("|\')(.*?)("|\');/is', $file, $smtp_host, 6);
			
			$file = sfile($__settings['softpath'].'/config/config.inc.php');

			$file = explode("\n", $file);

			if(empty($file)){
				return false;
			}

			foreach($file as $k => $v){		
				if(preg_match('/\$config\[("|\')default_host("|\')\](\s*?)=(\s*?)("|\')(.*?)("|\');/is',$v)){
					$file[$k] = '$config[\'imap_host\'] = \''.$imap_host.'\';';
				}
				
				if(preg_match('/\$config\[("|\')smtp_server("|\')\](\s*?)=(\s*?)("|\')(.*?)("|\');/is',$v)){
					$file[$k] = '$config[\'smtp_host\'] = \''.$smtp_host.'\';';
				}
			}

			swrite($__settings['softpath'].'/config/config.inc.php', implode("\n",$file), 1);
			
		}
		
		$notes = 'Complete the upgrade process by visiting upgrade URL and then please remove the following folder:<br />
		<b>'.$__settings['softpath'].'/installer</b> <br />
		and set the value for <b>$config[\'enable_installer\'] = false</b> in '.$__settings['softpath'].'/config/config.inc.php';
		
		$setupcontinue = 'installer';
	}
}

//Check whether the Minimum Software configuration matches
function __requirements(){

global $__settings, $error, $software, $globals;

	//If there are some shorfalls then pass it to $error and return false
	if($globals['os'] == 'windows' && sversion_compare($__settings['ver'], '1.0.0', '<')){
		$error[] = 'It is not possible to upgrade RoundCube on Windows.';
		return false;
	}
	
	// We need the user to create backup
	if(!sis_autoupgrading() && !checkbox('create_backup') && !checkbox('force_upgrade')){
		$error[] = 'It seems you have not chosen to back up your installation. Please choose the Create Backup checkbox to create a backup before upgrading.<br/>
		<input type="checkbox" name="force_upgrade" '.POSTChecked('force_upgrade').' /> Continue anyway ?';
		return false;
	}
	
	if(!function_exists('exec') && sversion_compare($__settings['ver'], '1.0.0', '<')){
		$error[] = 'exec() function is required to Upgrade RoundCube';
	}
	
	return true;

}

//===========================
// Software Vendors Functions
//===========================

//Just Validate an email
function __email_address($email){

global $error;

	if(!emailvalidation($email)){
		$error[] = '{{err_wrongemail}}';
	}		
	return $email;
}

?>