-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
195 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
|
||
namespace BDSCore\Application; | ||
|
||
/** | ||
* Class App | ||
* @package BDSCore\Application | ||
*/ | ||
class App | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $globalConfig = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $securityConfig = []; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $debugClass; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $routerClass; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $securityClass; | ||
|
||
/** | ||
* App constructor. | ||
* @param array $configs | ||
* @param array $classes | ||
*/ | ||
public function __construct(array $configs, array $classes) { | ||
$this->globalConfig = $configs['globalConfig']; | ||
$this->securityConfig = $configs['securityConfig']; | ||
|
||
$this->debugClass = $classes['debugClass']; | ||
$this->securityClass = $classes['securityClass']; | ||
$this->routerClass = $classes['routerClass']; | ||
} | ||
|
||
/** | ||
* @param $e | ||
*/ | ||
public function catchException($e) { | ||
try { | ||
$permsCode = substr(sprintf('%o', fileperms('cache/')), -4); | ||
if ($permsCode != '0777') { | ||
die("The access rights to the 'cache/' folder must be granted to the framework.<br />Current access rights: {$permsCode}<br />Example: sudo chmod -R 0777 cache/"); | ||
} | ||
|
||
$phpMajorVersion = PHP_MAJOR_VERSION; | ||
if ($phpMajorVersion < 7) { | ||
$phpVersion = $phpMajorVersion . '.' . PHP_MINOR_VERSION; | ||
die("To work properly, BDS Framework needs at least PHP version 7.0.<br />Current version of PHP: {$phpVersion}"); | ||
} | ||
|
||
require_once('vendor/autoload.php'); | ||
|
||
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); | ||
|
||
if ($this->globalConfig['errorLogger']) { | ||
$logger = new \Monolog\Logger('BDS_Framework'); | ||
$logger->pushHandler(new \Monolog\Handler\StreamHandler('storage/logs/frameworkLogs.log', \Monolog\Logger::WARNING)); | ||
|
||
$logger->warning($e); | ||
} | ||
|
||
$template = new \BDSCore\Twig\Template(); | ||
if ($this->globalConfig['showExceptions']) { | ||
$template->render('errors/error500.twig', [ | ||
'className' => get_class($e), | ||
'exception' => $e->getMessage() | ||
]); | ||
} else { | ||
$template->render('errors/error500.twig', ['exception' => false]); | ||
} | ||
|
||
exit(); | ||
} catch (Exception $ex) { | ||
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); | ||
die('-[ Error 500 -]'); | ||
} | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function startSession(): int { | ||
ini_set('session.cookie_lifetime', $this->securityConfig['sessionLifetime']); | ||
session_name('BDS_SESSION'); | ||
session_start(); | ||
|
||
return session_status(); | ||
} | ||
|
||
public function checkPermissions() { | ||
if ($this->securityConfig['checkPermissions']) { | ||
$this->securityClass->checkPermissions(); | ||
} | ||
} | ||
|
||
/** | ||
* @param $item | ||
* @return bool | ||
*/ | ||
public function debug($item): bool { | ||
\BDSCore\Debug\debugBar::pushElement('Debug#' . substr(uniqid(), 8), $item); | ||
|
||
return $this->debugClass->debug($item); | ||
} | ||
|
||
public function pushToDebugBar() { | ||
\BDSCore\Debug\debugBar::pushElement('DebugInFile', ($this->globalConfig['debugFile']) ? 'true' : 'false'); | ||
\BDSCore\Debug\debugBar::pushElement('Locale', $this->globalConfig['locale']); | ||
\BDSCore\Debug\debugBar::pushElement('Timezone', $this->globalConfig['timezone']); | ||
} | ||
|
||
/** | ||
* @param $timeStart | ||
*/ | ||
public function checkAuth($timeStart) { | ||
(!isset($_SESSION['auth'])) ? $_SESSION['auth'] = false : null; | ||
if ($this->securityConfig['authRequired']) { | ||
if ($_SESSION['auth'] !== true) { | ||
$router->activateLogin($this->securityConfig['authPage']); | ||
if ($this->globalConfig['debugBar']) { | ||
$timeStop = microtime(true); | ||
setcookie('BDS_loadingTime', '~' . round(($timeStop - $timeStart), 3) * 1000 . 'ms', time() + 15); | ||
} | ||
if ($_SERVER['REQUEST_URI'] !== '/login') { | ||
header('Location: login'); | ||
exit(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $timeStart | ||
*/ | ||
public function insertTimeToDebugBar($timeStart) { | ||
if ($this->globalConfig['debugBar']) { | ||
$timeStop = microtime(true); | ||
setcookie('BDS_loadingTime', '~' . round(($timeStop - $timeStart), 3) * 1000 . 'ms', time() + 15); | ||
} | ||
} | ||
|
||
/** | ||
* @param $timeStart | ||
*/ | ||
public function run($timeStart) { | ||
(isset($_GET['errorCode'])) ? \BDSCore\Errors\Errors::returnError($_GET['errorCode']) : null; | ||
$this->startSession(); | ||
$this->checkPermissions(); | ||
$this->checkAuth($timeStart); | ||
$this->pushToDebugBar(); | ||
$this->routerClass->run(); | ||
$this->insertTimeToDebugBar($timeStart); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,27 @@ | ||
<?php | ||
|
||
error_reporting(E_ALL ^ E_WARNING); | ||
$timeStart = microtime(true); | ||
|
||
/** | ||
* @param $e | ||
*/ | ||
function catchException($e) { | ||
try { | ||
$permsCode = substr(sprintf('%o', fileperms('cache/')), -4); | ||
if ($permsCode != '0777') { | ||
die("The access rights to the 'cache/' folder must be granted to the framework.<br />Current access rights: {$permsCode}<br />Example: sudo chmod -R 0777 cache/"); | ||
} | ||
|
||
$phpMajorVersion = PHP_MAJOR_VERSION; | ||
if($phpMajorVersion < 7) { | ||
$phpVersion = $phpMajorVersion . '.' . PHP_MINOR_VERSION; | ||
die("To work properly, BDS Framework needs at least PHP version 7.0.<br />Current version of PHP: {$phpVersion}"); | ||
} | ||
|
||
require_once('vendor/autoload.php'); | ||
|
||
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); | ||
|
||
if (\BDSCore\Config\Config::getConfig('errorLogger')) { | ||
$logger = new \Monolog\Logger('BDS_Framework'); | ||
$logger->pushHandler(new \Monolog\Handler\StreamHandler('storage/logs/frameworkLogs.log', \Monolog\Logger::WARNING)); | ||
|
||
$logger->warning($e); | ||
} | ||
|
||
$template = new \BDSCore\Twig\Template(); | ||
if (\BDSCore\Config\Config::getConfig('showExceptions')) { | ||
$template->render('errors/error500.twig', [ | ||
'className' => get_class($e), | ||
'exception' => $e->getMessage() | ||
]); | ||
} else { | ||
$template->render('errors/error500.twig', ['exception' => false]); | ||
} | ||
|
||
exit(); | ||
} catch (Exception $ex) { | ||
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); | ||
die('-[ Error 500 -]'); | ||
} | ||
} | ||
|
||
set_exception_handler('catchException'); | ||
$timeStart = microtime(true); | ||
|
||
require('vendor/autoload.php'); | ||
|
||
$securityConfig = \BDSCore\Config\Config::getAllSecurityConfig(); | ||
|
||
ini_set('session.cookie_lifetime', $securityConfig['sessionLifetime']); | ||
session_name('BDS_SESSION'); | ||
session_start(); | ||
$_SESSION['config'] = include('config/config.php'); | ||
|
||
if (isset($_GET['errorCode'])) { | ||
\BDSCore\Errors\Errors::returnError($_GET['errorCode']); | ||
} | ||
|
||
/** | ||
* @param $item | ||
* @return bool | ||
*/ | ||
function debug($item): bool { | ||
$debugClass = new \BDSCore\Debug\Debugger(); | ||
\BDSCore\Debug\debugBar::pushElement('Debug#' . substr(uniqid(), 8), $item); | ||
|
||
return $debugClass->debug($item); | ||
} | ||
|
||
if ($securityConfig['checkPermissions']) { | ||
$security = new \BDSCore\Security\Security($securityConfig['ipBan']); | ||
$security->checkPermissions(); | ||
$app = new \BDSCore\Application\App( | ||
[ | ||
'globalConfig' => \BDSCore\Config\Config::getAllConfig(), | ||
'securityConfig' => \BDSCore\Config\Config::getAllSecurityConfig() | ||
], | ||
[ | ||
'debugClass' => new \BDSCore\Debug\Debugger(), | ||
'securityClass' => new \BDSCore\Security\Security(), | ||
'routerClass' => new BDSCore\Router\Router() | ||
] | ||
); | ||
|
||
set_exception_handler([$app, 'catchException']); | ||
|
||
function debug($item) { | ||
$app->debug($item); | ||
} | ||
|
||
$config = \BDSCore\Config\Config::getAllConfig(); | ||
\BDSCore\Debug\debugBar::pushElement('DebugInFile', ($config['debugFile']) ? 'true' : 'false'); | ||
\BDSCore\Debug\debugBar::pushElement('Locale', $config['locale']); | ||
\BDSCore\Debug\debugBar::pushElement('Timezone', $config['timezone']); | ||
|
||
$router = new BDSCore\Router\Router(); | ||
|
||
(!isset($_SESSION['auth'])) ? $_SESSION['auth'] = false : null; | ||
if ($securityConfig['authRequired']) { | ||
if ($_SESSION['auth'] !== true) { | ||
$router->activateLogin($securityConfig['authPage']); | ||
if ($config['debugBar']) { | ||
$timeStop = microtime(true); | ||
setcookie('BDS_loadingTime', '~' . round(($timeStop - $timeStart), 3) * 1000 . 'ms', time() + 15); | ||
} | ||
if ($_SERVER['REQUEST_URI'] !== '/login') { | ||
header('Location: login'); | ||
exit(); | ||
} | ||
} | ||
} | ||
|
||
$router->run(); | ||
|
||
if ($config['debugBar']) { | ||
$timeStop = microtime(true); | ||
setcookie('BDS_loadingTime', '~' . round(($timeStop - $timeStart), 3) * 1000 . 'ms', time() + 15); | ||
} | ||
$app->run($timeStart); |