diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml new file mode 100644 index 0000000..9e8dd7c --- /dev/null +++ b/.github/workflows/code-style.yml @@ -0,0 +1,42 @@ +name: PHP-CS-Fixer + +on: + push: + branches: [ master, main ] + pull_request: + branches: [ master, main ] + +permissions: + contents: read + +jobs: + code-style: + + runs-on: ubuntu-latest + permissions: + contents: write # for Git to git apply + + steps: + - uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + extensions: gd, intl, pdo_mysql + coverage: none # disable xdebug, pcov + + # install dependencies from composer.json + - name: Install test dependencies + env: + COMPOSER: composer.json + run: composer install --prefer-dist --no-progress + + # run php-cs-fixer, fix code styles + - name: Run PHP CS Fixer + run: composer cs-fix + + # commit and push fixed files + - uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Apply php-cs-fixer changes diff --git a/.gitignore b/.gitignore index ea75c5d..72c620d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,7 @@ fragments/.DS_Store /playwright-report/ /playwright/.cache/ /screens/ +/.idea +/composer.lock +/.php-cs-fixer.cache +/vendor diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..72be593 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,11 @@ +in(__DIR__) +; + +return (new Redaxo\PhpCsFixerConfig\Config()) + ->setFinder($finder) + ; diff --git a/boot.php b/boot.php index 4c325b3..677bdeb 100644 --- a/boot.php +++ b/boot.php @@ -1,5 +1,7 @@ getConfig('block_frontend'))) { - - \FriendsOfREDAXO\Maintenance\Maintenance::checkFrontend(); + if (rex::isFrontend() && (bool) $addon->getConfig('block_frontend')) { + Maintenance::checkFrontend(); } - if (rex::isBackend() && boolval($addon->getConfig('block_backend'))) { - \FriendsOfREDAXO\Maintenance\Maintenance::checkBackend(); + if (rex::isBackend() && (bool) $addon->getConfig('block_backend')) { + Maintenance::checkBackend(); } if (rex::isBackend()) { - \FriendsOfREDAXO\Maintenance\Maintenance::setIndicators(); + Maintenance::setIndicators(); rex_view::addJsFile($addon->getAssetsUrl('dist/bootstrap-tokenfield.js')); rex_view::addJsFile($addon->getAssetsUrl('dist/init_bootstrap-tokenfield.js')); rex_view::addCssFile($addon->getAssetsUrl('dist/css/bootstrap-tokenfield.css')); @@ -35,7 +36,7 @@ if ('maintenance/frontend' === rex_be_controller::getCurrentPage()) { rex_extension::register('OUTPUT_FILTER', static function (rex_extension_point $ep) { $suchmuster = 'class="###maintenance-settings-editor###"'; - $ersetzen = strval(rex_config::get('maintenance', 'editor')); // @phpstan-ignore-line + $ersetzen = (string) rex_config::get('maintenance', 'editor'); // @phpstan-ignore-line $ep->setSubject(str_replace($suchmuster, $ersetzen, $ep->getSubject())); // @phpstan-ignore-line }); } diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e8dda5e --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "require-dev": { + "redaxo/php-cs-fixer-config": "^2.0", + "friendsofphp/php-cs-fixer": "^3.14" + }, + "replace": { + "psr/log": "*", + "psr/container": "*" + }, + "scripts": { + "cs-dry": "php-cs-fixer fix -v --ansi --dry-run --config=.php-cs-fixer.dist.php", + "cs-fix": "php-cs-fixer fix -v --ansi --config=.php-cs-fixer.dist.php" + } +} diff --git a/fragments/maintenance/announcement.php b/fragments/maintenance/announcement.php index 54009ce..5901468 100644 --- a/fragments/maintenance/announcement.php +++ b/fragments/maintenance/announcement.php @@ -4,10 +4,10 @@ $end_date = rex_config::get('maintenance', 'announcement_end_date'); $current_date = date('Y-m-d\TH:i:s'); -if ($announcement !== '' && $start_date <= $current_date && $end_date >= $current_date) { +if ('' !== $announcement && $start_date <= $current_date && $end_date >= $current_date) { ?>
- +
diff --git a/fragments/maintenance/login.php b/fragments/maintenance/login.php index bc0a3d1..91b651a 100644 --- a/fragments/maintenance/login.php +++ b/fragments/maintenance/login.php @@ -1,7 +1,7 @@ +if ('password' === rex_config::get('maintenance', 'authentification_mode', '')) { ?>
-
+ diff --git a/fragments/maintenance/reload.php b/fragments/maintenance/reload.php index 30ae048..b63ccb8 100644 --- a/fragments/maintenance/reload.php +++ b/fragments/maintenance/reload.php @@ -2,5 +2,5 @@ // reload current frontend page html button ?> diff --git a/install.php b/install.php index ab27c6e..6aad2f9 100644 --- a/install.php +++ b/install.php @@ -12,7 +12,7 @@ $addon = rex_addon::get('maintenance'); /** - * Addon in Setup-Addons mit aufnehmen der config.yml in dieser Installation aufnehmen + * Addon in Setup-Addons mit aufnehmen der config.yml in dieser Installation aufnehmen. */ $config_file = rex_path::coreData('config.yml'); $data = rex_file::getConfig($config_file); @@ -22,7 +22,7 @@ } /* Eigene IP-Adresse in die erlaubten IP-Adressen hinzufügen, sofern nicht bereits vorhanden */ -$allowed_ips = strval($addon->getConfig('allowed_ips')); // @phpstan-ignore-line +$allowed_ips = (string) $addon->getConfig('allowed_ips'); /** @phpstan-ignore-line */ $allowed_ips = array_filter(explode(',', $allowed_ips)); // Leere Elemente entfernen $ip = rex_server('SERVER_ADDR', 'string', ''); @@ -34,10 +34,10 @@ } /* Bei Installation standardmäßig ein zufälliges Secret generieren */ -if ($addon->getConfig('maintenance_secret') === '') { +if ('' === $addon->getConfig('maintenance_secret')) { $addon->setConfig('maintenance_secret', bin2hex(random_bytes(16))); } -if($addon->getConfig('announcement') === '') { +if ('' === $addon->getConfig('announcement')) { $addon->setConfig('announcement', '

Geplante Wartungsarbeiten am 01.01.2022 von 00:00 bis 06:00 Uhr. In dieser Zeit ist die Website möglicherweise nicht erreichbar.

'); } diff --git a/lib/Maintenance.php b/lib/Maintenance.php index e660cce..368264e 100644 --- a/lib/Maintenance.php +++ b/lib/Maintenance.php @@ -15,27 +15,28 @@ use rex; use rex_addon; use rex_article; -use rex_clang; -use rex_response; -use rex_yrewrite; -use rex_session; -use rex_request; -use rex_unset_session; use rex_backend_login; -use rex_user; -use rex_login; -use rex_server; +use rex_clang; use rex_extension; use rex_extension_point; use rex_fragment; +use rex_login; +use rex_response; +use rex_user; +use rex_yrewrite; + +use function in_array; + +use const FILTER_VALIDATE_IP; +use const FILTER_VALIDATE_URL; class Maintenance { /** @api */ public function checkUrl(string $url): ?bool { - if ($url !== '') { - if (filter_var($url, FILTER_VALIDATE_URL) === false) { + if ('' !== $url) { + if (false === filter_var($url, FILTER_VALIDATE_URL)) { return false; } return true; @@ -46,11 +47,10 @@ public function checkUrl(string $url): ?bool /** @api */ public function checkIp(string $ip): ?bool { - if ($ip !== '' && filter_var($ip, FILTER_VALIDATE_IP) === false) { + if ('' !== $ip && false === filter_var($ip, FILTER_VALIDATE_IP)) { return false; - } else { - return true; } + return true; } /** @api */ @@ -58,9 +58,9 @@ public static function isIpAllowed(): bool { $addon = rex_addon::get('maintenance'); $ip = rex_server('REMOTE_ADDR', 'string', ''); - $allowedIps = strval($addon->getConfig('allowed_ips')); // @phpstan-ignore-line + $allowedIps = (string) $addon->getConfig('allowed_ips'); // @phpstan-ignore-line - if ($allowedIps !== '') { + if ('' !== $allowedIps) { $allowedIpsArray = explode(',', $allowedIps); return in_array($ip, $allowedIpsArray, true); } @@ -73,9 +73,9 @@ public static function isHostAllowed(): bool { $addon = rex_addon::get('maintenance'); $host = rex_server('HTTP_HOST', 'string', ''); - $allowedHosts = strval($addon->getConfig('allowed_yrewrite_domains', false)); // @phpstan-ignore-line + $allowedHosts = (string) $addon->getConfig('allowed_yrewrite_domains', false); // @phpstan-ignore-line - if ($allowedHosts !== '') { + if ('' !== $allowedHosts) { $allowedHostsArray = explode(',', $allowedHosts); return in_array($host, $allowedHostsArray, true); } @@ -89,9 +89,9 @@ public static function isYrewriteDomainAllowed(): bool $addon = rex_addon::get('maintenance'); if ($ydomain = rex_yrewrite::getDomainByArticleId(rex_article::getCurrentId(), rex_clang::getCurrentId())) { $yrewrite_domain = $ydomain->getHost(); - $allowedDomains = strval($addon->getConfig('allowed_yrewrite_domains')); // @phpstan-ignore-line + $allowedDomains = (string) $addon->getConfig('allowed_yrewrite_domains'); // @phpstan-ignore-line - if ($allowedDomains !== '') { + if ('' !== $allowedDomains) { $allowedDomainsArray = explode('|', $allowedDomains); return in_array($yrewrite_domain, $allowedDomainsArray, true); } @@ -104,17 +104,17 @@ public static function isYrewriteDomainAllowed(): bool public static function isSecretAllowed(): bool { $addon = rex_addon::get('maintenance'); - $config_secret = strval($addon->getConfig('maintenance_secret')); + $config_secret = (string) $addon->getConfig('maintenance_secret'); // Bereits mit richtigem Secret eingeloggt - if ($config_secret != '' && rex_session('maintenance_secret', 'string', '') === $config_secret) { // @phpstan-ignore-line + if ('' != $config_secret && rex_session('maintenance_secret', 'string', '') === $config_secret) { // @phpstan-ignore-line return true; } $maintenance_secret = rex_request('maintenance_secret', 'string', ''); $authentification_mode = $addon->getConfig('authentification_mode'); - if (($authentification_mode === 'URL' || $authentification_mode === 'password') && $config_secret != '' && $maintenance_secret === $config_secret) { + if (('URL' === $authentification_mode || 'password' === $authentification_mode) && '' != $config_secret && $maintenance_secret === $config_secret) { rex_set_session('maintenance_secret', $maintenance_secret); return true; } @@ -136,7 +136,7 @@ public static function isUserAllowed(): bool } // Eingeloggte REDAXO-Benutzer dürfen sich einloggen, wenn es in den Einstellungen erlaubt ist - if ($user instanceof rex_user && boolval($addon->getConfig('allow_logged_in_users'))) { + if ($user instanceof rex_user && (bool) $addon->getConfig('allow_logged_in_users')) { return true; } @@ -178,7 +178,7 @@ public static function checkFrontend(): void // Wenn die Sitemap angefordert wird, Anfrage nicht sperren $REQUEST_URI = rex_server('REQUEST_URI', 'string', ''); - if (str_contains($REQUEST_URI, 'sitemap.xml') === true) { + if (true === str_contains($REQUEST_URI, 'sitemap.xml')) { return; } @@ -192,11 +192,11 @@ public static function checkFrontend(): void } // Alles, was bis hier hin nicht erlaubt wurde, blockieren wie in den Einstellungen gewählt - $redirect_url = strval($addon->getConfig('redirect_frontend_to_url')); // @phpstan-ignore-line - $responsecode = strval($addon->getConfig('http_response_code')); // @phpstan-ignore-line + $redirect_url = (string) $addon->getConfig('redirect_frontend_to_url'); /** @phpstan-ignore-line */ + $responsecode = (string) $addon->getConfig('http_response_code'); /** @phpstan-ignore-line */ $mpage = new rex_fragment(); - if ($redirect_url !== '') { + if ('' !== $redirect_url) { rex_response::setStatus(rex_response::HTTP_MOVED_TEMPORARILY); rex_response::sendRedirect($redirect_url); } @@ -209,11 +209,11 @@ public static function checkBackend(): void $addon = rex_addon::get('maintenance'); if (rex::getUser() instanceof rex_user && !rex::getUser()->isAdmin() && !rex::getImpersonator()) { - if (strval($addon->getConfig('redirect_backend_to_url'))) { // @phpstan-ignore-line - rex_response::sendRedirect(strval($addon->getConfig('redirect_backend_to_url'))); // @phpstan-ignore-line + if ((string) $addon->getConfig('redirect_backend_to_url')) { // @phpstan-ignore-line + rex_response::sendRedirect((string) $addon->getConfig('redirect_backend_to_url')); // @phpstan-ignore-line } $mpage = new rex_fragment(); - header('HTTP/1.1 ' . strval($addon->getConfig('http_response_code'))); // @phpstan-ignore-line + header('HTTP/1.1 ' . (string) $addon->getConfig('http_response_code')); // @phpstan-ignore-line exit($mpage->parse('maintenance/backend.php')); } } @@ -223,13 +223,13 @@ public static function setIndicators(): void $addon = rex_addon::get('maintenance'); $page = $addon->getProperty('page'); - if (boolval($addon->getConfig('block_backend'))) { + if ((bool) $addon->getConfig('block_backend')) { $page['title'] .= ' B'; $page['icon'] .= ' fa-toggle-on block_backend'; $addon->setProperty('page', $page); } - if (boolval($addon->getConfig('block_frontend'))) { + if ((bool) $addon->getConfig('block_frontend')) { $page['title'] .= ' F'; $page['icon'] .= ' fa-toggle-on block_frontend'; } @@ -248,12 +248,12 @@ public static function getAnnouncement(): string { $addon = rex_addon::get('maintenance'); - if (strval($addon->getConfig('announcement_start_date')) !== '') { // @phpstan-ignore-line - $start = strtotime(strval($addon->getConfig('announcement_start_date'))); // @phpstan-ignore-line - $end = strtotime(strval($addon->getConfig('announcement_end_date'))); // @phpstan-ignore-line + if ('' !== (string) $addon->getConfig('announcement_start_date')) { /** @phpstan-ignore-line */ + $start = strtotime((string) $addon->getConfig('announcement_start_date')); /** @phpstan-ignore-line */ + $end = strtotime((string) $addon->getConfig('announcement_end_date')); /** @phpstan-ignore-line */ $now = time(); if ($now >= $start && $now <= $end) { - return strval($addon->getConfig('announcement')); // @phpstan-ignore-line + return (string) $addon->getConfig('announcement'); // @phpstan-ignore-line } } diff --git a/lib/command/maintenance_off.php b/lib/command/maintenance_off.php index c72f4fe..e459fae 100644 --- a/lib/command/maintenance_off.php +++ b/lib/command/maintenance_off.php @@ -1,4 +1,5 @@ getName()); diff --git a/pages/frontend.php b/pages/frontend.php index ea6af34..bb256f5 100644 --- a/pages/frontend.php +++ b/pages/frontend.php @@ -10,8 +10,6 @@ * file that was distributed with this source code. */ -use FriendsOfREDAXO\Maintenance\MaintenanceUtil; - $addon = rex_addon::get('maintenance'); $form = rex_config_form::factory($addon->getName()); @@ -65,14 +63,13 @@ // Erlaubte IP-Adressen $field = $form->addTextField('allowed_ips'); $field->setLabel($addon->i18n('maintenance_allowed_ips_label')); -$field->setNotice($addon->i18n('maintenance_allowed_ips_notice', \rex_server('REMOTE_ADDR', 'string', ''), \rex_server('SERVER_ADDR', 'string', ''))); +$field->setNotice($addon->i18n('maintenance_allowed_ips_notice', rex_server('REMOTE_ADDR', 'string', ''), rex_server('SERVER_ADDR', 'string', ''))); $field->setAttribute('class', 'form-control'); $field->setAttribute('data-maintenance', 'tokenfield'); $field->setAttribute('data-beautify', 'false'); - // Wenn YRewrite installiert, dann erlaubte YRewrite-Domains auswählen -if (\rex_addon::get('yrewrite')->isAvailable() && count(\rex_yrewrite::getDomains()) > 1) { +if (rex_addon::get('yrewrite')->isAvailable() && count(rex_yrewrite::getDomains()) > 1) { $field = $form->addSelectField('allowed_yrewrite_domains'); $field->setAttribute('multiple', 'multiple'); @@ -81,7 +78,7 @@ $field->setLabel($addon->i18n('maintenance_allowed_yrewrite_domains_label')); $field->setNotice($addon->i18n('maintenance_allowed_yrewrite_domains_notice')); $select = $field->getSelect(); - foreach (\rex_yrewrite::getDomains() as $key => $domain) { + foreach (rex_yrewrite::getDomains() as $key => $domain) { $select->addOption($key, $key); } } @@ -101,7 +98,7 @@ $field = $form->addTextAreaField('announcement'); $field->setLabel($addon->i18n('maintenance_announcement_label')); $field->setNotice($addon->i18n('maintenance_announcement_notice')); -if (strval(rex_config::get('maintenance', 'editor')) !== '') { // @phpstan-ignore-line +if ('' !== (string) rex_config::get('maintenance', 'editor')) { // @phpstan-ignore-line $field->setAttribute('class', '###maintenance-settings-editor###'); } @@ -121,7 +118,6 @@ $field->setNotice($addon->i18n('maintenance_announcement_end_date_notice', date('Y-m-d H:i:s'))); $field->setAttribute('type', 'datetime-local'); - $fragment = new rex_fragment(); $fragment->setVar('class', 'edit'); $fragment->setVar('title', $addon->i18n('maintenance_settings_frontend_title')); @@ -158,16 +154,16 @@ // Ebenfalls für alle YRewrite-Domains ausgeben -if (\rex_addon::get('yrewrite')->isAvailable() && count(\rex_yrewrite::getDomains()) > 1) { - foreach (\rex_yrewrite::getDomains() as $key => $domain) { - if($key == 'default') { +if (rex_addon::get('yrewrite')->isAvailable() && count(rex_yrewrite::getDomains()) > 1) { + foreach (rex_yrewrite::getDomains() as $key => $domain) { + if ('default' == $key) { continue; } $url = $domain->getUrl() . '?maintenance_secret=' . rex_config::get('maintenance', 'maintenance_secret'); - $copy .= '
  • '; + $copy .= '
  • '; $copy .= ''; $copy .= ' - +
  • '; diff --git a/pages/index.php b/pages/index.php index 5063d81..7cbc95b 100644 --- a/pages/index.php +++ b/pages/index.php @@ -11,5 +11,5 @@ $addon = rex_addon::get('maintenance'); echo rex_view::title($addon->i18n('title')); -//include rex_be_controller::getCurrentPageObject()->getSubPath(); +// include rex_be_controller::getCurrentPageObject()->getSubPath(); rex_be_controller::includeCurrentPageSubPath(); diff --git a/uninstall.php b/uninstall.php index 8d89c18..6d6a5c3 100644 --- a/uninstall.php +++ b/uninstall.php @@ -3,8 +3,8 @@ /* Bei Deinstallation des Addons aus der config.yml entfernen */ $config_file = rex_path::coreData('config.yml'); $data = rex_file::getConfig($config_file); -if (key_exists('setup_addons', $data) && in_array('maintenance', $data['setup_addons'], true)) { - $data['setup_addons'] = array_filter($data['setup_addons'], fn ($e) => $e !== 'maintenance'); +if (array_key_exists('setup_addons', $data) && in_array('maintenance', $data['setup_addons'], true)) { + $data['setup_addons'] = array_filter($data['setup_addons'], static fn ($e) => 'maintenance' !== $e); rex_file::putConfig($config_file, $data); } diff --git a/update.php b/update.php index 64515ce..d6a432f 100644 --- a/update.php +++ b/update.php @@ -1,4 +1,5 @@ hasConfig('frontend_aktiv')) { - $addon->setConfig('block_frontend', $addon->getConfig('frontend_aktiv') === 'Deaktivieren' ? 0 : 1); + $addon->setConfig('block_frontend', 'Deaktivieren' === $addon->getConfig('frontend_aktiv') ? 0 : 1); } if ($addon->hasConfig('redirect_frontend')) { @@ -24,15 +25,15 @@ } if ($addon->hasConfig('backend_aktiv')) { - $addon->setConfig('block_backend', $addon->getConfig('backend_aktiv') === '1' ? 1 : 0); + $addon->setConfig('block_backend', '1' === $addon->getConfig('backend_aktiv') ? 1 : 0); } if ($addon->hasConfig('blockSession')) { - $addon->setConfig('block_frontend_rex_user', $addon->getConfig('blockSession') === 'Inaktiv' ? 0 : 1); + $addon->setConfig('block_frontend_rex_user', 'Inaktiv' === $addon->getConfig('blockSession') ? 0 : 1); } if ($addon->hasConfig('type')) { - $addon->setConfig('authentification_mode', $addon->getConfig('type') === 'Password' ? 'password' : 'URL'); + $addon->setConfig('authentification_mode', 'Password' === $addon->getConfig('type') ? 'password' : 'URL'); } if ($addon->hasConfig('secret')) {