Compare commits
5 commits
d422afa66e
...
5496ff55c0
Author | SHA1 | Date | |
---|---|---|---|
5496ff55c0 | |||
0eb2c543f1 | |||
93c38689db | |||
d30abeb008 | |||
1fff8f0af4 |
20 changed files with 641 additions and 690 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
use EesyPHP\Check;
|
||||
use EesyPHP\Cli;
|
||||
use EesyPHP\Date;
|
||||
use EesyPHP\I18n;
|
||||
use EesyPHP\Log;
|
||||
|
||||
|
@ -22,7 +23,7 @@ function print_item_info($item) {
|
|||
printf(_("Item #%s:\n"), $item['id']);
|
||||
printf("\t"._("ID: %s")."\n", $item['id']);
|
||||
printf("\t"._("Name: '%s'")."\n", $item['name']);
|
||||
printf("\t"._("Date: %s")."\n", format_time($item['date']));
|
||||
printf("\t"._("Date: %s")."\n", Date :: format($item['date']));
|
||||
printf(
|
||||
"\t"._("Description: %s")."\n",
|
||||
($item['description']?"'".$item['description']."'":_("Not set"))
|
||||
|
@ -99,7 +100,7 @@ function cli_list($command_args) {
|
|||
array(
|
||||
$info['id'],
|
||||
$info['name'],
|
||||
format_time($info['date']),
|
||||
Date :: format($info['date']),
|
||||
$info['status'],
|
||||
($info['description']?$info['description']:''),
|
||||
)
|
||||
|
@ -240,7 +241,7 @@ function cli_cron($command_args) {
|
|||
Log :: debug("cli_cron(): item max age = $item_max_age day(s)");
|
||||
|
||||
$limit = time() - ($item_max_age * 86400);
|
||||
Log :: debug("Handle items expiration with creation date limit ".format_time($limit).".");
|
||||
Log :: debug("Handle items expiration with creation date limit ".Date :: format($limit).".");
|
||||
|
||||
$items = search_items(array('all' => true));
|
||||
$error = false;
|
||||
|
@ -248,24 +249,24 @@ function cli_cron($command_args) {
|
|||
if ($item['date'] < $limit) {
|
||||
if ($just_try) {
|
||||
Log :: debug('Just-try mode: do not really delete item #%s (%s, creation date: %s)',
|
||||
$item['id'], $item['name'], format_time($item['date'])
|
||||
$item['id'], $item['name'], Date :: format($item['date'])
|
||||
);
|
||||
}
|
||||
else if (delete_item($item['id'])) {
|
||||
Log :: info('Item #%s (%s) deleted (creation date: %s)',
|
||||
$item['id'], $item['name'], format_time($item['date'])
|
||||
$item['id'], $item['name'], Date :: format($item['date'])
|
||||
);
|
||||
}
|
||||
else {
|
||||
Log :: error('Fail to delete item "%s" (%s, creation date: %s)',
|
||||
$item['id'], $item['name'], format_time($item['date'])
|
||||
$item['id'], $item['name'], Date :: format($item['date'])
|
||||
);
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Log :: debug('Item "%s" (%s) still valid (creation date: %s)',
|
||||
$item['id'], $item['name'], format_time($item['date'])
|
||||
$item['id'], $item['name'], Date :: format($item['date'])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ $status_list = array (
|
|||
);
|
||||
|
||||
require_once('cli.php');
|
||||
require_once('smarty.php');
|
||||
require_once('templates.php');
|
||||
Url::init(isset($public_root_url)?$public_root_url:null);
|
||||
require_once('url-helpers.php');
|
||||
require_once('db.php');
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
use EesyPHP\Check;
|
||||
use EesyPHP\Log;
|
||||
use EesyPHP\Session;
|
||||
use EesyPHP\Tpl;
|
||||
use EesyPHP\Url;
|
||||
|
||||
use function EesyPHP\vardump;
|
||||
|
||||
/*
|
||||
* Check values helpers
|
||||
|
@ -86,47 +90,6 @@ function handle_item_post_data(&$info, $enabled_fields=null, $required_fields=nu
|
|||
/*
|
||||
* Parser/formater values helpers
|
||||
*/
|
||||
$_date_format = "%d/%m/%Y";
|
||||
$_date_time_format = "%d/%m/%Y %H:%M:%S";
|
||||
function format_time($time, $with_time=true) {
|
||||
global $_date_format, $_date_time_format;
|
||||
if ($with_time)
|
||||
return strftime($_date_time_format, $time);
|
||||
return strftime($_date_format, $time);
|
||||
}
|
||||
|
||||
function parse_date($date, $with_time=true) {
|
||||
global $_date_format, $_date_time_format;
|
||||
if ($with_time)
|
||||
$ptime = strptime($date, $_date_time_format);
|
||||
else
|
||||
$ptime = strptime($date, $_date_format);
|
||||
if(is_array($ptime)) {
|
||||
return mktime(
|
||||
$ptime['tm_hour'],
|
||||
$ptime['tm_min'],
|
||||
$ptime['tm_sec'],
|
||||
$ptime['tm_mon']+1,
|
||||
$ptime['tm_mday'],
|
||||
$ptime['tm_year']+1900
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function format_size($size, $digit=False) {
|
||||
if (!$digit && $digit!==0) $digit=2;
|
||||
if ($size>=1099511627776)
|
||||
return number_format($size/1099511627776,$digit)."To";
|
||||
elseif ($size>=1073741824)
|
||||
return number_format($size/1073741824,$digit)."Go";
|
||||
else if ($size>=1048576)
|
||||
return number_format($size/1048576,$digit)."Mo";
|
||||
else if ($size>=1024)
|
||||
return number_format($size/1024,$digit)."Ko";
|
||||
else
|
||||
return $size."o";
|
||||
}
|
||||
|
||||
function can_modify($item) {
|
||||
return can_do(
|
||||
|
@ -153,172 +116,4 @@ function can_do($item, $status=array()) {
|
|||
return in_array($item['status'], $status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic Data/value helpers
|
||||
*/
|
||||
function vardump($data) {
|
||||
ob_start();
|
||||
var_dump($data);
|
||||
$data = ob_get_contents();
|
||||
ob_end_clean();
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a callable object for logging
|
||||
* @param string|array $callable The callable object
|
||||
* @return string The callable object string representation
|
||||
*/
|
||||
function format_callable($callable) {
|
||||
if (is_string($callable))
|
||||
return $callable."()";
|
||||
if (is_array($callable))
|
||||
if (is_string($callable[0]))
|
||||
return $callable[0]."::".$callable[1]."()";
|
||||
elseif (is_object($callable[0]))
|
||||
return get_class($callable[0])."->".$callable[1]."()";
|
||||
else
|
||||
return "Unkown->".$callable[1]."()";
|
||||
// @phpstan-ignore-next-line
|
||||
return vardump($callable);
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic file/directory helpers
|
||||
*/
|
||||
function dump_file($file_path, $max_age=3600) {
|
||||
if (is_file($file_path)) {
|
||||
header('Content-Type: '.mime_content_type($file_path));
|
||||
$last_modified_time = filemtime($file_path);
|
||||
$etag = md5_file($file_path);
|
||||
header("Cache-Control: max-age=$max_age, must-revalidate");
|
||||
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
|
||||
header("Etag: $etag");
|
||||
|
||||
if (
|
||||
(
|
||||
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
|
||||
@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
|
||||
) || (
|
||||
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
||||
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag
|
||||
)
|
||||
) {
|
||||
header("HTTP/1.1 304 Not Modified");
|
||||
exit();
|
||||
}
|
||||
|
||||
header('Pragma: public');
|
||||
header('Content-Length: ' . filesize($file_path));
|
||||
readfile($file_path);
|
||||
exit();
|
||||
}
|
||||
header("HTTP/1.1 404 Not found");
|
||||
exit();
|
||||
}
|
||||
|
||||
function delete_directory($dir, $recursive=true) {
|
||||
$files = array_diff(scandir($dir), array('.','..'));
|
||||
if ($recursive) {
|
||||
foreach ($files as $file) {
|
||||
if (is_dir("$dir/$file")) {
|
||||
if (!delete_directory("$dir/$file", true)) {
|
||||
Log :: error("delete_directory($dir) : Fail to delete sub-directory '$dir/$file'.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!unlink("$dir/$file")) {
|
||||
Log :: error("delete_directory($dir) : Fail to delete '$dir/$file'.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!empty($files)) {
|
||||
Log :: error("delete_directory($dir) : Directory is not empty.");
|
||||
return false;
|
||||
}
|
||||
return rmdir($dir);
|
||||
}
|
||||
|
||||
/*
|
||||
* Run external command helper
|
||||
*/
|
||||
/**
|
||||
* Run external command
|
||||
*
|
||||
* @param $command string|array The command. It's could be an array of the command with its
|
||||
* arguments.
|
||||
* @param $data_stdin string|null The command arguments (optional, default: null)
|
||||
* @param $escape_command_args boolean If true, the command will be escaped
|
||||
* (optional, default: true)
|
||||
*
|
||||
* @return false|array An array of return code, stdout and stderr result or False in case of fatal
|
||||
* error
|
||||
**/
|
||||
function run_external_command($command, $data_stdin=null, $escape_command_args=true) {
|
||||
if (is_array($command))
|
||||
$command = implode(' ', $command);
|
||||
if ($escape_command_args)
|
||||
$command = escapeshellcmd($command);
|
||||
Log :: debug("Run external command: '$command'");
|
||||
$descriptorspec = array(
|
||||
0 => array("pipe", "r"), // stdin
|
||||
1 => array("pipe", "w"), // stdout
|
||||
2 => array("pipe", "w"), // stderr
|
||||
);
|
||||
$process = proc_open($command, $descriptorspec, $pipes);
|
||||
|
||||
if (!is_resource($process)) {
|
||||
Log :: error("Fail to run external command: '$command'");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_null($data_stdin)) {
|
||||
fwrite($pipes[0], $data_stdin);
|
||||
}
|
||||
fclose($pipes[0]);
|
||||
|
||||
$stdout = stream_get_contents($pipes[1]);
|
||||
fclose($pipes[1]);
|
||||
|
||||
$stderr = stream_get_contents($pipes[2]);
|
||||
fclose($pipes[2]);
|
||||
|
||||
$return_value = proc_close($process);
|
||||
|
||||
$error = (!empty($stderr) || $return_value != 0);
|
||||
Log :: log(
|
||||
($error?'ERROR':'DEBUG'),
|
||||
"External command ".($error?"error":"result").":\n".
|
||||
"\tCommand : $command\n".
|
||||
"\tReturn code: $return_value\n".
|
||||
"\tOutput:\n".
|
||||
"\t\t- Stdout :\n$stdout\n\n".
|
||||
"\t\t- Stderr :\n$stderr"
|
||||
);
|
||||
|
||||
return array($return_value, $stdout, $stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check an AJAX request and trigger a fatal error on fail
|
||||
*
|
||||
* Check if session key is present and valid and set AJAX
|
||||
* mode.
|
||||
*
|
||||
* @param string|null $session_key string The current session key (optional)
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function check_ajax_request($session_key=null) {
|
||||
global $ajax, $debug_ajax;
|
||||
$ajax = true;
|
||||
|
||||
if (Session :: check_key($session_key))
|
||||
fatal_error('Invalid request');
|
||||
|
||||
if ($debug_ajax)
|
||||
Log :: debug("Ajax Request : ".vardump($_REQUEST));
|
||||
}
|
||||
|
||||
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|
||||
|
|
|
@ -1,329 +0,0 @@
|
|||
<?php
|
||||
|
||||
use EesyPHP\Log;
|
||||
use EesyPHP\SentrySpan;
|
||||
use EesyPHP\SentryTransaction;
|
||||
use EesyPHP\Url;
|
||||
|
||||
if (php_sapi_name() == "cli")
|
||||
return true;
|
||||
|
||||
$smarty = new Smarty();
|
||||
|
||||
/*
|
||||
* Configure Smarty regarding the installed version
|
||||
*/
|
||||
if (method_exists($smarty,'register_function')) {
|
||||
$_smarty_version = 2;
|
||||
|
||||
function smarty_enable_security_mode($functions=array(), $modifiers=array()) {
|
||||
global $smarty;
|
||||
// Enable security
|
||||
$smarty -> security = True;
|
||||
|
||||
// Allow functions in IF clauses
|
||||
foreach($functions as $function)
|
||||
$smarty -> security_settings['IF_FUNCS'][] = $function;
|
||||
|
||||
// Allow modifier functions
|
||||
foreach($modifiers as $modifier)
|
||||
$smarty -> security_settings['MODIFIER_FUNCS'][] = $modifier;
|
||||
}
|
||||
|
||||
function smarty_register_function($func_name, $func) {
|
||||
global $smarty;
|
||||
$smarty -> register_function($func_name, $func);
|
||||
}
|
||||
}
|
||||
elseif (method_exists($smarty,'registerPlugin')) {
|
||||
$_smarty_version = 3;
|
||||
|
||||
function smarty_enable_security_mode($functions=array(), $modifiers=array()) {
|
||||
global $smarty;
|
||||
|
||||
// Define security policy
|
||||
$smarty_security_policy = new Smarty_Security($smarty);
|
||||
|
||||
// Allow functions in IF clauses
|
||||
foreach($functions as $function)
|
||||
$smarty_security_policy->php_functions[] = $function;
|
||||
|
||||
// Allow modifier functions
|
||||
foreach($modifiers as $modifier)
|
||||
$smarty_security_policy->php_modifiers[] = $modifier;
|
||||
|
||||
// Enable security
|
||||
$smarty->enableSecurity($smarty_security_policy);
|
||||
}
|
||||
|
||||
function smarty_register_function($func_name, $func) {
|
||||
global $smarty;
|
||||
$smarty -> registerPlugin("function", $func_name, $func);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Log :: fatal(_('Smarty version not supported.'));
|
||||
}
|
||||
|
||||
// Configure templates/templates_c directories
|
||||
if (
|
||||
!isset($smarty_templates_dir)
|
||||
|| !is_dir($smarty_templates_dir)
|
||||
)
|
||||
Log :: fatal(
|
||||
"Template directory not found (%s)",
|
||||
isset($smarty_templates_dir)?$smarty_templates_dir:'not set');
|
||||
else
|
||||
$smarty->setTemplateDir($smarty_templates_dir);
|
||||
|
||||
if (
|
||||
!isset($smarty_templates_c_dir)
|
||||
|| !is_dir($smarty_templates_c_dir)
|
||||
|| !is_writable($smarty_templates_c_dir)
|
||||
)
|
||||
Log :: fatal(
|
||||
"Template cache directory not found or not writable (%s)",
|
||||
isset($smarty_templates_c_dir)?$smarty_templates_c_dir:'not set');
|
||||
else
|
||||
$smarty->setCompileDir($smarty_templates_c_dir);
|
||||
|
||||
// Enable Smarty security
|
||||
smarty_enable_security_mode(
|
||||
// Allow some functions in IF clauses
|
||||
array(
|
||||
'isset', 'empty', 'count', 'in_array', 'is_array', 'array_key_exists', 'is_null',
|
||||
'can_modify', 'can_archive', 'can_delete'
|
||||
),
|
||||
// Allow some modifier functions
|
||||
array('range', 'implode', 'stripslashes')
|
||||
);
|
||||
|
||||
// Defined some global template variables
|
||||
$smarty->assign('public_root_url', isset($public_root_url)?$public_root_url:'/');
|
||||
$smarty->assign('main_pagetitle', isset($main_pagetitle)?$main_pagetitle:null);
|
||||
$smarty->assign('session_key', $_SESSION['session_key']);
|
||||
|
||||
// Handle in-page errors & messages
|
||||
if (!isset($_SESSION['errors']))
|
||||
$_SESSION['errors'] = array();
|
||||
function add_error($error) {
|
||||
// If more than one arguments passed, format error message using sprintf
|
||||
if (func_num_args() > 1) {
|
||||
$error = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($error), array_slice(func_get_args(), 1))
|
||||
);
|
||||
}
|
||||
$_SESSION['errors'][] = $error;
|
||||
}
|
||||
|
||||
if (!isset($_SESSION['messages']))
|
||||
$_SESSION['messages'] = array();
|
||||
function add_message($message) {
|
||||
// If more than one arguments passed, format message using sprintf
|
||||
if (func_num_args() > 1) {
|
||||
$message = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($message), array_slice(func_get_args(), 1))
|
||||
);
|
||||
}
|
||||
$_SESSION['messages'][] = $message;
|
||||
}
|
||||
|
||||
// Handle CSS & JS files included
|
||||
if (isset($included_css_files) && is_array($included_css_files)) {
|
||||
$_css = $included_css_files;
|
||||
}
|
||||
else {
|
||||
$_css = array();
|
||||
}
|
||||
function add_css_file() {
|
||||
global $_css;
|
||||
foreach (func_get_args() as $files) {
|
||||
if (!is_array($files)) $files=array($files);
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file, $_css))
|
||||
$_css[]=$file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$_js = array();
|
||||
function add_js_file() {
|
||||
global $_js;
|
||||
foreach (func_get_args() as $files) {
|
||||
if (!is_array($files)) $files = array($files);
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file, $_js))
|
||||
$_js[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _defineCommonTemplateVariables($template, $pagetitle) {
|
||||
global $smarty, $_css, $_js, $status_list, $auth_user, $admin, $webstats_js_code;
|
||||
$smarty->assign('pagetitle', $pagetitle);
|
||||
|
||||
// Messages
|
||||
$smarty -> assign('errors', (isset($_SESSION['errors'])?$_SESSION['errors']:array()));
|
||||
$smarty -> assign('messages', (isset($_SESSION['messages'])?$_SESSION['messages']:array()));
|
||||
|
||||
// Files inclusions
|
||||
$smarty -> assign('css', $_css);
|
||||
$smarty -> assign('js', $_js);
|
||||
|
||||
// Authenticated user info
|
||||
if (isset($auth_user))
|
||||
$smarty->assign('auth_user', $auth_user);
|
||||
|
||||
// Webstats JS code
|
||||
$smarty->assign(
|
||||
'webstats_js_code',
|
||||
isset($webstats_js_code)?$webstats_js_code:null);
|
||||
}
|
||||
|
||||
function display_template($template, $pagetitle=false) {
|
||||
if (!$template)
|
||||
Log :: fatal(_("No template specified."));
|
||||
|
||||
// If refresh parameter is present, remove it and redirect
|
||||
if (isset($_GET['refresh'])) {
|
||||
unset($_GET['refresh']);
|
||||
$url = Url :: get_current_url();
|
||||
if (!empty($_GET))
|
||||
$url .= '?'.http_build_query($_GET);
|
||||
Url :: redirect($url);
|
||||
return;
|
||||
}
|
||||
|
||||
$sentry_span = new SentrySpan('smarty.display_template', "Display Smarty template");
|
||||
|
||||
global $smarty;
|
||||
// If more than 2 arguments passed, format pagetitle using sprintf
|
||||
if ($pagetitle && func_num_args() > 2) {
|
||||
$pagetitle = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($pagetitle), array_slice(func_get_args(), 2))
|
||||
);
|
||||
}
|
||||
try {
|
||||
_defineCommonTemplateVariables($template, $pagetitle);
|
||||
$smarty->display($template);
|
||||
unset($_SESSION['errors']);
|
||||
unset($_SESSION['messages']);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
Log :: exception($e, "Smarty - An exception occured displaying template '$template'");
|
||||
if ($template != 'fatal_error.tpl')
|
||||
Log :: fatal(_("An error occurred while displaying this page."));
|
||||
}
|
||||
|
||||
$sentry_span->finish();
|
||||
|
||||
}
|
||||
|
||||
function display_ajax_return($data=null, $pretty=false) {
|
||||
global $debug_ajax;
|
||||
if (is_null($data))
|
||||
$data = array();
|
||||
// Adjust HTTP error code on unsuccessfull request
|
||||
elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200)
|
||||
http_response_code(400);
|
||||
|
||||
if (isset($_SESSION['messages']) && !empty($_SESSION['messages'])) {
|
||||
$data['messages'] = $_SESSION['messages'];
|
||||
unset($_SESSION['messages']);
|
||||
}
|
||||
if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
|
||||
$data['errors'] = $_SESSION['errors'];
|
||||
unset($_SESSION['errors']);
|
||||
}
|
||||
if ($debug_ajax)
|
||||
Log :: debug("Ajax Response : ".vardump($data));
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0));
|
||||
exit();
|
||||
}
|
||||
|
||||
$ajax=false;
|
||||
function fatal_error($error) {
|
||||
global $smarty, $ajax;
|
||||
|
||||
// If more than one arguments passed, format error message using sprintf
|
||||
if (func_num_args() > 1) {
|
||||
$error = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($error), array_slice(func_get_args(), 1))
|
||||
);
|
||||
}
|
||||
|
||||
if (php_sapi_name() == "cli")
|
||||
die("FATAL ERROR : $error\n");
|
||||
|
||||
// Set HTTP reponse code to 500
|
||||
http_response_code(500);
|
||||
|
||||
if ($ajax || Url :: api_mode())
|
||||
display_ajax_return(array('success' => false, 'error' => $error));
|
||||
|
||||
$smarty->assign('fatal_error', $error);
|
||||
display_template('fatal_error.tpl');
|
||||
exit();
|
||||
}
|
||||
|
||||
// Templates functions
|
||||
function smarty_item_status($params) {
|
||||
global $status_list;
|
||||
$status2class = array (
|
||||
'pending' => 'info',
|
||||
'validated' => 'success',
|
||||
'refused' => 'danger',
|
||||
'archived' => 'secondary',
|
||||
);
|
||||
if (array_key_exists($params['item']['status'], $status2class)) {
|
||||
$class = $status2class[$params['item']['status']];
|
||||
}
|
||||
else
|
||||
$class='danger';
|
||||
echo "<span class='badge badge-$class'>";
|
||||
echo (
|
||||
array_key_exists($params['item']['status'], $status_list)?
|
||||
$status_list[$params['item']['status']]:
|
||||
"Inconnu (".$params['item']['status'].")"
|
||||
);
|
||||
echo "</span>";
|
||||
}
|
||||
smarty_register_function('item_status','smarty_item_status');
|
||||
|
||||
function smarty_format_time($params) {
|
||||
echo format_time($params['time'], (!isset($params['with_time']) || (bool)$params['with_time']));
|
||||
}
|
||||
smarty_register_function('format_time','smarty_format_time');
|
||||
|
||||
function smarty_format_size($params, $smarty) {
|
||||
if(!isset($params['digit'])) $params['digit'] = 2;
|
||||
echo format_size($params['size'],$params['digit']);
|
||||
}
|
||||
smarty_register_function('format_size','smarty_format_size');
|
||||
|
||||
function smarty_table_ordered_th($params, $smarty) {
|
||||
if ($params['order'] && $params['url'] && $params['text'] && is_array($params['search'])) {
|
||||
$params['url'] .= (strpos($params['url'], '?') === false?'?':'&')."order=".$params['order'];
|
||||
echo "<a href='".$params['url']."'>".$params['text']."</a>";
|
||||
}
|
||||
if ($params['order']==$params['search']['order']) {
|
||||
echo (
|
||||
' <i class="fa fa-sort-'.
|
||||
(strtolower($params['search']['order_direction'])=='asc'?'up':'down').
|
||||
'" aria-hidden="true"></i>');
|
||||
}
|
||||
}
|
||||
smarty_register_function('table_ordered_th','smarty_table_ordered_th');
|
||||
|
||||
function smarty_encodeJsonBase64($params, $smarty) {
|
||||
if (isset($params['data']))
|
||||
echo base64_encode(json_encode($params['data']));
|
||||
}
|
||||
smarty_register_function('encodeJsonBase64','smarty_encodeJsonBase64');
|
||||
|
||||
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|
110
includes/templates.php
Normal file
110
includes/templates.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
use EesyPHP\Date;
|
||||
use EesyPHP\Hook;
|
||||
use EesyPHP\Log;
|
||||
use EesyPHP\Tpl;
|
||||
|
||||
use function EesyPHP\format_size;
|
||||
|
||||
Tpl :: init(
|
||||
isset($smarty_templates_dir)?$smarty_templates_dir:null,
|
||||
isset($smarty_templates_c_dir)?$smarty_templates_c_dir:null,
|
||||
isset($debug_ajax)?$debug_ajax:false
|
||||
);
|
||||
|
||||
if (php_sapi_name() == "cli")
|
||||
return true;
|
||||
|
||||
// Enable Smarty security
|
||||
Tpl :: enable_security_mode(
|
||||
// Allow some functions in IF clauses
|
||||
array(
|
||||
'isset', 'empty', 'count', 'in_array', 'is_array', 'array_key_exists', 'is_null',
|
||||
'can_modify', 'can_archive', 'can_delete'
|
||||
),
|
||||
// Allow some modifier functions
|
||||
array('range', 'implode', 'stripslashes')
|
||||
);
|
||||
|
||||
// Defined some global template variables
|
||||
Tpl :: assign('public_root_url', isset($public_root_url)?$public_root_url:'/');
|
||||
Tpl :: assign('main_pagetitle', isset($main_pagetitle)?$main_pagetitle:null);
|
||||
Tpl :: assign('session_key', $_SESSION['session_key']);
|
||||
|
||||
// Handle CSS & JS files included
|
||||
if (isset($included_css_files) && is_array($included_css_files))
|
||||
Tpl :: add_css_file($included_css_files);
|
||||
if (isset($included_js_files) && is_array($included_js_files))
|
||||
Tpl :: add_css_file($included_js_files);
|
||||
|
||||
function define_common_template_variables($event) {
|
||||
global $status_list, $admin, $webstats_js_code;
|
||||
Tpl :: assign(
|
||||
'status_list',
|
||||
isset($status_list) && is_array($status_list)?
|
||||
$status_list:array()
|
||||
);
|
||||
Tpl :: assign('admin', isset($admin) && $admin);
|
||||
Tpl :: assign(
|
||||
'webstats_js_code',
|
||||
isset($webstats_js_code)?$webstats_js_code:null);
|
||||
}
|
||||
Hook :: register('before_displaying_template', 'define_common_template_variables');
|
||||
|
||||
// Templates functions
|
||||
function smarty_item_status($params) {
|
||||
global $status_list;
|
||||
$status2class = array (
|
||||
'pending' => 'info',
|
||||
'validated' => 'success',
|
||||
'refused' => 'danger',
|
||||
'archived' => 'secondary',
|
||||
);
|
||||
if (array_key_exists($params['item']['status'], $status2class)) {
|
||||
$class = $status2class[$params['item']['status']];
|
||||
}
|
||||
else
|
||||
$class='danger';
|
||||
echo "<span class='badge badge-$class'>";
|
||||
echo (
|
||||
array_key_exists($params['item']['status'], $status_list)?
|
||||
$status_list[$params['item']['status']]:
|
||||
"Inconnu (".$params['item']['status'].")"
|
||||
);
|
||||
echo "</span>";
|
||||
}
|
||||
Tpl :: register_function('item_status','smarty_item_status');
|
||||
|
||||
function smarty_format_time($params) {
|
||||
echo Date :: format($params['time'], (!isset($params['with_time']) || (bool)$params['with_time']));
|
||||
}
|
||||
Tpl :: register_function('format_time','smarty_format_time');
|
||||
|
||||
function smarty_format_size($params, $smarty) {
|
||||
if(!isset($params['digit'])) $params['digit'] = 2;
|
||||
echo format_size($params['size'],$params['digit']);
|
||||
}
|
||||
Tpl :: register_function('format_size','smarty_format_size');
|
||||
|
||||
function smarty_table_ordered_th($params, $smarty) {
|
||||
if ($params['order'] && $params['url'] && $params['text'] && is_array($params['search'])) {
|
||||
$params['url'] .= (strpos($params['url'], '?') === false?'?':'&')."order=".$params['order'];
|
||||
echo "<a href='".$params['url']."'>".$params['text']."</a>";
|
||||
}
|
||||
if ($params['order']==$params['search']['order']) {
|
||||
echo (
|
||||
' <i class="fa fa-sort-'.
|
||||
(strtolower($params['search']['order_direction'])=='asc'?'up':'down').
|
||||
'" aria-hidden="true"></i>');
|
||||
}
|
||||
}
|
||||
Tpl :: register_function('table_ordered_th','smarty_table_ordered_th');
|
||||
|
||||
function smarty_encodeJsonBase64($params, $smarty) {
|
||||
if (isset($params['data']))
|
||||
echo base64_encode(json_encode($params['data']));
|
||||
}
|
||||
Tpl :: register_function('encodeJsonBase64','smarty_encodeJsonBase64');
|
||||
|
||||
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use EesyPHP\Check;
|
||||
use EesyPHP\Log;
|
||||
use EesyPHP\Tpl;
|
||||
|
||||
function get_item_from_url($id, $fatal=false) {
|
||||
if (!Check :: id($id))
|
||||
|
@ -12,7 +13,7 @@ function get_item_from_url($id, $fatal=false) {
|
|||
$error = sprintf(_("Item #% s not found."), $id);
|
||||
if ($fatal)
|
||||
Log :: fatal($error);
|
||||
add_error($error);
|
||||
Tpl :: add_error($error);
|
||||
return false;
|
||||
}
|
||||
return $item;
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use EesyPHP\Check;
|
||||
use EesyPHP\Log;
|
||||
use EesyPHP\Tpl;
|
||||
use EesyPHP\Url;
|
||||
|
||||
use function EesyPHP\vardump;
|
||||
|
@ -10,12 +11,12 @@ if (php_sapi_name() == "cli")
|
|||
return true;
|
||||
|
||||
function handle_homepage($request) {
|
||||
display_template("homepage.tpl", _("Hello world !"));
|
||||
Tpl :: display("homepage.tpl", _("Hello world !"));
|
||||
}
|
||||
Url :: add_url_handler('#^$#', 'handle_homepage');
|
||||
|
||||
function handle_search($request) {
|
||||
global $smarty, $status_list;
|
||||
global $status_list;
|
||||
|
||||
// Manage params
|
||||
if(
|
||||
|
@ -39,7 +40,7 @@ function handle_search($request) {
|
|||
if (check_status($_REQUEST['status']) || $_REQUEST['status'] == 'all')
|
||||
$_SESSION['search']['status'] = $_REQUEST['status'];
|
||||
else
|
||||
$smarty -> assign('status_error', true);
|
||||
Tpl :: assign('status_error', true);
|
||||
}
|
||||
|
||||
if (isset($_REQUEST['pattern'])) {
|
||||
|
@ -48,7 +49,7 @@ function handle_search($request) {
|
|||
else if (Check :: search_pattern($_REQUEST['pattern']))
|
||||
$_SESSION['search']['pattern'] = $_REQUEST['pattern'];
|
||||
else
|
||||
$smarty -> assign('pattern_error', true);
|
||||
Tpl :: assign('pattern_error', true);
|
||||
}
|
||||
|
||||
// Order
|
||||
|
@ -91,23 +92,23 @@ function handle_search($request) {
|
|||
Log :: debug('Search params : '.vardump($_SESSION['search']));
|
||||
$result = search_items($_SESSION['search']);
|
||||
if (!is_array($result))
|
||||
fatal_error(
|
||||
Tpl :: fatal_error(
|
||||
_("An error occurred while listing the items. ".
|
||||
"If the problem persists, please contact support.")
|
||||
);
|
||||
|
||||
$smarty -> assign('result', $result);
|
||||
$smarty -> assign('search', $_SESSION['search']);
|
||||
$smarty -> assign('nbs_by_page', $nbs_by_page);
|
||||
$smarty -> assign('status_list', $status_list);
|
||||
Tpl :: assign('result', $result);
|
||||
Tpl :: assign('search', $_SESSION['search']);
|
||||
Tpl :: assign('nbs_by_page', $nbs_by_page);
|
||||
Tpl :: assign('status_list', $status_list);
|
||||
|
||||
add_js_file(array(
|
||||
Tpl :: add_js_file(array(
|
||||
'lib/bootstrap4dialog/dist/js/bootstrap4dialog.min.js',
|
||||
'js/myconfirm.js',
|
||||
'js/search.js'
|
||||
));
|
||||
|
||||
display_template("search.tpl", _("Search"));
|
||||
Tpl :: display("search.tpl", _("Search"));
|
||||
}
|
||||
Url :: add_url_handler('|^item/?$|', 'handle_search');
|
||||
|
||||
|
@ -116,21 +117,19 @@ Url :: add_url_handler('|^item/?$|', 'handle_search');
|
|||
*/
|
||||
|
||||
function handle_show($request) {
|
||||
global $smarty;
|
||||
|
||||
$item = get_item_from_url($request -> id);
|
||||
if (!$item)
|
||||
Url :: error_404();
|
||||
|
||||
$smarty->assign('item', $item);
|
||||
Tpl :: assign('item', $item);
|
||||
|
||||
// Dialog
|
||||
add_js_file(array(
|
||||
Tpl :: add_js_file(array(
|
||||
'lib/bootstrap4dialog/dist/js/bootstrap4dialog.min.js',
|
||||
'js/myconfirm.js',
|
||||
));
|
||||
|
||||
display_template(
|
||||
Tpl :: display(
|
||||
"show.tpl", _("Element %s"),
|
||||
(is_array($item)?$item['name']:"#".$request -> id)
|
||||
);
|
||||
|
@ -138,42 +137,42 @@ function handle_show($request) {
|
|||
Url :: add_url_handler('|^item/(?P<id>[0-9]+)$|', 'handle_show');
|
||||
|
||||
function handle_create($request) {
|
||||
global $smarty, $status_list;
|
||||
global $status_list;
|
||||
|
||||
$info = array();
|
||||
$field_errors = handle_item_post_data($info);
|
||||
if (isset($_POST['submit']) && empty($field_errors)) {
|
||||
$item = add_item($info);
|
||||
if (is_array($item)) {
|
||||
add_message(_("The element '% s' has been created."), $item['name']);
|
||||
Tpl :: add_message(_("The element '% s' has been created."), $item['name']);
|
||||
Url :: redirect('item/'.$item['id']);
|
||||
}
|
||||
else {
|
||||
add_error(_("An error occurred while saving this item."));
|
||||
Tpl :: add_error(_("An error occurred while saving this item."));
|
||||
}
|
||||
}
|
||||
Log :: debug('Validated data : '.vardump($info));
|
||||
Log :: debug('Fields errors : '.vardump($field_errors));
|
||||
if (isset($_POST['submit']) && !empty($field_errors))
|
||||
add_error(
|
||||
Tpl :: add_error(
|
||||
_("There are errors preventing this item from being saved. ".
|
||||
"Please correct them before attempting to add this item."));
|
||||
$smarty->assign('submited', isset($_POST['submit']));
|
||||
$smarty->assign('info', $info);
|
||||
$smarty->assign('field_errors', $field_errors);
|
||||
$smarty->assign('status_list', $status_list);
|
||||
Tpl :: assign('submited', isset($_POST['submit']));
|
||||
Tpl :: assign('info', $info);
|
||||
Tpl :: assign('field_errors', $field_errors);
|
||||
Tpl :: assign('status_list', $status_list);
|
||||
|
||||
display_template("form.tpl", _("New"));
|
||||
Tpl :: display("form.tpl", _("New"));
|
||||
}
|
||||
Url :: add_url_handler('|^item/new$|', 'handle_create');
|
||||
|
||||
function handle_modify($request) {
|
||||
global $smarty, $status_list;
|
||||
global $status_list;
|
||||
|
||||
$item = get_item_from_url($request -> id);
|
||||
if(is_array($item)) {
|
||||
if (!can_modify($item)) {
|
||||
add_error(_('You cannot edit this item.'));
|
||||
Tpl :: add_error(_('You cannot edit this item.'));
|
||||
Url :: redirect('item/'.$item['id']);
|
||||
}
|
||||
$info = array();
|
||||
|
@ -186,34 +185,34 @@ function handle_modify($request) {
|
|||
}
|
||||
Log :: debug('Changes : '.vardump($changes));
|
||||
if (empty($changes)) {
|
||||
add_message(_("You have not made any changes to element '% s'."), $item['name']);
|
||||
Tpl :: add_message(_("You have not made any changes to element '% s'."), $item['name']);
|
||||
Url :: redirect('item/'.$item['id']);
|
||||
}
|
||||
else if (update_item($item['id'], $changes) === true) {
|
||||
add_message(_("The element '% s' has been updated successfully."), $item['name']);
|
||||
Tpl :: add_message(_("The element '% s' has been updated successfully."), $item['name']);
|
||||
Url :: redirect('item/'.$item['id']);
|
||||
}
|
||||
else {
|
||||
add_error(_("An error occurred while updating this item."));
|
||||
Tpl :: add_error(_("An error occurred while updating this item."));
|
||||
}
|
||||
}
|
||||
Log :: debug('Validated data : '.vardump($info));
|
||||
Log :: debug('Fields errors : '.vardump($field_errors));
|
||||
$smarty->assign('submited', isset($_POST['submit']));
|
||||
Tpl :: assign('submited', isset($_POST['submit']));
|
||||
if (isset($_POST['submit']) && !empty($field_errors))
|
||||
add_error(
|
||||
Tpl :: add_error(
|
||||
_("There are errors preventing this item from being saved. ".
|
||||
"Please correct them before attempting to save your changes."));
|
||||
$smarty->assign('info', (!empty($info)?$info:$item));
|
||||
$smarty->assign('item_id', $item['id']);
|
||||
$smarty->assign('field_errors', $field_errors);
|
||||
$smarty -> assign('status_list', $status_list);
|
||||
Tpl :: assign('info', (!empty($info)?$info:$item));
|
||||
Tpl :: assign('item_id', $item['id']);
|
||||
Tpl :: assign('field_errors', $field_errors);
|
||||
Tpl :: assign('status_list', $status_list);
|
||||
}
|
||||
else {
|
||||
Url :: error_404();
|
||||
}
|
||||
|
||||
display_template(
|
||||
Tpl :: display(
|
||||
"form.tpl", _("Element %s: Modification"),
|
||||
(is_array($item)?$item['name']:"#".$request -> id)
|
||||
);
|
||||
|
@ -221,44 +220,40 @@ function handle_modify($request) {
|
|||
Url :: add_url_handler('|^item/(?P<id>[0-9]+)/modify$|', 'handle_modify');
|
||||
|
||||
function handle_archive($request) {
|
||||
global $smarty;
|
||||
|
||||
$item = get_item_from_url($request -> id);
|
||||
if(!is_array($item)) {
|
||||
add_error(_("Item #% s not found."), $request -> id);
|
||||
Tpl :: add_error(_("Item #% s not found."), $request -> id);
|
||||
Url :: redirect('item');
|
||||
}
|
||||
elseif ($item['status'] == 'archived') {
|
||||
add_message(_("This item is already archived."));
|
||||
Tpl :: add_message(_("This item is already archived."));
|
||||
}
|
||||
else if (!can_archive($item)) {
|
||||
add_error(_('You cannot archive this item.'));
|
||||
Tpl :: add_error(_('You cannot archive this item.'));
|
||||
}
|
||||
else if (archive_item($item['id']) === true) {
|
||||
add_message(_("The element '% s' has been archived successfully."), $item['name']);
|
||||
Tpl :: add_message(_("The element '% s' has been archived successfully."), $item['name']);
|
||||
}
|
||||
else {
|
||||
add_error(_('An error occurred while archiving this item.'));
|
||||
Tpl :: add_error(_('An error occurred while archiving this item.'));
|
||||
}
|
||||
Url :: redirect('item/'.$item['id']);
|
||||
}
|
||||
Url :: add_url_handler('|^item/(?P<id>[0-9]+)/archive$|', 'handle_archive');
|
||||
|
||||
function handle_delete($request) {
|
||||
global $smarty;
|
||||
|
||||
$item = get_item_from_url($request -> id);
|
||||
if(!is_array($item)) {
|
||||
add_error(_("Item #% s not found."), $request -> id);
|
||||
Tpl :: add_error(_("Item #% s not found."), $request -> id);
|
||||
}
|
||||
else if (!can_delete($item)) {
|
||||
add_error(_('You cannot delete this item.'));
|
||||
Tpl :: add_error(_('You cannot delete this item.'));
|
||||
}
|
||||
else if (delete_item($item['id']) === true) {
|
||||
add_message(_("The element '% s' has been deleted successfully."), $item['name']);
|
||||
Tpl :: add_message(_("The element '% s' has been deleted successfully."), $item['name']);
|
||||
}
|
||||
else {
|
||||
add_error(_('An error occurred while deleting this item.'));
|
||||
Tpl :: add_error(_('An error occurred while deleting this item.'));
|
||||
Url :: redirect('item/'.$item['id']);
|
||||
}
|
||||
Url :: redirect('item');
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Cli {
|
||||
|
|
56
src/Date.php
Normal file
56
src/Date.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace EesyPHP;
|
||||
|
||||
class Date {
|
||||
|
||||
/**
|
||||
* The date format
|
||||
* @see strftime()
|
||||
* @var string
|
||||
*/
|
||||
public static string $date_format = "%d/%m/%Y";
|
||||
|
||||
/**
|
||||
* The datetime format
|
||||
* @see strftime()
|
||||
* @var string
|
||||
*/
|
||||
public static string $date_time_format = "%d/%m/%Y %H:%M:%S";
|
||||
|
||||
/**
|
||||
* Format a timestamp as date/datetime string
|
||||
* @param int $time The timestamp to format
|
||||
* @param bool $with_time If true, include time in formated string
|
||||
* (optional, default: true)
|
||||
*/
|
||||
public static function format($time, $with_time=true) {
|
||||
if ($with_time)
|
||||
return strftime(self :: $date_time_format, $time);
|
||||
return strftime(self :: $date_format, $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a date/datetime string as timestamp
|
||||
* @param string $date The date string to parse
|
||||
* @param bool $with_time If true, consider the date string included time
|
||||
* (optional, default: true)
|
||||
*/
|
||||
public static function parse($date, $with_time=true) {
|
||||
if ($with_time)
|
||||
$ptime = strptime($date, self :: $date_time_format);
|
||||
else
|
||||
$ptime = strptime($date, self :: $date_format);
|
||||
if(is_array($ptime)) {
|
||||
return mktime(
|
||||
$ptime['tm_hour'],
|
||||
$ptime['tm_min'],
|
||||
$ptime['tm_sec'],
|
||||
$ptime['tm_mon']+1,
|
||||
$ptime['tm_mday'],
|
||||
$ptime['tm_year']+1900
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
use function EesyPHP\vardump;
|
||||
|
||||
use Exception;
|
||||
use PDO;
|
||||
use \Envms\FluentPDO\Query;
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
|
||||
use PEAR;
|
||||
use Mail;
|
||||
use Mail_mime;
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Hook {
|
||||
|
|
35
src/I18n.php
35
src/I18n.php
|
@ -2,11 +2,7 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Cli;
|
||||
use EesyPHP\Log;
|
||||
|
||||
use Locale;
|
||||
use add_js_file;
|
||||
|
||||
class I18n {
|
||||
// Gettext text domain
|
||||
|
@ -109,9 +105,9 @@ class I18n {
|
|||
php_sapi_name() != "cli"
|
||||
&& isset($root_dir_path) && $root_dir_path
|
||||
&& is_file("$root_dir_path/public_html/$js_translation_file")
|
||||
&& function_exists('add_js_file')
|
||||
&& Tpl :: initialized()
|
||||
) {
|
||||
add_js_file(array("lib/babel.js", "js/translation.js", $js_translation_file));
|
||||
Tpl :: add_js_file(array("lib/babel.js", "js/translation.js", $js_translation_file));
|
||||
}
|
||||
|
||||
if (php_sapi_name() == 'cli') {
|
||||
|
@ -254,7 +250,10 @@ class I18n {
|
|||
* @return void
|
||||
*/
|
||||
public static function cli_extract_messages($command_args) {
|
||||
global $root_dir_path, $smarty_templates_dir;
|
||||
global $root_dir_path;
|
||||
|
||||
// Store list of generated POT files
|
||||
$pot_files = array();
|
||||
|
||||
// List PHP files to parse
|
||||
$php_files = run_external_command(
|
||||
|
@ -281,7 +280,7 @@ class I18n {
|
|||
);
|
||||
if (!is_array($result) || $result[0] != 0)
|
||||
Log :: fatal(_("Fail to extract messages from PHP files using xgettext."));
|
||||
|
||||
$pot_files[] = self :: $root_path."/php-messages.pot";
|
||||
|
||||
// List JS files to parse
|
||||
$js_files = run_external_command(
|
||||
|
@ -308,18 +307,22 @@ class I18n {
|
|||
);
|
||||
if (!is_array($result) || $result[0] != 0)
|
||||
Log :: fatal(_("Fail to extract messages from JS files using xgettext."));
|
||||
$pot_files[] = self :: $root_path."/js-messages.pot";
|
||||
|
||||
if (Tpl :: initialized()) {
|
||||
// Extract messages from templates files using tsmarty2c.php
|
||||
$result = run_external_command(
|
||||
array (
|
||||
"$root_dir_path/vendor/bin/tsmarty2c.php",
|
||||
"-o", self :: $root_path."/templates-messages.pot",
|
||||
$smarty_templates_dir,
|
||||
Tpl :: templates_directory(),
|
||||
)
|
||||
);
|
||||
if (!is_array($result) || $result[0] != 0)
|
||||
Log :: fatal(
|
||||
_("Fail to extract messages from template files using tsmarty2c.php script."));
|
||||
$pot_files[] = self :: $root_path."/templates-messages.pot";
|
||||
}
|
||||
|
||||
$fd = fopen(self :: $root_path."/headers.pot", 'w');
|
||||
$headers = array(
|
||||
|
@ -335,14 +338,14 @@ class I18n {
|
|||
fclose($fd);
|
||||
|
||||
// Merge previous results in messages.pot file using msgcat
|
||||
$result = run_external_command(array(
|
||||
$result = run_external_command(array_merge(
|
||||
array(
|
||||
'msgcat',
|
||||
self :: $root_path."/headers.pot",
|
||||
self :: $root_path."/php-messages.pot",
|
||||
self :: $root_path."/js-messages.pot",
|
||||
self :: $root_path."/templates-messages.pot",
|
||||
"-t", "utf-8", "--use-first",
|
||||
"-o", self :: $root_path."/messages.pot",
|
||||
self :: $root_path."/headers.pot",
|
||||
),
|
||||
$pot_files
|
||||
));
|
||||
if (!is_array($result) || $result[0] != 0)
|
||||
Log :: fatal(_("Fail to merge messages using msgcat."));
|
||||
|
@ -356,7 +359,7 @@ class I18n {
|
|||
* @return bool
|
||||
*/
|
||||
public static function cli_update_messages($command_args) {
|
||||
global $root_dir_path, $smarty_templates_dir;
|
||||
global $root_dir_path;
|
||||
|
||||
$compendium_args = array();
|
||||
foreach ($command_args as $path) {
|
||||
|
@ -444,7 +447,7 @@ class I18n {
|
|||
* @return bool
|
||||
*/
|
||||
public static function cli_compile_messages($command_args) {
|
||||
global $root_dir_path, $smarty_templates_dir;
|
||||
global $root_dir_path;
|
||||
|
||||
if ($dh = opendir(self :: $root_path)) {
|
||||
$error = False;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace EesyPHP;
|
||||
use EesyPHP\SentryIntegration;
|
||||
|
||||
use Throwable;
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
use Throwable;
|
||||
use Exception;
|
||||
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
|
||||
|
||||
/**
|
||||
* Session management
|
||||
*/
|
||||
|
|
354
src/Tpl.php
Normal file
354
src/Tpl.php
Normal file
|
@ -0,0 +1,354 @@
|
|||
<?php
|
||||
|
||||
namespace EesyPHP;
|
||||
|
||||
use Exception;
|
||||
use Smarty;
|
||||
use Smarty_Security;
|
||||
|
||||
class Tpl {
|
||||
|
||||
/**
|
||||
* Smarty object
|
||||
* @var \Smarty
|
||||
*/
|
||||
public static Smarty $smarty;
|
||||
|
||||
/**
|
||||
* Smarty_Security object
|
||||
* @var \Smarty_Security|null
|
||||
*/
|
||||
public static $smarty_security_policy = null;
|
||||
|
||||
/**
|
||||
* Smarty templates directory path
|
||||
* @var string
|
||||
*/
|
||||
public static string $templates_dir;
|
||||
|
||||
/**
|
||||
* Smarty cache templates directory path
|
||||
* @var string
|
||||
*/
|
||||
public static string $templates_c_dir;
|
||||
|
||||
/**
|
||||
* Enable/disable AJAX returned data debugging in logs
|
||||
* @var bool
|
||||
*/
|
||||
public static bool $_debug_ajax;
|
||||
|
||||
/**
|
||||
* CSS files to load in next displayed page
|
||||
* @var array<string>
|
||||
*/
|
||||
private static array $css_files = array();
|
||||
|
||||
/**
|
||||
* JavaScript files to load in next displayed page
|
||||
* @var array<string>
|
||||
*/
|
||||
private static array $js_files = array();
|
||||
|
||||
/**
|
||||
* Initialization
|
||||
* @param string $templates_dir Smarty templates directory path
|
||||
* @param string $templates_c_dir Smarty cache templates directory path
|
||||
* @param bool $debug_ajax Enable/disable AJAX returned data debugging in logs
|
||||
* (optional, default: false)
|
||||
* @return void
|
||||
*/
|
||||
public static function init($templates_dir, $templates_c_dir, $debug_ajax=false) {
|
||||
// Check templates/templates_c directories
|
||||
if (!$templates_dir || !is_dir($templates_dir)) {
|
||||
Log :: fatal(
|
||||
"Template directory not found (%s)",
|
||||
$templates_dir?$templates_dir:'not set');
|
||||
return;
|
||||
}
|
||||
if (!$templates_c_dir || !is_dir($templates_c_dir) || !is_writable($templates_c_dir)) {
|
||||
Log :: fatal(
|
||||
"Template cache directory not found or not writable (%s)",
|
||||
$templates_c_dir?$templates_c_dir:'not set');
|
||||
return;
|
||||
}
|
||||
self :: $smarty = new Smarty();
|
||||
self :: $smarty->setTemplateDir($templates_dir);
|
||||
self :: $smarty->setCompileDir($templates_c_dir);
|
||||
self :: $_debug_ajax = boolval($debug_ajax);
|
||||
Log :: register_fatal_error_handler(array('\\EesyPHP\\Tpl', 'fatal_error'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable security in mode to limit functions (in IF clauses) and modifiers usable from
|
||||
* template files
|
||||
* @param array<string>|null $functions List of function names granted in IF clauses
|
||||
* @param array<string>|null $modifiers List of modifier names granted
|
||||
* @return void
|
||||
*/
|
||||
public static function enable_security_mode($functions=null, $modifiers=null) {
|
||||
// Define security policy
|
||||
self :: $smarty_security_policy = new Smarty_Security(self :: $smarty);
|
||||
|
||||
// Allow functions in IF clauses
|
||||
if (is_array($functions))
|
||||
foreach($functions as $function)
|
||||
self :: $smarty_security_policy->php_functions[] = $function;
|
||||
|
||||
// Allow modifier functions
|
||||
if (is_array($modifiers))
|
||||
foreach($modifiers as $modifier)
|
||||
self :: $smarty_security_policy->php_modifiers[] = $modifier;
|
||||
|
||||
// Enable security
|
||||
self :: $smarty -> enableSecurity(self :: $smarty_security_policy);
|
||||
|
||||
// Initialize errors & messages session variables
|
||||
if (!isset($_SESSION['errors']))
|
||||
$_SESSION['errors'] = array();
|
||||
if (!isset($_SESSION['messages']))
|
||||
$_SESSION['messages'] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a function usable from template files
|
||||
* @param string $name The function name
|
||||
* @param callable $callable The function
|
||||
* @return void
|
||||
*/
|
||||
public static function register_function($name, $callable) {
|
||||
self :: $smarty -> registerPlugin("function", $name, $callable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign template variable
|
||||
* @param string $name The variable name
|
||||
* @param mixed $value The variable value
|
||||
* @return void
|
||||
*/
|
||||
public static function assign($name, $value) {
|
||||
self :: $smarty -> assign($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error message
|
||||
* @param string $error The message
|
||||
* @param array $extra_args Extra arguments to use to compute error message using sprintf
|
||||
* @return void
|
||||
*/
|
||||
public static function add_error($error, ...$extra_args) {
|
||||
// If extra arguments passed, format error message using sprintf
|
||||
if ($extra_args) {
|
||||
$error = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($error), $extra_args)
|
||||
);
|
||||
}
|
||||
$_SESSION['errors'][] = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add informational message
|
||||
* @param string $message The message
|
||||
* @param array $extra_args Extra arguments to use to compute message using sprintf
|
||||
* @return void
|
||||
*/
|
||||
public static function add_message($message, ...$extra_args) {
|
||||
// If extra arguments passed, format message using sprintf
|
||||
if ($extra_args) {
|
||||
$message = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($message), $extra_args)
|
||||
);
|
||||
}
|
||||
$_SESSION['messages'][] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register CSS file(s) to load on next displayed page
|
||||
* @param array<string|array<string>> $args CSS files to load
|
||||
* @return void
|
||||
*/
|
||||
public static function add_css_file(...$args) {
|
||||
foreach ($args as $files) {
|
||||
if (!is_array($files)) $files = array($files);
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file, self :: $css_files))
|
||||
self :: $css_files[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register JS file(s) to load on next displayed page
|
||||
* @param array<string|array<string>> $args JS files to load
|
||||
* @return void
|
||||
*/
|
||||
public static function add_js_file(...$args) {
|
||||
foreach ($args as $files) {
|
||||
if (!is_array($files)) $files = array($files);
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file, self :: $js_files))
|
||||
self :: $js_files[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define common variables
|
||||
* @param string|null $pagetitle The page title
|
||||
* @return void
|
||||
*/
|
||||
protected static function define_common_variables($pagetitle=null) {
|
||||
global $auth_user;
|
||||
self :: assign('pagetitle', $pagetitle);
|
||||
|
||||
// Messages
|
||||
self :: assign('errors', (isset($_SESSION['errors'])?$_SESSION['errors']:array()));
|
||||
self :: assign('messages', (isset($_SESSION['messages'])?$_SESSION['messages']:array()));
|
||||
|
||||
// Files inclusions
|
||||
self :: assign('css', self :: $css_files);
|
||||
self :: assign('js', self :: $js_files);
|
||||
|
||||
// Authenticated user info
|
||||
if (isset($auth_user))
|
||||
self :: assign('auth_user', $auth_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the template
|
||||
* @param string $template The template to display
|
||||
* @param string|null $pagetitle The page title (optional)
|
||||
* @param array $extra_args Extra arguments to use to compute the page title using sprintf
|
||||
* @return void
|
||||
*/
|
||||
public static function display($template, $pagetitle=null, ...$extra_args) {
|
||||
if (!$template) {
|
||||
Log :: fatal(_("No template specified."));
|
||||
return;
|
||||
}
|
||||
|
||||
// If refresh parameter is present, remove it and redirect
|
||||
if (isset($_GET['refresh'])) {
|
||||
unset($_GET['refresh']);
|
||||
$url = Url :: get_current_url();
|
||||
if (!empty($_GET))
|
||||
$url .= '?'.http_build_query($_GET);
|
||||
Url :: redirect($url);
|
||||
return;
|
||||
}
|
||||
|
||||
$sentry_span = new SentrySpan('smarty.display_template', "Display Smarty template");
|
||||
|
||||
// If extra arguments passed, format pagetitle using sprintf
|
||||
if ($pagetitle && $extra_args) {
|
||||
$pagetitle = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($pagetitle), $extra_args)
|
||||
);
|
||||
}
|
||||
try {
|
||||
Hook :: trigger('before_displaying_template');
|
||||
self :: define_common_variables($pagetitle);
|
||||
self :: $smarty->display($template);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
Log :: exception($e, "Smarty - An exception occured displaying template '$template'");
|
||||
if ($template != 'fatal_error.tpl')
|
||||
Log :: fatal(_("An error occurred while displaying this page."));
|
||||
return;
|
||||
}
|
||||
unset($_SESSION['errors']);
|
||||
unset($_SESSION['messages']);
|
||||
Hook :: trigger('after_displaying_template');
|
||||
$sentry_span->finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display AJAX return
|
||||
* @param array|null $data AJAX returned data (optional)
|
||||
* @param bool $pretty AJAX returned data
|
||||
* (optional, default: true if $_REQUEST['pretty'] is set, False otherwise)
|
||||
* @return void
|
||||
*/
|
||||
public static function display_ajax_return($data=null, $pretty=false) {
|
||||
if (!is_array($data))
|
||||
$data = array();
|
||||
// Adjust HTTP error code on unsuccessfull request
|
||||
elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200)
|
||||
http_response_code(400);
|
||||
|
||||
if (isset($_SESSION['messages']) && !empty($_SESSION['messages'])) {
|
||||
$data['messages'] = $_SESSION['messages'];
|
||||
unset($_SESSION['messages']);
|
||||
}
|
||||
if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
|
||||
$data['errors'] = $_SESSION['errors'];
|
||||
unset($_SESSION['errors']);
|
||||
}
|
||||
if (self :: $_debug_ajax)
|
||||
Log :: debug("AJAX Response : ".vardump($data));
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0));
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a fatal error
|
||||
* @param string $error The error message
|
||||
* @param array $extra_args Extra arguments to use to compute the error message using sprintf
|
||||
* @return void
|
||||
*/
|
||||
public static function fatal_error($error, ...$extra_args) {
|
||||
// If extra arguments passed, format error message using sprintf
|
||||
if ($extra_args) {
|
||||
$error = call_user_func_array(
|
||||
'sprintf',
|
||||
array_merge(array($error), $extra_args)
|
||||
);
|
||||
}
|
||||
|
||||
if (php_sapi_name() == "cli")
|
||||
die("FATAL ERROR : $error\n");
|
||||
|
||||
// Set HTTP reponse code to 500
|
||||
http_response_code(500);
|
||||
|
||||
// Handle API mode
|
||||
if (Url :: api_mode()) {
|
||||
self :: display_ajax_return(array('success' => false, 'error' => $error));
|
||||
return;
|
||||
}
|
||||
|
||||
self :: assign('fatal_error', $error);
|
||||
self :: display('fatal_error.tpl');
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get/set AJAX debug mode
|
||||
* @param bool|null $value If boolean, the current API mode will be changed
|
||||
* @return bool Current API mode
|
||||
*/
|
||||
public static function debug_ajax($value=null) {
|
||||
if (is_bool($value)) self :: $_debug_ajax = $value;
|
||||
return self :: $_debug_ajax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if initialized
|
||||
* @return bool
|
||||
*/
|
||||
public static function initialized() {
|
||||
return isset(self :: $smarty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the templates directory path
|
||||
* @return string|null
|
||||
*/
|
||||
public static function templates_directory() {
|
||||
return isset(self :: $templates_directory)?self :: $templates_directory:null;
|
||||
}
|
||||
}
|
23
src/Url.php
23
src/Url.php
|
@ -2,14 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
use EesyPHP\SentrySpan;
|
||||
use EesyPHP\SentryTransaction;
|
||||
use EesyPHP\UrlRequest;
|
||||
|
||||
use function EesyPHP\format_callable;
|
||||
use function EesyPHP\vardump;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Url {
|
||||
|
@ -141,7 +133,6 @@ class Url {
|
|||
* @return void
|
||||
**/
|
||||
public static function error_page($request=null, $error_code=null) {
|
||||
global $smarty;
|
||||
$http_errors = array(
|
||||
400 => array(
|
||||
'pagetitle' => _("Bad request"),
|
||||
|
@ -170,11 +161,13 @@ class Url {
|
|||
);
|
||||
http_response_code($error_code);
|
||||
|
||||
if (isset($smarty) && $smarty)
|
||||
$smarty -> assign('message', $error['message']);
|
||||
display_template('error_page.tpl', $error['pagetitle']);
|
||||
if (Tpl :: initialized()) {
|
||||
Tpl :: assign('message', $error['message']);
|
||||
Tpl :: display('error_page.tpl', $error['pagetitle']);
|
||||
exit();
|
||||
}
|
||||
die($error['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Error 404 handler
|
||||
|
@ -391,8 +384,6 @@ class Url {
|
|||
* @return void
|
||||
**/
|
||||
public static function handle_request($default_url=null) {
|
||||
global $smarty;
|
||||
|
||||
$sentry_span = new SentrySpan('http.handle_request', 'Handle the HTTP request');
|
||||
|
||||
$request = self :: get_request($default_url);
|
||||
|
@ -406,8 +397,8 @@ class Url {
|
|||
|
||||
if ($request -> api_mode)
|
||||
self :: $_api_mode = true;
|
||||
if (isset($smarty) && $smarty)
|
||||
$smarty -> assign('request', $request);
|
||||
if (Tpl :: initialized())
|
||||
Tpl :: assign('request', $request);
|
||||
|
||||
// Check authentication (if need)
|
||||
if($request -> authenticated)
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
|
||||
|
||||
/**
|
||||
* URL request abstraction
|
||||
*
|
||||
|
|
|
@ -2,39 +2,9 @@
|
|||
|
||||
namespace EesyPHP;
|
||||
|
||||
use EesyPHP\Log;
|
||||
|
||||
/*
|
||||
* Parser/formater values helpers
|
||||
*/
|
||||
$_date_format = "%d/%m/%Y";
|
||||
$_date_time_format = "%d/%m/%Y %H:%M:%S";
|
||||
function format_time($time, $with_time=true) {
|
||||
global $_date_format, $_date_time_format;
|
||||
if ($with_time)
|
||||
return strftime($_date_time_format, $time);
|
||||
return strftime($_date_format, $time);
|
||||
}
|
||||
|
||||
function parse_date($date, $with_time=true) {
|
||||
global $_date_format, $_date_time_format;
|
||||
if ($with_time)
|
||||
$ptime = strptime($date, $_date_time_format);
|
||||
else
|
||||
$ptime = strptime($date, $_date_format);
|
||||
if(is_array($ptime)) {
|
||||
return mktime(
|
||||
$ptime['tm_hour'],
|
||||
$ptime['tm_min'],
|
||||
$ptime['tm_sec'],
|
||||
$ptime['tm_mon']+1,
|
||||
$ptime['tm_mday'],
|
||||
$ptime['tm_year']+1900
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function format_size($size, $digit=False) {
|
||||
if (!$digit && $digit!==0) $digit=2;
|
||||
if ($size>=1099511627776)
|
||||
|
@ -213,4 +183,25 @@ function run_external_command($command, $data_stdin=null, $escape_command_args=t
|
|||
return array($return_value, $stdout, $stderr);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check an AJAX request and trigger a fatal error on fail
|
||||
*
|
||||
* Check if session key is present and valid and set AJAX
|
||||
* mode.
|
||||
*
|
||||
* @param string|null $session_key string The current session key (optional)
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function check_ajax_request($session_key=null) {
|
||||
Url :: api_mode(true);
|
||||
|
||||
if (Session :: check_key($session_key))
|
||||
Tpl :: fatal_error('Invalid request');
|
||||
|
||||
if (Tpl :: debug_ajax())
|
||||
Log :: debug("Ajax Request : ".vardump($_REQUEST));
|
||||
}
|
||||
|
||||
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|
||||
|
|
Loading…
Reference in a new issue