Skip to content

Commit fc998f2

Browse files
committed
refactor: removed direct ini_set() usage, now using Symfony ErrorHandler
1 parent c501a41 commit fc998f2

File tree

11 files changed

+64
-81
lines changed

11 files changed

+64
-81
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ This is a log of major user-visible changes in each phpMyFAQ release.
2222
- added Urdu translation (Thorsten)
2323
- added dark mode / light mode toggle (Thorsten)
2424
- added support for PHP 8.5 (Thorsten)
25+
- added support for .env files (Thorsten)
2526
- added experimental support for FrankenPHP (Thorsten)
2627
- added experimental support for OpenSearch (Thorsten)
2728
- added experimental support for LDAP group support (Thorsten)
2829
- added experimental MCP Server (Thorsten)
2930
- added experimental update via command line (Thorsten)
30-
- added experimental support for .env files (Thorsten)
3131
- added experimental support for Mago (Thorsten)
3232
- improved online update feature (Thorsten)
3333
- updated Spanish translation (Thorsten)

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"symfony/dependency-injection": "^7.3",
4141
"symfony/dotenv": "^7.3",
4242
"symfony/event-dispatcher": "^7.3",
43+
"symfony/error-handler": "^7.3",
4344
"symfony/html-sanitizer": "^7.3",
4445
"symfony/http-client": "^7.3",
4546
"symfony/http-foundation": "^7.3",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpmyfaq/.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Debug Configuration
22
DEBUG=false
3-
DEBUG_LEVEL=0
43
DEBUG_LOG_QUERIES=false
54

65
# Application Configuration

phpmyfaq/frankenphp-worker.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
66
* It preloads phpMyFAQ's bootstrap and handles requests in a long-running process.
77
*
88
* @package phpMyFAQ
9-
* @author phpMyFAQ Team
10-
* @since 2025-01-01
9+
* @author Thorsten Rinne <[email protected]>
10+
* @copyright 2025 phpMyFAQ Team
11+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
* @link https://www.phpmyfaq.de
13+
* @since 2025-07-06
1114
*/
1215

13-
// Set up error reporting
14-
error_reporting(E_ALL);
15-
ini_set('display_errors', 1);
16-
17-
// Include phpMyFAQ bootstrap
16+
// Include phpMyFAQ bootstrap (sets up error handling via Environment)
1817
require_once __DIR__ . '/src/Bootstrap.php';
1918

2019
// FrankenPHP worker loop
21-
while ($worker = \frankenphp_handle_request()) {
20+
while ($worker = frankenphp_handle_request()) {
2221
// The worker will handle each request here
23-
// phpMyFAQ's routing and processing will be handled by the main application
22+
// the main application will handle phpMyFAQ's routing and processing
2423
// through the Bootstrap.php inclusion
25-
}
24+
}

phpmyfaq/search.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@
175175
$searchResult = '';
176176
$relTags = '';
177177
$tags = [];
178+
179+
// Set the base URL scheme for fulltext search
180+
$baseUrl = sprintf(
181+
'%ssearch.html?search=%s&seite=%d%s&pmf-search-category=%d',
182+
$faqConfig->getDefaultUrl(),
183+
urlencode($inputSearchTerm),
184+
$page,
185+
$languages,
186+
$inputCategory,
187+
);
178188
}
179189

180190
//
@@ -257,7 +267,7 @@
257267
$options = [
258268
'baseUrl' => $baseUrl,
259269
'total' => $numOfResults,
260-
'perPage' => $faqConfig->get('records.numberOfRecordsPerPage'),
270+
'perPage' => (int) $faqConfig->get('records.numberOfRecordsPerPage'),
261271
'pageParamName' => 'seite',
262272
'layoutTpl' => '<ul class="pagination justify-content-center">{LAYOUT_CONTENT}</ul>',
263273
];

phpmyfaq/services/azure/callback.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
use Symfony\Component\HttpFoundation\Session\Session;
2828
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
2929

30-
ini_set('display_errors', '1');
31-
ini_set('display_startup_errors', '1');
32-
error_reporting(-1);
33-
3430
if (session_status() !== PHP_SESSION_ACTIVE) {
3531
session_start();
3632
session_regenerate_id(true);
@@ -115,5 +111,3 @@
115111
} else {
116112
$redirect->send();
117113
}
118-
119-

phpmyfaq/src/Bootstrap.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
use Symfony\Component\HttpFoundation\Session\Session;
3232
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
3333

34-
ini_set('display_errors', '1');
35-
ini_set('display_startup_errors', '1');
36-
error_reporting(-1);
37-
3834
//
3935
// Fix the PHP include path if PMF is running under a "strange" PHP configuration
4036
//
@@ -48,7 +44,7 @@
4844
++$i;
4945
}
5046
if (!$foundCurrPath) {
51-
ini_set('include_path', '.' . PATH_SEPARATOR . ini_get('include_path'));
47+
set_include_path('.' . PATH_SEPARATOR . get_include_path());
5248
}
5349

5450
//
@@ -121,8 +117,8 @@
121117
//
122118
// Set the error handler and the exception handler
123119
//
124-
set_error_handler('\phpMyFAQ\Core\Error::errorHandler');
125-
set_exception_handler('\phpMyFAQ\Core\Error::exceptionHandler');
120+
set_error_handler('\\phpMyFAQ\\Core\\Error::errorHandler');
121+
set_exception_handler('\\phpMyFAQ\\Core\\Error::exceptionHandler');
126122

