2026-06-02 13:47:36 -05:00
<? php
/**
* @package MokoJoomBackup
* @author Moko Consulting <hello@mokoconsulting.tech>
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*/
defined ( '_JEXEC' ) or die ;
use Joomla\CMS\Factory ;
use Joomla\CMS\Installer\InstallerAdapter ;
use Joomla\CMS\Language\Text ;
2026-06-04 08:01:05 -05:00
use Joomla\CMS\Router\Route ;
2026-06-02 13:47:36 -05:00
2026-06-06 14:52:27 -05:00
class Pkg_MokoJoomBackupInstallerScript
2026-06-02 13:47:36 -05:00
{
/**
* Minimum Joomla version required
*
* @var string
*/
protected $minimumJoomla = '4.0.0' ;
/**
* Minimum PHP version required
*
* @var string
*/
protected $minimumPhp = '8.1.0' ;
/**
* Called before any install/update/uninstall action.
*
* @param string $type Action type (install, update, uninstall)
* @param InstallerAdapter $parent Installer adapter
*
* @return bool
*/
public function preflight ( string $type , InstallerAdapter $parent ) : bool
{
if ( version_compare ( PHP_VERSION , $this -> minimumPhp , '<' )) {
Factory :: getApplication () -> enqueueMessage (
2026-06-06 14:52:27 -05:00
Text :: sprintf ( 'PKG_MOKOJOOMBACKUP_PHP_VERSION_ERROR' , $this -> minimumPhp ),
2026-06-02 13:47:36 -05:00
'error'
);
return false ;
}
2026-06-06 16:32:28 -05:00
// Save download key before Joomla re-registers the update site
if ( $type === 'update' ) {
$this -> preflight_saveKey ();
}
2026-06-02 13:47:36 -05:00
return true ;
}
/**
* Called after install/update.
*
* @param string $type Action type
* @param InstallerAdapter $parent Installer adapter
*
* @return void
*/
2026-06-06 16:32:28 -05:00
/**
* Called before install/update to preserve the download key.
*
* Joomla re-registers update sites from the manifest on every update,
* which can reset the extra_query (download key). We save it here
* and restore it in postflight.
*/
private ? string $savedDownloadKey = null ;
public function preflight_saveKey () : void
{
try {
$db = Factory :: getDbo ();
$query = $db -> getQuery ( true )
-> select ( $db -> quoteName ( 'us.extra_query' ))
-> from ( $db -> quoteName ( '#__update_sites' , 'us' ))
-> join (
'INNER' ,
$db -> quoteName ( '#__update_sites_extensions' , 'use' )
. ' ON ' . $db -> quoteName ( 'use.update_site_id' ) . ' = ' . $db -> quoteName ( 'us.update_site_id' )
)
-> join (
'INNER' ,
$db -> quoteName ( '#__extensions' , 'e' )
. ' ON ' . $db -> quoteName ( 'e.extension_id' ) . ' = ' . $db -> quoteName ( 'use.extension_id' )
)
-> where ( $db -> quoteName ( 'e.element' ) . ' = ' . $db -> quote ( 'pkg_mokojoombackup' ))
-> where ( $db -> quoteName ( 'e.type' ) . ' = ' . $db -> quote ( 'package' ))
-> setLimit ( 1 );
$db -> setQuery ( $query );
$key = $db -> loadResult ();
if ( ! empty ( $key )) {
$this -> savedDownloadKey = $key ;
}
} catch ( \Throwable $e ) {
2026-06-06 21:10:11 -05:00
error_log ( 'MokoJoomBackup: Could not save download key: ' . $e -> getMessage ());
2026-06-06 16:32:28 -05:00
}
}
2026-06-02 13:47:36 -05:00
public function postflight ( string $type , InstallerAdapter $parent ) : void
{
2026-06-06 16:32:28 -05:00
// Restore download key if it was saved before update
if ( $this -> savedDownloadKey !== null ) {
$this -> restoreDownloadKey ();
}
2026-06-02 13:47:36 -05:00
if ( $type === 'install' ) {
// Enable the system plugin automatically on fresh install
$db = Factory :: getDbo ();
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'system' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-02 13:47:36 -05:00
$db -> setQuery ( $query );
$db -> execute ();
2026-06-02 18:55:12 -05:00
// Enable the quickicon plugin automatically
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'quickicon' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-02 18:55:12 -05:00
$db -> setQuery ( $query );
$db -> execute ();
2026-06-02 13:58:50 -05:00
// Enable the task plugin automatically
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'task' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-02 13:58:50 -05:00
$db -> setQuery ( $query );
$db -> execute ();
2026-06-02 13:47:36 -05:00
// Enable the webservices plugin automatically
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'webservices' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-02 13:47:36 -05:00
$db -> setQuery ( $query );
$db -> execute ();
2026-06-04 11:20:35 -05:00
// Enable the console plugin automatically
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'console' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-04 11:20:35 -05:00
$db -> setQuery ( $query );
$db -> execute ();
// Enable the content plugin automatically
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'content' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-04 11:20:35 -05:00
$db -> setQuery ( $query );
$db -> execute ();
// Enable the actionlog plugin automatically
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__extensions' ))
-> set ( $db -> quoteName ( 'enabled' ) . ' = 1' )
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'plugin' ))
-> where ( $db -> quoteName ( 'folder' ) . ' = ' . $db -> quote ( 'actionlog' ))
2026-06-06 14:52:27 -05:00
-> where ( $db -> quoteName ( 'element' ) . ' = ' . $db -> quote ( 'mokojoombackup' ));
2026-06-04 11:20:35 -05:00
$db -> setQuery ( $query );
$db -> execute ();
2026-06-07 09:38:43 -05:00
// Create and protect default backup directory
2026-06-06 14:52:27 -05:00
$backupDir = JPATH_ADMINISTRATOR . '/components/com_mokojoombackup/backups' ;
2026-06-02 13:47:36 -05:00
if ( ! is_dir ( $backupDir )) {
mkdir ( $backupDir , 0755 , true );
2026-06-07 09:38:43 -05:00
}
if ( is_dir ( $backupDir )) {
$htaccess = $backupDir . '/.htaccess' ;
if ( ! is_file ( $htaccess )) {
file_put_contents ( $htaccess , "# Apache 2.4+ \n <IfModule mod_authz_core.c> \n Require all denied \n </IfModule> \n # Apache 2.2 \n <IfModule !mod_authz_core.c> \n Order deny,allow \n Deny from all \n </IfModule> \n " );
}
$index = $backupDir . '/index.html' ;
2026-06-02 13:47:36 -05:00
2026-06-07 09:38:43 -05:00
if ( ! is_file ( $index )) {
file_put_contents ( $index , '<!DOCTYPE html><title></title>' );
}
2026-06-02 13:47:36 -05:00
}
2026-06-07 09:50:32 -05:00
// Create default scheduled task — every 30 days, profile 1
$this -> createDefaultScheduledTask ();
2026-06-02 13:47:36 -05:00
}
2026-06-04 08:01:05 -05:00
2026-06-07 09:46:27 -05:00
if ( $type === 'uninstall' ) {
return ;
}
2026-06-07 08:58:52 -05:00
// Sync submenu icons in #__menu (Joomla doesn't update icons on upgrades)
$this -> syncMenuIcons ();
2026-06-07 09:46:27 -05:00
// Warn if no license key configured
$this -> warnMissingLicenseKey ();
// Warn if any profile still uses the default backup directory
$this -> warnDefaultBackupDir ();
// Remind user to review backup profile settings
if ( $type === 'install' ) {
$profileUrl = Route :: _ ( 'index.php?option=com_mokojoombackup&view=profiles' );
Factory :: getApplication () -> enqueueMessage (
'<strong>Review Your Backup Settings</strong> — '
. 'A default backup profile has been created. Review the profile settings to configure '
. 'backup type, schedule, storage location, and notifications. '
. '<a href="' . $profileUrl . '" class="btn btn-sm btn-primary ms-2">Review Profiles</a>' ,
'info'
);
}
}
private function warnDefaultBackupDir () : void
{
try {
$db = Factory :: getDbo ();
$query = $db -> getQuery ( true )
-> select ( 'COUNT(*)' )
-> from ( $db -> quoteName ( '#__mokojoombackup_profiles' ))
-> where ( $db -> quoteName ( 'published' ) . ' = 1' )
-> where ( '(' . $db -> quoteName ( 'backup_dir' ) . ' = ' . $db -> quote ( 'administrator/components/com_mokojoombackup/backups' )
. ' OR ' . $db -> quoteName ( 'backup_dir' ) . ' = ' . $db -> quote ( '[DEFAULT_DIR]' )
. ' OR ' . $db -> quoteName ( 'backup_dir' ) . ' = ' . $db -> quote ( '' )
. ' OR ' . $db -> quoteName ( 'backup_dir' ) . ' IS NULL)' );
$db -> setQuery ( $query );
if (( int ) $db -> loadResult () > 0 ) {
$profileUrl = Route :: _ ( 'index.php?option=com_mokojoombackup&view=profiles' );
Factory :: getApplication () -> enqueueMessage (
'<strong>Backup Directory Warning</strong> — '
. 'One or more profiles store backups in the default directory inside the web root. '
. 'For better security, configure a backup directory outside the web root. '
. '<a href="' . $profileUrl . '" class="btn btn-sm btn-warning ms-2">Edit Profiles</a>' ,
'warning'
);
}
} catch ( \Throwable $e ) {
error_log ( 'MokoJoomBackup: warnDefaultBackupDir() failed: ' . $e -> getMessage ());
2026-06-07 06:54:12 -05:00
}
2026-06-04 08:01:05 -05:00
}
2026-06-07 09:50:32 -05:00
private function createDefaultScheduledTask () : void
{
try {
$db = Factory :: getDbo ();
// Check if a MokoJoomBackup task already exists
$query = $db -> getQuery ( true )
-> select ( 'COUNT(*)' )
-> from ( $db -> quoteName ( '#__scheduler_tasks' ))
-> where ( $db -> quoteName ( 'type' ) . ' = ' . $db -> quote ( 'mokojoombackup.run_profile' ));
$db -> setQuery ( $query );
if (( int ) $db -> loadResult () > 0 ) {
return ;
}
$now = date ( 'Y-m-d H:i:s' );
$task = ( object ) [
'title' => 'MokoJoomBackup — Monthly Full Backup' ,
'type' => 'mokojoombackup.run_profile' ,
'execution_rules' => json_encode ([
'rule-type' => 'interval-days' ,
'interval-days' => '30' ,
'exec-day' => '1' ,
'exec-time' => '03:00:00' ,
]),
'cron_rules' => json_encode ([
'type' => 'interval' ,
'exp' => 'P30D' ,
]),
'state' => 1 ,
'params' => json_encode ([
'profile_id' => 1 ,
'individual_log' => true ,
'log_file' => '' ,
'notifications' => [
'success_mail' => '0' ,
'failure_mail' => '1' ,
'notification_failure_groups' => [ '8' ],
'fatal_failure_mail' => '1' ,
'notification_fatal_groups' => [ '8' ],
'orphan_mail' => '0' ,
],
]),
'priority' => 0 ,
'ordering' => 0 ,
'cli_exclusive' => 0 ,
'note' => '' ,
'created' => $now ,
'created_by' => Factory :: getApplication () -> getIdentity () -> id ?? 0 ,
'next_execution' => date ( 'Y-m-d 03:00:00' , strtotime ( '+1 day' )),
];
$db -> insertObject ( '#__scheduler_tasks' , $task );
} catch ( \Throwable $e ) {
error_log ( 'MokoJoomBackup: createDefaultScheduledTask() failed: ' . $e -> getMessage ());
}
}
2026-06-07 08:58:52 -05:00
private function syncMenuIcons () : void
{
$iconMap = [
'view=dashboard' => 'class:home' ,
'view=backups' => 'class:database' ,
'view=profiles' => 'class:cog' ,
];
try {
$db = Factory :: getDbo ();
foreach ( $iconMap as $linkFragment => $icon ) {
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__menu' ))
-> set ( $db -> quoteName ( 'img' ) . ' = ' . $db -> quote ( $icon ))
-> where ( $db -> quoteName ( 'client_id' ) . ' = 1' )
-> where ( $db -> quoteName ( 'link' ) . ' LIKE ' . $db -> quote ( '%com_mokojoombackup%' . $linkFragment . '%' ));
$db -> setQuery ( $query );
$db -> execute ();
}
// Set top-level component menu icon
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__menu' ))
-> set ( $db -> quoteName ( 'img' ) . ' = ' . $db -> quote ( 'class:archive' ))
-> where ( $db -> quoteName ( 'client_id' ) . ' = 1' )
-> where ( $db -> quoteName ( 'link' ) . ' LIKE ' . $db -> quote ( 'index.php?option=com_mokojoombackup' ))
-> where ( $db -> quoteName ( 'level' ) . ' = 1' );
$db -> setQuery ( $query );
$db -> execute ();
} catch ( \Throwable $e ) {
2026-06-07 09:11:39 -05:00
error_log ( 'MokoJoomBackup: syncMenuIcons() failed: ' . $e -> getMessage ());
2026-06-07 08:58:52 -05:00
}
}
2026-06-06 16:32:28 -05:00
/**
* Restore the download key to the (possibly new) update site record.
*/
private function restoreDownloadKey () : void
{
try {
$db = Factory :: getDbo ();
$query = $db -> getQuery ( true )
-> select ( $db -> quoteName ( 'us.update_site_id' ))
-> from ( $db -> quoteName ( '#__update_sites' , 'us' ))
-> join (
'INNER' ,
$db -> quoteName ( '#__update_sites_extensions' , 'use' )
. ' ON ' . $db -> quoteName ( 'use.update_site_id' ) . ' = ' . $db -> quoteName ( 'us.update_site_id' )
)
-> join (
'INNER' ,
$db -> quoteName ( '#__extensions' , 'e' )
. ' ON ' . $db -> quoteName ( 'e.extension_id' ) . ' = ' . $db -> quoteName ( 'use.extension_id' )
)
-> where ( $db -> quoteName ( 'e.element' ) . ' = ' . $db -> quote ( 'pkg_mokojoombackup' ))
-> where ( $db -> quoteName ( 'e.type' ) . ' = ' . $db -> quote ( 'package' ))
-> setLimit ( 1 );
$db -> setQuery ( $query );
$updateSiteId = ( int ) $db -> loadResult ();
if ( $updateSiteId > 0 ) {
$query = $db -> getQuery ( true )
-> update ( $db -> quoteName ( '#__update_sites' ))
-> set ( $db -> quoteName ( 'extra_query' ) . ' = ' . $db -> quote ( $this -> savedDownloadKey ))
-> where ( $db -> quoteName ( 'update_site_id' ) . ' = ' . $updateSiteId );
$db -> setQuery ( $query );
$db -> execute ();
}
} catch ( \Throwable $e ) {
2026-06-06 21:10:11 -05:00
error_log ( 'MokoJoomBackup: Could not restore download key: ' . $e -> getMessage ());
2026-06-06 16:32:28 -05:00
}
}
2026-06-06 17:25:38 -05:00
private function warnMissingLicenseKey () : void
2026-06-04 08:01:05 -05:00
{
2026-06-06 17:25:38 -05:00
try
{
2026-06-04 08:01:05 -05:00
$db = Factory :: getDbo ();
2026-06-06 17:25:38 -05:00
$db -> setQuery (
$db -> getQuery ( true )
-> select ([ $db -> quoteName ( 'update_site_id' ), $db -> quoteName ( 'extra_query' )])
-> from ( $db -> quoteName ( '#__update_sites' ))
-> where ( '(' . $db -> quoteName ( 'name' ) . ' LIKE ' . $db -> quote ( '%MokoJoomBackup%' ) . ' OR ' . $db -> quoteName ( 'location' ) . ' LIKE ' . $db -> quote ( '%MokoJoomBackup%' ) . ')' )
-> setLimit ( 1 )
);
$site = $db -> loadObject ();
2026-06-04 08:01:05 -05:00
2026-06-06 17:25:38 -05:00
if ( $site )
{
$eq = ( string ) ( $site -> extra_query ?? '' );
if ( ! empty ( $eq ) && strpos ( $eq , 'dlid=' ) !== false ) { parse_str ( $eq , $p ); if ( ! empty ( $p [ 'dlid' ])) { return ; } }
$editUrl = 'index.php?option=com_installer&task=updatesite.edit&update_site_id=' . ( int ) $site -> update_site_id ;
2026-06-04 08:01:05 -05:00
}
2026-06-06 17:25:38 -05:00
else
{
$editUrl = 'index.php?option=com_installer&view=updatesites' ;
}
Factory :: getApplication () -> enqueueMessage (
'<strong>Moko Consulting License Key Required</strong> — '
. 'No download key is configured. Updates will not be available until a valid license key is entered. '
. '<a href="' . $editUrl . '" class="btn btn-sm btn-warning ms-2">Enter License Key</a>' ,
'warning'
);
2026-06-04 08:01:05 -05:00
}
2026-06-06 21:10:11 -05:00
catch ( \Throwable $e ) {
error_log ( 'MokoJoomBackup: License key check failed: ' . $e -> getMessage ());
}
2026-06-02 13:47:36 -05:00
}
}