File: //var/softaculous/moodle30/_upgrade.php
<?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, $globals, $software, $error, $setupcontinue, $notes, $decoded_paths_of_plugins, $boxfile_is_there;
	foreach($decoded_paths_of_plugins as $path){
			$new_path = preg_replace('#^'.preg_quote($__settings['softpath'], '/').'#', $__settings['softpath'].'/soft_tmp', $path);
			@srename($new_path, $path);
	}
	
	if($boxfile_is_there){
		scopy($__settings['softpath'].'/soft_tmp/boxlib.php', $__settings['softpath'].'/lib/boxlib.php');
	}
	
	srm($__settings['softpath'].'/soft_tmp/');
	
	//Legacy code from Moodle 2.6.x not needed now
/* 	$ver = substr($version_from, 0, 3);
	$ver = (int) str_replace('.', '',  $ver); */
	
	// Need to update the database type 
	$conn = @soft_mysql_connect($__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], true);
	$output = soft_mysql_get_server_info($conn);
		
	if(preg_match('/mariadb/is', $output)){
		$__settings['dbtype'] = 'mariadb';
			
		$file = sfile($__settings['softpath'].'/config.php');
		
		$file = explode("\n", $file);
		
		foreach($file as $k => $v){
			if(preg_match('/\$CFG->dbtype(\s*?)=(\s*?)("|\')(.*?)("|\');/is',$v)){
				$file[$k] = '$CFG->dbtype = \''.$__settings['dbtype'].'\';';
			}
		}
		swrite($__settings['softpath'].'/config.php', implode("\n",$file), 1);
	}
}
//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(sversion_compare($__settings['ver'], '4.5', '<')){
		if(!sis_autoupgrading() && !checkbox('plugings_extensions_backup')){
			$error[] = '<input type="checkbox" name="plugings_extensions_backup" '.POSTChecked('plugings_extensions_backup').' /> 
						Please make sure all your Plugins/Extensions in the existing installation are compatible with the '.$software['ver'].' version before attempting to upgrade. Continue ? ';
		}
	}	
	
	return true;
}
// Remove the files before unzipping starts
function __pre_unzip(){
	
	global $__settings, $error, $globals, $software, $decoded_paths_of_plugins, $boxfile_is_there;
	
	@schmod($__settings['softdatadir'], $globals['odc'], 1);
	
	$__settings['autopass'] = srandstr(32);
	sconfigure('get_plugin_list.php', false, 0, 1);
	
	$get_paths_of_plugins = curl_call($__settings['softurl'].'/get_plugin_list.php?pass='.$__settings['autopass'], 0, 5, array(), '', 1);
	soft_log(2, $__settings['softurl'].' Get Plugins List curl response : '.var_export($get_paths_of_plugins, true));
	
	if(!empty($get_paths_of_plugins)){
		if(preg_match('/<moodle_plugins>(.*?)<\/moodle_plugins>/is', $get_paths_of_plugins, $matches)){
			if(!empty($matches[1])){
				$path_list = $matches[1];
				$found_plugins = 1;
			}
		}
	}
	
	// Try with CLI once for Cloudflare domains
	if(empty($found_plugins)){
		
		sconfigure('get_plugin_list.php', false, 0, 1);
		
		$get_paths_of_plugins = sshell_exec(eu_php_bin().' '.$__settings['softpath'].'/get_plugin_list.php --pass='.xss($__settings['autopass']));
		
		soft_log(2, $__settings['softurl'].' Get Plugins List CLI response : '.var_export($get_paths_of_plugins, true));
	
		if(!empty($get_paths_of_plugins)){
			if(preg_match('/<moodle_plugins>(.*?)<\/moodle_plugins>/is', $get_paths_of_plugins, $matches)){
				if(!empty($matches[1])){
					$path_list = $matches[1];
					$found_plugins = 1;
				}
			}
		}
	}
	
	if(!empty($path_list)){
		$decoded_paths_of_plugins = json_decode($path_list, 1);
	}
	
	if(empty($found_plugins)){
		$error[] = 'Unable to fetch the plugins list to copy after the upgrade';
		return false;
	}
	
	soft_log(2, $__settings['softurl'].' Moodle Plugins paths : '.var_export($decoded_paths_of_plugins, true));
	
	smkdir($__settings['softpath'].'/soft_tmp/', $globals['odc']);
	@schmod($__settings['softpath'].'/soft_tmp/', $globals['odc'], 1);
		
	############# THIS IS BECAUSE BOXNET PLUGIN IS REMOVED IN MOODLE 4.0 #####################
	if(sfile_exists($__settings['softpath'].'/lib/boxlib.php')){
		scopy($__settings['softpath'].'/lib/boxlib.php',$__settings['softpath'].'/soft_tmp/boxlib.php');
		$boxfile_is_there=1;
	}
	
	if(!empty($found_plugins)){
	
		foreach($decoded_paths_of_plugins as $path){
			$new_path = preg_replace('#^'.preg_quote($__settings['softpath'], '/').'#', $__settings['softpath'].'/soft_tmp', $path);
			smkdir($new_path, $globals['ocdc'], 1);
			@srename($path, $new_path);
		}
	}
	
	foreach($__settings['fileindex'] as $k => $v){
		if(!empty($v) && $v != 'soft_tmp' && $v != 'config.php' && $v != '.htaccess' && $v != '.git' && $v != '.gitignore'){
			srm($__settings['softpath'].'/'.$v, 1);
		}
	}
	
	return true;	
}
?>