Skip to content

Commit

Permalink
Optimisation du fichier index.php
Browse files Browse the repository at this point in the history
  • Loading branch information
fkeloks committed Aug 7, 2017
1 parent 9e369ab commit c6639a2
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 104 deletions.
171 changes: 171 additions & 0 deletions core/Application/App.php
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);
}

}
11 changes: 6 additions & 5 deletions core/Security/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
class Security
{


/**
* @var array
*/
private $ipBan = [];

public function __construct(array $ipBan = []) {
$this->ipBan = $ipBan;
public function __construct() {
$this->ipBan = \BDSCore\Config\Config::getSecurityConfig('ipBan');
}

/**
* @return bool
*/
public function checkIp() {
public function checkIp(): bool {
if (in_array($_SERVER['REMOTE_ADDR'], $this->ipBan)) {
return true;
}
Expand All @@ -41,9 +42,9 @@ public function allowIp() {
}

/**
* @param $errorCode
* @param int $errorCode
*/
private function returnError($errorCode) {
private function returnError(int $errorCode) {
try {
\BDSCore\Errors::returnError($errorCode);
} catch (\Exception $e) {
Expand Down
117 changes: 18 additions & 99 deletions index.php
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);

0 comments on commit c6639a2

Please sign in to comment.