[CLEANUP] Code cleanup in ext:reports
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / SystemStatus.php
1 <?php
2 namespace TYPO3\CMS\Reports\Report\Status;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Registry;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Lang\LanguageService;
19 use TYPO3\CMS\Reports\Status;
20 use TYPO3\CMS\Reports\StatusProviderInterface;
21
22 /**
23 * Performs several checks about the system's health
24 */
25 class SystemStatus implements StatusProviderInterface
26 {
27 /**
28 * Determines the Install Tool's status, mainly concerning its protection.
29 *
30 * @return array List of statuses
31 */
32 public function getStatus()
33 {
34 $this->executeAdminCommand();
35 $statuses = array(
36 'PhpPeakMemory' => $this->getPhpPeakMemoryStatus(),
37 'PhpModules' => $this->getMissingPhpModulesOfExtensions()
38 );
39 return $statuses;
40 }
41
42 /**
43 * Executes commands like clearing the memory status flag
44 *
45 * @return void
46 */
47 protected function executeAdminCommand()
48 {
49 $command = GeneralUtility::_GET('adminCmd');
50 switch ($command) {
51 case 'clear_peak_memory_usage_flag':
52 /** @var Registry $registry */
53 $registry = GeneralUtility::makeInstance(Registry::class);
54 $registry->remove('core', 'reports-peakMemoryUsage');
55 break;
56 default:
57 // Do nothing
58 }
59 }
60
61 /**
62 * Checks if there was a request in the past which approached the memory limit
63 *
64 * @return \TYPO3\CMS\Reports\Status A status of whether the memory limit was approached by one of the requests
65 */
66 protected function getPhpPeakMemoryStatus()
67 {
68 /** @var Registry $registry */
69 $registry = GeneralUtility::makeInstance(Registry::class);
70 $peakMemoryUsage = $registry->get('core', 'reports-peakMemoryUsage');
71 $memoryLimit = GeneralUtility::getBytesFromSizeMeasurement(ini_get('memory_limit'));
72 $value = $this->getLanguageService()->getLL('status_ok');
73 $message = '';
74 $severity = Status::OK;
75 $bytesUsed = $peakMemoryUsage['used'];
76 $percentageUsed = $memoryLimit ? number_format($bytesUsed / $memoryLimit * 100, 1) . '%' : '?';
77 $dateOfPeak = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'], $peakMemoryUsage['tstamp']);
78 $urlOfPeak = '<a href="' . htmlspecialchars($peakMemoryUsage['url']) . '">' . htmlspecialchars($peakMemoryUsage['url']) . '</a>';
79 $clearFlagUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=clear_peak_memory_usage_flag';
80 if ($peakMemoryUsage['used']) {
81 $message = sprintf($this->getLanguageService()->getLL('status_phpPeakMemoryTooHigh'), GeneralUtility::formatSize($peakMemoryUsage['used']), $percentageUsed, GeneralUtility::formatSize($memoryLimit), $dateOfPeak, $urlOfPeak);
82 $message .= ' <a href="' . $clearFlagUrl . '">' . $this->getLanguageService()->getLL('status_phpPeakMemoryClearFlag') . '</a>.';
83 $severity = Status::WARNING;
84 $value = $percentageUsed;
85 }
86 return GeneralUtility::makeInstance(Status::class, $this->getLanguageService()->getLL('status_phpPeakMemory'), $value, $message, $severity);
87 }
88
89 /**
90 * Reports whether extensions need additional PHP modules different from standard core requirements
91 *
92 * @return \TYPO3\CMS\Reports\Status A status of missing PHP modules
93 */
94 protected function getMissingPhpModulesOfExtensions()
95 {
96 $modules = array();
97 if (is_array(${$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install/mod/class.tx_install.php']['requiredPhpModules']})) {
98 foreach (${$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install/mod/class.tx_install.php']['requiredPhpModules']} as $classData) {
99 $hookObject = GeneralUtility::getUserObj($classData);
100 $modules = $hookObject->setRequiredPhpModules($modules, $this);
101 }
102 }
103 $missingPhpModules = array();
104 foreach ($modules as $module) {
105 if (is_array($module)) {
106 $detectedSubmodules = false;
107 foreach ($module as $submodule) {
108 if (extension_loaded($submodule)) {
109 $detectedSubmodules = true;
110 }
111 }
112 if ($detectedSubmodules === false) {
113 $missingPhpModules[] = sprintf($this->getLanguageService()->getLL('status_phpModulesGroup'), '(' . implode(', ', $module) . ')');
114 }
115 } elseif (!extension_loaded($module)) {
116 $missingPhpModules[] = $module;
117 }
118 }
119 if (!empty($missingPhpModules)) {
120 $value = $this->getLanguageService()->getLL('status_phpModulesMissing');
121 $message = sprintf($this->getLanguageService()->getLL('status_phpModulesList'), implode(', ', $missingPhpModules));
122 $message .= ' ' . $this->getLanguageService()->getLL('status_phpModulesInfo');
123 $severity = Status::ERROR;
124 } else {
125 $value = $this->getLanguageService()->getLL('status_phpModulesPresent');
126 $message = '';
127 $severity = Status::OK;
128 }
129 return GeneralUtility::makeInstance(Status::class, $this->getLanguageService()->getLL('status_phpModules'), $value, $message, $severity);
130 }
131
132 /**
133 * @return LanguageService
134 */
135 protected function getLanguageService()
136 {
137 return $GLOBALS['LANG'];
138 }
139 }