- Fixed errors/violations noticed by PHPMD and PHPCS

- Removed parameter perform_sql from public method controllers/system/extensions/CLI_Manager->installExtension
- Added new public method controllers/system/extensions/CLI_Manager->installExtensionNoSQL
- Improved code quality in controllers/system/extensions/Manager->toggleExtension
- controllers/system/extensions/Manager->uploadExtension better check of
  HTTP POST parameter notPerformSql
- application/views/system/extensions/manager.php renamed checkbox
  performSql to notPerformSql
- application/views/system/extensions/tableWidget.php removed PHP close
  tag at the end of the file
- Improved code quality in application/widgets/TableWidget.php
- Removed private method _getColumnsNames from application/widgets/TableWidget.php
- Improved code quality in application/libraries/ExtensionsLib.php
- Added new private methods _getExtensionsPath and _getUploadPath to application/libraries/ExtensionsLib.php
- Changed application/libraries/ExtensionsLib->_toggleExtension to public method toggleExtension
- Removed private properties UPLOAD_PATH and EXTENSIONS_PATH from application/libraries/ExtensionsLib
- Added use imports in application/libraries/ExtensionsLib
- application/libraries/ExtensionsLib->installExtension changed parameters default values
This commit is contained in:
Paolo
2022-03-04 12:26:37 +01:00
parent 8bbc68ca75
commit 15f9762d01
6 changed files with 235 additions and 240 deletions
+167 -161
View File
@@ -2,6 +2,9 @@
if (! defined('BASEPATH')) exit('No direct script access allowed');
use \stdClass as stdClass;
use \PharData as PharData;
/**
* Library to manage core extensions
*/
@@ -15,11 +18,18 @@ class ExtensionsLib
const EXTENSION_JSON_NAME = 'extension.json'; // file that contains extension data
const EXTENSIONS_DIR_NAME = 'extensions'; // name of the directories where will be created the symlinks
private $_ci;
const UPLOAD_PATH = 'tmp/';
private $ARCHIVE_EXTENSIONS = array('.tgz', '.tbz2'); // accepted file extensions for an uploaded extension
private $UPLOAD_PATH; // temporary directory to store the upload file and checks the archive
private $EXTENSIONS_PATH; // directory where all the extensions are
private $_ci; // Code igniter instance
private $_errorOccurred; // boolean, true if an error occurred while installing an extension
private $_currentInstalledExtensionVersion; // contains the version of the current installation of an extension
// NOTE: the following have been declared as properties to maintain compatibility with previous PHP versions
// where arrays cannot be declared as constants
// Accepted file extensions for an uploaded extension
private $ARCHIVE_EXTENSIONS = array('.tgz', '.tbz2');
// Directories that are part of the extension archive
private $SOFTLINK_TARGET_DIRECTORIES = array(
@@ -27,17 +37,11 @@ class ExtensionsLib
DOC_ROOT => array('public')
);
private $_errorOccurred; // boolean, true if an error occurred while installing an extension
private $_currentInstalledExtensionVersion; // contains the version of the current installation of an extension
/**
* Class constructor
*/
public function __construct()
{
$this->UPLOAD_PATH = APPPATH.'tmp/';
$this->EXTENSIONS_PATH = APPPATH.'extensions/';
// Get code igniter instance
$this->_ci =& get_instance();
@@ -72,20 +76,20 @@ class ExtensionsLib
* @param $extensionName string Name of Extension (optional)
* @param $filename Path to tgz Extension File (optional)
*/
public function installExtension($extensionName = null, $filename = null, $perform_sql = true)
public function installExtension($extensionName, $filename, $perform_sql)
{
$extensionDB = null; // contains data from DB about an extension
$extensionJson = null; // contains the extension.json data
$this->_printInfo('WARNING!!! Please do not change page or stop this procedure before it is finished');
if (!is_null($extensionName) && !is_null($filename))
{
$uploadData = new stdClass();
$uploadData->fullPath = $filename;
$uploadData->extensionName = $extensionName;
}
else
// Create an object with the given paramenters
$uploadData = new stdClass();
$uploadData->fullPath = $filename;
$uploadData->extensionName = $extensionName;
// If no extension name or file name are provided
if (is_null($extensionName) || is_null($filename))
{
$this->_loadUploadLibrary(); // loads CI upload library
$uploadData = $this->_uploadExtension(); // perform the upload of the file and returns info about it
@@ -98,7 +102,8 @@ class ExtensionsLib
$uploadData = null; // then it is a wrong one!
}
if ($uploadData != null) // if no error occurred
// If the no error occurred
if ($uploadData != null)
{
$this->_extractExtension($uploadData->fullPath); // extract the archive of the uploaded extension
@@ -128,13 +133,14 @@ class ExtensionsLib
// Remove any previous installation from file system and database
$this->_cleanPreviousInstallation($extensionJson);
$this->_installExtension($extensionJson); // records extension data in DB
// Records extension data in DB
$this->_installExtension($extensionJson);
if (!$this->_errorOccurred && $perform_sql === true) // if no error occurred
{
// Loads and executes neede SQL scripts
// Loads and executes needed SQL scripts
$this->_loadSQLs(
$this->UPLOAD_PATH.$extensionJson->name.'/'.ExtensionsLib::SQL_DIRECTORY,
$this->_getUploadPath().$extensionJson->name.DIRECTORY_SEPARATOR.ExtensionsLib::SQL_DIRECTORY,
$extensionJson
);
}
@@ -151,14 +157,6 @@ class ExtensionsLib
$this->_createSymLinks($extensionJson->name);
}
}
else
{
$this->_errorOccurred = true;
}
}
else
{
$this->_errorOccurred = true;
}
if ($this->_errorOccurred === false) // if no errors occurred
@@ -193,16 +191,21 @@ class ExtensionsLib
if (getData($result)[0]->server_kurzbz != SERVER_NAME) return false;
$extensionName = getData($result)[0]->name; // extension name
$this->_delSoftLinks($extensionName); // not to be checked, could fail if the extension is disabled
// remove the extension from the extensions installation directory
$delExtension = $this->_rrm($this->EXTENSIONS_PATH.$extensionName);
// Not to be checked, could fail if the extension is disabled
$this->_delSoftLinks($extensionName);
// Remove the extension from the extensions installation directory
$delExtension = $this->_rrm($this->_getExtensionsPath().$extensionName);
// Select all the version of this extension
$this->_ci->ExtensionsModel->addSelect('extension_id');
$result = $this->_ci->ExtensionsModel->loadWhere(array('name' => $extensionName, 'server_kurzbz' => SERVER_NAME));
if (hasData($result)) // if something was found
// If something was found
if (hasData($result))
{
foreach (getData($result) as $key => $extension) // loops on them
foreach (getData($result) as $extension) // loops on them
{
// Remove them all
$result = $this->_ci->ExtensionsModel->delete($extension->extension_id);
@@ -230,19 +233,49 @@ class ExtensionsLib
}
/**
* To enable an extension
* To enable/disable an extension
*/
public function enableExtension($extensionId)
public function toggleExtension($extensionId, $enabled)
{
return $this->_toggleExtension($extensionId, true);
}
$_toggleExtension = false;
/**
* To disable an extension
*/
public function disableExtension($extensionId)
{
return $this->_toggleExtension($extensionId, false);
// Loads data from DB about the given extension
$result = $this->_ci->ExtensionsModel->load($extensionId);
if (hasData($result))
{
// If this server is _not_ the same where the extension was installed then exit with a failure
if (getData($result)[0]->server_kurzbz != SERVER_NAME) return false;
$extensionName = getData($result)[0]->name; // extension name
// If to be enabled
if ($enabled === true)
{
// Add the symlinks
$_toggleExtension = $this->_addSoftLinks($extensionName);
}
else // If to be disabled
{
// Remove all the symlinks
$_toggleExtension = $this->_delSoftLinks($extensionName);
}
if ($_toggleExtension) // if is a success
{
// Updates DB
$result = $this->_ci->ExtensionsModel->update($extensionId, array('enabled' => $enabled));
if (isSuccess($result))
{
$_toggleExtension = true;
}
else // if DB update fails remove symlinks from file system
{
$this->_delSoftLinks($extensionName);
}
}
}
return $_toggleExtension;
}
// -------------------------------------------------------------------------------------------------
@@ -256,7 +289,7 @@ class ExtensionsLib
$this->_ci->load->library(
'upload',
array(
'upload_path' => $this->UPLOAD_PATH,
'upload_path' => $this->_getUploadPath(),
'allowed_types' => '*',
'overwrite' => true
)
@@ -320,7 +353,7 @@ class ExtensionsLib
// Extracts the uploaded file
$pd = new PharData($uploadPath);
$pd->extractTo($this->UPLOAD_PATH, null, true);
$pd->extractTo($this->_getUploadPath(), null, true);
}
catch (UnexpectedValueException $uva)
{
@@ -375,18 +408,14 @@ class ExtensionsLib
$this->_errorOccurred = true;
$this->_printFailure('data base error: '.getData($result));
}
else // otherwise
elseif (hasData($result))
{
if (hasData($result)) // if found
{
$extensionDB = getData($result)[0]; // return it!
}
else
{
$this->_printMessage('not found');
}
$extensionDB = getData($result)[0];
}
// If no data have been found
if ($extensionDB == null) $this->_printMessage('not found');
$this->_printSuccess(!$this->_errorOccurred);
$this->_printEnd();
@@ -402,10 +431,10 @@ class ExtensionsLib
$this->_printStart('Checking extension file system structure');
// Checks if the root directory of this archive has the same name of the extension
if (is_dir($this->UPLOAD_PATH.$extensionName))
if (is_dir($this->_getUploadPath().$extensionName))
{
// Checks if file extension.json exists inside the uploaded archive
if (!file_exists($this->UPLOAD_PATH.$extensionName.'/'.ExtensionsLib::EXTENSION_JSON_NAME))
if (!file_exists($this->_getUploadPath().$extensionName.DIRECTORY_SEPARATOR.ExtensionsLib::EXTENSION_JSON_NAME))
{
$this->_errorOccurred = true;
$this->_printFailure('missing extension.json');
@@ -427,11 +456,14 @@ class ExtensionsLib
*/
private function _chkExtensionJson($extensionName, $extensionDB)
{
// Set a default FHComplete version
$fhcomplete_version = 0;
$this->_printStart('Parsing and checking extension.json');
// Decodes extension.json
$extensionJson = json_decode(
file_get_contents($this->UPLOAD_PATH.$extensionName.'/'.ExtensionsLib::EXTENSION_JSON_NAME)
file_get_contents($this->_getUploadPath().$extensionName.DIRECTORY_SEPARATOR.ExtensionsLib::EXTENSION_JSON_NAME)
);
// Checks if the parameter name of the extension.json has the same value of the extension name
@@ -634,8 +666,11 @@ class ExtensionsLib
// Loops through the versions
for ($sqlDir = $startVersion; $sqlDir <= $extensionJson->version; $sqlDir++)
{
// Gets all the SQL files in the directory having the same name of the version
$files = glob($pkgSQLsPath.DIRECTORY_SEPARATOR.$sqlDir.'/*'.ExtensionsLib::SQL_FILE_EXTENSION);
// If a directory with the same value of the version is present in the sql scripts directory
if (($files = glob($pkgSQLsPath.'/'.$sqlDir.'/*'.ExtensionsLib::SQL_FILE_EXTENSION)) != false)
if ($files != false)
{
// Loads every sql files
foreach ($files as $file)
@@ -646,7 +681,10 @@ class ExtensionsLib
$this->_printMessage($sql);
// Try to execute that
if (!isSuccess($result = @$this->_ci->ExtensionsModel->executeQuery($sql)))
$result = @$this->_ci->ExtensionsModel->executeQuery($sql);
// If was not a success
if (!isSuccess($result))
{
$this->_errorOccurred = true;
$this->_printFailure(' error occurred while executing the query');
@@ -660,7 +698,7 @@ class ExtensionsLib
$this->_ci->eprintflib->printEOL();
}
}
}
}
}
$this->_printSuccess(!$this->_errorOccurred);
@@ -675,12 +713,12 @@ class ExtensionsLib
{
$this->_printStart('Moving the upload extension from upload folder to extension folder');
$this->_printMessage('Current extension directory: '.$this->UPLOAD_PATH.$extensionName);
$this->_printMessage('Directory where it will be moved: '.$this->EXTENSIONS_PATH.$extensionName);
$this->_printMessage('Current extension directory: '.$this->_getUploadPath().$extensionName);
$this->_printMessage('Directory where it will be moved: '.$this->_getExtensionsPath().$extensionName);
if (!file_exists($this->EXTENSIONS_PATH.$extensionName))
if (!file_exists($this->_getExtensionsPath().$extensionName))
{
if (rename($this->UPLOAD_PATH.$extensionName.'/', $this->EXTENSIONS_PATH.$extensionName))
if (rename($this->_getUploadPath().$extensionName.DIRECTORY_SEPARATOR, $this->_getExtensionsPath().$extensionName))
{
$this->_printSuccess(true);
}
@@ -722,13 +760,21 @@ class ExtensionsLib
{
$_delSoftLinks = false;
// For every set of target directories where:
// rootPath: is the prefix of the absolute path for the target directory
// targetDirectories: is an array of target directories
foreach ($this->SOFTLINK_TARGET_DIRECTORIES as $rootPath => $targetDirectories)
{
foreach ($targetDirectories as $key => $targetDirectory)
// For every target directory in the current set
foreach ($targetDirectories as $targetDirectory)
{
if (file_exists($rootPath.$targetDirectory.'/'.ExtensionsLib::EXTENSIONS_DIR_NAME.'/'.$extensionName))
// If the symlink exists
if (file_exists($rootPath.$targetDirectory.DIRECTORY_SEPARATOR.ExtensionsLib::EXTENSIONS_DIR_NAME.DIRECTORY_SEPARATOR.$extensionName))
{
$_delSoftLinks = unlink($rootPath.$targetDirectory.'/'.ExtensionsLib::EXTENSIONS_DIR_NAME.'/'.$extensionName);
// Try to delete it
$_delSoftLinks = unlink(
$rootPath.$targetDirectory.DIRECTORY_SEPARATOR.ExtensionsLib::EXTENSIONS_DIR_NAME.DIRECTORY_SEPARATOR.$extensionName
);
}
}
}
@@ -741,29 +787,23 @@ class ExtensionsLib
*/
private function _rrm($dir)
{
if (!file_exists($dir))
// If the directory does not exist return success
if (!file_exists($dir)) return true;
// If it is not a directory then delete it and return the result
if (!is_dir($dir)) return unlink($dir);
// For each subdirectory
foreach (scandir($dir) as $subdir)
{
return true;
}
if (!is_dir($dir))
{
return unlink($dir);
}
foreach (scandir($dir) as $item)
{
if ($item == '.' || $item == '..')
{
continue;
}
if (!$this->_rrm($dir.DIRECTORY_SEPARATOR.$item))
{
return false;
}
// If it is the same directory or the parent directory skip to the next one
if ($subdir == '.' || $subdir == '..') continue;
// Try to remove a subdirectory, if it fails then return a failure
if (!$this->_rrm($dir.DIRECTORY_SEPARATOR.$subdir)) return false;
}
// Delete the directory and return the result
return rmdir($dir);
}
@@ -773,42 +813,41 @@ class ExtensionsLib
private function _addSoftLinks($extensionName)
{
$_addSoftLinks = false;
$extensionPath = $this->EXTENSIONS_PATH.$extensionName.'/';
$extensionPath = $this->_getExtensionsPath().$extensionName.DIRECTORY_SEPARATOR;
// For every target directory
// For every set of target directories where:
// rootPath: is the prefix of the absolute path for the target directory
// targetDirectories: is an array of target directories
foreach ($this->SOFTLINK_TARGET_DIRECTORIES as $rootPath => $targetDirectories)
{
foreach ($targetDirectories as $key => $targetDirectory)
// For every target directory in the current set
foreach ($targetDirectories as $targetDirectory)
{
// If destination of the symlink does not exist
if (!file_exists($rootPath.$targetDirectory.'/'.ExtensionsLib::EXTENSIONS_DIR_NAME.'/'.$extensionName))
// Checks if the link already exists
$_addSoftLinks = file_exists(
$rootPath.$targetDirectory.DIRECTORY_SEPARATOR.ExtensionsLib::EXTENSIONS_DIR_NAME.DIRECTORY_SEPARATOR.$extensionName
);
// If the symlink does not exist
if (!$_addSoftLinks)
{
// If the target directory does not exist than creates that
if (!is_dir($extensionPath.$targetDirectory))
{
mkdir($extensionPath.$targetDirectory);
}
// If the target directory does not exist than creates it
if (!is_dir($extensionPath.$targetDirectory)) mkdir($extensionPath.$targetDirectory);
if (!file_exists($rootPath.$targetDirectory.'/'.ExtensionsLib::EXTENSIONS_DIR_NAME.'/'.$extensionName))
{
// Create the symlink
$_addSoftLinks = symlink(
$extensionPath.$targetDirectory,
$rootPath.$targetDirectory.'/'.ExtensionsLib::EXTENSIONS_DIR_NAME.'/'.$extensionName
);
}else
{
$_addSoftLinks = true;
}
// Create the symlink
$_addSoftLinks = @symlink(
// Target
$extensionPath.$targetDirectory,
// Link
$rootPath.$targetDirectory.DIRECTORY_SEPARATOR.ExtensionsLib::EXTENSIONS_DIR_NAME.DIRECTORY_SEPARATOR.$extensionName
);
// On failure
if (!$_addSoftLinks)
{
log_message('error', 'Failed to create Symlink to '.$extensionPath.$targetDirectory);
break;
}
}else
{
$_addSoftLinks = true;
}
}
}
@@ -833,9 +872,9 @@ class ExtensionsLib
$this->_printMessage('Removing the extracted data from the upload directory');
if ($uploadData != null
&& isset($uploadData->extensionName)
&& file_exists($this->UPLOAD_PATH.$uploadData->extensionName))
&& file_exists($this->_getUploadPath().$uploadData->extensionName))
{
$this->_rrm($this->UPLOAD_PATH.$uploadData->extensionName);
$this->_rrm($this->_getUploadPath().$uploadData->extensionName);
}
// If the upload of the file is a success and the extension name is present and no previous installation were found
@@ -852,13 +891,10 @@ class ExtensionsLib
$this->delExtension(getData($result)[0]->extension_id);
}
}
else // otherwise
// Otherwise remove them all only from DB
elseif ($extensionJson != null && isset($extensionJson->extension_id))
{
// Remove them all only from DB
if ($extensionJson != null && isset($extensionJson->extension_id))
{
$this->_ci->ExtensionsModel->delete($extensionJson->extension_id);
}
$this->_ci->ExtensionsModel->delete($extensionJson->extension_id);
}
$this->_printMessage('Rollback finished');
@@ -867,49 +903,19 @@ class ExtensionsLib
}
/**
* To enable/disable an extension
* Returns the absolute upload path
*/
private function _toggleExtension($extensionId, $enabled)
private function _getUploadPath()
{
$_toggleExtension = false;
return APPPATH.self::UPLOAD_PATH;
}
// Loads data from DB about the given extension
$result = $this->_ci->ExtensionsModel->load($extensionId);
if (hasData($result))
{
// If this server is _not_ the same where the extension was installed then exit with a failure
if (getData($result)[0]->server_kurzbz != SERVER_NAME) return false;
$extensionName = getData($result)[0]->name; // extension name
// If to be enabled
if ($enabled === true)
{
// Add the symlinks
$_toggleExtension = $this->_addSoftLinks($extensionName);
}
else // If to be disabled
{
// Remove all the symlinks
$_toggleExtension = $this->_delSoftLinks($extensionName);
}
if ($_toggleExtension) // if is a success
{
// Updates DB
$result = $this->_ci->ExtensionsModel->update($extensionId, array('enabled' => $enabled));
if (isSuccess($result))
{
$_toggleExtension = true;
}
else // if DB update fails remove symlinks from file system
{
$this->_delSoftLinks($extensionName);
}
}
}
return $_toggleExtension;
/**
* Returns the absolute extensions path
*/
private function _getExtensionsPath()
{
return APPPATH.self::EXTENSIONS_DIR_NAME.DIRECTORY_SEPARATOR;
}
/**