127123
//
128124
// Request
@@ -163,22 +159,20 @@
163159
// We always need a valid, secure session!
164160
//
165161
if (session_status() !== PHP_SESSION_ACTIVE) {
166-
ini_set('session.use_only_cookies', '1'); // Avoid any PHP version to move sessions on URLs
167-
ini_set('session.auto_start', '0'); // Prevent error to use session_start() if it's active in php.ini
168-
ini_set('session.use_trans_sid', '0');
169-
ini_set('session.cookie_samesite', 'Strict');
170-
ini_set('session.cookie_httponly', 'true');
171-
ini_set('session.cookie_secure', $request->isSecure());
172-
ini_set('url_rewriter.tags', '');
173-
174-
//
162+
$sessionOptions = [
163+
'use_only_cookies' => 1,
164+
'use_trans_sid' => 0,
165+
'cookie_samesite' => 'Strict',
166+
'cookie_httponly' => true,
167+
'cookie_secure' => $request->isSecure(),
168+
];
169+
175170
// Start the PHP session
176-
//
177171
if (defined('PMF_SESSION_SAVE_PATH') && !empty(PMF_SESSION_SAVE_PATH)) {
178-
session_save_path(PMF_SESSION_SAVE_PATH);
172+
$sessionOptions['save_path'] = PMF_SESSION_SAVE_PATH;
179173
}
180174

181-
session_start();
175+
session_start($sessionOptions);
182176
$session = new Session(new PhpBridgeSessionStorage());
183177
$session->start();
184178
}

phpmyfaq/src/phpMyFAQ/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function setDatabase(DatabaseDriver $databaseDriver): void
7676

7777
/**
7878
* Sets the Monolog logger instance, logs into a normal logfile
79-
* If DEBUG is true, it logs to the browser console as well.
8079
*/
8180
public function setLogger(): void
8281
{

phpmyfaq/src/phpMyFAQ/Environment.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919

2020
namespace phpMyFAQ;
2121

22+
use Monolog\Handler\StreamHandler;
23+
use Monolog\Logger;
2224
use Symfony\Component\Dotenv\Dotenv;
25+
use Symfony\Component\ErrorHandler\Debug;
26+
use Symfony\Component\ErrorHandler\ErrorHandler;
2327

2428
class Environment
2529
{
2630
private static bool $debugMode = false;
2731

28-
private static int $debugLevel = 0;
29-
3032
private static bool $debugLogQueries = false;
3133

3234
private static bool $initialized = false;
@@ -56,20 +58,9 @@ public static function enableTestMode(): void
5658
self::$testMode = true;
5759
}
5860

59-
public static function reset(): void
60-
{
61-
self::$debugMode = false;
62-
self::$debugLevel = 0;
63-
self::$debugLogQueries = false;
64-
self::$initialized = false;
65-
self::$environment = 'production';
66-
self::$testMode = false;
67-
self::$dotenv = null;
68-
}
69-
7061
private static function loadEnvironment(): void
7162
{
72-
$envPath = dirname(__DIR__, 2);
63+
$envPath = dirname(__DIR__, levels: 2);
7364
$envFile = $envPath . '/.env';
7465

7566
if (file_exists($envFile)) {
@@ -80,37 +71,43 @@ private static function loadEnvironment(): void
8071

8172
private static function setupDebugMode(): void
8273
{
74+
if (self::$testMode) {
75+
return;
76+
}
77+
8378
self::$debugMode = filter_var($_ENV['DEBUG'] ?? false, FILTER_VALIDATE_BOOLEAN);
84-
self::$debugLevel = (int) ($_ENV['DEBUG_LEVEL'] ?? 0);
8579
self::$debugLogQueries = filter_var($_ENV['DEBUG_LOG_QUERIES'] ?? false, FILTER_VALIDATE_BOOLEAN);
8680
self::$environment = $_ENV['APP_ENV'] ?? 'production';
8781

88-
// Legacy support
89-
if (!defined('DEBUG')) {
90-
define('DEBUG', self::$debugMode);
91-
}
82+
error_reporting(self::$debugMode ? E_ALL : E_ERROR | E_WARNING | E_PARSE);
9283

93-
// Error reporting based on debug mode
9484
if (self::$debugMode) {
95-
ini_set('display_errors', '1');
96-
ini_set('display_startup_errors', '1');
97-
error_reporting(E_ALL);
98-
} else {
99-
ini_set('display_errors', '0');
100-
error_reporting(E_ERROR | E_WARNING | E_PARSE);
85+
Debug::enable();
86+
ErrorHandler::register();
87+
return;
10188
}
89+
90+
$handler = ErrorHandler::register();
91+
92+
$logger = new Logger(name: 'phpmyfaq');
93+
$logTarget = $_ENV['ERROR_LOG'] ?? 'php://stderr';
94+
$logger->pushHandler(new StreamHandler($logTarget, Logger::WARNING));
95+
96+
$levelsMap = [
97+
E_DEPRECATED => null,
98+
E_USER_DEPRECATED => null,
99+
];
100+
$handler->setDefaultLogger($logger, $levelsMap);
101+
102+
$handler->screamAt(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
103+
$handler->scopeAt(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
102104
}
103105

104106
public static function isDebugMode(): bool
105107
{
106108
return self::$debugMode;
107109
}
108110

109-
public static function getDebugLevel(): int
110-
{
111-
return self::$debugLevel;
112-
}
113-
114111
public static function shouldLogQueries(): bool
115112
{
116113
return self::$debugLogQueries;

0 commit comments

Comments
 (0)