<?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;
	
}

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

global $__settings, $error, $software;

	//If there are some shorfalls then pass it to $error and return false
	
	return true;

}

//This is the function that will parse
function __parse($string){

global $__settings;

	foreach($__settings as $k => $v){	
		$string = str_replace('[['.$k.']]', $v, $string);	
	}	
	return $string;
	
}


// Reads the file from $from, parses it and overwrites it at $to
// NOTE: $from is relative to the package folder
//       $to is relative to the SETUP LOCATION
//       IF $to is not specified $from will be assumed the destination file as well
function __configure($from, $to = false){

global $__settings, $error, $software;
	
	$to = (empty($to) ? $from : $to);
	$file = $__settings['softpath'].'/'.$to;
	
	//The file in the PACKAGE Folder
	$data = @implode(file($software['path'].'/'.$from));
	
	if(empty($data)){
		$error[] = '{{err_openconfig}} <b>'.$from.'</b>';
		return false;
	}
	
	//Parse the DATA
	$data = __parse($data);
	
	// OVERWITE the file
	// NOTE: writefile() is an internal function of SOFTACULOUS
	// The last parameter of writefile() is OVERWRITE!
	if(!writefile($file, $data, 1)){
		$error[] = '{{err_writeconfig}} <b>'.$from.'</b>';
		return false;
	}
	
	return true;

}


//The Softwares MySQL functions
//The software can do anything in this function
//1) It can call load a SQL file
//2) It can call a PHP file having SQL queries
// The Software is to read, handle and INSERT/UPDATE its SQL queries
function __mysql(){

global $__settings, $error, $software;
	
	if(!@include_once($software['path'].'/mysql.php')){
		$error[] = '{{err_openmy}}';
		return false;
	}
	
	//Make the Connection
	$__conn = @mysql_connect($__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], true);
	
	//CHECK Errors and SELECT DATABASE
	if(!empty($__conn)){	
		if(!(@mysql_select_db($__settings['softdb'], $__conn))){
			$error[] = '{{err_selectmy}}';
			return false;
		}
	}else{
		$error[] = '{{err_myconn}}';
		return false;
	}
	
	//Start the Queries
	foreach($queries as $k => $q){	
		
		//PARSE the String and make the QUERY
		$result = mysql_query(__parse($q), $__conn);
		
		//Looks like there was an error
		if(!$result){			
			$error[] = '{{err_makequery}} : '.$k.'<br />{{err_mynum}} : '.mysql_errno($__conn).'<br />{{err_myerr}} : '.mysql_error($__conn);			
			return false;				
		}
	
	}

}

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

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

global $error;

	if(!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([.])+([a-zA-Z0-9\._-]+)+$/", $email)){
		$error[] = '{{err_wrongemail}}';
	}		
	return $email;
}

//The function name should begin with double UNDERSCORE '__'
function __function_name($val){

	//The function to perform

}

//The function name should begin with double UNDERSCORE '__'
//This is an example of a <handle>__handle_function_name</handle>
//Softaculous will call __handle_function_name(value)
//It must return the value as PROCESSED by the Software Vendor
//The function can trigger error in $error[] = '{{string}}';
function __handle_function_name($val){
global $error;

	//The function to perform

}

//Generates random strings
function __generateRandStr($length){
	
	$randstr = "";	
	for($i = 0; $i < $length; $i++){	
		$randnum = mt_rand(0,61);		
		if($randnum < 10){		
			$randstr .= chr($randnum+48);			
		}elseif($randnum < 36){		
			$randstr .= chr($randnum+55);			
		}else{		
			$randstr .= chr($randnum+61);			
		}		
	}	
	return strtolower($randstr);
}

?>