PHP_SMART_HOME_V3/app/Bootstrap.php

120 lines
3.5 KiB
PHP
Raw Normal View History

2020-05-16 15:18:27 +00:00
<?php
//Debug
error_reporting(E_ALL);
ini_set( 'display_errors','1');
//setup
parse_str($_SERVER['QUERY_STRING'], $params);
2020-12-09 13:30:41 +00:00
if (defined ("BASEDIR")) {
$urlSes = BASEDIR;
} else {
$urlSes = str_replace((!empty ($params['url']) ? $params['url'] : ""), "", str_replace('https://' . $_SERVER['HTTP_HOST'], "", $_SERVER['REQUEST_URI']));
}
if (defined ("BASEDIR") && defined ("BASEURL")) {
$domain = str_replace("http://", "", str_replace("https://", "", str_replace(BASEDIR, "", BASEURL)));
} else {
$domain = str_replace("/var/www/", "", $_SERVER['DOCUMENT_ROOT']);
}
2020-10-26 08:44:59 +00:00
session_set_cookie_params(
1209600,
$urlSes,
2020-12-09 13:30:41 +00:00
$domain,
2020-10-26 08:44:59 +00:00
true,
true
);
/*ini_set ('session.cookie_httponly', '1');
ini_set('session.cookie_domain', $_SERVER['HTTP_HOST']);
2020-10-26 08:44:59 +00:00
ini_set('session.cookie_path', str_replace('login', "", str_replace('https://' . $_SERVER['HTTP_HOST'], "", $_SERVER['REQUEST_URI'])));
ini_set('session.cookie_secure', '1');
2020-10-26 08:44:59 +00:00
ini_set('session.gc_maxlifetime', 1209600);*/
mb_internal_encoding ("UTF-8");
2020-05-16 23:26:49 +00:00
session_start();
2020-05-16 15:18:27 +00:00
2020-05-22 20:15:49 +00:00
// import configs
require_once '../library/Debugger.php';
Debugger::flag('loaders');
2020-05-16 15:18:27 +00:00
//Autoloader
class Autoloader {
protected static $extension = ".php";
protected static $root = __DIR__;
protected static $files = [];
static function ClassLoader ($className = ""){
$directorys = new RecursiveDirectoryIterator(static::$root, RecursiveDirectoryIterator::SKIP_DOTS);
//echo '<pre>';
//var_dump($directorys);
//echo '</pre>';
$files = new RecursiveIteratorIterator($directorys, RecursiveIteratorIterator::LEAVES_ONLY);
$filename = $className . static::$extension;
foreach ($files as $key => $file) {
if (strtolower($file->getFilename()) === strtolower($filename) && $file->isReadable()) {
include_once $file->getPathname();
return;
}
}
}
static function setRoot($rootPath){
static::$root = $rootPath;
}
}
spl_autoload_register("Autoloader::ClassLoader");
Autoloader::setRoot('/var/www/dev.steelants.cz/vasek/home-update/');
2020-07-28 07:02:46 +00:00
// import configs
require_once '../config/config.php';
2020-05-16 15:18:27 +00:00
class ErrorHandler {
static function exception($exception){
error_log($exception);
http_response_code($exception->getCode());
$message = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
2020-07-27 14:57:01 +00:00
'file' => $exception->getFile(),
'line' => $exception->getLine(),
2020-07-29 06:44:11 +00:00
'trace' => $exception->getTrace(),
2020-05-16 15:18:27 +00:00
];
echo json_encode($message);
2020-05-20 07:07:27 +00:00
$apiLogManager = new LogManager('../logs/apache/'. date("Y-m-d").'.log');
2020-07-28 07:02:46 +00:00
$apiLogManager->setLevel(LOGLEVEL);
2020-07-28 09:29:36 +00:00
$apiLogManager->write("[APACHE]\n" . json_encode($message, JSON_PRETTY_PRINT), LogRecordTypes::ERROR);
2020-05-16 15:18:27 +00:00
}
}
set_exception_handler("ErrorHandler::exception");
2020-05-22 20:15:49 +00:00
Debugger::flag('preload');
2020-05-16 15:18:27 +00:00
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
2020-05-20 07:07:27 +00:00
$apiLogManager = new LogManager('../logs/api/'. date("Y-m-d").'.log');
2020-07-28 07:02:46 +00:00
$apiLogManager->setLevel(LOGLEVEL);
2020-05-16 15:18:27 +00:00
2020-07-28 07:02:46 +00:00
$apiLogManager->write("[API] headers\n" . json_encode($_SERVER, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
2020-05-16 15:18:27 +00:00
2020-05-22 20:15:49 +00:00
Debugger::flag('dbconnect');
2021-03-01 15:02:21 +00:00
//DB Conector
2020-05-16 15:18:27 +00:00
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
2020-05-22 20:15:49 +00:00
Debugger::flag('routes');
2020-05-16 15:18:27 +00:00
// import routes
require_once '../app/Routes.php';
2020-05-22 20:15:49 +00:00
Debugger::flag('done');
// echo Debugger::showFlags(false);