[TASK] PHP 5.4 adjustments
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / reports / status / class.tx_reports_reports_status_configurationstatus.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Performs some checks about the install tool protection status
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage reports
32 */
33 class tx_reports_reports_status_ConfigurationStatus implements tx_reports_StatusProvider {
34
35 // 10 MB
36 protected $deprecationLogFileSizeWarningThreshold = 10485760;
37 // 100 MB
38 protected $deprecationLogFileSizeErrorThreshold = 104857600;
39
40 /**
41 * Backpath to the typo3 main directory
42 *
43 * @var string
44 */
45 protected $backPath = '../';
46
47 /**
48 * Determines the Install Tool's status, mainly concerning its protection.
49 *
50 * @return array List of statuses
51 * @see typo3/sysext/reports/interfaces/tx_reports_StatusProvider::getStatus()
52 */
53 public function getStatus() {
54 $this->executeAdminCommand();
55
56 $statuses = array(
57 'emptyReferenceIndex' => $this->getReferenceIndexStatus(),
58 'deprecationLog' => $this->getDeprecationLogStatus()
59 );
60
61 // Do not show status about non-existant features
62 if (version_compare(phpversion(), '5.4', '<')) {
63 $statuses['safeModeEnabled'] = $this->getPhpSafeModeStatus();
64 $statuses['magicQuotesGpcEnabled'] = $this->getPhpMagicQuotesGpcStatus();
65 }
66
67 if ($this->isMemcachedUsed()) {
68 $statuses['memcachedConnection'] = $this->getMemcachedConnectionStatus();
69 }
70
71 return $statuses;
72 }
73
74 /**
75 * Checks if sys_refindex is empty.
76 *
77 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether the reference index is empty or not
78 */
79 protected function getReferenceIndexStatus() {
80 $value = $GLOBALS['LANG']->getLL('status_ok');
81 $message = '';
82 $severity = tx_reports_reports_status_Status::OK;
83
84 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'sys_refindex');
85 $registry = t3lib_div::makeInstance('t3lib_Registry');
86 $lastRefIndexUpdate = $registry->get('core', 'sys_refindex_lastUpdate');
87
88 if (!$count && $lastRefIndexUpdate) {
89 $value = $GLOBALS['LANG']->getLL('status_empty');
90 $severity = tx_reports_reports_status_Status::WARNING;
91
92 $url = 'sysext/lowlevel/dbint/index.php?&id=0&SET[function]=refindex';
93 $message = sprintf(
94 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.backend_reference_index'),
95 '<a href="' . $url . '">',
96 '</a>',
97 t3lib_BeFunc::dateTime($lastRefIndexUpdate)
98 );
99 }
100 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
101 $GLOBALS['LANG']->getLL('status_referenceIndex'), $value, $message, $severity
102 );
103 }
104
105 /**
106 * Checks if PHP safe_mode is enabled.
107 *
108 * @return tx_reports_reports_status_Status A tx_reports_reports_status_Status object representing whether the safe_mode is enabled or not
109 */
110 protected function getPhpSafeModeStatus() {
111 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:disabled');
112 $message = '';
113 $severity = tx_reports_reports_status_Status::OK;
114
115 if (t3lib_utility_PhpOptions::isSafeModeEnabled()) {
116 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:enabled');
117 $severity = tx_reports_reports_status_Status::WARNING;
118 $message = $GLOBALS['LANG']->getLL('status_configuration_PhpSafeModeEnabled');
119 }
120
121 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
122 $GLOBALS['LANG']->getLL('status_PhpSafeMode'), $value, $message, $severity
123 );
124 }
125
126 /**
127 * Checks if PHP magic_quotes_gpc is enabled.
128 *
129 * @return tx_reports_reports_status_Status A tx_reports_reports_status_Status object representing whether the magic_quote_gpc is enabled or not
130 */
131 protected function getPhpMagicQuotesGpcStatus() {
132 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:disabled');
133 $message = '';
134 $severity = tx_reports_reports_status_Status::OK;
135
136 if (t3lib_utility_PhpOptions::isMagicQuotesGpcEnabled()) {
137 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:enabled');
138 $severity = tx_reports_reports_status_Status::WARNING;
139 $message = $GLOBALS['LANG']->getLL('status_configuration_PhpMagicQuotesGpcEnabled');
140 }
141
142 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
143 $GLOBALS['LANG']->getLL('status_PhpMagicQuotesGpc'), $value, $message, $severity
144 );
145 }
146
147 /**
148 * Checks whether memcached is configured, if that's the case we asume it's also used.
149 *
150 * @return boolean TRUE if memcached is used, FALSE otherwise.
151 */
152 protected function isMemcachedUsed() {
153 $memcachedUsed = FALSE;
154
155 $memcachedServers = $this->getConfiguredMemcachedServers();
156 if (count($memcachedServers)) {
157 $memcachedUsed = TRUE;
158 }
159
160 return $memcachedUsed;
161 }
162
163 /**
164 * Gets the configured memcached server connections.
165 *
166 * @return array An array of configured memcached server connections.
167 */
168 protected function getConfiguredMemcachedServers() {
169 $memcachedServers = array();
170
171 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'])) {
172 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] as $table => $conf) {
173 if (is_array($conf)) {
174 foreach ($conf as $key => $value) {
175 if (!is_array($value) && $value === 't3lib_cache_backend_MemcachedBackend') {
176 $memcachedServers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$table]['options']['servers'];
177 break;
178 }
179 }
180 }
181 }
182 }
183
184 return $memcachedServers;
185 }
186
187 /**
188 * Checks whether TYPO3 can connect to the configured memcached servers.
189 *
190 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether TYPO3 can connect to the configured memcached servers
191 */
192 protected function getMemcachedConnectionStatus() {
193 $value = $GLOBALS['LANG']->getLL('status_ok');
194 $message = '';
195 $severity = tx_reports_reports_status_Status::OK;
196
197 $failedConnections = array();
198 $defaultMemcachedPort = ini_get('memcache.default_port');
199 $memcachedServers = $this->getConfiguredMemcachedServers();
200
201 if (function_exists('memcache_connect') && is_array($memcachedServers)) {
202 foreach ($memcachedServers as $testServer) {
203 $configuredServer = $testServer;
204 if (substr($testServer, 0, 7) == 'unix://') {
205 $host = $testServer;
206 $port = 0;
207 } else {
208 if (substr($testServer, 0, 6) === 'tcp://') {
209 $testServer = substr($testServer, 6);
210 }
211 if (strstr($testServer, ':') !== FALSE) {
212 list($host, $port) = explode(':', $testServer, 2);
213 } else {
214 $host = $testServer;
215 $port = $defaultMemcachedPort;
216 }
217 }
218 $memcachedConnection = @memcache_connect($host, $port);
219 if ($memcachedConnection != NULL) {
220 memcache_close($memcachedConnection);
221 } else {
222 $failedConnections[] = $configuredServer;
223 }
224 }
225 }
226
227 if (count($failedConnections)) {
228 $value = $GLOBALS['LANG']->getLL('status_connectionFailed');
229 $severity = tx_reports_reports_status_Status::WARNING;
230
231 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.memcache_not_usable')
232 . '<br /><br />'
233 . '<ul><li>'
234 . implode('</li><li>', $failedConnections)
235 . '</li></ul>';
236 }
237
238 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
239 $GLOBALS['LANG']->getLL('status_memcachedConfiguration'), $value, $message, $severity
240 );
241 }
242
243 /**
244 * Provides status information on the deprecation log, whether it's enabled
245 * and if so whether certain limits in file size are reached.
246 *
247 * @return tx_reports_reports_status_Status The deprecation log status.
248 */
249 protected function getDeprecationLogStatus() {
250 $title = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLog');
251 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:disabled');
252 $message = '';
253 $severity = tx_reports_reports_status_Status::OK;
254
255 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog']) {
256 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:enabled');
257 $message = '<p>' . $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogEnabled') . '</p>';
258 $severity = tx_reports_reports_status_Status::NOTICE;
259
260 $logFile = t3lib_div::getDeprecationLogFileName();
261 $logFileSize = 0;
262
263 if (@file_exists($logFile)) {
264 $logFileSize = filesize($logFile);
265
266 $message .= '<p>' . sprintf(
267 $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogFile'),
268 $this->getDeprecationLogFileLink()
269 ) . '</p>';
270
271 $removeDeprecationLogFileUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=removeDeprecationLogFile';
272 $message .= '<p>' . sprintf(
273 $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogSize'),
274 t3lib_div::formatSize($logFileSize)
275 )
276 . ' <a href="' . $removeDeprecationLogFileUrl . '">' . $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeleteLink') . '</a></p>';
277 }
278
279 if ($logFileSize > $this->deprecationLogFileSizeWarningThreshold) {
280 $severity = tx_reports_reports_status_Status::WARNING;
281 }
282
283 if ($logFileSize > $this->deprecationLogFileSizeErrorThreshold) {
284 $severity = tx_reports_reports_status_Status::ERROR;
285 }
286 }
287
288 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
289 $title, $value, $message, $severity
290 );
291 }
292
293 /**
294 * Creates a link to the deprecation log file with the absolute path as the
295 * link text.
296 *
297 * @return string Link to the deprecation log file
298 */
299 protected function getDeprecationLogFileLink() {
300 $logFile = t3lib_div::getDeprecationLogFileName();
301 $relativePath = t3lib_div::resolveBackPath(
302 $this->backPath . substr($logFile, strlen(PATH_site))
303 );
304 $link = '<a href="' . $relativePath . '">' . $logFile . '</a>';
305
306 return $link;
307 }
308
309 /**
310 * Executes admin commands.
311 *
312 * Currently implemented commands are:
313 * - Remove deprecation log file
314 *
315 * @return void
316 */
317 protected function executeAdminCommand() {
318 $command = t3lib_div::_GET('adminCmd');
319
320 switch ($command) {
321 case 'removeDeprecationLogFile':
322 self::removeDeprecationLogFile();
323 break;
324 default:
325 // intentionally left blank
326 break;
327 }
328 }
329
330 /**
331 * Remove deprecation log file.
332 *
333 * @return void
334 */
335 protected static function removeDeprecationLogFile() {
336 if (@unlink(t3lib_div::getDeprecationLogFileName())) {
337 $message = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeletedSuccessful');
338 $severity = t3lib_FlashMessage::OK;
339 } else {
340 $message = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeletionFailed');
341 $severity = t3lib_FlashMessage::ERROR;
342 }
343 t3lib_FlashMessageQueue::addMessage(t3lib_div::makeInstance(
344 't3lib_FlashMessage',
345 $message,
346 '',
347 $severity,
348 TRUE
349 ));
350 }
351 }
352
353 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/reports/status/class.tx_reports_reports_status_configurationstatus.php'])) {
354 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/reports/status/class.tx_reports_reports_status_configurationstatus.php']);
355 }
356
357 ?>