Repository navigation
Expand file tree
/
Copy pathApp.php
More file actions
executable file
·371 lines (307 loc) · 10.3 KB
/
Copy pathApp.php
File metadata and controls
executable file
·371 lines (307 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
use Util\Messages;
/**
* Classe per la gestione delle utenze.
*
* @since 2.4
*/
class App
{
public static $docroot;
public static $rootdir;
public static $baseurl;
/** @var array Identificativo del modulo corrente */
protected static $current_module;
/** @var int Identificativo dell'elemento corrente */
protected static $current_element;
/** @var Messages Gestione dei messaggi flash */
protected static $flash;
/** @var bool Stato di debug */
protected static $config = [];
/** @var array Elenco degli assets del progetto */
protected static $assets = [
// CSS
'css' => [
'app.min.css',
'themes.min.css',
'style.min.css',
],
// Print CSS
'print' => [
'print.min.css',
],
// JS
'js' => [
'app.min.js',
'ua-parser.min.js',
'functions.min.js',
'custom.min.js',
'i18n/parsleyjs/|lang|.min.js',
'i18n/select2/|lang|.min.js',
'i18n/moment/|lang|.min.js',
'i18n/locales.min.js',
'i18n/@fullcalendar/|lang|.min.js',
'api.js',
],
];
/**
* Restituisce la configurazione dell'installazione in utilizzo del progetto.
*
* @return array
*/
public static function getConfig()
{
if (empty(self::$config['db_host'])) {
if (file_exists(base_dir().'/config.inc.php')) {
include base_dir().'/config.inc.php';
$config = get_defined_vars();
} else {
$config = [];
}
$defaultConfig = self::getDefaultConfig();
$result = array_merge($defaultConfig, $config);
// Operazioni di normalizzazione sulla configurazione
$result['debug'] = self::$config['debug'] ?? !empty($result['debug']);
$result['lang'] = $result['lang'] == 'it' ? 'it_IT' : $result['lang'];
self::$config = $result;
}
return self::$config;
}
/**
* Imposta e restituisce lo stato di debug del progetto.
*
* @param bool $value
*
* @return bool
*/
public static function debug($value = null)
{
if (is_bool($value)) {
self::$config['debug'] = $value;
}
if (!isset(self::$config['debug'])) {
App::getConfig();
}
return self::$config['debug'];
}
/**
* Restituisce l'oggetto dedicato alla gestione dei messaggi per l'utente.
*
* @return Messages
*/
public static function flash()
{
if (empty(self::$flash)) {
$storage = null;
self::$flash = new Messages($storage, 'messages');
}
return self::$flash;
}
/**
* Individua i percorsi di base necessari per il funzionamento del gestionale.
* <b>Attenzione<b>: questo metodo deve essere eseguito all'interno di un file nella cartella principale del progetto per permettere il corretto funzionamento degli URL.
*/
public static function definePaths($docroot)
{
if (!defined('DOCROOT')) {
// Individuazione di $rootdir
$rootdir = substr((string) $_SERVER['SCRIPT_NAME'], 0, strrpos((string) $_SERVER['SCRIPT_NAME'], '/')).'/';
if (strrpos($rootdir, '/'.basename((string) $docroot).'/') !== false) {
$rootdir = substr($rootdir, 0, strrpos($rootdir, '/'.basename((string) $docroot).'/')).'/'.basename((string) $docroot);
} else {
$rootdir = '/';
}
$rootdir = rtrim($rootdir, '/');
$rootdir = str_replace('%2F', '/', rawurlencode($rootdir));
// Individuazione di $baseurl
if (php_sapi_name() == 'cli') {
$baseurl = '';
} else {
$baseurl = (isHTTPS(true) ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$rootdir;
}
// Impostazione delle variabili globali
define('DOCROOT', $docroot);
define('ROOTDIR', $rootdir);
define('BASEURL', $baseurl);
self::$docroot = $docroot;
self::$rootdir = $rootdir;
self::$baseurl = $baseurl;
}
}
/**
* Individua i percorsi principali del progetto.
*
* @return array
*/
public static function getPaths()
{
$assets = base_path_osm().'/assets/dist';
return [
'assets' => $assets,
'css' => $assets.'/css',
'js' => $assets.'/js',
'img' => $assets.'/img',
];
}
/**
* Restituisce l'elenco degli assets del progetto.
*
* @return array
*/
public static function getAssets()
{
// Assets aggiuntivi
$config = self::getConfig();
$version = Update::getVersion();
// Impostazione dei percorsi
$paths = self::getPaths();
$lang = trans_osm()->getCurrentLocale();
// Sezioni: nome - percorso
$sections = [
'css' => 'css',
'print' => 'css',
'js' => 'js',
];
$first_lang = explode('_', $lang);
$lang_replace = [
$lang,
strtolower($lang),
strtolower($first_lang[0]),
strtoupper($first_lang[0]),
str_replace('_', '-', $lang),
str_replace('_', '-', strtolower($lang)),
];
$assets = [];
foreach ($sections as $section => $dir) {
$result = array_unique(array_merge(self::$assets[$section], $config['assets'][$section]));
foreach ($result as $key => $element) {
$element = string_starts_with($element, 'http') ? $element : $paths[$dir].'/'.$element;
if (string_contains($element, '|lang|')) {
foreach ($lang_replace as $replace) {
$name = str_replace('|lang|', $replace, $element);
if (file_exists(base_dir().str_replace(base_path_osm(), '', $name))) {
$element = $name;
break;
}
}
}
$cache_version = $version;
if (!string_starts_with($element, 'http')) {
$asset_path = base_dir().str_replace(base_path_osm(), '', $element);
if (file_exists($asset_path)) {
$cache_version .= '-'.filemtime($asset_path);
}
}
$result[$key] = $element.'?v='.$cache_version;
}
$assets[$section] = $result;
}
return $assets;
}
/**
* Restituisce il codice HTML per il form contenente il file indicato.
*
* @param string $file
* @param array $result
* @param array $options
* @param bool $disableForm
*
* @return string
*/
public static function load($file, $result, $options, $disableForm = false)
{
$form = $disableForm ? '|response|' : self::internalLoad('form.php', $result, $options);
$response = self::internalLoad($file, $result, $options);
$form = str_replace('|response|', $response, $form);
return $form;
}
/**
* Restituisce il codice HTML generato del file indicato.
*
* @param string $file
* @param array $result
* @param array $options
* @param string $directory
*
* @return string
*/
public static function internalLoad($file, $result, $options, $directory = null)
{
$module = Modules::getCurrent();
$database = $dbo = database();
$id_module = $module['id'];
$id_record = filter('id_record');
$id_plugin = $options['id_plugin'];
$directory = empty($directory) ? 'include|custom|/common/' : $directory;
ob_start();
include self::filepath($directory, $file);
$response = ob_get_clean();
return $response;
}
/**
* Individua il percorso per il file da includere considerando gli eventuali custom.
*
* @param string $path
* @param string $file
*
* @return string|null
*/
public static function filepath($path, $file = null)
{
$path = string_contains($path, base_dir()) ? $path : base_dir().'/'.ltrim($path, '/');
$path = empty($file) ? $path : rtrim($path, '/').'/'.$file;
$original_file = str_replace('|custom|', '', $path);
$custom_file = str_replace('|custom|', '/custom', $path);
$result = '';
if (file_exists($custom_file)) {
$result = $custom_file;
} elseif (file_exists($original_file)) {
$result = $original_file;
}
return slashes($result);
}
/**
* Restituisce la configurazione di default del progetto.
*
* @return array
*/
protected static function getDefaultConfig()
{
if (file_exists(base_dir().'/config.example.php')) {
include base_dir().'/config.example.php';
}
$db_host = '';
$db_username = '';
$db_password = '';
$db_name = '';
$port = '';
$lang = '';
$formatter = [
'timestamp' => 'd/m/Y H:i',
'date' => 'd/m/Y',
'time' => 'H:i',
'number' => [
'decimals' => ',',
'thousands' => '.',
],
];
return get_defined_vars();
}
}