Hooks

From Softaculous Wiki
Jump to: navigation, search

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 :

Hooks directory is located at :

/path/to/softaculous/enduser/hooks/

Note : We have added Filters in Softaculous 4.7.9 and we strongly recommend using Filters over Hooks. You can refer to the documentation here [1]

pre_install

  • This hook will be executed before a script is installed.
  • Rename the pre_install.txt in the hooks folder to pre_install.php and write your code.

post_install

  • This hook will be executed immediately after a script is installed.
  • Rename the post_install.txt in the hooks folder to post_install.php and write your code.

pre_upgrade

  • This hook will be executed before a script is upgraded.
  • Rename the pre_upgrade.txt in the hooks folder to pre_upgrade.php and write your code.

post_upgrade

  • This hook will be executed immediately after a script is upgraded.
  • Rename the post_upgrade.txt in the hooks folder to post_upgrade.php and write your code.

pre_remove

  • This hook will be executed before a script is removed.
  • Rename the pre_remove.txt in the hooks folder to pre_remove.php and write your code.

post_remove

  • This hook will be executed immediately after a script is removed.
  • Rename the post_remove.txt in the hooks folder to post_remove.php and write your code.

pre_clone

  • This hook will be executed before an installation is cloned.
  • Rename the pre_clone.txt in the hooks folder to pre_clone.php and write your code.

post_clone

  • This hook will be executed immediately after an installation is cloned.
  • Rename the post_clone.txt in the hooks folder to post_clone.php and write your code.

pre_import

  • This hook will be executed before a script is being imported.
  • Rename the pre_import.txt in the hooks folder to pre_import.php and write your code.

post_import

  • This hook will be executed immediately after a script has been imported.
  • Rename the post_import.txt in the hooks folder to post_import.php and write your code.

pre_remove

  • This hook will be executed before an installation is being removed.
  • Rename the pre_remove.txt in the hooks folder to pre_remove.php and write your code.

post_remove

  • This hook will be executed immediately after an installation has been removed.
  • Rename the post_remove.txt in the hooks folder to post_remove.php and write your code.

post_adddomain

  • This hook will be executed immediately after a domain has been added in Softaculous Remote.
  • Rename the post_adddomain.txt in the hooks folder to post_adddomain.php and write your code.

post_editdomain

  • This hook will be executed immediately after a domain has been edited in Softaculous Remote.
  • Rename the post_editdomain.txt in the hooks folder to post_editdomain.php and write your code.

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 these files pre_install.txt, post_install.txt, pre_upgrade.txt, post_upgrade.txt, pre_remove.txt and post_remove.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 , post upgrade, pre remove and post remove.


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.
  • The above example is for pre_install hook you can similarly configure the pre_upgrade hook to skip the php version check during the upgrade process.


Define PERL Version

You can also define the PERL version in the Pre Install Hook.

  • Rename the pre_install.txt to pre_install.php in the Hooks folder
  • Define the PERL version in the function __pre_install()
function __pre_install(){	
	global $soft, $software, $globals;
	// Your Code to Identify the PERL version
	// E.g
	define('perl_version', '5.8.8');
}
  • If the PERL version is defined here then Softaculous will skip its process to detect the PERL version while installing a Script.
  • The above example is for pre_install hook you can similarly configure the pre_upgrade hook to skip the PERL version check during the upgrade process.


Define MySQL Version

You can also define the MySQL version in the Pre Install Hook.

  • Rename the pre_install.txt to pre_install.php in the Hooks folder
  • Define the MySQL version in the function __pre_install()
function __pre_install(){	
	global $soft, $software, $globals;
	// Your Code to Identify the MySQL version
	// E.g
	define('mysql_version', '5.1.65');
}
  • If the MySQL version is defined here then Softaculous will skip its process to detect the MySQL version while installing a Script.
  • The above example is for pre_install hook you can similarly configure the pre_upgrade hook to skip the MySQL version check during the upgrade process.


Define Loaded Extensions

You can also define the list of enabled PHP extensions in the Pre Install Hook.

  • Rename the pre_install.txt to pre_install.php in the Hooks folder
  • Define the list of enabled PHP extensions in the function __pre_install()
function __pre_install(){	
	global $soft, $software, $globals, $__hooks;
	// E.g
	$__hooks['loaded_extension'] = array('mysqli', 'curl', 'gd');

        //If $__hooks['loaded_extension'] is the only extension loaded list and you do not want Softaculous to detect the extensions, then you can define the following constant.
        define('PHP_EXT_EXHAUSTIVE', 1);
}
  • If the extension is defined as enabled here then Softaculous will skip its process to detect the PHP extension enabled while installing a Script.
  • The above example is for pre_install hook you can similarly configure the pre_upgrade hook to skip the extension enabled check during the upgrade process.


Define Functions Enabled

