openstamanager/core.php

315 lines
11 KiB
PHP
Raw Normal View History

<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-06-28 15:52:56 +02:00
// Rimozione header X-Powered-By
2024-03-22 15:52:24 +01:00
// header_remove('X-Powered-By');
2018-06-28 15:52:56 +02:00
// Impostazioni di configurazione PHP
date_default_timezone_set('Europe/Rome');
// Controllo sulla versione PHP
2022-02-07 13:36:31 +01:00
$minimum = '7.2.0';
if (version_compare(phpversion(), $minimum) < 0) {
echo '
<p>Stai utilizzando la versione PHP '.phpversion().', non compatibile con OpenSTAManager.</p>
<p>Aggiorna PHP alla versione >= '.$minimum.'.</p>';
2024-01-15 15:30:45 +01:00
exit;
}
// Caricamento delle impostazioni personalizzabili
if (file_exists(__DIR__.'/config.inc.php')) {
include_once __DIR__.'/config.inc.php';
}
// Caricamento delle dipendenze e delle librerie del progetto
2018-09-25 16:47:44 +02:00
$loader = require_once __DIR__.'/vendor/autoload.php';
$namespaces = require_once __DIR__.'/config/namespaces.php';
foreach ($namespaces as $path => $namespace) {
2018-09-28 10:15:05 +02:00
$loader->addPsr4($namespace.'\\', __DIR__.'/'.$path.'/custom/src');
$loader->addPsr4($namespace.'\\', __DIR__.'/'.$path.'/src');
2018-09-25 16:47:44 +02:00
}
// Individuazione dei percorsi di base
App::definePaths(__DIR__);
$docroot = DOCROOT;
$rootdir = ROOTDIR;
$baseurl = BASEURL;
// Controllo che le intestazioni non siano già state inviate.
2024-03-22 15:52:24 +01:00
if (!headers_sent()) {
// Sicurezza della sessioni
ini_set('session.cookie_samesite', 'lax');
ini_set('session.use_trans_sid', '0');
ini_set('session.use_only_cookies', '1');
2020-10-16 17:58:31 +02:00
session_set_cookie_params(0, base_path(), null, isHTTPS(true));
session_start();
}
2020-10-16 17:58:31 +02:00
// Lettura della configurazione
2018-09-20 12:05:22 +02:00
$config = App::getConfig();
// Redirect al percorso HTTPS se impostato nella configurazione
2018-09-20 12:05:22 +02:00
if (!empty($config['redirectHTTPS']) && !isHTTPS(true)) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
2024-01-15 15:30:45 +01:00
exit;
}
2018-12-28 08:07:56 +01:00
/* GESTIONE DEGLI ERRORI */
// Logger per la segnalazione degli errori
2017-09-15 16:37:19 +02:00
$logger = new Monolog\Logger('Logs');
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushProcessor(new Monolog\Processor\WebProcessor());
2017-09-15 16:37:19 +02:00
// Registrazione globale del logger
Monolog\Registry::addLogger($logger, 'logs');
use Monolog\Handler\FilterHandler;
use Monolog\Handler\RotatingFileHandler;
2018-12-29 12:03:22 +01:00
use Monolog\Handler\StreamHandler;
$handlers = [];
2019-07-19 15:23:00 +02:00
if (!API\Response::isAPIRequest()) {
2019-01-11 12:02:11 +01:00
// File di log di base (logs/error.log, logs/setup.log)
$handlers[] = new StreamHandler(base_dir().'/logs/error.log', Monolog\Logger::ERROR);
$handlers[] = new StreamHandler(base_dir().'/logs/setup.log', Monolog\Logger::EMERGENCY);
2019-01-11 12:02:11 +01:00
// Messaggi grafici per l'utente
$handlers[] = new Extensions\MessageHandler(Monolog\Logger::ERROR);
2019-01-11 12:02:11 +01:00
// File di log ordinati in base alla data
if (App::debug()) {
$handlers[] = new RotatingFileHandler(base_dir().'/logs/error.log', 30, Monolog\Logger::ERROR);
$handlers[] = new RotatingFileHandler(base_dir().'/logs/setup.log', 30, Monolog\Logger::EMERGENCY);
2019-01-11 12:02:11 +01:00
}
2019-01-11 12:02:11 +01:00
// Inizializzazione Whoops
$whoops = new Whoops\Run();
2019-01-15 19:01:25 +01:00
if (App::debug()) {
2019-01-11 12:02:11 +01:00
$whoops->pushHandler(new Whoops\Handler\PrettyPageHandler());
}
2019-01-11 12:02:11 +01:00
// Abilita la gestione degli errori nel caso la richiesta sia di tipo AJAX
2019-01-15 19:01:25 +01:00
if (Whoops\Util\Misc::isAjaxRequest()) {
2019-01-11 12:02:11 +01:00
$whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
}
2019-01-11 12:02:11 +01:00
$whoops->register();
2019-02-15 10:42:39 +01:00
// Aggiunta di Monolog a Whoops
$whoops->pushHandler(function ($exception, $inspector, $run) use ($logger) {
$logger->addError($exception->getMessage(), [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString(),
]);
});
} else {
$handlers[] = new StreamHandler(base_dir().'/logs/api.log', Monolog\Logger::ERROR);
}
2019-01-11 12:02:11 +01:00
// Disabilita i messaggi nativi di PHP
ini_set('display_errors', 0);
// Ignora gli avvertimenti e le informazioni relative alla deprecazione di componenti
2019-11-12 16:48:06 +01:00
error_reporting(E_ALL & ~E_WARNING & ~E_CORE_WARNING & ~E_NOTICE & ~E_USER_DEPRECATED & ~E_STRICT);
2018-05-11 14:48:46 +02:00
2019-01-11 12:02:11 +01:00
$pattern = '[%datetime%] %channel%.%level_name%: %message% %context%'.PHP_EOL.'%extra% '.PHP_EOL;
2018-05-11 14:48:46 +02:00
$monologFormatter = new Monolog\Formatter\LineFormatter($pattern);
2019-01-11 12:02:11 +01:00
$monologFormatter->includeStacktraces(App::debug());
2018-05-11 14:48:46 +02:00
// Filtra gli errori per livello preciso del gestore dedicato
foreach ($handlers as $handler) {
2017-09-15 16:37:19 +02:00
$handler->setFormatter($monologFormatter);
$logger->pushHandler(new FilterHandler($handler, [$handler->getLevel()]));
}
// Imposta Monolog come gestore degli errori
2021-07-28 15:17:56 +02:00
$handler = new Monolog\ErrorHandler($logger);
if (!API\Response::isAPIRequest()) {
$handler->registerErrorHandler([]);
$handler->registerExceptionHandler(Monolog\Logger::ERROR);
}
$handler->registerFatalHandler(Monolog\Logger::ERROR);
// Database
2018-09-20 12:05:22 +02:00
$dbo = $database = database();
2018-12-28 08:07:56 +01:00
/* INTERNAZIONALIZZAZIONE */
// Istanziamento del gestore delle traduzioni del progetto
2020-09-23 11:47:59 +02:00
$lang = !empty($config['lang']) ? $config['lang'] : (isset($_GET['lang']) ? $_GET['lang'] : null);
2018-09-20 12:05:22 +02:00
$formatter = !empty($config['formatter']) ? $config['formatter'] : [];
$translator = trans();
$translator->addLocalePath(base_dir().'/locale');
$translator->addLocalePath(base_dir().'/modules/*/locale');
// Individuazione di versione e revisione del progetto
$version = Update::getVersion();
$revision = Update::getRevision();
2018-12-28 08:07:56 +01:00
/* ACCESSO E INSTALLAZIONE */
// Controllo sulla presenza dei permessi di accesso basilari
$continue = $dbo->isInstalled() && !Update::isUpdateAvailable() && (Auth::check() || API\Response::isAPIRequest()) && (empty($config['maintenance_ip']) || $config['maintenance_ip'] == $_SERVER['REMOTE_ADDR']);
2018-04-23 09:53:53 +02:00
if (!empty($skip_permissions)) {
Permissions::skip();
}
if (!$continue && getURLPath() != slashes(base_path().'/index.php') && !Permissions::getSkip()) {
if (Auth::check()) {
Auth::logout();
}
redirect(base_path().'/index.php');
2024-01-15 15:30:45 +01:00
exit;
}
2018-12-28 08:07:56 +01:00
/* INIZIALIZZAZIONE GENERALE */
// Operazione aggiuntive (richieste non API)
2019-07-19 15:23:00 +02:00
if (!API\Response::isAPIRequest()) {
2018-06-28 15:23:52 +02:00
// Impostazioni di Content-Type e Charset Header
header('Content-Type: text/html; charset=UTF-8');
// Controllo CSRF
2018-09-21 15:50:42 +02:00
if (empty($config['disableCSRF'])) {
csrfProtector::init();
}
// Aggiunta del wrapper personalizzato per la generazione degli input
2018-09-20 12:05:22 +02:00
if (!empty($config['HTMLWrapper'])) {
HTMLBuilder\HTMLBuilder::setWrapper($config['HTMLWrapper']);
}
// Aggiunta dei gestori personalizzati per la generazione degli input
2018-09-20 12:05:22 +02:00
foreach ((array) $config['HTMLHandlers'] as $key => $value) {
HTMLBuilder\HTMLBuilder::setHandler($key, $value);
}
// Aggiunta dei gestori per componenti personalizzate
2018-09-20 12:05:22 +02:00
foreach ((array) $config['HTMLManagers'] as $key => $value) {
HTMLBuilder\HTMLBuilder::setManager($key, $value);
}
// Registrazione globale del template per gli input HTML
register_shutdown_function('translateTemplate');
2018-06-28 15:23:52 +02:00
ob_start();
2018-07-19 12:47:28 +02:00
// Retrocompatibilità
2018-06-23 15:41:32 +02:00
$_SESSION['infos'] = isset($_SESSION['infos']) ? array_unique($_SESSION['infos']) : [];
$_SESSION['warnings'] = isset($_SESSION['warnings']) ? array_unique($_SESSION['warnings']) : [];
$_SESSION['errors'] = isset($_SESSION['errors']) ? array_unique($_SESSION['errors']) : [];
// Impostazione del tema grafico di default
2018-09-20 12:05:22 +02:00
$theme = !empty($config['theme']) ? $config['theme'] : 'default';
2024-01-31 14:23:46 +01:00
if ($continue) {
2018-05-11 14:48:46 +02:00
// Periodo di visualizzazione dei record
// Personalizzato
if (!empty($_GET['period_start'])) {
$_SESSION['period_start'] = $_GET['period_start'];
$_SESSION['period_end'] = $_GET['period_end'];
}
// Dal 01-01-yyy al 31-12-yyyy
elseif (!isset($_SESSION['period_start'])) {
$_SESSION['period_start'] = date('Y').'-01-01';
$_SESSION['period_end'] = date('Y').'-12-31';
}
2018-09-19 10:44:32 +02:00
$id_record = filter('id_record');
$id_parent = filter('id_parent');
2018-09-19 10:44:32 +02:00
Modules::setCurrent(filter('id_module'));
Plugins::setCurrent(filter('id_plugin'));
// Variabili fondamentali
$module = Modules::getCurrent();
$plugin = Plugins::getCurrent();
$structure = isset($plugin) ? $plugin : $module;
2024-04-10 17:43:54 +02:00
$id_module = $module ? $module->id : null;
$id_plugin = $plugin ? $plugin->id : null;
2018-09-19 10:44:32 +02:00
$user = Auth::user();
if (!empty($id_module)) {
2018-06-23 15:41:32 +02:00
// Segmenti
if (!isset($_SESSION['module_'.$id_module]['id_segment'])) {
2018-06-23 15:41:32 +02:00
$segments = Modules::getSegments($id_module);
$_SESSION['module_'.$id_module]['id_segment'] = isset($segments[0]['id']) ? $segments[0]['id'] : null;
2018-06-23 15:41:32 +02:00
}
Permissions::addModule($id_module);
}
Permissions::check();
}
2018-07-19 15:33:32 +02:00
// Retrocompatibilità
$post = Filter::getPOST();
$get = Filter::getGET();
}
2019-01-11 12:02:11 +01:00
// Inclusione dei file modutil.php
// TODO: sostituire * con lista module dir {aggiornamenti,anagrafiche,articoli}
// TODO: sostituire tutte le funzioni dei moduli con classi Eloquent relative
$files = glob(__DIR__.'/{modules,plugins}/*/modutil.php', GLOB_BRACE);
$custom_files = glob(__DIR__.'/{modules,plugins}/*/custom/modutil.php', GLOB_BRACE);
foreach ($custom_files as $key => $value) {
$index = array_search(str_replace('custom/', '', $value), $files);
if ($index !== false) {
unset($files[$index]);
}
}
$list = array_merge($files, $custom_files);
foreach ($list as $file) {
include_once $file;
}
2019-07-19 16:51:52 +02:00
// Inclusione dei file vendor/autoload.php di Composer
$files = glob(__DIR__.'/{modules,plugins}/*/vendor/autoload.php', GLOB_BRACE);
$custom_files = glob(__DIR__.'/{modules,plugins}/*/custom/vendor/autoload.php', GLOB_BRACE);
foreach ($custom_files as $key => $value) {
$index = array_search(str_replace('custom/', '', $value), $files);
if ($index !== false) {
unset($files[$index]);
}
}
$list = array_merge($files, $custom_files);
foreach ($list as $file) {
include_once $file;
2024-03-15 13:17:23 +01:00
}
// Inizializzazione traduzioni
2024-03-19 17:17:07 +01:00
if (database()->tableExists('zz_settings') && database()->tableExists('zz_langs')) {
2024-03-20 16:15:37 +01:00
$id_lang = setting('Lingua');
2024-03-22 15:52:24 +01:00
Models\Locale::setDefault($id_lang);
2024-03-27 16:43:30 +01:00
Models\Locale::setPredefined();
2024-03-27 16:59:46 +01:00
2024-03-22 15:52:24 +01:00
$lang = Models\Locale::find($id_lang)->language_code;
2024-03-27 15:38:39 +01:00
$translator->setLocale($lang, $formatter);
2024-03-15 13:17:23 +01:00
}