eb3c0fe86139b2088af5ee75e6793633577619c0
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / SystemStatus.php
1 <?php
2 namespace TYPO3\CMS\Reports\Report\Status;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Performs several checks about the system's health
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 */
31 class SystemStatus implements \TYPO3\CMS\Reports\StatusProviderInterface {
32
33 /**
34 * Determines the Install Tool's status, mainly concerning its protection.
35 *
36 * @return array List of statuses
37 */
38 public function getStatus() {
39 $this->executeAdminCommand();
40 $statuses = array(
41 'PhpPeakMemory' => $this->getPhpPeakMemoryStatus(),
42 'PhpModules' => $this->getMissingPhpModulesOfExtensions()
43 );
44 return $statuses;
45 }
46
47 /**
48 * Executes commands like clearing the memory status flag
49 *
50 * @return void
51 */
52 protected function executeAdminCommand() {
53 $command = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('adminCmd');
54 switch ($command) {
55 case 'clear_peak_memory_usage_flag':
56 /** @var $registry \TYPO3\CMS\Core\Registry */
57 $registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
58 $registry->remove('core', 'reports-peakMemoryUsage');
59 break;
60 default:
61 // Do nothing
62 }
63 }
64
65 /**
66 * Checks if there was a request in the past which approached the memory limit
67 *
68 * @return \TYPO3\CMS\Reports\Status A status of whether the memory limit was approached by one of the requests
69 */
70 protected function getPhpPeakMemoryStatus() {
71 /** @var $registry \TYPO3\CMS\Core\Registry */
72 $registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
73 $peakMemoryUsage = $registry->get('core', 'reports-peakMemoryUsage');
74 $memoryLimit = \TYPO3\CMS\Core\Utility\GeneralUtility::getBytesFromSizeMeasurement(ini_get('memory_limit'));
75 $value = $GLOBALS['LANG']->getLL('status_ok');
76 $message = '';
77 $severity = \TYPO3\CMS\Reports\Status::OK;
78 $bytesUsed = $peakMemoryUsage['used'];
79 $percentageUsed = $memoryLimit ? number_format($bytesUsed / $memoryLimit * 100, 1) . '%' : '?';
80 $dateOfPeak = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'], $peakMemoryUsage['tstamp']);
81 $urlOfPeak = '<a href="' . htmlspecialchars($peakMemoryUsage['url']) . '">' . htmlspecialchars($peakMemoryUsage['url']) . '</a>';
82 $clearFlagUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=clear_peak_memory_usage_flag';
83 if ($peakMemoryUsage['used']) {
84 $message = sprintf($GLOBALS['LANG']->getLL('status_phpPeakMemoryTooHigh'), \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($peakMemoryUsage['used']), $percentageUsed, \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize($memoryLimit), $dateOfPeak, $urlOfPeak);
85 $message .= ' <a href="' . $clearFlagUrl . '">' . $GLOBALS['LANG']->getLL('status_phpPeakMemoryClearFlag') . '</a>.';
86 $severity = \TYPO3\CMS\Reports\Status::WARNING;
87 $value = $percentageUsed;
88 }
89 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $GLOBALS['LANG']->getLL('status_phpPeakMemory'), $value, $message, $severity);
90 }
91
92 /**
93 * Reports whether extensions need additional PHP modules different from standard core requirements
94 *
95 * @return \TYPO3\CMS\Reports\Status A status of missing PHP modules
96 */
97 protected function getMissingPhpModulesOfExtensions() {
98 $modules = array();
99 if (is_array(${$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install/mod/class.tx_install.php']['requiredPhpModules']})) {
100 foreach (${$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install/mod/class.tx_install.php']['requiredPhpModules']} as $classData) {
101 $hookObject = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($classData);
102 $modules = $hookObject->setRequiredPhpModules($modules, $this);
103 }
104 }
105 $missingPhpModules = array();
106 foreach ($modules as $module) {
107 if (is_array($module)) {
108 $detectedSubmodules = FALSE;
109 foreach ($module as $submodule) {
110 if (extension_loaded($submodule)) {
111 $detectedSubmodules = TRUE;
112 }
113 }
114 if ($detectedSubmodules === FALSE) {
115 $missingPhpModules[] = sprintf($GLOBALS['LANG']->getLL('status_phpModulesGroup'), '(' . implode(', ', $module) . ')');
116 }
117 } elseif (!extension_loaded($module)) {
118 $missingPhpModules[] = $module;
119 }
120 }
121 if (count($missingPhpModules) > 0) {
122 $value = $GLOBALS['LANG']->getLL('status_phpModulesMissing');
123 $message = sprintf($GLOBALS['LANG']->getLL('status_phpModulesList'), implode(', ', $missingPhpModules));
124 $message .= ' ' . $GLOBALS['LANG']->getLL('status_phpModulesInfo');
125 $severity = \TYPO3\CMS\Reports\Status::ERROR;
126 } else {
127 $value = $GLOBALS['LANG']->getLL('status_phpModulesPresent');
128 $message = '';
129 $severity = \TYPO3\CMS\Reports\Status::OK;
130 }
131 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $GLOBALS['LANG']->getLL('status_phpModules'), $value, $message, $severity);
132 }
133
134 }