2023-02-08 02:27:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace EesyPHP;
|
|
|
|
|
|
|
|
class App {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-22 18:17:57 +01:00
|
|
|
protected static $options = array(
|
|
|
|
'init_time' => null,
|
|
|
|
);
|
2023-02-08 02:27:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Root directory path
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected static $root_directory_path = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization
|
|
|
|
* @param string|null $config_file Application configuration file path
|
|
|
|
* @param array|null $options Application options (default: null)
|
|
|
|
* @param string|null $root_directory_path Application root directory path (default: null)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function init($config_file, $options=null, $root_directory_path=null) {
|
2023-03-22 18:17:57 +01:00
|
|
|
Config::register_extra_variable('init_time', hrtime(true));
|
2023-02-08 02:27:15 +01:00
|
|
|
if (is_array($options)) self :: $options = $options;
|
|
|
|
|
|
|
|
if (is_null($root_directory_path)) {
|
|
|
|
$traces = debug_backtrace();
|
|
|
|
$root_directory_path = realpath(dirname($traces[0]['file']).'/../');
|
|
|
|
}
|
|
|
|
self :: $root_directory_path = $root_directory_path;
|
|
|
|
Config::register_extra_variable('root_directory_path', $root_directory_path);
|
|
|
|
|
|
|
|
$config_file = Config::replace_variables($config_file);
|
2023-07-26 16:51:16 +02:00
|
|
|
if ($config_file && !Config::load($config_file))
|
2023-02-08 02:27:15 +01:00
|
|
|
Log::fatal('Fail to load configuration file (%s)', $config_file);
|
|
|
|
|
2023-03-01 16:22:11 +01:00
|
|
|
// Set config default values
|
|
|
|
App :: set_defaults(
|
|
|
|
array(
|
|
|
|
'overwrite_config_files' => array(),
|
|
|
|
'upload_tmp_directory' => null,
|
|
|
|
'upload_max_filesize' => null,
|
|
|
|
'sentry.enabled' => true,
|
|
|
|
'log.enabled' => true,
|
|
|
|
'session.enabled' => true,
|
|
|
|
'templates.enabled' => true,
|
|
|
|
'url.enabled' => true,
|
|
|
|
'mail.enabled' => true,
|
|
|
|
'i18n.enabled' => true,
|
|
|
|
'cli.enabled' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
// Load overwrite configuration file
|
2023-03-01 16:22:11 +01:00
|
|
|
foreach (self :: get('overwrite_config_files', null, 'array') as $file) {
|
2023-02-08 02:27:15 +01:00
|
|
|
$file = Config::replace_variables($file);
|
|
|
|
if (is_file($file)) Config::load($file, true);
|
|
|
|
}
|
|
|
|
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('sentry.enabled', null, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
SentryIntegration :: init();
|
|
|
|
$sentry_transaction = new SentryTransaction();
|
|
|
|
$sentry_span = new SentrySpan('app.init', 'Application initialization');
|
|
|
|
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('log.enabled', null, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Log::init();
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('session.enabled', null, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Session::init();
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('templates.enabled', null, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Tpl :: init();
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('url.enabled', null, 'bool')) {
|
2023-02-08 02:27:15 +01:00
|
|
|
Url::init();
|
2023-02-28 19:07:58 +01:00
|
|
|
Url :: add_url_handler(null, array('EesyPHP\\App', 'handle_homepage'));
|
2023-02-13 00:42:20 +01:00
|
|
|
}
|
2023-02-25 05:02:27 +01:00
|
|
|
if (Auth :: enabled()) {
|
|
|
|
Auth :: init();
|
|
|
|
Url :: add_url_handler('#^logout$#', array('EesyPHP\\App', 'handle_logout'), null, false);
|
|
|
|
}
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('mail.enabled', null, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Email :: init();
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('i18n.enabled', null, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
I18n::init();
|
2023-03-01 16:22:11 +01:00
|
|
|
if (self :: get('cli.enabled', null, 'bool'))
|
2023-02-26 23:48:53 +01:00
|
|
|
Cli::init();
|
2023-03-02 10:53:22 +01:00
|
|
|
|
|
|
|
// Define common upload_tmp_dir & upload_max_filesize PHP ini
|
|
|
|
if (self::isset('upload_tmp_directory'))
|
|
|
|
Config :: ini_set('upload_tmp_dir', self::get('upload_tmp_directory', null, 'string'));
|
|
|
|
if (self::isset('upload_max_filesize')) {
|
|
|
|
Config :: ini_set('upload_max_filesize', self::get('upload_max_filesize', null, 'string'));
|
|
|
|
// post_max_size must be larger than upload_max_filesize
|
|
|
|
// See: https://www.php.net/manual/en/ini.core.php#ini.post-max-size
|
|
|
|
Config :: ini_set('post_max_size', strval(self::get('upload_max_filesize', null, 'int') * 1.1));
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
$sentry_span->finish();
|
|
|
|
}
|
|
|
|
|
2023-02-12 00:30:36 +01:00
|
|
|
/**
|
|
|
|
* Check if the application is initialized
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function initialized() {
|
|
|
|
return !is_null(self :: $root_directory_path);
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:15:30 +01:00
|
|
|
/**
|
|
|
|
* Check if a specific configuration variable is set
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
* @return bool
|
|
|
|
**/
|
2023-02-27 17:44:19 +01:00
|
|
|
public static function isset($key) {
|
2023-02-27 16:15:30 +01:00
|
|
|
return Config::isset($key, self :: $options) || (Config::loaded() && Config::isset($key));
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
/**
|
|
|
|
* Get a specific option value
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
* @param mixed $default The default value to return if configuration variable
|
|
|
|
* is not set (Default : null)
|
|
|
|
* @param string $cast The type of expected value. The configuration variable
|
|
|
|
* value will be cast as this type. Could be : bool, int,
|
|
|
|
* float or string. (Optional, default : raw value)
|
|
|
|
* @param bool $split If true, $cast is 'array' and value retreived from configuration
|
|
|
|
* is a string, split the value by comma (optional, default: true)
|
|
|
|
* @return mixed The configuration variable value
|
|
|
|
**/
|
2023-02-12 00:30:36 +01:00
|
|
|
public static function get($key, $default=null, $cast=null, $split=true) {
|
2023-02-27 17:52:51 +01:00
|
|
|
$default = self :: get_default($key, $default, $cast, $split);
|
2023-02-08 02:27:15 +01:00
|
|
|
return Config::get(
|
|
|
|
$key,
|
|
|
|
Config::loaded()?Config::get($key, $default, $cast, $split):$default,
|
|
|
|
$cast,
|
|
|
|
$split,
|
2023-03-03 10:36:08 +01:00
|
|
|
self :: $options,
|
|
|
|
array(self :: class, 'get')
|
2023-02-08 02:27:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:52:51 +01:00
|
|
|
/**
|
|
|
|
* Get a specific option default value
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
* @param mixed $default Default value provided by context
|
|
|
|
* (optional, priority if not nul, default : null)
|
|
|
|
* @param string $cast The type of expected value. The configuration variable
|
|
|
|
* default value will be cast as this type. Could be : bool, int,
|
|
|
|
* float or string. (Optional, default : raw value)
|
|
|
|
* @param bool $split If true, $cast is 'array' and value retreived from configuration
|
|
|
|
* is a string, split the value by comma (optional, default: true)
|
|
|
|
* @return mixed The configuration variable default value
|
|
|
|
**/
|
|
|
|
public static function get_default($key, $default=null, $cast=null, $split=true) {
|
|
|
|
if (!is_null($default))
|
|
|
|
return $default;
|
|
|
|
return Config::get(
|
|
|
|
"default.$key",
|
|
|
|
$default,
|
|
|
|
$cast,
|
|
|
|
$split,
|
|
|
|
self :: $options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:23:48 +01:00
|
|
|
/**
|
|
|
|
* Get list of keys of a specific option
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
*
|
|
|
|
* @return array An array of the keys of a specific option
|
|
|
|
**/
|
|
|
|
public static function keys($key) {
|
|
|
|
$value = self :: get($key);
|
|
|
|
return (is_array($value)?array_keys($value):array());
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:15:30 +01:00
|
|
|
/**
|
|
|
|
* Set a specific option value
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
* @param mixed $value The configuration variable value
|
|
|
|
* @return boolean
|
|
|
|
**/
|
|
|
|
public static function set($key, $value) {
|
|
|
|
if (Config::isset($key, self :: $options))
|
|
|
|
return Config::set($key, $value, self :: $options);
|
|
|
|
return Config::set($key, $value);
|
|
|
|
}
|
|
|
|
|
2023-03-01 16:22:11 +01:00
|
|
|
/**
|
|
|
|
* Set a specific option default value
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
* @param mixed $value The configuration variable default value
|
|
|
|
* @return boolean
|
|
|
|
**/
|
|
|
|
public static function set_default($key, $value) {
|
|
|
|
return Config::set("default.$key", $value, self :: $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a specific options default value
|
|
|
|
*
|
|
|
|
* @param array<string,mixed> $values Associative array of configuration variables name and
|
|
|
|
* default values
|
|
|
|
* @return boolean
|
|
|
|
**/
|
|
|
|
public static function set_defaults($values) {
|
|
|
|
$error = false;
|
|
|
|
foreach($values as $key => $value)
|
|
|
|
if (!self :: set_default($key, $value))
|
|
|
|
$error = true;
|
|
|
|
return !$error;
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
/**
|
|
|
|
* Retreive application root directory path
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function root_directory_path() {
|
|
|
|
return self :: $root_directory_path?self :: $root_directory_path:'.';
|
|
|
|
}
|
|
|
|
|
2023-02-13 00:42:20 +01:00
|
|
|
/**
|
|
|
|
* Default homepage handler
|
|
|
|
* @param UrlRequest $request
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function handle_homepage($request) {
|
|
|
|
if (Tpl::initialized())
|
2023-02-14 01:21:52 +01:00
|
|
|
Tpl :: display("homepage.tpl", I18n::_("Hello world !"));
|
2023-02-13 00:42:20 +01:00
|
|
|
else
|
2023-02-14 01:21:52 +01:00
|
|
|
echo "<h1>".I18n::_("Hello world!")."</h1>";
|
2023-02-13 00:42:20 +01:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:02:27 +01:00
|
|
|
/**
|
|
|
|
* Default logout handler
|
|
|
|
* @param UrlRequest $request
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function handle_logout($request) {
|
|
|
|
Auth::logout();
|
|
|
|
if (Tpl::initialized())
|
|
|
|
Tpl :: display("logout.tpl", I18n::_("Disconnected"));
|
|
|
|
else
|
|
|
|
echo "<h1>".I18n::_("You are now disconnected.")."</h1>";
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
}
|