From 9e6048c9617f59461b10d9d10b9084ba591dfa2c Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 8 Nov 2023 05:32:13 +0100 Subject: [PATCH] Rely on static methods rather than constants comparison Impacts: - DEV mode - DEMO mode - GALETTE_SQL_DEBUG - GALETTE_NIGHTLY --- galette/includes/dependencies.php | 6 +-- galette/includes/galette.inc.php | 2 +- galette/includes/i18n.inc.php | 15 +++---- galette/includes/main.inc.php | 2 +- .../Crud/DynamicFieldsController.php | 5 ++- .../Controllers/Crud/MailingsController.php | 4 +- .../Galette/Controllers/GaletteController.php | 10 ++--- .../Galette/Controllers/PluginsController.php | 4 +- galette/lib/Galette/Core/Db.php | 4 +- galette/lib/Galette/Core/Galette.php | 44 ++++++++++++++++++- galette/lib/Galette/Core/GaletteMail.php | 2 +- galette/lib/Galette/Core/LightSlimApp.php | 2 +- galette/lib/Galette/Core/Preferences.php | 18 ++++---- galette/lib/Galette/Core/SysInfos.php | 2 +- galette/lib/Galette/IO/News.php | 3 +- .../components/forms/picture.html.twig | 2 +- .../default/elements/modes.html.twig | 2 +- .../navigation/navigation_topbar.html.twig | 2 +- galette/templates/default/pages/500.html.twig | 2 +- .../templates/default/pages/desktop.html.twig | 2 +- .../default/pages/mailing_form.html.twig | 8 ++-- .../templates/default/pages/plugins.html.twig | 2 +- .../default/pages/preferences.html.twig | 12 ++--- 23 files changed, 98 insertions(+), 57 deletions(-) diff --git a/galette/includes/dependencies.php b/galette/includes/dependencies.php index 04c0f7efb..8a751a9c0 100644 --- a/galette/includes/dependencies.php +++ b/galette/includes/dependencies.php @@ -72,14 +72,14 @@ $container->set('Slim\Views\Twig', function (ContainerInterface $c) { $templates, [ 'cache' => rtrim(GALETTE_CACHE_DIR, DIRECTORY_SEPARATOR), - 'debug' => (GALETTE_MODE === \Galette\Core\Galette::MODE_DEV), - 'strict_variables' => (GALETTE_MODE == \Galette\Core\Galette::MODE_DEV) + 'debug' => \Galette\Core\Galette::isDebugEnabled(), + 'strict_variables' => \Galette\Core\Galette::isDebugEnabled() ] ); //Twig extensions $view->addExtension(new \Galette\Twig\CsrfExtension($c->get('csrf'))); - if (GALETTE_MODE === \Galette\Core\Galette::MODE_DEV) { + if (\Galette\Core\Galette::isDebugEnabled()) { $view->addExtension(new \Twig\Extension\DebugExtension()); } //End Twig extensions diff --git a/galette/includes/galette.inc.php b/galette/includes/galette.inc.php index e8b594837..5b5897e73 100644 --- a/galette/includes/galette.inc.php +++ b/galette/includes/galette.inc.php @@ -142,7 +142,7 @@ Analog::$format = "%s - %s - %s - %s\n"; $galette_run_log = null; if (!defined('GALETTE_LOG_LVL')) { - if (GALETTE_MODE === 'DEV') { + if (\Galette\Core\Galette::isDebugEnabled()) { define('GALETTE_LOG_LVL', Analog::DEBUG); } elseif (defined('GALETTE_TESTS')) { define('GALETTE_LOG_LVL', Analog::NOTICE); diff --git a/galette/includes/i18n.inc.php b/galette/includes/i18n.inc.php index 395e22318..b1360a41b 100644 --- a/galette/includes/i18n.inc.php +++ b/galette/includes/i18n.inc.php @@ -7,7 +7,7 @@ * * PHP version 5 * - * Copyright © 2003-2020 The Galette Team + * Copyright © 2003-2023 The Galette Team * * This file is part of Galette (http://galette.tuxfamily.org). * @@ -30,7 +30,7 @@ * @author Frédéric Jacquot * @author Georges Khaznadar (i18n using gettext) * @author Johan Cwiklinski - * @copyright 2003-2020 The Galette Team + * @copyright 2003-2023 The Galette Team * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version * @link http://galette.tuxfamily.org * @since Available since 0.62 @@ -41,8 +41,7 @@ if (!defined('GALETTE_ROOT')) { } use Analog\Analog; -use Laminas\Db\Sql\Expression; -use Galette\Core\L10n; +use Galette\Core\Galette; $i18n->updateEnv(); global $language; @@ -92,7 +91,7 @@ function _T($string, $domain = 'galette', $nt = true) if (!$trans) { $trans = $string; - if (GALETTE_MODE == 'DEV' && $nt === true) { + if (Galette::isDebugEnabled() && $nt === true) { $trans .= ' (not translated)'; } } @@ -145,7 +144,7 @@ function _Tn($singular, $plural, $count, $domain = 'galette', $nt = true) if (!$trans) { $trans = ($count > 1 ? $plural : $singular); - if (GALETTE_MODE == 'DEV' && $nt === true) { + if (Galette::isDebugEnabled() && $nt === true) { $trans .= ' (not translated)'; } } @@ -183,7 +182,7 @@ function _Tx($context, $string, $domain = 'galette', $nt = true) if (!$trans) { $trans = $ret; - if (GALETTE_MODE == 'DEV' && $nt === true) { + if (Galette::isDebugEnabled() && $nt === true) { $trans .= ' (not translated)'; } } @@ -236,7 +235,7 @@ function _Tnx($context, $singular, $plural, $count, $domain = 'galette', $nt = t if (!$trans) { $trans = $ret; - if (GALETTE_MODE == 'DEV' && $nt === true) { + if (Galette::isDebugEnabled() && $nt === true) { $trans .= ' (not translated)'; } } diff --git a/galette/includes/main.inc.php b/galette/includes/main.inc.php index 7f27eab44..9ee31b98e 100644 --- a/galette/includes/main.inc.php +++ b/galette/includes/main.inc.php @@ -227,7 +227,7 @@ $app->addRoutingMiddleware(); * for middleware added after it. */ $errorMiddleware = $app->addErrorMiddleware( - (GALETTE_MODE === 'DEV'), + Galette::isDebugEnabled(), true, true ); diff --git a/galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php b/galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php index 367d3138e..f18495d79 100644 --- a/galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php +++ b/galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php @@ -36,6 +36,7 @@ namespace Galette\Controllers\Crud; +use Galette\Core\Galette; use Galette\IO\File; use Galette\Repository\DynamicFieldsSet; use Throwable; @@ -132,7 +133,7 @@ class DynamicFieldsController extends CrudController $e->getMessage(), Analog::ERROR ); - if (GALETTE_MODE == 'DEV') { + if (Galette::isDebugEnabled()) { throw $e; } $error_detected[] = _T('An error occurred adding dynamic field :('); @@ -478,7 +479,7 @@ class DynamicFieldsController extends CrudController $e->getMessage(), Analog::ERROR ); - if (GALETTE_MODE == 'DEV') { + if (Galette::isDebugEnabled()) { throw $e; } $error_detected[] = _T('An error occurred editing dynamic field :('); diff --git a/galette/lib/Galette/Controllers/Crud/MailingsController.php b/galette/lib/Galette/Controllers/Crud/MailingsController.php index 457cdd817..e7523c23f 100644 --- a/galette/lib/Galette/Controllers/Crud/MailingsController.php +++ b/galette/lib/Galette/Controllers/Crud/MailingsController.php @@ -97,7 +97,7 @@ class MailingsController extends CrudController if ( $this->preferences->pref_mail_method == Mailing::METHOD_DISABLED - && !(GALETTE_MODE === Galette::MODE_DEMO) + && !Galette::isDemo() ) { $this->history->add( _T("Trying to load mailing while email is disabled in preferences.") @@ -249,7 +249,7 @@ class MailingsController extends CrudController if ( $this->preferences->pref_mail_method == Mailing::METHOD_DISABLED - && !(GALETTE_MODE === Galette::MODE_DEMO) + && !Galette::isDemo() ) { $this->history->add( _T("Trying to load mailing while email is disabled in preferences.") diff --git a/galette/lib/Galette/Controllers/GaletteController.php b/galette/lib/Galette/Controllers/GaletteController.php index 43c6e7327..ec8922a68 100644 --- a/galette/lib/Galette/Controllers/GaletteController.php +++ b/galette/lib/Galette/Controllers/GaletteController.php @@ -188,7 +188,7 @@ class GaletteController extends AbstractController 'pref_card_vspace' => 1 ); - if ($this->login->isSuperAdmin() && GALETTE_MODE !== Galette::MODE_DEMO) { + if ($this->login->isSuperAdmin() && !Galette::isDemo()) { $required['pref_admin_login'] = 1; } @@ -295,7 +295,7 @@ class GaletteController extends AbstractController $warning_detected = array_merge($warning_detected, $this->preferences->checkCardsSizes()); // picture upload - if (GALETTE_MODE !== Galette::MODE_DEMO && isset($_FILES['logo'])) { + if (!Galette::isDemo() && isset($_FILES['logo'])) { if ($_FILES['logo']['error'] === UPLOAD_ERR_OK) { if ($_FILES['logo']['tmp_name'] != '') { if (is_uploaded_file($_FILES['logo']['tmp_name'])) { @@ -318,7 +318,7 @@ class GaletteController extends AbstractController } } - if (GALETTE_MODE !== Galette::MODE_DEMO && isset($post['del_logo'])) { + if (!Galette::isDemo() && isset($post['del_logo'])) { if (!$this->logo->delete()) { $error_detected[] = _T("Delete failed"); } else { @@ -327,7 +327,7 @@ class GaletteController extends AbstractController } // Card logo upload - if (GALETTE_MODE !== Galette::MODE_DEMO && isset($_FILES['card_logo'])) { + if (!Galette::isDemo() && isset($_FILES['card_logo'])) { if ($_FILES['card_logo']['error'] === UPLOAD_ERR_OK) { if ($_FILES['card_logo']['tmp_name'] != '') { if (is_uploaded_file($_FILES['card_logo']['tmp_name'])) { @@ -350,7 +350,7 @@ class GaletteController extends AbstractController } } - if (GALETTE_MODE !== Galette::MODE_DEMO && isset($post['del_card_logo'])) { + if (!Galette::isDemo() && isset($post['del_card_logo'])) { if (!$this->print_logo->delete()) { $error_detected[] = _T("Delete failed"); } else { diff --git a/galette/lib/Galette/Controllers/PluginsController.php b/galette/lib/Galette/Controllers/PluginsController.php index 251ca1189..6354e7652 100644 --- a/galette/lib/Galette/Controllers/PluginsController.php +++ b/galette/lib/Galette/Controllers/PluginsController.php @@ -100,7 +100,7 @@ class PluginsController extends AbstractController */ public function togglePlugin(Request $request, Response $response, string $action, string $module_id): Response { - if (GALETTE_MODE !== Galette::MODE_DEMO) { + if (!Galette::isDemo()) { $plugins = $this->plugins; $reload_plugins = false; if ($action == 'activate') { @@ -163,7 +163,7 @@ class PluginsController extends AbstractController */ public function initPluginDb(Request $request, Response $response, string $id): Response { - if (GALETTE_MODE === Galette::MODE_DEMO) { + if (Galette::isDemo()) { Analog::log( 'Trying to access plugin database initialization in DEMO mode.', Analog::WARNING diff --git a/galette/lib/Galette/Core/Db.php b/galette/lib/Galette/Core/Db.php index 7dbbfbce7..52d2aa1b2 100644 --- a/galette/lib/Galette/Core/Db.php +++ b/galette/lib/Galette/Core/Db.php @@ -236,7 +236,7 @@ class Db */ public function checkDbVersion() { - if (GALETTE_MODE === 'DEV') { + if (Galette::isDebugEnabled()) { Analog::log( 'Database version not checked in DEV mode.', Analog::INFO @@ -983,7 +983,7 @@ class Db */ protected function log($query) { - if (GALETTE_MODE == 'DEV' || defined('GALETTE_SQL_DEBUG')) { + if (Galette::isSqlDebugEnabled()) { $logfile = GALETTE_LOGS_PATH . 'galette_sql.log'; file_put_contents($logfile, $query . "\n", FILE_APPEND); } diff --git a/galette/lib/Galette/Core/Galette.php b/galette/lib/Galette/Core/Galette.php index c8dbb0000..d78183e06 100644 --- a/galette/lib/Galette/Core/Galette.php +++ b/galette/lib/Galette/Core/Galette.php @@ -68,7 +68,7 @@ class Galette { $galette_version = GALETTE_VERSION; - //used for both gith and nightly installs + //used for both git and nightly installs $version = str_replace('-dev', '-git', GALETTE_VERSION); if (strstr($version, '-git') === false) { $version .= '-git'; @@ -85,7 +85,7 @@ class Galette $commitHash, $commitDate->format(($time ? 'Y-m-d H:i:s T' : 'Y-m-d')) ); - } elseif (GALETTE_NIGHTLY !== false) { + } elseif (static::isNightly()) { $galette_version = $version . '-' . GALETTE_NIGHTLY; } return $galette_version; @@ -942,4 +942,44 @@ class Galette } return $actions; } + + /** + * Is demonstration mode enabled + * + * @return bool + */ + public static function isDemo(): bool + { + return GALETTE_MODE === static::MODE_DEMO; + } + + /** + * Is debug mode enabled + * + * @return bool + */ + public static function isDebugEnabled(): bool + { + return GALETTE_MODE === static::MODE_DEV; + } + + /** + * Is SQL debug mode enabled + * + * @return bool + */ + public static function isSqlDebugEnabled(): bool + { + return defined('GALETTE_SQL_DEBUG') || static::isDebugEnabled(); + } + + /** + * Is a nightly build + * + * @return bool + */ + public static function isNightly(): bool + { + return GALETTE_NIGHTLY !== false; + } } diff --git a/galette/lib/Galette/Core/GaletteMail.php b/galette/lib/Galette/Core/GaletteMail.php index c54741853..18517c8dd 100644 --- a/galette/lib/Galette/Core/GaletteMail.php +++ b/galette/lib/Galette/Core/GaletteMail.php @@ -116,7 +116,7 @@ class GaletteMail //if we want to send emails using a smtp server $this->mail->IsSMTP(); // enables SMTP debug information - if (GALETTE_MODE == 'DEV') { + if (Galette::isDebugEnabled()) { $this->mail->SMTPDebug = 4; //cannot use a callable here; this prevents class to be serialized //see https://bugs.galette.eu/issues/1468 diff --git a/galette/lib/Galette/Core/LightSlimApp.php b/galette/lib/Galette/Core/LightSlimApp.php index e50d51455..7f9fd0b17 100644 --- a/galette/lib/Galette/Core/LightSlimApp.php +++ b/galette/lib/Galette/Core/LightSlimApp.php @@ -71,7 +71,7 @@ class LightSlimApp $builder->useAttributes(true); $builder->addDefinitions([ 'templates.path' => GALETTE_ROOT . GALETTE_THEME, - 'settings.displayErrorDetails' => (GALETTE_MODE === 'DEV'), + 'settings.displayErrorDetails' => Galette::isDebugEnabled(), 'settings.addContentLengthHeader' => false, 'galette' => [ 'mode' => $this->mode, diff --git a/galette/lib/Galette/Core/Preferences.php b/galette/lib/Galette/Core/Preferences.php index e3bedf85b..e8ff09541 100644 --- a/galette/lib/Galette/Core/Preferences.php +++ b/galette/lib/Galette/Core/Preferences.php @@ -496,7 +496,7 @@ class Preferences public function check(array $values, Login $login) { $insert_values = array(); - if ($login->isSuperAdmin() && GALETTE_MODE !== Galette::MODE_DEMO) { + if ($login->isSuperAdmin() && !Galette::isDemo()) { $this->required[] = 'pref_admin_login'; } @@ -512,7 +512,7 @@ class Preferences } //cleanup fields for demo - if (GALETTE_MODE == Galette::MODE_DEMO) { + if (Galette::isDemo()) { unset( $insert_values['pref_admin_login'], $insert_values['pref_admin_pass'], @@ -522,7 +522,7 @@ class Preferences // missing relations if ( - GALETTE_MODE !== Galette::MODE_DEMO + !Galette::isDemo() && isset($insert_values['pref_mail_method']) ) { if ($insert_values['pref_mail_method'] > GaletteMail::METHOD_DISABLED) { @@ -596,7 +596,7 @@ class Preferences } } - if (GALETTE_MODE !== Galette::MODE_DEMO && isset($values['pref_admin_pass_check'])) { + if (!Galette::isDemo() && isset($values['pref_admin_pass_check'])) { // Check passwords. Hash will be done into the Preferences class if (strcmp($insert_values['pref_admin_pass'], $values['pref_admin_pass_check']) != 0) { $this->errors[] = _T("Passwords mismatch"); @@ -674,7 +674,7 @@ class Preferences } break; case 'pref_admin_login': - if (GALETTE_MODE === Galette::MODE_DEMO) { + if (Galette::isDemo()) { Analog::log( 'Trying to set superadmin login while in DEMO.', Analog::WARNING @@ -729,7 +729,7 @@ class Preferences } break; case 'pref_admin_pass': - if (GALETTE_MODE == Galette::MODE_DEMO) { + if (Galette::isDemo()) { Analog::log( 'Trying to set superadmin pass while in DEMO.', Analog::WARNING @@ -799,7 +799,7 @@ class Preferences foreach (self::$defaults as $k => $v) { if ( - GALETTE_MODE == Galette::MODE_DEMO + Galette::isDemo() && in_array($k, ['pref_admin_pass', 'pref_admin_login', 'pref_mail_method']) ) { continue; @@ -970,7 +970,7 @@ class Preferences if (!in_array($name, $forbidden) && isset($this->prefs[$name])) { if ( - GALETTE_MODE === Galette::MODE_DEMO + Galette::isDemo() && $name == 'pref_mail_method' ) { return GaletteMail::METHOD_DISABLED; @@ -1071,7 +1071,7 @@ class Preferences || $name == 'pref_email_newadh' || $name == 'pref_email_reply_to' ) { - if (GALETTE_MODE === Galette::MODE_DEMO) { + if (Galette::isDemo()) { Analog::log( 'Trying to set pref_email while in DEMO.', Analog::WARNING diff --git a/galette/lib/Galette/Core/SysInfos.php b/galette/lib/Galette/Core/SysInfos.php index abc48bf55..816b1adb7 100644 --- a/galette/lib/Galette/Core/SysInfos.php +++ b/galette/lib/Galette/Core/SysInfos.php @@ -65,7 +65,7 @@ class SysInfos $str = str_pad('Galette version:', 20, '.') . ' ' . \Galette\Core\Galette::gitVersion(true) . "\n"; - if (GALETTE_MODE == \Galette\Core\Galette::MODE_DEMO) { + if (Galette::isDemo()) { $str .= $this->getPluginsInfo($plugins); return $str; } diff --git a/galette/lib/Galette/IO/News.php b/galette/lib/Galette/IO/News.php index bab841490..009c6b8ee 100644 --- a/galette/lib/Galette/IO/News.php +++ b/galette/lib/Galette/IO/News.php @@ -36,6 +36,7 @@ namespace Galette\IO; +use Galette\Core\Galette; use Throwable; use Analog\Analog; @@ -76,7 +77,7 @@ class News $this->feed_url = $this->getFeedURL($url); //only if cache should be used - if ($nocache === false && GALETTE_MODE !== 'DEV') { + if ($nocache === false && !Galette::isDebugEnabled()) { if (!$this->checkCache()) { $this->makeCache(); } else { diff --git a/galette/templates/default/components/forms/picture.html.twig b/galette/templates/default/components/forms/picture.html.twig index 71376ff6a..176c3d821 100644 --- a/galette/templates/default/components/forms/picture.html.twig +++ b/galette/templates/default/components/forms/picture.html.twig @@ -13,7 +13,7 @@
-
- {% if login.isLogged() and login.isAdmin() or constant('GALETTE_MODE') == 'DEV' %} + {% if login.isLogged() and login.isAdmin() or callstatic('\\Galette\\Core\\Galette', 'isDebugEnabled') %} {{ _self.render_ex(exception) }} {% endif %} {% endblock %} diff --git a/galette/templates/default/pages/desktop.html.twig b/galette/templates/default/pages/desktop.html.twig index 659a1ebef..e2f67fea6 100644 --- a/galette/templates/default/pages/desktop.html.twig +++ b/galette/templates/default/pages/desktop.html.twig @@ -4,7 +4,7 @@ {% block content %}
-{% if not hide_telemetry and constant('GALETTE_MODE') != 'DEMO' %} +{% if not hide_telemetry and not callstatic('\\Galette\\Core\\Galette', 'isDemo') %}
{{ _T("Help us know about you!") }}
diff --git a/galette/templates/default/pages/mailing_form.html.twig b/galette/templates/default/pages/mailing_form.html.twig index b810e02d8..04da0b441 100644 --- a/galette/templates/default/pages/mailing_form.html.twig +++ b/galette/templates/default/pages/mailing_form.html.twig @@ -1,7 +1,7 @@ {% extends 'page.html.twig' %} {% block content %} -{% if preferences.pref_mail_method == constant('Galette\\Core\\Mailing::METHOD_DISABLED') and constant('GALETTE_MODE') != 'DEMO' %} +{% if preferences.pref_mail_method == constant('Galette\\Core\\Mailing::METHOD_DISABLED') and not callstatic('\\Galette\\Core\\Galette', 'isDemo') %}
@@ -153,7 +153,7 @@ {{ _T("Save") }} - @@ -201,7 +201,7 @@ {{ _T("Modifiy mailing") }} - @@ -221,7 +221,7 @@ {% endblock %} {% block javascripts %} -{% if (preferences.pref_mail_method != constant('Galette\\Core\\Mailing::METHOD_DISABLED') or constant('GALETTE_MODE') != 'DEMO') and mailing_saved is not defined %} +{% if (preferences.pref_mail_method != constant('Galette\\Core\\Mailing::METHOD_DISABLED') or not callstatic('\\Galette\\Core\\Galette', 'isDemo')) and mailing_saved is not defined %} {% if mailing.current_step != constant('Galette\\Core\\Mailing::STEP_SENT') %}