mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-02 21:30:16 +09:00
* Add `is_custom` and file-backed email templates * Detect and handle missing default email templates * Standardize capitalization in email templates * Group OR conditions with parentheses in queries * Add `is_custom` and file-backed email templates * Detect and handle missing default email templates * Standardize capitalization in email templates * Group OR conditions with parentheses in queries * Fix test expectations to match updated OR condition SQL format Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/932b3ffe-6478-4ed4-b74d-a1af2681ff0d * Update src/modules/Email/templates/admin/mod_email_template.html.twig Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Wrap order status OR conditions in parentheses * Add tests to preserve client scope in searches * Optimize `getTemplateList`: DB-level filtering/pagination, remove per-request batch sync (#3373) * Optimize getTemplateList: DB-level filtering, pagination, remove templateBatchGenerate from list Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/fab0e6c0-51ef-4501-98f8-f5ab823816ab * Fix test: remove stale decrypt expectation now that list uses deep=false Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/6a891d33-a7dc-436d-9bd2-4a48923d91d7 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * Add test for resetting custom email template * Add `is_overridden` flag for email templates * Improve detection of overridden email templates * Update src/modules/Email/Service.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Detect template overrides by comparing defaults * Migrate Email module to Doctrine * Ignore whitespace when comparing email templates * Use Doctrine `EmailTemplate` repo; update tests * Avoid re-enabling templates; handle rows * Add email template schema check and row mapping * Add Type and Status columns to email templates * Limit run-patcher & remove email schema migration * Update tests-legacy/modules/Email/Api/Api_AdminTest.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/library/FOSSBilling/UpdatePatcher.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Email/Service.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Email/Service.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update tests-legacy/modules/Email/ServiceTest.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix getTemplateList skipping EmailTemplate entities from pagination result (#3379) * Initial plan * Fix getTemplateList to handle EmailTemplate entities from pagination result Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/5567a644-4032-40aa-a6af-8eda8cbcea36 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
306 lines
9.8 KiB
PHP
306 lines
9.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* Copyright 2022-2025 FOSSBilling
|
|
* Copyright 2011-2021 BoxBilling, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0.
|
|
*
|
|
* @copyright FOSSBilling (https://www.fossbilling.org)
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
|
|
*/
|
|
|
|
use FOSSBilling\Config;
|
|
use FOSSBilling\Environment;
|
|
use FOSSBilling\SentryHelper;
|
|
use FOSSBilling\Tools;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
use Symfony\Component\Filesystem\Path;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Whoops\Handler\PrettyPageHandler;
|
|
use Whoops\Run;
|
|
|
|
/*
|
|
* Check if the installer is present.
|
|
*/
|
|
function checkInstaller(): void
|
|
{
|
|
global $filesystem;
|
|
|
|
// Ignore this check if not in production environment.
|
|
if (!Environment::isProduction()) {
|
|
return;
|
|
}
|
|
|
|
// Check if /install directory still exists after installation has been completed.
|
|
if ($filesystem->exists(PATH_CONFIG) && $filesystem->exists(Path::join('install', 'install.php'))) {
|
|
// Throw exception only if debug mode is NOT enabled.
|
|
if (!empty(Config::getProperty('debug_and_monitoring.debug'))) {
|
|
throw new Exception('For security reasons, you have to delete the install directory before you can use FOSSBilling.', 2);
|
|
}
|
|
}
|
|
|
|
// If the config file exists and not install.php, but the install folder does, perform some cleanup.
|
|
if ($filesystem->exists(PATH_CONFIG) && $filesystem->exists(Path::normalize('install')) && !DEBUG) {
|
|
$filesystem->remove('install');
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Check if SSL required, and enforce if so.
|
|
*/
|
|
function checkSSL(): void
|
|
{
|
|
global $request;
|
|
|
|
if (!empty(Config::getProperty('security.force_https')) && Config::getProperty('security.force_https') && !Environment::isCLI()) {
|
|
if (!Tools::isHTTPS()) {
|
|
header('Location: https://' . $request->getHost() . $request->getRequestUri());
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Check if the update patcher needs to be run.
|
|
*/
|
|
function checkUpdatePatcher(): void
|
|
{
|
|
global $di, $filesystem, $request;
|
|
|
|
if ($request->getPathInfo() !== '/run-patcher' && $request->query->get('_url') !== '/run-patcher') {
|
|
return;
|
|
}
|
|
|
|
$patcher = new FOSSBilling\UpdatePatcher();
|
|
$patcher->setDi($di);
|
|
|
|
$version = FOSSBilling\Version::VERSION;
|
|
if ($di['cache']->getItem('updatePatcher')->isHit()
|
|
&& $version === $di['cache']->getItem('updatePatcher')->get()
|
|
&& $patcher->availablePatches() === 0
|
|
) {
|
|
exit('The update patcher has already been run for this version.');
|
|
}
|
|
|
|
try {
|
|
$patcher->applyConfigPatches();
|
|
$patcher->applyCorePatches();
|
|
|
|
// Clear the file cache after applying patches.
|
|
$filesystem->remove(PATH_CACHE);
|
|
$filesystem->mkdir(PATH_CACHE);
|
|
|
|
$di['cache']->getItem('updatePatcher')->set(FOSSBilling\Version::VERSION);
|
|
|
|
exit('Any missing config migrations or database patches have been applied and the cache has been cleared.');
|
|
} catch (Exception $e) {
|
|
exit("An error occurred while attempting to apply patches: <br>{$e->getMessage()}.");
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Check the web server config.
|
|
*/
|
|
function checkWebServer(): void
|
|
{
|
|
global $filesystem;
|
|
|
|
// Check for missing required .htaccess on Apache and Apache-compatible web servers.
|
|
$webServer = SentryHelper::estimateWebServer();
|
|
if ($webServer === 'Apache' || $webServer === 'Litespeed') {
|
|
if (!$filesystem->exists('.htaccess')) {
|
|
throw new Exception('Missing .htaccess file', 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Error handler.
|
|
*/
|
|
function errorHandler(int $number, string $message, string $file, int $line): bool
|
|
{
|
|
// If it's an exception, handle it. Otherwise we don't need to do anything as PHP will log it for us.
|
|
if ($number === E_RECOVERABLE_ERROR) {
|
|
exceptionHandler(new ErrorException($message, $number, 0, $file, $line));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Exception handler.
|
|
*/
|
|
function exceptionHandler(Exception|Error $e)
|
|
{
|
|
global $filesystem;
|
|
|
|
if (Environment::isTesting()) {
|
|
echo $e->getMessage() . PHP_EOL;
|
|
|
|
return;
|
|
}
|
|
error_log("{$e->getMessage()} at {$e->getFile()} : {$e->getLine()}");
|
|
|
|
$message = htmlspecialchars($e->getMessage());
|
|
|
|
if (defined('API_MODE')) {
|
|
$code = $e->getCode() ?: 9998;
|
|
$result = ['result' => null, 'error' => ['message' => $message, 'code' => $code]];
|
|
echo json_encode($result);
|
|
|
|
return false;
|
|
}
|
|
|
|
if (defined('DEBUG') && DEBUG && $filesystem->exists(PATH_VENDOR)) {
|
|
/**
|
|
* If advanced debugging is enabled, print Whoops instead of our error page.
|
|
* filp/whoops documentation: https://github.com/filp/whoops/blob/master/docs/API%20Documentation.md.
|
|
*/
|
|
$whoops = new Run();
|
|
$prettyPage = new PrettyPageHandler();
|
|
$prettyPage->setPageTitle('An error occurred');
|
|
$prettyPage->addDataTable('FOSSBilling environment', [
|
|
'PHP Version' => PHP_VERSION,
|
|
'Error code' => $e->getCode(),
|
|
'Instance ID' => INSTANCE_ID ?? 'Unknown',
|
|
]);
|
|
$whoops->pushHandler($prettyPage);
|
|
$whoops->allowQuit(false);
|
|
$whoops->writeToOutput(false);
|
|
|
|
echo $whoops->handleException($e);
|
|
} else {
|
|
$errorPage = new FOSSBilling\ErrorPage();
|
|
$errorPage->generatePage($e->getCode(), $message);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Pre-initialization.
|
|
*/
|
|
function preInit(): void
|
|
{
|
|
// Define root path.
|
|
define('PATH_ROOT', __DIR__);
|
|
|
|
// Check vendor folder exists and load Composer autoloader.
|
|
define('PATH_VENDOR', PATH_ROOT . DIRECTORY_SEPARATOR . 'vendor');
|
|
if (!file_exists(PATH_VENDOR)) {
|
|
throw new Exception('The composer packages are missing.', 1);
|
|
}
|
|
require PATH_VENDOR . DIRECTORY_SEPARATOR . 'autoload.php';
|
|
|
|
// Define global paths.
|
|
define('PATH_LIBRARY', Path::join(PATH_ROOT, 'library'));
|
|
define('PATH_THEMES', Path::join(PATH_ROOT, 'themes'));
|
|
define('PATH_MODS', Path::join(PATH_ROOT, 'modules'));
|
|
define('PATH_LANGS', Path::join(PATH_ROOT, 'locale'));
|
|
define('PATH_UPLOADS', Path::join(PATH_ROOT, 'data', 'uploads'));
|
|
define('PATH_CONFIG', Path::join(PATH_ROOT, 'config.php'));
|
|
|
|
// Load required FOSSBilling libraries.
|
|
require Path::join(PATH_LIBRARY, 'FOSSBilling', 'ErrorPage.php');
|
|
require Path::join(PATH_LIBRARY, 'FOSSBilling', 'SentryHelper.php');
|
|
require Path::join(PATH_LIBRARY, 'FOSSBilling', 'Environment.php');
|
|
require Path::join(PATH_LIBRARY, 'FOSSBilling', 'Config.php');
|
|
require Path::join(PATH_LIBRARY, 'FOSSBilling', 'Tools.php');
|
|
}
|
|
|
|
/*
|
|
* Initialize the application.
|
|
*/
|
|
function init(): void
|
|
{
|
|
// Define custom error handlers.
|
|
set_exception_handler(exceptionHandler(...));
|
|
set_error_handler(errorHandler(...));
|
|
|
|
// Initialize required Symfony components.
|
|
global $filesystem, $request;
|
|
$filesystem = new Filesystem();
|
|
$request = Request::createFromGlobals();
|
|
|
|
// Check config exists, redirecting to installer or throwing an exception if not.
|
|
if (!$filesystem->exists(PATH_CONFIG) && $filesystem->exists(Path::join('install', 'install.php'))) {
|
|
$response = new RedirectResponse($request->getSchemeAndHttpHost() . $request->getBasePath() . '/install/install.php', 307);
|
|
$response->send();
|
|
exit;
|
|
} elseif (!$filesystem->exists(PATH_CONFIG) && !$filesystem->exists(Path::join('install', 'install.php'))) {
|
|
throw new Exception('The FOSSBilling configuration file is empty or invalid.', 3);
|
|
}
|
|
|
|
// Set globals and relevant settings based on the config.
|
|
date_default_timezone_set(Config::getProperty('i18n.timezone', 'UTC'));
|
|
define('ADMIN_PREFIX', Config::getProperty('admin_area_prefix'));
|
|
define('DEBUG', (bool) Config::getProperty('debug_and_monitoring.debug', false));
|
|
define('PATH_DATA', Path::normalize(Config::getProperty('path_data')));
|
|
define('PATH_CACHE', Path::join(PATH_DATA, 'cache'));
|
|
define('PATH_LOG', Path::join(PATH_DATA, 'log'));
|
|
define('INSTANCE_ID', Config::getProperty('info.instance_id', 'Unknown'));
|
|
|
|
// Set the system URL.
|
|
$scheme = Config::getProperty('security.force_https', true) || Tools::isHTTPS() ? 'https://' : 'http://';
|
|
|
|
// Keep the app working correctly if the URL didn't get correctly updated
|
|
$url = str_replace(['https://', 'http://'], '', Config::getProperty('url'));
|
|
define('SYSTEM_URL', $scheme . $url);
|
|
|
|
// Set the default interface.
|
|
define('BIND_TO', Tools::getDefaultInterface());
|
|
|
|
// Initial setup and checks passed, now we setup our custom autoloader.
|
|
require Path::join(PATH_LIBRARY, 'FOSSBilling', 'Autoloader.php');
|
|
$loader = new FOSSBilling\AutoLoader();
|
|
$loader->register();
|
|
|
|
// Load the DI container.
|
|
global $di;
|
|
$di = require Path::join(PATH_ROOT, 'di.php');
|
|
|
|
// Now that the config file is loaded, we can enable Sentry.
|
|
SentryHelper::registerSentry();
|
|
}
|
|
|
|
/*
|
|
* Post-initialization.
|
|
*/
|
|
function postInit(): void
|
|
{
|
|
// Set error and exception handlers, and default logging settings.
|
|
ini_set('log_errors', '1');
|
|
ini_set('html_errors', false);
|
|
ini_set('error_log', Path::join(PATH_LOG, 'php_error.log'));
|
|
error_reporting(E_ALL);
|
|
|
|
if (DEBUG) {
|
|
ini_set('display_errors', '1');
|
|
ini_set('display_startup_errors', '1');
|
|
} else {
|
|
ini_set('display_errors', '0');
|
|
ini_set('display_startup_errors', '0');
|
|
}
|
|
}
|
|
|
|
// Perform pre-initialization (loading required dependencies, etc.).
|
|
preInit();
|
|
|
|
// Initialize the application.
|
|
init();
|
|
|
|
// Verify the installer was removed.
|
|
checkInstaller();
|
|
|
|
// Check if SSL required, and enforce if so.
|
|
checkSSL();
|
|
|
|
// Check web server and web server settings.
|
|
checkWebServer();
|
|
|
|
// Check whether the patcher needs to be run.
|
|
checkUpdatePatcher();
|
|
|
|
// Perform post-initialization (setting error handlers, etc).
|
|
postInit();
|