mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 15:22:17 +02:00
N°8796 - Add PHP code style validation in iTop and extensions - format whole code base
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
// Copyright (C) 2014-2024 Combodo SAS
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
@@ -14,30 +15,24 @@
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
require_once(APPROOT.'setup/setuputils.class.inc.php');
|
||||
require_once(APPROOT.'setup/backup.class.inc.php');
|
||||
require_once(APPROOT.'core/mutex.class.inc.php');
|
||||
|
||||
|
||||
define('BACKUP_DEFAULT_FORMAT', '__DB__-%Y-%m-%d_%H_%M');
|
||||
|
||||
class BackupHandler extends ModuleHandlerAPI
|
||||
{
|
||||
public static function OnMetaModelStarted()
|
||||
{
|
||||
try
|
||||
{
|
||||
try {
|
||||
$oRestoreMutex = new iTopMutex('restore.'.utils::GetCurrentEnvironment());
|
||||
if ($oRestoreMutex->IsLocked())
|
||||
{
|
||||
if ($oRestoreMutex->IsLocked()) {
|
||||
IssueLog::Info(__class__.'::'.__function__.' A user is trying to use iTop while a restore is running. The requested page is in read-only mode.');
|
||||
MetaModel::GetConfig()->Set('access_mode', ACCESS_READONLY, 'itop-backup');
|
||||
MetaModel::GetConfig()->Set('access_message', ' - '.dict::S('bkp-restore-running'), 'itop-backup');
|
||||
}
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
} catch (Exception $e) {
|
||||
IssueLog::Error(__class__.'::'.__function__.' Failed to check if a backup/restore is running: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -65,22 +60,20 @@ class DBBackupScheduled extends DBBackup
|
||||
*/
|
||||
public function ListFiles($sBackupDir)
|
||||
{
|
||||
$aFiles = array();
|
||||
$aTimes = array();
|
||||
$aFiles = [];
|
||||
$aTimes = [];
|
||||
// Legacy format -limited to 4 Gb
|
||||
foreach(glob($sBackupDir.'*.zip') as $sFilePath)
|
||||
{
|
||||
foreach (glob($sBackupDir.'*.zip') as $sFilePath) {
|
||||
$aFiles[] = $sFilePath;
|
||||
$aTimes[] = filemtime($sFilePath); // unix time
|
||||
}
|
||||
// Modern format
|
||||
foreach(glob($sBackupDir.'*.tar.gz') as $sFilePath)
|
||||
{
|
||||
foreach (glob($sBackupDir.'*.tar.gz') as $sFilePath) {
|
||||
$aFiles[] = $sFilePath;
|
||||
$aTimes[] = filemtime($sFilePath); // unix time
|
||||
}
|
||||
array_multisort($aTimes, $aFiles);
|
||||
|
||||
|
||||
return $aFiles;
|
||||
}
|
||||
}
|
||||
@@ -107,20 +100,14 @@ class BackupExec extends AbstractWeeklyScheduledProcess
|
||||
*/
|
||||
public function __construct($sBackupDir = null, $iRetentionCount = null)
|
||||
{
|
||||
if (is_null($sBackupDir))
|
||||
{
|
||||
if (is_null($sBackupDir)) {
|
||||
$this->sBackupDir = APPROOT.'data/backups/auto/';
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$this->sBackupDir = $sBackupDir;
|
||||
}
|
||||
if (is_null($iRetentionCount))
|
||||
{
|
||||
if (is_null($iRetentionCount)) {
|
||||
$this->iRetentionCount = MetaModel::GetConfig()->GetModuleSetting($this->GetModuleName(), 'retention_count', 5);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$this->iRetentionCount = $iRetentionCount;
|
||||
}
|
||||
}
|
||||
@@ -135,53 +122,43 @@ class BackupExec extends AbstractWeeklyScheduledProcess
|
||||
$oMutex = new iTopMutex('backup.'.utils::GetCurrentEnvironment());
|
||||
$oMutex->Lock();
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
// Make sure the target directory exists
|
||||
SetupUtils::builddir($this->sBackupDir);
|
||||
|
||||
|
||||
$oBackup = new DBBackupScheduled();
|
||||
|
||||
// Eliminate files exceeding the retention setting
|
||||
//
|
||||
if ($this->iRetentionCount > 0)
|
||||
{
|
||||
if ($this->iRetentionCount > 0) {
|
||||
$aFiles = $oBackup->ListFiles($this->sBackupDir);
|
||||
while (count($aFiles) >= $this->iRetentionCount)
|
||||
{
|
||||
while (count($aFiles) >= $this->iRetentionCount) {
|
||||
$sFileToDelete = array_shift($aFiles);
|
||||
unlink($sFileToDelete);
|
||||
if (file_exists($sFileToDelete))
|
||||
{
|
||||
if (file_exists($sFileToDelete)) {
|
||||
// Ok, do not loop indefinitely on this
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Do execute the backup
|
||||
//
|
||||
$oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting($this->GetModuleName(), 'mysql_bindir', ''));
|
||||
|
||||
$sBackupFileFormat = MetaModel::GetConfig()->GetModuleSetting($this->GetModuleName(), 'file_name_format', '__DB__-%Y-%m-%d_%H_%M');
|
||||
$sName = $oBackup->MakeName($sBackupFileFormat);
|
||||
if ($sName == '')
|
||||
{
|
||||
if ($sName == '') {
|
||||
$sName = $oBackup->MakeName(BACKUP_DEFAULT_FORMAT);
|
||||
}
|
||||
$sBackupFile = $this->sBackupDir.$sName;
|
||||
$sSourceConfigFile = APPCONF.utils::GetCurrentEnvironment().'/'.ITOP_CONFIG_FILE;
|
||||
try
|
||||
{
|
||||
try {
|
||||
$oBackup->CreateCompressedBackup($sBackupFile, $sSourceConfigFile);
|
||||
}
|
||||
catch (BackupException $e)
|
||||
{
|
||||
} catch (BackupException $e) {
|
||||
throw new ProcessFatalException($e->getMessage());
|
||||
}
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
} catch (Exception $e) {
|
||||
$oMutex->Unlock();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user