From: Marcin Haba Date: Mon, 12 Aug 2019 04:32:11 +0000 (+0200) Subject: baculum: Improve checking dependencies X-Git-Tag: Release-9.6.0~144 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77111237d1f0878b2cd21078ee0725547bdf5b28;p=thirdparty%2Fbacula.git baculum: Improve checking dependencies - Add dependencies checking to API - Remove PHP Multibyte String (mbstring) extension from dependencies - Prepare general class to check common API and Web dependencies --- diff --git a/gui/baculum/examples/rpm-template/baculum.spec b/gui/baculum/examples/rpm-template/baculum.spec index d1ad17f72..8f90ac6bc 100644 --- a/gui/baculum/examples/rpm-template/baculum.spec +++ b/gui/baculum/examples/rpm-template/baculum.spec @@ -20,7 +20,6 @@ BuildRequires: checkpolicy Requires: php >= 5.3.4 Requires: php-bcmath Requires: php-common -Requires: php-mbstring Requires: php-mysqlnd Requires: php-pdo Requires: php-pgsql @@ -69,7 +68,6 @@ Requires: php >= 5.3.4 Requires: php-common Requires: php-json Requires: php-bcmath -Requires: php-mbstring Requires: php-xml %description web diff --git a/gui/baculum/examples/rpm/baculum.spec b/gui/baculum/examples/rpm/baculum.spec index d1ad17f72..8f90ac6bc 100644 --- a/gui/baculum/examples/rpm/baculum.spec +++ b/gui/baculum/examples/rpm/baculum.spec @@ -20,7 +20,6 @@ BuildRequires: checkpolicy Requires: php >= 5.3.4 Requires: php-bcmath Requires: php-common -Requires: php-mbstring Requires: php-mysqlnd Requires: php-pdo Requires: php-pgsql @@ -69,7 +68,6 @@ Requires: php >= 5.3.4 Requires: php-common Requires: php-json Requires: php-bcmath -Requires: php-mbstring Requires: php-xml %description web diff --git a/gui/baculum/protected/API/Class/BaculumAPIPage.php b/gui/baculum/protected/API/Class/BaculumAPIPage.php index 00ec70655..3cf84f466 100644 --- a/gui/baculum/protected/API/Class/BaculumAPIPage.php +++ b/gui/baculum/protected/API/Class/BaculumAPIPage.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2016 Kern Sibbald + * Copyright (C) 2013-2019 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -22,6 +22,7 @@ session_start(); +Prado::using('Application.API.Pages.Requirements'); Prado::using('Application.Common.Class.BaculumPage'); Prado::using('Application.API.Class.APIConfig'); diff --git a/gui/baculum/protected/API/Pages/Requirements.php b/gui/baculum/protected/API/Pages/Requirements.php new file mode 100644 index 000000000..5b399a1f4 --- /dev/null +++ b/gui/baculum/protected/API/Pages/Requirements.php @@ -0,0 +1,61 @@ + 'bcmath', + 'help_msg' => 'Please install PHP BCMath module.' + ) + ); + + public function __construct($app_dir, $base_dir) { + parent::__construct($app_dir, $base_dir); + $this->validateEnvironment(); + parent::showResult('Baculum API'); + } + + /** + * Validate all API environment depenencies. + * + * @return none + */ + public function validateEnvironment() { + parent::validateExtensions($this->req_exts); + } +} +$service_dir = dirname(__DIR__); +new Requirements(APPLICATION_DIRECTORY, $service_dir); +?> diff --git a/gui/baculum/protected/Common/Class/GeneralRequirements.php b/gui/baculum/protected/Common/Class/GeneralRequirements.php new file mode 100644 index 000000000..78ee2a16b --- /dev/null +++ b/gui/baculum/protected/Common/Class/GeneralRequirements.php @@ -0,0 +1,146 @@ + 'json', + 'help_msg' => 'Please install PHP JSON module.' + ), + array( + 'ext' => 'dom', + 'help_msg' => 'Please install PHP DOM module to support XML documents (usually included in php-xml binary package).' + ) + ); + + /** + * Required read/write access for these application directories. + */ + private $req_app_rw_dirs = array( + 'assets', 'protected/runtime', + ); + + /** + * Required read/wrie access for these directories in base directory. + */ + private $req_base_rw_dirs = array( + 'Config', 'Logs', + ); + + /** + * Generic help message for directories without fulfilled dependencies. + */ + const DIR_HELP_MSG = 'Please make readable and writeable by the web server user the following directory: %s'; + + /** + * This static variable stores all dependency messages to show on the page. + * If empty, dependencies are fulfilled. + */ + protected static $requirements = array(); + + public function __construct($app_dir, $base_dir) { + $this->validateEnvironment($app_dir, $base_dir); + } + + /** + * Validate all environment depenencies. + * + * @param string $app_dir full path to main application directory + * @param string $base_dir full path to service specific base directory + * @return none + */ + private function validateEnvironment($app_dir, $base_dir) { + $this->validateDirectories($app_dir, $base_dir); + $this->validateExtensions($this->req_exts); + } + + /** + * Validate directory access depenencies. + * + * @param string $app_dir full path to main application directory + * @param string $base_dir full path to service specific base directory + * @return none + */ + private function validateDirectories($app_dir, $base_dir) { + for ($i = 0; $i < count($this->req_app_rw_dirs); $i++) { + $dir = $app_dir . '/' . $this->req_app_rw_dirs[$i]; + if (is_readable($dir) && is_writeable($dir)) { + // test passed, skip + continue; + } + self::$requirements[] = sprintf(self::DIR_HELP_MSG, $dir); + } + for ($i = 0; $i < count($this->req_base_rw_dirs); $i++) { + $dir = $base_dir . '/' . $this->req_base_rw_dirs[$i]; + if (is_readable($dir) && is_writeable($dir)) { + // test passed, skip + continue; + } + self::$requirements[] = sprintf(self::DIR_HELP_MSG, $dir); + } + } + + /** + * Validate PHP extensions. + * + * @param array $req_exts extension list + * @return none + */ + protected static function validateExtensions($req_exts) { + for ($i = 0; $i < count($req_exts); $i++) { + if (!extension_loaded($req_exts[$i]['ext'])) { + self::$requirements[] = $req_exts[$i]['help_msg']; + } + } + } + + /** + * Simple method to show results. + * + * @param string $product product name ('Baculum Web' or 'Baculum API'...etc.) + * @return none + */ + protected static function showResult($product) { + if (count(self::$requirements) > 0) { + echo '

' . $product . ' - Missing dependencies

'; + echo 'To run ' . $product . ' please correct above requirements and refresh this page in web browser.'; + echo ''; + exit(); + } + } +} +?> diff --git a/gui/baculum/protected/Web/Class/BaculumWebPage.php b/gui/baculum/protected/Web/Class/BaculumWebPage.php index b461c6488..371abecc9 100644 --- a/gui/baculum/protected/Web/Class/BaculumWebPage.php +++ b/gui/baculum/protected/Web/Class/BaculumWebPage.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2018 Kern Sibbald + * Copyright (C) 2013-2019 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -22,6 +22,7 @@ session_start(); +Prado::using('Application.Web.Pages.Requirements'); Prado::using('Application.Common.Class.BaculumPage'); Prado::using('Application.Web.Init'); Prado::using('Application.Web.Class.WebConfig'); diff --git a/gui/baculum/protected/Web/Init.php b/gui/baculum/protected/Web/Init.php index 54139122c..94271cdbc 100644 --- a/gui/baculum/protected/Web/Init.php +++ b/gui/baculum/protected/Web/Init.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2016 Kern Sibbald + * Copyright (C) 2013-2019 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -40,10 +40,4 @@ if (!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['PHP_AUTH_PW']) && isse // initialize required auth superglobal $_SERVER array list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', $decoded_credentials); } - -// Check requirements and if are some needed then show requirements page -require_once('Pages/Requirements.php'); -$service_dir = __DIR__; -new Requirements(APPLICATION_DIRECTORY, $service_dir); - ?> diff --git a/gui/baculum/protected/Web/Pages/Requirements.php b/gui/baculum/protected/Web/Pages/Requirements.php index ec1ecfd4a..0befa5c3c 100644 --- a/gui/baculum/protected/Web/Pages/Requirements.php +++ b/gui/baculum/protected/Web/Pages/Requirements.php @@ -3,7 +3,7 @@ * Bacula(R) - The Network Backup Solution * Baculum - Bacula web interface * - * Copyright (C) 2013-2016 Kern Sibbald + * Copyright (C) 2013-2019 Kern Sibbald * * The main author of Baculum is Marcin Haba. * The original author of Bacula is Kern Sibbald, with contributions @@ -19,72 +19,45 @@ * * Bacula(R) is a registered trademark of Kern Sibbald. */ - -class Requirements { - const ASSETS_DIR = 'assets'; - const CONFIG_DIR = 'Config'; - const LOG_DIR = 'Logs'; - const RUNTIME_DIR = 'protected/runtime'; - public function __construct($app_dir, $base_dir) { - $app_dir .= '/'; - $base_dir .= '/'; - $this->validateEnvironment($app_dir, $base_dir); - } - - private function validateEnvironment($app_dir, $base_dir) { - $requirements = array(); - $assets_dir = $app_dir . self::ASSETS_DIR; - if(!is_writable(self::ASSETS_DIR)) { - $requirements[] = 'Please make writable by the web server next directory: ' . $assets_dir . ''; - } - - $config_dir = $base_dir . self::CONFIG_DIR; - if(!is_writable($config_dir)) { - $requirements[] = 'Please make writable by the web server next directory: ' . $config_dir . ''; - } - - $log_dir = $base_dir . self::LOG_DIR; - if(!is_writable($log_dir)) { - $requirements[] = 'Please make writable by the web server next directory: ' . $log_dir . ''; - } - - $runtime_dir = $app_dir . self::RUNTIME_DIR; - if(!is_writable($runtime_dir)) { - $requirements[] = 'Please make writable by the web server next directory: ' . $runtime_dir . ''; - } - - if(!function_exists('curl_init') || !function_exists('curl_setopt') || !function_exists('curl_exec') || !function_exists('curl_close')) { - $requirements[] = 'Please install cURL PHP module.'; - } +Prado::using('Application.Common.Class.GeneralRequirements'); - if(!function_exists('bcmul') || !function_exists('bcpow')) { - $requirements[] = 'Please install BCMath PHP module.'; - } - - if(!function_exists('mb_strlen')) { - $requirements[] = 'Please install MB String PHP module for support for multi-byte string handling to PHP.'; - } - - if(!function_exists('json_decode')) { - $requirements[] = 'Please install Module for JSON functions in PHP scripts.'; - } +/** + * Web part requirements class. + */ +class Requirements extends GeneralRequirements { + + /** + * Required PHP extensions. + * + * Note, requirements page is visible before any language is set and before + * translation engine initialization. From this reason all messages are not + * translated. + */ + private $req_exts = array( + array( + 'ext' => 'curl', + 'help_msg' => 'Please install PHP cURL module.' + ) + ); - if(!class_exists('DOMDocument')) { - $requirements[] = 'Please install PHP DOM XML to support XML documents (usually included in php-xml binary package).'; - } + public function __construct($app_dir, $base_dir) { + parent::__construct($app_dir, $base_dir); + $this->validateEnvironment(); + parent::showResult('Baculum Web'); + } - if(count($requirements) > 0) { - echo '

Baculum - Missing dependencies

'; - echo 'To run Baculum please correct above requirements and refresh this page in web browser.'; - echo ''; - exit(); - } + /** + * Validate all Web environment depenencies. + * + * @return none + */ + public function validateEnvironment() { + parent::validateExtensions($this->req_exts); } } + +// Check requirements and if are some needed then show requirements page +$service_dir = dirname(__DIR__); +new Requirements(APPLICATION_DIRECTORY, $service_dir); ?>