-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
System.php
690 lines (598 loc) · 20.2 KB
/
System.php
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\CoreBundle\Config\Loader\XliffFileLoader;
use Contao\CoreBundle\Translation\MessageCatalogue;
use Contao\CoreBundle\Util\LocaleUtil;
use Contao\Database\Installer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Abstract library base class
*
* @property Automator $Automator The automator object
* @property Config $Config The config object
* @property Database $Database The database object
* @property Environment $Environment The environment object
* @property Files $Files The files object
* @property Input $Input The input object
* @property Installer $Installer The database installer object
* @property Messages $Messages The messages object
* @property Session $Session The session object
* @property BackendUser|FrontendUser $User The user object
*/
abstract class System
{
/**
* Container
* @var ContainerInterface
*/
protected static $objContainer;
/**
* @var array|null
*/
private static $removedServiceIds;
/**
* Default libraries
* @var array
*/
protected $arrObjects = array();
/**
* Static objects
* @var array
*/
protected static $arrStaticObjects = array();
/**
* Singletons
* @var array
*/
protected static $arrSingletons = array();
/**
* Available languages
* @var array
*/
protected static $arrLanguages = array();
/**
* Loaded language files
* @var array
*/
protected static $arrLanguageFiles = array();
/**
* Available image sizes
* @var array
*/
protected static $arrImageSizes = array();
/**
* Available language files
* @var array|false|null
*/
protected static $arrAvailableLanguageFiles;
/**
* Import the Config instance
*/
protected function __construct()
{
$this->import(Config::class, 'Config'); // backwards compatibility
}
/**
* Get an object property
*
* @param string $strKey The property name
*
* @return mixed|null The property value or null
*/
public function __get($strKey)
{
if (!isset($this->arrObjects[$strKey]))
{
return null;
}
trigger_deprecation('contao/core-bundle', '5.2', 'Using objects that have been imported via "Contao\System::import()" has been deprecated and will no longer work in Contao 6. Use "Contao\System::importStatic()" or dependency injection instead.');
return $this->arrObjects[$strKey];
}
/**
* Import a library and make it accessible by its name or an optional key
*
* @param string|object $strClass The class name
* @param string|object $strKey An optional key to store the object under
* @param boolean $blnForce If true, existing objects will be overridden
*
* @throws ServiceNotFoundException
*/
protected function import($strClass, $strKey=null, $blnForce=false)
{
$strKey = $strKey ?: $strClass;
if (\is_object($strKey))
{
$strKey = \get_class($strClass);
}
if ($blnForce || !isset($this->arrObjects[$strKey]))
{
$container = static::getContainer();
if (null === $container)
{
throw new \RuntimeException('The Symfony container is not available, did you initialize the Contao framework?');
}
if (\is_object($strClass))
{
$this->arrObjects[$strKey] = $strClass;
}
elseif (isset(static::$arrSingletons[$strClass]))
{
$this->arrObjects[$strKey] = static::$arrSingletons[$strClass];
}
elseif ($container->has($strClass) && (str_contains($strClass, '\\') || !class_exists($strClass)))
{
$this->arrObjects[$strKey] = $container->get($strClass);
}
elseif (($container->getParameter('kernel.debug') || !class_exists($strClass)) && self::isServiceInlined($strClass))
{
// In debug mode, we check for inlined services before trying to create a new instance of the class
throw new ServiceNotFoundException($strClass, null, null, array(), sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $strClass));
}
elseif (!class_exists($strClass))
{
throw new \RuntimeException('System::import() failed because class "' . $strClass . '" is not a valid class name or does not exist.');
}
elseif (\in_array('getInstance', get_class_methods($strClass)))
{
$this->arrObjects[$strKey] = static::$arrSingletons[$strClass] = \call_user_func(array($strClass, 'getInstance'));
}
else
{
try
{
$this->arrObjects[$strKey] = new $strClass();
}
catch (\Throwable $t)
{
if (!$container->getParameter('kernel.debug') && self::isServiceInlined($strClass))
{
throw new ServiceNotFoundException($strClass, null, null, array(), sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $strClass));
}
throw $t;
}
}
}
}
/**
* Import a library in non-object context
*
* @param string|object $strClass The class name
* @param string|object $strKey An optional key to store the object under
* @param boolean $blnForce If true, existing objects will be overridden
*
* @throws ServiceNotFoundException
*
* @return object The imported object
*/
public static function importStatic($strClass, $strKey=null, $blnForce=false)
{
$strKey = $strKey ?: $strClass;
if (\is_object($strKey))
{
$strKey = \get_class($strClass);
}
if ($blnForce || !isset(static::$arrStaticObjects[$strKey]))
{
$container = static::getContainer();
if (null === $container)
{
throw new \RuntimeException('The Symfony container is not available, did you initialize the Contao framework?');
}
if (\is_object($strClass))
{
static::$arrStaticObjects[$strKey] = $strClass;
}
elseif (isset(static::$arrSingletons[$strClass]))
{
static::$arrStaticObjects[$strKey] = static::$arrSingletons[$strClass];
}
elseif ($container->has($strClass) && (str_contains($strClass, '\\') || !class_exists($strClass)))
{
static::$arrStaticObjects[$strKey] = $container->get($strClass);
}
elseif (($container->getParameter('kernel.debug') || !class_exists($strClass)) && self::isServiceInlined($strClass))
{
// In debug mode, we check for inlined services before trying to create a new instance of the class
throw new ServiceNotFoundException($strClass, null, null, array(), sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $strClass));
}
elseif (!class_exists($strClass))
{
throw new \RuntimeException('System::importStatic() failed because class "' . $strClass . '" is not a valid class name or does not exist.');
}
elseif (\in_array('getInstance', get_class_methods($strClass)))
{
static::$arrStaticObjects[$strKey] = static::$arrSingletons[$strClass] = \call_user_func(array($strClass, 'getInstance'));
}
else
{
try
{
static::$arrStaticObjects[$strKey] = new $strClass();
}
catch (\Throwable $t)
{
if (!$container->getParameter('kernel.debug') && self::isServiceInlined($strClass))
{
throw new ServiceNotFoundException($strClass, null, null, array(), sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $strClass));
}
throw $t;
}
}
}
return static::$arrStaticObjects[$strKey];
}
private static function isServiceInlined($strClass)
{
$container = static::getContainer();
if (!$container instanceof Container)
{
return false;
}
if (null === self::$removedServiceIds)
{
self::$removedServiceIds = $container->getRemovedIds();
}
return isset(self::$removedServiceIds[$strClass]);
}
/**
* Return the container object
*
* @return ContainerInterface The container object
*/
public static function getContainer()
{
return static::$objContainer;
}
/**
* Set the container object
*
* @param ContainerInterface $container The container object
*/
public static function setContainer(ContainerInterface $container)
{
static::$objContainer = $container;
}
/**
* Return the referer URL and optionally encode ampersands
*
* @param boolean $blnEncodeAmpersands If true, ampersands will be encoded
* @param string $strTable An optional table name
*
* @return string The referer URL
*/
public static function getReferer($blnEncodeAmpersands=false, $strTable=null)
{
$objSession = static::getContainer()->get('request_stack')->getSession();
$ref = Input::get('ref');
$key = Input::get('popup') ? 'popupReferer' : 'referer';
$session = $objSession->get($key);
$return = null;
$request = static::getContainer()->get('request_stack')->getCurrentRequest();
$isBackend = $request && static::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request);
$isFrontend = $request && static::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest($request);
if (null !== $session)
{
// Unique referer ID
if ($ref && isset($session[$ref]))
{
$session = $session[$ref];
}
elseif ($isBackend && \is_array($session))
{
$session = end($session);
}
// Use a specific referer
if ($strTable && isset($session[$strTable]) && Input::get('act') != 'select')
{
$session['current'] = $session[$strTable];
}
// Remove parameters helper
$cleanUrl = static function ($url, $params = array('rt', 'ref', 'revise')) {
if (!$url || !str_contains($url, '?'))
{
return $url;
}
list($path, $query) = explode('?', $url, 2);
parse_str($query, $pairs);
foreach ($params as $param)
{
unset($pairs[$param]);
}
if (empty($pairs))
{
return $path;
}
return $path . '?' . http_build_query($pairs, '', '&', PHP_QUERY_RFC3986);
};
// Determine current or last
$strUrl = ($cleanUrl($session['current'] ?? null) != $cleanUrl(Environment::get('requestUri'))) ? ($session['current'] ?? null) : ($session['last'] ?? null);
// Remove the "toggle" and "toggle all" parameters
$return = $cleanUrl($strUrl, array('tg', 'ptg'));
}
// Fallback to the generic referer in the front end
if (!$return && $isFrontend)
{
$return = Environment::get('httpReferer');
}
// Fallback to the current URL if there is no referer
if (!$return)
{
if ($isBackend)
{
$return = static::getContainer()->get('router')->generate('contao_backend');
}
else
{
$return = Environment::get('url');
}
}
// Do not urldecode here!
return preg_replace('/&(amp;)?/i', $blnEncodeAmpersands ? '&' : '&', $return);
}
/**
* Load a set of language files
*
* @param string $strName The table name
* @param string|null $strLanguage An optional language code
* @param boolean $blnNoCache If true, the cache will be bypassed
*/
public static function loadLanguageFile($strName, $strLanguage=null, $blnNoCache=false)
{
if ($strLanguage === null)
{
$strLanguage = LocaleUtil::formatAsLocale($GLOBALS['TL_LANGUAGE'] ?? 'en');
}
// Fall back to English
if (!$strLanguage)
{
$strLanguage = 'en';
}
if (1 !== preg_match('/^[a-z0-9_-]+$/i', $strName))
{
throw new \InvalidArgumentException(sprintf('Invalid language file name "%s"', $strName));
}
// Return if the language file has been loaded already
if (!$blnNoCache && array_key_last(static::$arrLanguageFiles[$strName] ?? array()) === $strLanguage)
{
return;
}
$strCacheKey = $strLanguage;
// Make sure the language exists
if ($strLanguage != 'en' && !static::isInstalledLanguage($strLanguage))
{
$strShortLang = substr($strLanguage, 0, 2);
// Fall back to "de" if "de_DE" does not exist
if ($strShortLang != $strLanguage && static::isInstalledLanguage($strShortLang))
{
$strLanguage = $strShortLang;
}
// Fall back to English (see #6581)
else
{
$strLanguage = 'en';
}
}
// Unset to move the new array key to the last position
unset(static::$arrLanguageFiles[$strName][$strCacheKey]);
// Use a global cache variable to support nested calls
static::$arrLanguageFiles[$strName][$strCacheKey] = $strLanguage;
// Fall back to English
$arrCreateLangs = ($strLanguage == 'en') ? array('en') : array('en', $strLanguage);
// Prepare the XLIFF loader
$container = static::getContainer();
$xlfLoader = new XliffFileLoader($container->getParameter('kernel.project_dir'), true);
$strCacheDir = $container->getParameter('kernel.cache_dir');
if (null === self::$arrAvailableLanguageFiles)
{
$availLangFilesPath = Path::join($strCacheDir, 'contao/config/available-language-files.php');
self::$arrAvailableLanguageFiles = file_exists($availLangFilesPath) ? include $availLangFilesPath : false;
}
// Load the language(s)
foreach ($arrCreateLangs as $strCreateLang)
{
// Skip languages that are not available (#6454)
if (\is_array(self::$arrAvailableLanguageFiles) && !isset(self::$arrAvailableLanguageFiles[$strCreateLang][$strName]))
{
continue;
}
// Try to load from cache
if (file_exists($strCacheDir . '/contao/languages/' . $strCreateLang . '/' . $strName . '.php'))
{
include $strCacheDir . '/contao/languages/' . $strCreateLang . '/' . $strName . '.php';
}
else
{
// Find the given filename either as .php or .xlf file
$finder = $container->get('contao.resource_finder')->findIn('languages/' . $strCreateLang)->name('/^' . $strName . '\.(php|xlf)$/');
/** @var SplFileInfo $file */
foreach ($finder as $file)
{
switch ($file->getExtension())
{
case 'php':
include $file;
break;
case 'xlf':
$xlfLoader->load($file, $strCreateLang);
break;
default:
throw new \RuntimeException(sprintf('Invalid language file extension: %s', $file->getExtension()));
}
}
// Also populate $GLOBALS['TL_LANG'] with the Symfony translations of the 'contao_' domains.
$catalogue = $container->get('translator', ContainerInterface::NULL_ON_INVALID_REFERENCE)?->getCatalogue($strLanguage);
if ($catalogue instanceof MessageCatalogue)
{
$catalogue->populateGlobals('contao_' . $strName);
}
}
}
// Set MSC.textDirection (see #3360)
if ('default' === $strName)
{
$GLOBALS['TL_LANG']['MSC']['textDirection'] = (\ResourceBundle::create($strLanguage, 'ICUDATA', true)['layout']['characters'] ?? null) === 'right-to-left' ? 'rtl' : 'ltr';
}
// HOOK: allow loading custom labels
if (isset($GLOBALS['TL_HOOKS']['loadLanguageFile']) && \is_array($GLOBALS['TL_HOOKS']['loadLanguageFile']))
{
foreach ($GLOBALS['TL_HOOKS']['loadLanguageFile'] as $callback)
{
static::importStatic($callback[0])->{$callback[1]}($strName, $strLanguage, $strCacheKey);
}
}
// Handle single quotes in the deleteConfirm message
if ($strName == 'default' && isset($GLOBALS['TL_LANG']['MSC']['deleteConfirm']))
{
$GLOBALS['TL_LANG']['MSC']['deleteConfirm'] = str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['deleteConfirm']);
}
}
/**
* Check whether a language is installed
*
* @param boolean $strLanguage The language code
*
* @return boolean True if the language is installed
*/
public static function isInstalledLanguage($strLanguage)
{
if (!isset(static::$arrLanguages[$strLanguage]))
{
if (LocaleUtil::canonicalize($strLanguage) !== $strLanguage)
{
return false;
}
$projectDir = self::getContainer()->getParameter('kernel.project_dir');
if (is_dir($projectDir . '/vendor/contao/core-bundle/contao/languages/' . $strLanguage))
{
static::$arrLanguages[$strLanguage] = true;
}
elseif (is_dir(static::getContainer()->getParameter('kernel.cache_dir') . '/contao/languages/' . $strLanguage))
{
static::$arrLanguages[$strLanguage] = true;
}
else
{
static::$arrLanguages[$strLanguage] = static::getContainer()->get('contao.resource_finder')->findIn('languages')->depth(0)->directories()->name($strLanguage)->hasResults();
}
}
return static::$arrLanguages[$strLanguage];
}
/**
* Urlencode a file path preserving slashes
*
* @param string $strPath The file path
*
* @return string The encoded file path
*/
public static function urlEncode($strPath)
{
return str_replace('%2F', '/', rawurlencode((string) $strPath));
}
/**
* Set a cookie
*
* @param string $strName The cookie name
* @param mixed $varValue The cookie value
* @param integer $intExpires The expiration date
* @param string|null $strPath An optional path
* @param string|null $strDomain An optional domain name
* @param boolean|null $blnSecure If true, the secure flag will be set
* @param boolean $blnHttpOnly If true, the http-only flag will be set
*/
public static function setCookie($strName, $varValue, $intExpires, $strPath=null, $strDomain=null, $blnSecure=null, $blnHttpOnly=false)
{
trigger_deprecation('contao/core-bundle', '5.3', 'Using "Contao\System::setCookie()" has been deprecated and will no longer work in Contao 6. Use Symfony\'s HttpFoundation and kernel.response events instead.');
if (!$strPath)
{
$strPath = Environment::get('path') ?: '/'; // see #4390
}
if ($blnSecure === null)
{
$blnSecure = false;
if ($request = static::getContainer()->get('request_stack')->getCurrentRequest())
{
$blnSecure = $request->isSecure();
}
}
$objCookie = new \stdClass();
$objCookie->strName = $strName;
$objCookie->varValue = $varValue;
$objCookie->intExpires = $intExpires;
$objCookie->strPath = $strPath;
$objCookie->strDomain = $strDomain;
$objCookie->blnSecure = $blnSecure;
$objCookie->blnHttpOnly = $blnHttpOnly;
// HOOK: allow adding custom logic
if (isset($GLOBALS['TL_HOOKS']['setCookie']) && \is_array($GLOBALS['TL_HOOKS']['setCookie']))
{
trigger_deprecation('contao/core-bundle', '5.3', 'Using the "setCookie" hook has been deprecated and will no longer work in Contao 6. Use the kernel.response events instead.');
foreach ($GLOBALS['TL_HOOKS']['setCookie'] as $callback)
{
$objCookie = static::importStatic($callback[0])->{$callback[1]}($objCookie);
}
}
setcookie($objCookie->strName, $objCookie->varValue, $objCookie->intExpires, $objCookie->strPath, $objCookie->strDomain, $objCookie->blnSecure, $objCookie->blnHttpOnly);
}
/**
* Convert a byte value into a human-readable format
*
* @param integer $intSize The size in bytes
* @param integer $intDecimals The number of decimals to show
*
* @return string The human-readable size
*/
public static function getReadableSize($intSize, $intDecimals=1)
{
for ($i=0; $intSize>=1024; $i++)
{
$intSize /= 1024;
}
return static::getFormattedNumber($intSize, $intDecimals) . ' ' . $GLOBALS['TL_LANG']['UNITS'][$i];
}
/**
* Format a number
*
* @param mixed $varNumber An integer or float number
* @param integer $intDecimals The number of decimals to show
*
* @return mixed The formatted number
*/
public static function getFormattedNumber($varNumber, $intDecimals=2)
{
return number_format(round($varNumber, $intDecimals), $intDecimals, $GLOBALS['TL_LANG']['MSC']['decimalSeparator'], $GLOBALS['TL_LANG']['MSC']['thousandsSeparator']);
}
/**
* Anonymize an IP address by overriding the last chunk
*
* @param string $strIp The IP address
*
* @return string The encoded IP address
*/
public static function anonymizeIp($strIp)
{
// Localhost
if ($strIp == '127.0.0.1' || $strIp == '::1')
{
return $strIp;
}
// IPv6
if (str_contains($strIp, ':'))
{
return substr_replace($strIp, ':0000', strrpos($strIp, ':'));
}
// IPv4
return substr_replace($strIp, '.0', strrpos($strIp, '.'));
}
}