[FEATURE] Add a link to delete the deprecation log file
[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 'safeModeEnabled' => $this->getPhpSafeModeStatus(),
60 'magicQuotesGpcEnabled' => $this->getPhpMagicQuotesGpcStatus(),
61 );
62
63 if ($this->isMemcachedUsed()) {
64 $statuses['memcachedConnection'] = $this->getMemcachedConnectionStatus();
65 }
66
67 return $statuses;
68 }
69
70 /**
71 * Checks if sys_refindex is empty.
72 *
73 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether the reference index is empty or not
74 */
75 protected function getReferenceIndexStatus() {
76 $value = $GLOBALS['LANG']->getLL('status_ok');
77 $message = '';
78 $severity = tx_reports_reports_status_Status::OK;
79
80 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'sys_refindex');
81 $registry = t3lib_div::makeInstance('t3lib_Registry');
82 $lastRefIndexUpdate = $registry->get('core', 'sys_refindex_lastUpdate');
83
84 if (!$count && $lastRefIndexUpdate) {
85 $value = $GLOBALS['LANG']->getLL('status_empty');
86 $severity = tx_reports_reports_status_Status::WARNING;
87
88 $url = 'sysext/lowlevel/dbint/index.php?&id=0&SET[function]=refindex';
89 $message = sprintf(
90 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.backend_reference_index'),
91 '<a href="' . $url . '">',
92 '</a>',
93 t3lib_BeFunc::dateTime($lastRefIndexUpdate)
94 );
95 }
96 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
97 $GLOBALS['LANG']->getLL('status_referenceIndex'), $value, $message, $severity
98 );
99 }
100
101 /**
102 * Checks if PHP safe_mode is enabled.
103 *
104 * @return tx_reports_reports_status_Status A tx_reports_reports_status_Status object representing whether the safe_mode is enabled or not
105 */
106 protected function getPhpSafeModeStatus() {
107 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:disabled');
108 $message = '';
109 $severity = tx_reports_reports_status_Status::OK;
110
111 if (t3lib_utility_PhpOptions::isSafeModeEnabled()) {
112 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:enabled');
113 $severity = tx_reports_reports_status_Status::WARNING;
114 $message = $GLOBALS['LANG']->getLL('status_configuration_PhpSafeModeEnabled');
115 }
116
117 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
118 $GLOBALS['LANG']->getLL('status_PhpSafeMode'), $value, $message, $severity
119 );
120 }
121
122 /**
123 * Checks if PHP magic_quotes_gpc is enabled.
124 *
125 * @return tx_reports_reports_status_Status A tx_reports_reports_status_Status object representing whether the magic_quote_gpc is enabled or not
126 */
127 protected function getPhpMagicQuotesGpcStatus() {
128 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:disabled');
129 $message = '';
130 $severity = tx_reports_reports_status_Status::OK;
131
132 if (t3lib_utility_PhpOptions::isMagicQuotesGpcEnabled()) {
133 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:enabled');
134 $severity = tx_reports_reports_status_Status::WARNING;
135 $message = $GLOBALS['LANG']->getLL('status_configuration_PhpMagicQuotesGpcEnabled');
136 }
137
138 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
139 $GLOBALS['LANG']->getLL('status_PhpMagicQuotesGpc'), $value, $message, $severity
140 );
141 }
142
143 /**
144 * Checks whether memcached is configured, if that's the case we asume it's also used.
145 *
146 * @return boolean TRUE if memcached is used, FALSE otherwise.
147 */
148 protected function isMemcachedUsed() {
149 $memcachedUsed = FALSE;
150
151 $memcachedServers = $this->getConfiguredMemcachedServers();
152 if (count($memcachedServers)) {
153 $memcachedUsed = TRUE;
154 }
155
156 return $memcachedUsed;
157 }
158
159 /**
160 * Gets the configured memcached server connections.
161 *
162 * @return array An array of configured memcached server connections.
163 */
164 protected function getConfiguredMemcachedServers() {
165 $memcachedServers = array();
166
167 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'])) {
168 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] as $table => $conf) {
169 if (is_array($conf)) {
170 foreach ($conf as $key => $value) {
171 if (!is_array($value) && $value === 't3lib_cache_backend_MemcachedBackend') {
172 $memcachedServers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$table]['options']['servers'];
173 break;
174 }
175 }
176 }
177 }
178 }
179
180 return $memcachedServers;
181 }
182
183 /**
184 * Checks whether TYPO3 can connect to the configured memcached servers.
185 *
186 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether TYPO3 can connect to the configured memcached servers
187 */
188 protected function getMemcachedConnectionStatus() {
189 $value = $GLOBALS['LANG']->getLL('status_ok');
190 $message = '';
191 $severity = tx_reports_reports_status_Status::OK;
192
193 $failedConnections = array();
194 $defaultMemcachedPort = ini_get('memcache.default_port');
195 $memcachedServers = $this->getConfiguredMemcachedServers();
196
197 if (function_exists('memcache_connect') && is_array($memcachedServers)) {
198 foreach ($memcachedServers as $testServer) {
199 $configuredServer = $testServer;
200 if (substr($testServer, 0, 7) == 'unix://') {
201 $host = $testServer;
202 $port = 0;
203 } else {
204 if (substr($testServer, 0, 6) === 'tcp://') {
205 $testServer = substr($testServer, 6);
206 }
207 if (strstr($testServer, ':') !== FALSE) {
208 list($host, $port) = explode(':', $testServer, 2);
209 } else {
210 $host = $testServer;
211 $port = $defaultMemcachedPort;
212 }
213 }
214 $memcachedConnection = @memcache_connect($host, $port);
215 if ($memcachedConnection != NULL) {
216 memcache_close($memcachedConnection);
217 } else {
218 $failedConnections[] = $configuredServer;
219 }
220 }
221 }
222
223 if (count($failedConnections)) {
224 $value = $GLOBALS['LANG']->getLL('status_connectionFailed');
225 $severity = tx_reports_reports_status_Status::WARNING;
226
227 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.memcache_not_usable')
228 . '<br /><br />'
229 . '<ul><li>'
230 . implode('</li><li>', $failedConnections)
231 . '</li></ul>';
232 }
233
234 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
235 $GLOBALS['LANG']->getLL('status_memcachedConfiguration'), $value, $message, $severity
236 );
237 }
238
239 /**
240 * Provides status information on the deprecation log, whether it's enabled
241 * and if so whether certain limits in file size are reached.
242 *
243 * @return tx_reports_reports_status_Status The deprecation log status.
244 */
245 protected function getDeprecationLogStatus() {
246 $title = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLog');
247 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:disabled');
248 $message = '';
249 $severity = tx_reports_reports_status_Status::OK;
250
251 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog']) {
252 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xml:enabled');
253 $message = '<p>' . $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogEnabled') . '</p>';
254 $severity = tx_reports_reports_status_Status::NOTICE;
255
256 $logFile = t3lib_div::getDeprecationLogFileName();
257 $logFileSize = 0;
258
259 if (@file_exists($logFile)) {
260 $logFileSize = filesize($logFile);
261
262 $message .= '<p>' . sprintf(
263 $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogFile'),
264 $this->getDeprecationLogFileLink()
265 ) . '</p>';
266
267 $removeDeprecationLogFileUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=removeDeprecationLogFile';
268 $message .= '<p>' . sprintf(
269 $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogSize'),
270 t3lib_div::formatSize($logFileSize)
271 )
272 . ' <a href="' . $removeDeprecationLogFileUrl . '">' . $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeleteLink') . '</a></p>';
273 }
274
275 if ($logFileSize > $this->deprecationLogFileSizeWarningThreshold) {
276 $severity = tx_reports_reports_status_Status::WARNING;
277 }
278
279 if ($logFileSize > $this->deprecationLogFileSizeErrorThreshold) {
280 $severity = tx_reports_reports_status_Status::ERROR;
281 }
282 }
283
284 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
285 $title, $value, $message, $severity
286 );
287 }
288
289 /**
290 * Creates a link to the deprecation log file with the absolute path as the
291 * link text.
292 *
293 * @return string Link to the deprecation log file
294 */
295 protected function getDeprecationLogFileLink() {
296 $logFile = t3lib_div::getDeprecationLogFileName();
297 $relativePath = t3lib_div::resolveBackPath(
298 $this->backPath . substr($logFile, strlen(PATH_site))
299 );
300 $link = '<a href="' . $relativePath . '">' . $logFile . '</a>';
301
302 return $link;
303 }
304
305 /**
306 * Executes admin commands.
307 *
308 * Currently implemented commands are:
309 * - Remove deprecation log file
310 *
311 * @return void
312 */
313 protected function executeAdminCommand() {
314 $command = t3lib_div::_GET('adminCmd');
315
316 switch ($command) {
317 case 'removeDeprecationLogFile':
318 self::removeDeprecationLogFile();
319 break;
320 default:
321 // intentionally left blank
322 break;
323 }
324 }
325
326 /**
327 * Remove deprecation log file.
328 *
329 * @return void
330 */
331 protected static function removeDeprecationLogFile() {
332 if (@unlink(t3lib_div::getDeprecationLogFileName())) {
333 $message = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeletedSuccessful');
334 $severity = t3lib_FlashMessage::OK;
335 } else {
336 $message = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeletionFailed');
337 $severity = t3lib_FlashMessage::ERROR;
338 }
339 t3lib_FlashMessageQueue::addMessage(t3lib_div::makeInstance(
340 't3lib_FlashMessage',
341 $message,
342 '',
343 $severity,
344 TRUE
345 ));
346 }
347 }
348
349 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/reports/status/class.tx_reports_reports_status_configurationstatus.php'])) {
350 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/reports/reports/status/class.tx_reports_reports_status_configurationstatus.php']);
351 }
352
353 ?>