Skip to content

Commit

Permalink
Merge pull request #114 from FriendsOfREDAXO/add-code-style-workflow
Browse files Browse the repository at this point in the history
add code-style workflow
  • Loading branch information
alxndr-w authored Sep 13, 2024
2 parents 280ea40 + cc6a407 commit 8d5d660
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 76 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ fragments/.DS_Store
/playwright-report/
/playwright/.cache/
/screens/
/.idea
/composer.lock
/.php-cs-fixer.cache
/vendor
11 changes: 11 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
;

return (new Redaxo\PhpCsFixerConfig\Config())
->setFinder($finder)
;
17 changes: 9 additions & 8 deletions boot.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use FriendsOfREDAXO\Maintenance\Maintenance;

/**
* This file is part of the maintenance package.
*
Expand All @@ -13,19 +15,18 @@
if (rex::isSetup()) {
return;
}
rex_extension::register('PACKAGES_INCLUDED', function () {
rex_extension::register('PACKAGES_INCLUDED', static function () {
$addon = rex_addon::get('maintenance');

if (rex::isFrontend() && boolval($addon->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'));
Expand All @@ -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
});
}
Expand Down
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 2 additions & 2 deletions fragments/maintenance/announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
?>
<div class="maintenance-announcement">
<?php echo $announcement; ?>
<?= $announcement ?>
</div>
<?php }
?>
4 changes: 2 additions & 2 deletions fragments/maintenance/login.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
if(rex_config::get('maintenance', 'authentification_mode', '') === 'password') { ?>
if ('password' === rex_config::get('maintenance', 'authentification_mode', '')) { ?>
<div class="maintenance-login">
<form action="<?= rex_url::base(); ?>" method="post">
<form action="<?= rex_url::base() ?>" method="post">
<label for="maintenance_secret">Access-Code</label>
<input name="maintenance_secret" class="maintenance-pw-input" type="password" placeholder=""/>
<button type="submit" class="maintenance-pw-btn">Login</button>
Expand Down
2 changes: 1 addition & 1 deletion fragments/maintenance/reload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// reload current frontend page html button
?>
<div class="maintenance-reload">
<a href="<?= $_SERVER['REQUEST_URI']; ?>" class="maintenance-btn">Reload</a>
<a href="<?= $_SERVER['REQUEST_URI'] ?>" class="maintenance-btn">Reload</a>
</div>
8 changes: 4 additions & 4 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', '');

Expand All @@ -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', '<p>Geplante Wartungsarbeiten am 01.01.2022 von 00:00 bis 06:00 Uhr. In dieser Zeit ist die Website möglicherweise nicht erreichbar.</p>');
}
74 changes: 37 additions & 37 deletions lib/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,21 +47,20 @@ 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 */
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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand All @@ -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'));
}
}
Expand All @@ -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'] .= ' <span class="label label-info pull-right">B</span>';
$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'] .= ' <span class="label label-danger pull-right">F</span>';
$page['icon'] .= ' fa-toggle-on block_frontend';
}
Expand All @@ -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
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/command/maintenance_off.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down
1 change: 1 addition & 0 deletions lib/command/maintenance_on.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down
2 changes: 0 additions & 2 deletions pages/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading

0 comments on commit 8d5d660

Please sign in to comment.