Hooks
Contents |
Overview
Hooks enable the admin to modify certain aspects of the task being done. There are some Hooks in Softaculous that you can take advantage of :
pre_install
post_install
pre_upgrade
post_upgrade
The following guide will show you how the Server Admin can configure hooks to customize the installations and upgrades of installations.
- SSH to your server and go to the following path:
/path-to-softaculous/enduser/hooks/
- You can see 4 files pre_install.txt, post_install.txt, pre_upgrade.txt and post_upgrade.txt
- Choose the file you would like to execute depending upon your requirement and rename it to make it a PHP file.
- Add the code in the function available in the file and save the file.
- That's it you have configured the hook.
- For eg: You would like to make some changes for WordPress and you want to apply this change after every installation of WordPress done on the server. For this you will have to rename the file post_install.txt to post_install.php because the change is to be done after installation and add the code in the function __post_install().
function __post_install($installation){
global $soft, $software, $globals;
// The id for WordPress is 26
if($soft == 26){
/* Do things only if its WordPress */
}
}
So when ever a new installation of WordPress is done the code in the function __post_install() will be executed.
- Similarly you can do this for any script for pre installation, post installation, pre upgrade and also post upgrade.
Explanation of the Variables
- $soft is a string which contains the Script id. The script id can be obtained from $scripts in scripts.php
/path-to-softaculous/enduser/scripts.php
- $installation in an array which contains the details of the installation.
Array
(
[0] => Array
(
[insid] => installation id
[sid] => script id
[ver] => installed version of script
[itime] => installation time
[softpath] => path to installation
[softurl] => installation URL
[softdb] => database name of installation
[softdbuser] => database user of installation
[softdbhost] => database host of installation
[softdbpass] => database password of installation
)
)
- $software is an array which has the details of the software.
Array
(
[name] => Name of the script being installed
[softname] => softaculous name of script being installed
[desc] => Description of the script
[ins] => 1
[cat] => Category under which the script falls
[type] => Type of script(php,js,perl)
[ver] => Current version of script
[path] => path where the script package is available
[spacereq] => Space required for the script to be installed in bytes
[adminurl] => The admin url of the script
)
Define PHP Version
You can also define the PHP version in the Pre Install Hook if you are using multiple PHP versions on your server.
- Rename the pre_install.txt to pre_install.php in the Hooks folder
- Add your code to Identify the PHP version in the function __pre_install()
function __pre_install(){
global $soft, $software, $globals;
// Your Code to Identify the PHP version
// E.g
$version = phpversion();
define('php_version', $version);
}
- If the PHP version is defined here then Softaculous will skip its process to detect the PHP version while installing a Script.
pre_update_email
This HOOK is called before an EMAIL is sent to the user notifying him of the updates. The main usage of this hook is if you want to send out a CUSTOM Email when a script update is available. e.g. if a new version of WordPress is available, Softaculous will load all the list of users who have wordpress and will email them of the same. But if you want to send them an email yourself, you can use this hook.
- SSH to your server and go to the following path:
/path-to-softaculous/enduser/hooks/
- Rename the file pre_update_email.txt to pre_update_email.php
- Add the code in the function available in the file and save the file.
- That's it you have configured the hook.
The code is as follows :
/**
* This function will be called whenever an UPDATE becomes available for a script AND before an email is sent to the user
* informing him about the update
*
* @package hooks
* @author Pulkit Gupta
* @return bool
* @since 4.0
*/
function __pre_update_email(){
global $globals, $ins_list, $updated_scripts, $scripts;
//////////////////////////////////////////////////////////////////////////////////
// $ins_list - Will contain the details of the OUTDATED installations
// of all users immediately when AN update becomes available
// $updated_scripts - The scripts which just got updated
// $scripts - Detailed information about the all script.
//////////////////////////////////////////////////////////////////////////////////
foreach($ins_list as $username => $scriptwise){
// Do what needs to be done !
// $scriptwise will now contain the list of installations in the format of array(SCRIPTID => array());
foreach($scriptwise as $_sid => $_ins){
// Loop through the installations
foreach($_ins as $kk => $vv){
}
}
}
}