You can also define the list of enabled PHP functions in the Pre Install Hook.

  • Rename the pre_install.txt to pre_install.php in the Hooks folder
  • Define the list of enabled PHP functions in the function __pre_install()
function __pre_install(){	
	global $soft, $software, $globals, $__hooks;
	// E.g
	$__hooks['function_exists'] = array('phpinfo', 'exec', 'shell_exec');
}
  • If the function is defined as enabled here then Softaculous will skip its process to detect the PHP function enabled while installing a Script.
  • The above example is for pre_install hook you can similarly configure the pre_upgrade hook to skip the function enabled check during the upgrade process.


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){
							
			}
			
		}
		
	}

}


pre_mail

This HOOK is called before an any EMAIL is sent to the user (i.e. installation mail, removal mail, etc..) The main usage of this hook is if you want to send out a CUSTOM Email or want to perform any action before an email is sent. e.g. if an Admin does not want to send an EMAIL of the installation of a script or any other process, than the Admin can Disable Email forcefully using this HOOK.

  • SSH to your server and go to the following path:
/path-to-softaculous/enduser/hooks/
  • Rename the file pre_mail.txt to pre_mail.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 any mail is sent (e.g installing scripts, removing scripts, etc)
 * $array which is passed as the param in function , it will have details of the email message.
 *
 * @package      hooks 
 * @author       Chirag Nagda
 * @return       bool
 * @since     	 4.0.9
 */
function __pre_mail($array){

	//////////////////////////////////////////////////////////////////////////////////
	// $array	 - Will contain the Email Content which is being sent           //
	//////////////////////////////////////////////////////////////////////////////////

	global $globals;
	foreach($array as $k => $v){
            
                if($v['to'] == USER_EMAIL){

                     // Do the STUFF

                }
        }
}
  • $array is an array which contains the details of an Email which is being sent.
Array
(
    [0] => Array
         (
             [to] => Email of USER
             [subject] => Email SUBJECT
             [message] => Email BODY
          )

)

Change PHP Version using .htaccess

.htaccess is used by many web hosts to change PHP version for their users. You can do this using Softaculous for the scripts that require PHP 5.3+
Note : This example assumes that you run PHP 5.2 as default for the user and this hooks will switch the version to PHP 5.3.

  • Add the following code to the __pre_install() function in the file /path/to/softaculous/enduser/hooks/pre_install.php
global $soft, $software, $globals, $scripts;
	// Do stuff here e.g. is as follows	

	if($scripts[$soft]['php_min'] >= '5.3'){
		$version = '5.3.24';
		define('php_version', $version);	
	}
  • This pre install hook will skip the PHP version check by Softaculous.
  • Now you have to create the .htaccess file with the content to switch PHP version to 5.3
  • Add the following code to the __post_install() function in the file /path/to/softaculous/enduser/hooks/post_install.php
global $soft, $software, $globals, $scripts;
	// Do stuff here e.g. is as follows
	
	if($scripts[$soft]['php_min'] >= '5.3'){
	
		$data = "AddHandler application/x-httpd-php53 .php\n";
		
		if(sfile_exists($installation[0]['softpath'].'/.htaccess')){
			$tmp_data = sfile($installation[0]['softpath'].'/.htaccess');
			if(!preg_match('/AddHandler(\s*?)application/ies', $tmp_data)){
				$data .= $tmp_data;
			}
		}
		
		swrite($installation[0]['softpath'].'/.htaccess', $data, 1);	
	}
  • This will create the .htaccess file with the required content and if the script has a .htaccess then it will write the content to the existing file.
  • Now we also need to define the PHP version during the Upgrade process.
  • Add the following code to the __pre_upgrade() function in the file /path/to/softaculous/enduser/hooks/pre_upgrade.php
global $soft, $software, $globals, $scripts;
	// Do stuff here e.g. is as follows	

	if($scripts[$soft]['php_min'] >= '5.3'){
		$version = '5.3.24';
		define('php_version', $version);	
	}
  • This pre upgrade hook will skip the PHP version check by Softaculous. You do not need to configure the post_upgrade hook as the .htaccess file already exists.
  • Add the following code to the __post_upgrade() function in the file /path/to/softaculous/enduser/hooks/post_upgrade.php
global $soft, $software, $globals, $scripts;
	// Do stuff here e.g. is as follows
	
	if($scripts[$soft]['php_min'] >= '5.3'){
	
		$data = "AddHandler application/x-httpd-php53 .php\n";
		
		if(sfile_exists($installation[0]['softpath'].'/.htaccess')){
			$tmp_data = sfile($installation[0]['softpath'].'/.htaccess');
			if(!preg_match('/AddHandler(\s*?)application/ies', $tmp_data)){
				$data .= $tmp_data;
			}
		}
		
		swrite($installation[0]['softpath'].'/.htaccess', $data, 1);	
	}
  • This will create the .htaccess file with the required content if the content does not exist.
  • Thats it you have the hook configured to switch PHP version.

Detect PHP Version on CageFS