diff --git a/.gitignore b/.gitignore index 4981f25a5..dbd3b9a15 100644 --- a/.gitignore +++ b/.gitignore @@ -18,9 +18,14 @@ composer.phar public report/* data/db + +### phpunit/phpunit ### .phpunit.result.cache -### node_moduels ### +### node_modules ### node_modules npm-debug.log .history/ + +### friendsofphp/php-cs-fixer ### +src/.php-cs-fixer.cache diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 000000000..b5f72a57e --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,82 @@ +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] +# Change these settings to your own preference +indent_style = space +indent_size = 4 + +# We recommend you to keep these unchanged +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.feature] +indent_style = space +indent_size = 4 + +[*.js] +indent_style = space +indent_size = 2 + +[*.json] +indent_style = space +indent_size = 2 + +[*.md] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = false + +[*.neon] +indent_style = space +indent_size = 4 + +[*.php] +indent_style = space +indent_size = 4 + +[*.sh] +indent_style = space +indent_size = 4 + +[*.{yaml,yml}] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = false + +[.babelrc] +indent_style = space +indent_size = 2 + +[.gitmodules] +indent_style = tab +indent_size = 4 + +[.php_cs{,.dist}] +indent_style = space +indent_size = 4 + +[composer.json] +indent_style = space +indent_size = 4 + +[package.json] +indent_style = space +indent_size = 2 + +[phpspec.yml{,.dist}] +indent_style = space +indent_size = 4 + +[phpstan.neon] +indent_style = space +indent_size = 4 + +[phpunit.xml{,.dist}] +indent_style = space +indent_size = 4 diff --git a/src/.php-cs-fixer.dist.php b/src/.php-cs-fixer.dist.php new file mode 100644 index 000000000..47366a954 --- /dev/null +++ b/src/.php-cs-fixer.dist.php @@ -0,0 +1,26 @@ +setRules([ + '@PHP71Migration' => true, + '@Symfony' => true, + 'protected_to_private' => false, + 'nullable_type_declaration_for_default_null_value' => ['use_nullable_type_declaration' => false], + 'phpdoc_to_comment' => false, + ]) + ->setRiskyAllowed(false) + ->setFinder( + (new PhpCsFixer\Finder()) + ->in(__DIR__) + ->exclude([ + 'bb-data', + 'bb-library', + 'bb-locale', + 'bb-themes', + 'bb-vendor', + 'install', + 'bb-modules/Wysiwyg' + ]) + ->notPath('rb.php') + ) +; diff --git a/src/bb-config-sample.php b/src/bb-config-sample.php index f0856dce1..1cfa8a120 100755 --- a/src/bb-config-sample.php +++ b/src/bb-config-sample.php @@ -1,6 +1,6 @@ '', +return [ + 'salt' => '', /** - * Full URL where BoxBilling is installed with trailing slash + * Full URL where BoxBilling is installed with trailing slash. */ - 'url' => 'http://localhost/', + 'url' => 'http://localhost/', /** - * The URL prefix to access the BB admin area. Ex: '/bb-admin' for https://example.com/bb-admin + * The URL prefix to access the BB admin area. Ex: '/bb-admin' for https://example.com/bb-admin. */ - 'admin_area_prefix' => '/bb-admin', + 'admin_area_prefix' => '/bb-admin', /** * Enable or disable displaying advanced debugging messages. * You should keep this disabled unless you're making tests as it can reveal some information about your server. */ - 'debug' => false, + 'debug' => false, - 'maintenance_mode' => array( + 'maintenance_mode' => [ /** * Enable or disable the system maintenance mode. * Enabling this will block public access to your website, and API endpoints except the allowed ones won't work * However, this will not block access to the administrator area. - * + * * @since 4.22.0 */ - 'enabled' => false, + 'enabled' => false, /** * Don't block these URLs when the maintenance is going on. - * Supports wildcard (e.g. '/api/guest/staff/*') + * Supports wildcard (e.g. '/api/guest/staff/*'). * * @since 4.22.0 */ - 'allowed_urls' => array(), + 'allowed_urls' => [], /** * Don't block these IP/Subnet addresses when the maintenance is going on. - * Supported formats: 127.0.0.1、127.0.0.1/32 + * Supported formats: 127.0.0.1、127.0.0.1/32. * * @since 4.22.0 */ - 'allowed_ips' => array(), - ), - + 'allowed_ips' => [], + ], + /** * Enable or disable search engine friendly URLs. * Configure .htaccess file before enabling this feature - * Set to TRUE if using nginx + * Set to TRUE if using nginx. */ - 'sef_urls' => true, + 'sef_urls' => true, /** - * System timezone + * System timezone. */ - 'timezone' => 'UTC', + 'timezone' => 'UTC', /** - * BoxBilling locale + * BoxBilling locale. */ - 'locale' => 'en_US', + 'locale' => 'en_US', /** - * Set default date format for localized strings + * Set default date format for localized strings. + * * @see http://php.net/manual/en/function.strftime.php */ - 'locale_date_format' => '%A, %d %B %G', + 'locale_date_format' => '%A, %d %B %G', /** - * Set default time format for localized strings + * Set default time format for localized strings. + * * @see http://php.net/manual/en/function.strftime.php */ - 'locale_time_format' => ' %T', + 'locale_time_format' => ' %T', /** - * Set location to store sensitive data + * Set location to store sensitive data. */ - 'path_data' => dirname(__FILE__) . '/bb-data', + 'path_data' => dirname(__FILE__).'/bb-data', - 'path_logs' => dirname(__FILE__) . '/bb-data/log/application.log', + 'path_logs' => dirname(__FILE__).'/bb-data/log/application.log', - 'log_to_db' => true, + 'log_to_db' => true, - 'db' => array( + 'db' => [ /** * Database type. Don't change this if in doubt. */ - 'type' => 'mysql', + 'type' => 'mysql', /** * Database hostname. Don't change this if in doubt. */ - 'host' => getenv('DB_HOST') ?: '127.0.0.1', + 'host' => getenv('DB_HOST') ?: '127.0.0.1', /** - * The name of the database for BoxBilling + * The name of the database for BoxBilling. */ - 'name' => getenv('DB_NAME') ?: 'boxbilling', + 'name' => getenv('DB_NAME') ?: 'boxbilling', /** - * Database username + * Database username. */ - 'user' => getenv('DB_USER') ?: 'foo', + 'user' => getenv('DB_USER') ?: 'foo', /** - * Database password + * Database password. */ - 'password' => getenv('DB_PASS') ?: 'foo', + 'password' => getenv('DB_PASS') ?: 'foo', /** - * Database Port + * Database Port. */ - 'port' => getenv('DB_PORT') ?: '3306', - ), + 'port' => getenv('DB_PORT') ?: '3306', + ], - 'twig' => array( - 'debug' => false, - 'auto_reload' => false, - 'cache' => dirname(__FILE__) . '/bb-data/cache', - ), + 'twig' => [ + 'debug' => false, + 'auto_reload' => false, + 'cache' => dirname(__FILE__).'/bb-data/cache', + ], - 'api' => array( + 'api' => [ // All requests made to the API must have referrer request header with the same URL as the BoxBilling installation - 'require_referrer_header' => false, + 'require_referrer_header' => false, // Empty array will allow all IPs to access the API - 'allowed_ips' => array(), + 'allowed_ips' => [], // Time span for limit in seconds - 'rate_span' => 60 * 60, + 'rate_span' => 60 * 60, // How many requests allowed per time span - 'rate_limit' => 1000, + 'rate_limit' => 1000, /** * Note about rate-limiting login attempts: - * When the limit is reached, a default delay of 2 seconds is added to the request. + * When the limit is reached, a default delay of 2 seconds is added to the request. * This makes brute-forcing a password useless while not outright blocking legitimate traffic. * When calculating, ensure the rate-limited traffic can still make enough requests to stay rate limited - * Ex: One request every 2 seconds is more than 20 times in 1 minute, so the IP will remain throttled + * Ex: One request every 2 seconds is more than 20 times in 1 minute, so the IP will remain throttled. * * @since 4.22.0 */ // Throttling delay - 'throttle_delay' => 2, + 'throttle_delay' => 2, // Time span login for limit in seconds - 'rate_span_login' => 60, + 'rate_span_login' => 60, // How many login requests allowed per time span - 'rate_limit_login' => 20, - ), + 'rate_limit_login' => 20, + ], - 'guzzle' => array( + 'guzzle' => [ /** - * The user agent to be used when making requests to external services + * The user agent to be used when making requests to external services. * * @since 4.22.0 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent */ - 'user_agent' => 'Mozilla/5.0 (RedHatEnterpriseLinux; Linux x86_64; BoxBilling; +http://boxbilling.org) Gecko/20100101 Firefox/93.0', + 'user_agent' => 'Mozilla/5.0 (RedHatEnterpriseLinux; Linux x86_64; BoxBilling; +http://boxbilling.org) Gecko/20100101 Firefox/93.0', /** * Default request timeout - * Setting 0 will disable this limitation + * Setting 0 will disable this limitation. * * @since 4.22.0 * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout */ - 'timeout' => 0, + 'timeout' => 0, /** * The HTTP Upgrade-Insecure-Requests header sends a signal to the server * expressing the client’s preference for an encrypted response. - * + * * 0: don't ask for an encrypted response * 1: ask for an encrypted response - * + * * @since 4.22.0 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade-Insecure-Requests */ - 'upgrade_insecure_requests' => 0 - ), -); \ No newline at end of file + 'upgrade_insecure_requests' => 0, + ], +]; diff --git a/src/bb-cron.php b/src/bb-cron.php index 5c385a34f..ad73a0280 100644 --- a/src/bb-cron.php +++ b/src/bb-cron.php @@ -1,6 +1,6 @@ getLastExecutionTime() . ".\e[0m"); + if ('cli' == php_sapi_name()) { + echo "\e[34mLast executed: ".$service->getLastExecutionTime().".\e[0m"; } $service->runCrons($interval); - } catch (Exception $exception) { +} catch (Exception $exception) { throw new Exception($exception); - } finally { - if (php_sapi_name() == 'cli') { - print ("\e[32mSuccessfully ran the cron jobs.\e[0m"); +} finally { + if ('cli' == php_sapi_name()) { + echo "\e[32mSuccessfully ran the cron jobs.\e[0m"; } else { $login_url = $di['url']->link('bb-admin'); unset($service, $interval, $di); header("Location: $login_url"); } - } +} diff --git a/src/bb-di.php b/src/bb-di.php index 7ba839136..06bc541cf 100644 --- a/src/bb-di.php +++ b/src/bb-di.php @@ -1,6 +1,6 @@ setDi($di); $log_to_db = isset($di['config']['log_to_db']) && $di['config']['log_to_db']; if ($log_to_db) { $activity_service = $di['mod_service']('activity'); - $writer2 = new Box_LogDb($activity_service); - if ($di['auth']->isAdminLoggedIn()){ - $admin = $di['loggedin_admin']; - $log->setEventItem('admin_id', $admin->id); - } - elseif ($di['auth']->isClientLoggedIn()) { + $writer2 = new Box_LogDb($activity_service); + if ($di['auth']->isAdminLoggedIn()) { + $admin = $di['loggedin_admin']; + $log->setEventItem('admin_id', $admin->id); + } elseif ($di['auth']->isClientLoggedIn()) { $client = $di['loggedin_client']; $log->setEventItem('client_id', $client->id); } $log->addWriter($writer2); } else { $logFile = $di['config']['path_logs']; - $writer = new Box_LogStream($logFile); + $writer = new Box_LogStream($logFile); $log->addWriter($writer); } return $log; }; -$di['crypt'] = function() use ($di) { - $crypt = new Box_Crypt(); +$di['crypt'] = function () use ($di) { + $crypt = new Box_Crypt(); $crypt->setDi($di); + return $crypt; }; -$di['pdo'] = function() use ($di) { +$di['pdo'] = function () use ($di) { $c = $di['config']['db']; $pdo = new PDO($c['type'].':host='.$c['host'].';port='.$c['port'].';dbname='.$c['name'], $c['user'], $c['password'], - array( - PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - ) + [ + PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ] ); - if(isset($c['debug']) && $c['debug']) { - $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array ('Box_DbLoggedPDOStatement')); + if (isset($c['debug']) && $c['debug']) { + $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Box_DbLoggedPDOStatement']); } - if($c['type'] == 'mysql') { - $pdo->exec( 'SET NAMES "utf8"' ); - $pdo->exec( 'SET CHARACTER SET utf8' ); - $pdo->exec( 'SET CHARACTER_SET_CONNECTION = utf8' ); - $pdo->exec( 'SET character_set_results = utf8' ); - $pdo->exec( 'SET character_set_server = utf8' ); - $pdo->exec( 'SET SESSION interactive_timeout = 28800' ); - $pdo->exec( 'SET SESSION wait_timeout = 28800' ); + if ('mysql' == $c['type']) { + $pdo->exec('SET NAMES "utf8"'); + $pdo->exec('SET CHARACTER SET utf8'); + $pdo->exec('SET CHARACTER_SET_CONNECTION = utf8'); + $pdo->exec('SET character_set_results = utf8'); + $pdo->exec('SET character_set_server = utf8'); + $pdo->exec('SET SESSION interactive_timeout = 28800'); + $pdo->exec('SET SESSION wait_timeout = 28800'); } return $pdo; }; -$di['db'] = function() use ($di) { - - require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rb.php'; +$di['db'] = function () use ($di) { + require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'rb.php'; R::setup($di['pdo']); - \RedBeanPHP\Util\DispenseHelper::setEnforceNamingPolicy( false ); + \RedBeanPHP\Util\DispenseHelper::setEnforceNamingPolicy(false); $helper = new Box_BeanHelper(); $helper->setDi($di); $mapper = new \RedBeanPHP\Facade(); $mapper->getRedBean()->setBeanHelper($helper); - $freeze = isset($di['config']['db']['freeze']) ? (bool)$di['config']['db']['freeze'] : true; + $freeze = isset($di['config']['db']['freeze']) ? (bool) $di['config']['db']['freeze'] : true; $mapper->freeze($freeze); $db = new Box_Database(); $db->setDi($di); $db->setDataMapper($mapper); + return $db; }; -$di['pager'] = function() use($di) { +$di['pager'] = function () use ($di) { $service = new Box_Pagination(); $service->setDi($di); + return $service; }; -$di['url'] = function() use ($di) { - $url = new Box_Url(); +$di['url'] = function () use ($di) { + $url = new Box_Url(); $url->setDi($di); $url->setBaseUri(BB_URL); + return $url; }; -$di['mod'] = $di->protect(function ($name) use($di) { +$di['mod'] = $di->protect(function ($name) use ($di) { $mod = new Box_Mod($name); $mod->setDi($di); + return $mod; }); -$di['mod_service'] = $di->protect(function ($mod, $sub = '') use($di) { +$di['mod_service'] = $di->protect(function ($mod, $sub = '') use ($di) { return $di['mod']($mod)->getService($sub); }); -$di['mod_config'] = $di->protect(function ($name) use($di) { +$di['mod_config'] = $di->protect(function ($name) use ($di) { return $di['mod']($name)->getConfig(); }); -$di['events_manager'] = function() use ($di) { +$di['events_manager'] = function () use ($di) { $service = new Box_EventManager(); $service->setDi($di); + return $service; }; $di['session'] = function () use ($di) { $handler = new PdoSessionHandler($di['pdo']); + return new Box_Session($handler); }; $di['cookie'] = function () use ($di) { $service = new Box_Cookie(); $service->setDi($di); + return $service; }; $di['request'] = function () use ($di) { $service = new Box_Request(); $service->setDi($di); + return $service; }; -$di['cache'] = function () use ($di) { return new FileCache();}; -$di['auth'] = function () use ($di) { return new Box_Authorization($di);}; +$di['cache'] = function () { return new FileCache(); }; +$di['auth'] = function () use ($di) { return new Box_Authorization($di); }; $di['twig'] = $di->factory(function () use ($di) { $config = $di['config']; $options = $config['twig']; @@ -147,18 +153,17 @@ $di['twig'] = $di->factory(function () use ($di) { $box_extensions = new Box_TwigExtensions(); $box_extensions->setDi($di); - //$twig->addExtension(new Twig\Extension\OptimizerExtension()); - $twig->addExtension(new \Twig\Extension\StringLoaderExtension()); - $twig->addExtension(new Twig\Extension\DebugExtension()); - $twig->addExtension(new Twig\Extensions\I18nExtension()); + // $twig->addExtension(new Twig\Extension\OptimizerExtension()); + $twig->addExtension(new \Twig\Extension\StringLoaderExtension()); + $twig->addExtension(new Twig\Extension\DebugExtension()); + $twig->addExtension(new Twig\Extensions\I18nExtension()); $twig->addExtension($box_extensions); - $twig->getExtension(Twig\Extension\CoreExtension::class)->setDateFormat($config['locale_date_format']); - $twig->getExtension(Twig\Extension\CoreExtension::class)->setTimezone($config['timezone']); - + $twig->getExtension(Twig\Extension\CoreExtension::class)->setDateFormat($config['locale_date_format']); + $twig->getExtension(Twig\Extension\CoreExtension::class)->setTimezone($config['timezone']); // add globals - if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { - $_GET['ajax'] = TRUE; + if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) { + $_GET['ajax'] = true; } $twig->addGlobal('request', $_GET); @@ -167,58 +172,58 @@ $di['twig'] = $di->factory(function () use ($di) { return $twig; }); -$di['is_client_logged'] = function() use($di) { - if(!$di['auth']->isClientLoggedIn()) { +$di['is_client_logged'] = function () use ($di) { + if (!$di['auth']->isClientLoggedIn()) { $api_str = '/api/'; $url = $di['request']->getQuery('_url'); - if (strncasecmp($url, $api_str , strlen($api_str)) === 0) { - //Throw Exception if api request + if (0 === strncasecmp($url, $api_str, strlen($api_str))) { + // Throw Exception if api request throw new Exception('Client is not logged in'); - } - else { - //Redirect to login page if browser request + } else { + // Redirect to login page if browser request $login_url = $di['url']->link('login'); header("Location: $login_url"); } - } + return true; }; -$di['is_admin_logged'] = function() use($di) { - - if(!$di['auth']->isAdminLoggedIn()) { - $api_str = '/api/'; - $url = $di['request']->getQuery('_url'); - if (strncasecmp($url, $api_str , strlen($api_str)) === 0) { - //Throw Exception if api request - throw new Exception('Admin is not logged in'); - } - else { - //Redirect to login page if browser request - $login_url = $di['url']->adminLink('staff/login'); - header("Location: $login_url"); +$di['is_admin_logged'] = function () use ($di) { + if (!$di['auth']->isAdminLoggedIn()) { + $api_str = '/api/'; + $url = $di['request']->getQuery('_url'); + if (0 === strncasecmp($url, $api_str, strlen($api_str))) { + // Throw Exception if api request + throw new Exception('Admin is not logged in'); + } else { + // Redirect to login page if browser request + $login_url = $di['url']->adminLink('staff/login'); + header("Location: $login_url"); } } + return true; }; -$di['loggedin_client'] = function() use ($di) { +$di['loggedin_client'] = function () use ($di) { $di['is_client_logged']; $client_id = $di['session']->get('client_id'); + return $di['db']->getExistingModelById('Client', $client_id); }; -$di['loggedin_admin'] = function() use ($di) { - if(php_sapi_name() == 'cli' || substr(php_sapi_name(), 0, 3) == 'cgi') { +$di['loggedin_admin'] = function () use ($di) { + if ('cli' == php_sapi_name() || 'cgi' == substr(php_sapi_name(), 0, 3)) { return $di['mod_service']('staff')->getCronAdmin(); } $di['is_admin_logged']; $admin = $di['session']->get('admin'); + return $di['db']->getExistingModelById('Admin', $admin['id']); }; -$di['api'] = $di->protect(function($role) use($di) { +$di['api'] = $di->protect(function ($role) use ($di) { switch ($role) { case 'guest': $identity = new \Model_Guest(); @@ -241,31 +246,34 @@ $di['api'] = $di->protect(function($role) use($di) { } $api = new Api_Handler($identity); $api->setDi($di); + return $api; }); -$di['api_guest'] = function() use ($di) { return $di['api']('guest'); }; -$di['api_client'] = function() use ($di) { return $di['api']('client');}; -$di['api_admin'] = function() use ($di) { return $di['api']('admin'); }; -$di['api_system'] = function() use ($di) { return $di['api']('system'); }; +$di['api_guest'] = function () use ($di) { return $di['api']('guest'); }; +$di['api_client'] = function () use ($di) { return $di['api']('client'); }; +$di['api_admin'] = function () use ($di) { return $di['api']('admin'); }; +$di['api_system'] = function () use ($di) { return $di['api']('system'); }; -$di['tools'] = function () use ($di){ +$di['tools'] = function () use ($di) { $service = new Box_Tools(); $service->setDi($di); + return $service; }; -$di['validator'] = function () use ($di){ +$di['validator'] = function () use ($di) { $validator = new Box_Validate(); $validator->setDi($di); + return $validator; }; -$di['guzzle_client'] = function () use($di) { +$di['guzzle_client'] = function () use ($di) { return new GuzzleHttp\Client([ 'headers' => [ 'User-Agent' => $di['config']['guzzle']['user_agent'], - 'Upgrade-Insecure-Requests' => $di['config']['guzzle']['upgrade_insecure_requests'] + 'Upgrade-Insecure-Requests' => $di['config']['guzzle']['upgrade_insecure_requests'], ], - 'timeout' => $di['config']['guzzle']['timeout'] + 'timeout' => $di['config']['guzzle']['timeout'], ]); }; $di['mail'] = function () { @@ -274,97 +282,110 @@ $di['mail'] = function () { $di['extension'] = function () use ($di) { $extension = new \Box_Extension(); $extension->setDi($di); + return $extension; }; $di['updater'] = function () use ($di) { $updater = new \Box_Update(); $updater->setDi($di); + return $updater; }; $di['curl'] = function ($url) use ($di) { $curl = new \Box_Curl($url); $curl->setDi($di); + return $curl; - }; -$di['zip_archive'] = function () use ($di) {return new ZipArchive();}; +$di['zip_archive'] = function () {return new ZipArchive(); }; -$di['server_package'] = function () use ($di) {return new Server_Package();}; -$di['server_client'] = function () use ($di) {return new Server_Client();}; -$di['server_account'] = function () use ($di) {return new Server_Account();}; +$di['server_package'] = function () {return new Server_Package(); }; +$di['server_client'] = function () {return new Server_Client(); }; +$di['server_account'] = function () {return new Server_Account(); }; -$di['server_manager'] = $di->protect(function ($manager, $config) use($di) { +$di['server_manager'] = $di->protect(function ($manager, $config) { $class = sprintf('Server_Manager_%s', ucfirst($manager)); + return new $class($config); }); $di['requirements'] = function () use ($di) { $r = new Box_Requirements(); $r->setDi($di); + return $r; }; -$di['period'] = $di->protect(function($code) use($di){ return new \Box_Period($code); }); +$di['period'] = $di->protect(function ($code) { return new \Box_Period($code); }); -$di['theme'] = function() use($di) { +$di['theme'] = function () use ($di) { $service = $di['mod_service']('theme'); + return $service->getCurrentClientAreaTheme(); }; -$di['cart'] = function() use($di) { +$di['cart'] = function () use ($di) { $service = $di['mod_service']('cart'); + return $service->getSessionCart(); }; -$di['table'] = $di->protect(function ($name) use($di) { +$di['table'] = $di->protect(function ($name) use ($di) { $tools = new Box_Tools(); $tools->setDi($di); - $table = $tools->getTable($name); + $table = $tools->getTable($name); $table->setDi($di); + return $table; }); $di['license_server'] = function () use ($di) { $server = new \Box\Mod\Servicelicense\Server($di['logger']); $server->setDi($di); + return $server; }; -$di['solusvm'] = $di->protect(function ($config) use($di) { +$di['solusvm'] = $di->protect(function ($config) use ($di) { $solusVM = new \Box\Mod\Servicesolusvm\SolusVM(); $solusVM->setDi($di); $solusVM->setConfig($config); + return $solusVM; }); -$di['service_boxbilling'] = $di->protect(function ($config) use($di) { +$di['service_boxbilling'] = $di->protect(function ($config) { $service = new \Box\Mod\Serviceboxbillinglicense\ServiceBoxbilling($config); + return $service; }); -$di['ftp'] = $di->protect(function($params) use($di){ return new \Box_Ftp($params); }); +$di['ftp'] = $di->protect(function ($params) { return new \Box_Ftp($params); }); $di['pdf'] = function () use ($di) { - include BB_PATH_LIBRARY . '/PDF_ImageAlpha.php'; + include BB_PATH_LIBRARY.'/PDF_ImageAlpha.php'; + return new \PDF_ImageAlpha(); }; -$di['geoip'] = function () use ($di) { return new \GeoIp2\Database\Reader(BB_PATH_LIBRARY . '/GeoLite2-Country.mmdb'); }; +$di['geoip'] = function () { return new \GeoIp2\Database\Reader(BB_PATH_LIBRARY.'/GeoLite2-Country.mmdb'); }; -$di['password'] = function() use ($di) { return new Box_Password();}; -$di['translate'] = $di->protect(function($textDomain = '') use ($di) { +$di['password'] = function () { return new Box_Password(); }; +$di['translate'] = $di->protect(function ($textDomain = '') use ($di) { $tr = new Box_Translate(); - if (!empty($textDomain)){ + if (!empty($textDomain)) { $tr->setDomain($textDomain); } $cookieBBlang = $di['cookie']->get('BBLANG'); - $locale = !empty($cookieBBlang) ? $cookieBBlang : $di['config']['locale']; + $locale = !empty($cookieBBlang) ? $cookieBBlang : $di['config']['locale']; $tr->setDi($di); $tr->setLocale($locale); $tr->setup(); + return $tr; }); -$di['array_get'] = $di->protect(function (array $array, $key, $default = null) use ($di) { - return array_key_exists($key, $array) ? $array[$key] : $default; +$di['array_get'] = $di->protect(function (array $array, $key, $default = null) { + return array_key_exists($key, $array) ? $array[$key] : $default; }); + return $di; diff --git a/src/bb-ipn.php b/src/bb-ipn.php index 4e9399f22..362a0b25f 100644 --- a/src/bb-ipn.php +++ b/src/bb-ipn.php @@ -1,6 +1,6 @@ true, - 'bb_invoice_id' => $bb_invoice_id, - 'bb_gateway_id' => $bb_gateway_id, - 'get' => $_GET, - 'post' => $_POST, - 'server' => $_SERVER, - 'http_raw_post_data' => file_get_contents("php://input"), -); +$ipn = [ + 'skip_validation' => true, + 'bb_invoice_id' => $bb_invoice_id, + 'bb_gateway_id' => $bb_gateway_id, + 'get' => $_GET, + 'post' => $_POST, + 'server' => $_SERVER, + 'http_raw_post_data' => file_get_contents('php://input'), +]; try { $service = $di['mod_service']('invoice', 'transaction'); $output = $service->createAndProcess($ipn); - $res = array('result'=>$output, 'error'=>null); -} catch(Exception $e) { - $res = array('result'=>null, 'error'=> array('message' => $e->getMessage())); + $res = ['result' => $output, 'error' => null]; +} catch (Exception $e) { + $res = ['result' => null, 'error' => ['message' => $e->getMessage()]]; $output = false; } // redirect to invoice if gateways requires -if(isset($_GET['bb_redirect']) && isset($_GET['bb_invoice_hash'])) { +if (isset($_GET['bb_redirect']) && isset($_GET['bb_invoice_hash'])) { $url = $di['url']->link('invoice/'.$_GET['bb_invoice_hash']); header("Location: $url"); exit; @@ -58,6 +57,6 @@ if(isset($_GET['bb_redirect']) && isset($_GET['bb_invoice_hash'])) { header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json; charset=utf-8'); - print json_encode($res); + echo json_encode($res); exit; -} \ No newline at end of file +} diff --git a/src/bb-load.php b/src/bb-load.php index 60d9f68b5..feaa815b9 100644 --- a/src/bb-load.php +++ b/src/bb-load.php @@ -1,6 +1,6 @@ getCode() ."] occurred in [". $e->getFile() ."] at line [". $e->getLine() ."]: [". trim(strip_tags($e->getMessage())) ."]"; - } else { - if(APPLICATION_ENV == 'testing') { - print $e->getMessage() . PHP_EOL; - return ; - } - error_log($e->getMessage()); - - if(defined('BB_MODE_API')) { - $code = $e->getCode() ? $e->getCode() : 9998; - $result = array('result'=>NULL, 'error'=>array('message'=>$e->getMessage(), 'code'=>$code)); - print json_encode($result); - return false; - } - - if(defined('BB_DEBUG') && BB_DEBUG && file_exists(BB_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 \Whoops\Run; - $prettyPage = new \Whoops\Handler\PrettyPageHandler; - $prettyPage->setPageTitle("An error ocurred"); - $prettyPage->addDataTable('BoxBilling environment', [ - 'PHP Version' => phpversion(), - 'Error code' => $e->getCode() - ]); - $whoops->pushHandler($prettyPage); - $whoops->allowQuit(false); - $whoops->writeToOutput(false); - - print($whoops->handleException($e)); + if (isCLI) { + echo 'Error #['.$e->getCode().'] occurred in ['.$e->getFile().'] at line ['.$e->getLine().']: ['.trim(strip_tags($e->getMessage())).']'; } else { - $page = " + if (APPLICATION_ENV == 'testing') { + echo $e->getMessage().PHP_EOL; + + return; + } + error_log($e->getMessage()); + + if (defined('BB_MODE_API')) { + $code = $e->getCode() ? $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(BB_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 \Whoops\Run(); + $prettyPage = new \Whoops\Handler\PrettyPageHandler(); + $prettyPage->setPageTitle('An error ocurred'); + $prettyPage->addDataTable('BoxBilling environment', [ + 'PHP Version' => phpversion(), + 'Error code' => $e->getCode(), + ]); + $whoops->pushHandler($prettyPage); + $whoops->allowQuit(false); + $whoops->writeToOutput(false); + + echo $whoops->handleException($e); + } else { + $page = "
@@ -205,57 +207,56 @@ function handler_exception($e)%s
', $e->getMessage()); - - print sprintf('Powered by BoxBilling
%s
', $e->getMessage()); + + echo sprintf('Powered by BoxBilling