getCode() . '] occurred in [' . $e->getFile() . '] at line [' . $e->getLine() . ']: [' . trim(strip_tags($e->getMessage())) . ']'; } else { if (APPLICATION_ENV === 'testing') { echo $e->getMessage() . PHP_EOL; return; } error_log($e->getMessage()); if (defined('BB_MODE_API')) { $code = $e->getCode() ?: 9998; $result = ['result' => null, 'error' => ['message' => $e->getMessage(), 'code' => $code]]; echo json_encode($result); return false; } if (defined('BB_DEBUG') && BB_DEBUG && file_exists(PATH_VENDOR)) { /** * If advanced debugging is enabled, print Whoops instead of our error page. * flip/whoops documentation: https://github.com/filp/whoops/blob/master/docs/API%20Documentation.md. */ $whoops = new Run(); $prettyPage = new PrettyPageHandler(); $prettyPage->setPageTitle('An error ocurred'); $prettyPage->addDataTable('FOSSBilling environment', [ 'PHP Version' => PHP_VERSION, 'Error code' => $e->getCode(), ]); $whoops->pushHandler($prettyPage); $whoops->allowQuit(false); $whoops->writeToOutput(false); echo $whoops->handleException($e); } else { $page = " An error ocurred

Error

"; $page = str_replace(PHP_EOL, '', $page); echo $page; if ($e->getCode()) { echo sprintf('

Error code: %s

', $e->getCode()); } echo sprintf('

%s

', $e->getMessage()); echo sprintf('

Look for detailed error explanation

', urlencode($e->getMessage())); echo '

Powered by FOSSBilling

'; } } } set_exception_handler('handler_exception'); set_error_handler('handler_error'); // Check for Composer packages if (!file_exists(PATH_VENDOR)) { throw new Exception("It seems like Composer packages are missing. You have to run \"composer install\" in order to install them. For detailed instruction, you can see Composer's getting started guide.

If you have downloaded FOSSBilling from GitHub releases, this shouldn't happen.", 110); } // Multisite support. Load new configuration depending on the current hostname // If being run from CLI, first parameter must be the hostname $configPath = PATH_ROOT . '/config.php'; if ((isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']) || ('cli' === PHP_SAPI && isset($argv[1]))) { if ('cli' === PHP_SAPI) { $host = $argv[1]; } else { $host = $_SERVER['HTTP_HOST']; } $predictConfigPath = PATH_ROOT . '/config-' . $host . '.php'; if (file_exists($predictConfigPath)) { $configPath = $predictConfigPath; } } // Rename the old "bb-config" file to the new name if it exists if(!file_exists($configPath) && file_exists(PATH_ROOT . '/bb-config.php')){ rename(PATH_ROOT . '/bb-config.php', $configPath); } // Try to check if configuration is available if (!file_exists($configPath) || 0 === filesize($configPath)) { // Try to create an empty configuration file @file_put_contents($configPath, ''); $base_url = 'http' . ((isset($_SERVER['HTTPS']) && ('on' === $_SERVER['HTTPS'] || 1 == $_SERVER['HTTPS'])) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST']; $base_url .= rtrim(dirname($_SERVER['SCRIPT_NAME']), '/'); $url = $base_url . '/install/index.php'; if (file_exists(PATH_ROOT . '/install/index.php')) { header("Location: $url"); } $configFile = pathinfo($configPath, PATHINFO_BASENAME); $msg = "Your $configFile file seems to be invalid. It's possible that your preexisting configuration file may not contain the required configuration parameters or have become corrupted. FOSSBilling needs to have a valid configuration file present in order to function properly.

Please use the example config as reference here. You may need to manually restore a old config file or fix your existing one.

"; throw new Exception($msg, 101); } // Try to check if /install directory still exists, even after the installation was completed and display a message if (file_exists($configPath) && 0 !== filesize($configPath) && file_exists(PATH_ROOT . '/install/index.php')) { throw new Exception('For safety reasons, you have to delete the /install directory to start using FOSSBilling.

Please delete the /install directory from your web server.', 102); } // Detect old for files from an old BoxBilling or FOSSBilling preview installation. function detectOldFiles(){ $i = 0; $msg = ''; $foundOld = false; $oldFolderNames = ['bb-data','bb-library','bb-locale','bb-modules','bb-themes','bb-uploads','bb-cron.php','bb-di.php','bb-load.php','bb-config.php']; $newFolderNames = ['data','library','locale','modules','themes','uploads','cron.php','di.php','load.php','config.php']; foreach ($oldFolderNames as $folder){ $toCheck = PATH_ROOT . DIRECTORY_SEPARATOR . $folder; $newName = $newFolderNames[$i]; if(file_exists($toCheck) or is_dir($toCheck)){ $msg .= "$folder --> $newName
"; $foundOld = true; } $i++; } if($foundOld){ $finalMsg = "The FOSSBilling file structure has been changed, please migrate any custom files and folders from the old folder to the new folder and then delete the old ones. You will also need to update the paths in your config.php file.
"; $finalMsg .= "Feel free to join our Discord Server for assistance.

"; $finalMsg .= "Files and folders renamed:
"; $finalMsg .= $msg; throw new Exception($finalMsg); } } detectOldFiles(); // Try to check if /install directory still exists, even after the installation was completed if (file_exists($configPath) && 0 !== filesize($configPath) && file_exists(PATH_ROOT . '/install/index.php')) { throw new Exception('For safety reasons, you have to delete the /install directory to start using FOSSBilling.

Please delete the /install directory from your web server.', 102); } $config = require $configPath; require PATH_VENDOR . '/autoload.php'; date_default_timezone_set($config['timezone']); define('BB_DEBUG', $config['debug']); define('BB_URL', $config['url']); define('BB_SEF_URLS', $config['sef_urls']); define('PATH_CACHE', $config['path_data'] . '/cache'); define('PATH_LOG', $config['path_data'] . '/log'); define('BB_SSL', str_starts_with($config['url'], 'https')); if ($config['sef_urls']) { define('BB_URL_API', $config['url'] . 'api/'); } else { define('BB_URL_API', $config['url'] . 'index.php?_url=/api/'); } if ($config['debug']) { error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); } else { error_reporting(E_RECOVERABLE_ERROR); ini_set('display_errors', '0'); ini_set('display_startup_errors', '0'); } ini_set('log_errors', '1'); ini_set('html_errors', false); ini_set('error_log', PATH_LOG . '/php_error.log'); $isApache = (function_exists('apache_get_version')) ? true : false; $serverSoftware = (isset($_SERVER['SERVER_SOFTWARE'])) ? $_SERVER['SERVER_SOFTWARE'] : ''; if ($isApache or (false !== stripos($serverSoftware, 'apache'))) { if (!file_exists(PATH_ROOT . '/.htaccess')) { throw new Exception('Error: You appear to be running an Apache server without a valid .htaccess file. You may need to rename htaccess.txt to .htaccess'); } } // If the configured security mode is strict, redirect to HTTPS if (isset($config['security']['force_https']) && $config['security']['force_https'] && 'cli' !== PHP_SAPI){ $isHTTPS = false; if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { $isHTTPS = true; } if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') { $isHTTPS = true; } if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) == 'on') { $isHTTPS = true; } if(!$isHTTPS){ $url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; header('Location: ' . $url); exit; } }