Files
FreshRSS/lib/Minz/FrontController.php
Inverle 06c0329de2 Fix wrong status code on failed login (#9566)
Regression from https://github.com/FreshRSS/FreshRSS/pull/9358 (PR not present in current release, stopped sending 403 on failed logins).

This PR fixes the Minz bug mentioned in https://github.com/FreshRSS/FreshRSS/pull/9358#discussion_r4382254359 which occurred with `redirect: false` redirects.

But it also takes a different approach for redirecting, by using HTML instead of the `Location` header (to address one additional issue with a resubmit prompt appearing when refreshing the login page after a failed login).

403 is now returned during failed reauthentication as well.
2026-11-15 23:34:53 +01:00

121 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
# ***** BEGIN LICENSE BLOCK *****
# MINZ - a free PHP Framework like Zend Framework
# Copyright (C) 2011 Marien Fressinaud
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ***** END LICENSE BLOCK *****
/**
* The Minz_FrontController class is the framework Dispatcher.
* It runs the application.
* It is generally invoqued by an index.php file at the root.
*/
class Minz_FrontController {
protected Minz_Dispatcher $dispatcher;
/**
* Constructor
* Initializes the dispatcher and updates the request
*/
public function __construct() {
try {
$this->setReporting();
Minz_Request::init();
// Build the current request's URL and forward to it directly.
// If needed, a redirect is issued instead to correct the public relative path.
// Only GET parameters are part of the URL, and hence of the original request or of a redirect.
Minz_Request::forward(Minz_Url::build(), redirect: Minz_Request::pathInfo() !== '');
// POST parameters (which may contain passwords) are added afterwards so that they never end up in a URL.
$post = array_filter($_POST, 'is_string', ARRAY_FILTER_USE_KEY);
unset($post['rid']); // Request ID is only taken from GET
Minz_Request::_params(array_merge(Minz_Request::params(), $post));
} catch (Minz_Exception $e) {
Minz_Log::error($e->getMessage());
self::killApp($e->getMessage());
}
$this->dispatcher = Minz_Dispatcher::getInstance();
}
/**
* Starts the application (runs the dispatcher and sends the response)
*/
public function run(): void {
try {
$this->dispatcher->run();
} catch (Minz_BadRequestException $e) {
Minz_Error::error(400, ['error' => [$e->getMessage()]], redirect: true);
} catch (Minz_Exception $e) {
try {
Minz_Log::error($e->getMessage());
} catch (Minz_PermissionDeniedException $e) {
self::killApp($e->getMessage());
}
if ($e instanceof Minz_FileNotExistException ||
$e instanceof Minz_ControllerNotExistException ||
$e instanceof Minz_ControllerNotActionControllerException ||
$e instanceof Minz_ActionException) {
Minz_Error::error(404, ['error' => [$e->getMessage()]], true);
} else {
self::killApp($e->getMessage());
}
}
}
/**
* Kills the programme
*/
public static function killApp(string $txt = ''): never {
header('HTTP/1.1 500 Internal Server Error', true, 500);
if (function_exists('errorMessageInfo')) {
//If the application has defined a custom error message function
die(errorMessageInfo('Application problem', $txt));
}
die('### Application problem ###<br />' . "\n" . $txt);
}
private function setReporting(): void {
$envType = getenv('FRESHRSS_ENV');
if ($envType == '') {
$conf = Minz_Configuration::get('system');
$envType = $conf->environment;
}
switch ($envType) {
case 'development':
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('log_errors', 'On');
break;
case 'silent':
error_reporting(0);
break;
case 'production':
default:
error_reporting(E_ALL);
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
break;
}
}
}