9ad30b204aa5b55ee81ea5b4a8ffa4133e6be096
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / ConfigurationStatus.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 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Performs some checks about the install tool protection status
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 */
34 class ConfigurationStatus implements \TYPO3\CMS\Reports\StatusProviderInterface {
35
36 /**
37 * 10MB
38 *
39 * @var integer
40 */
41 protected $deprecationLogFileSizeWarningThreshold = 10485760;
42
43 /**
44 * 100MB
45 *
46 * @var integer
47 */
48 protected $deprecationLogFileSizeErrorThreshold = 104857600;
49
50 /**
51 * Backpath to the typo3 main directory
52 *
53 * @var string
54 */
55 protected $backPath = '../';
56
57 /**
58 * Determines the Install Tool's status, mainly concerning its protection.
59 *
60 * @return array List of statuses
61 */
62 public function getStatus() {
63 $this->executeAdminCommand();
64 $statuses = array(
65 'emptyReferenceIndex' => $this->getReferenceIndexStatus(),
66 'deprecationLog' => $this->getDeprecationLogStatus()
67 );
68 if ($this->isMemcachedUsed()) {
69 $statuses['memcachedConnection'] = $this->getMemcachedConnectionStatus();
70 }
71 if (TYPO3_OS !== 'WIN') {
72 $statuses['createdFilesWorldWritable'] = $this->getCreatedFilesWorldWritableStatus();
73 $statuses['createdDirectoriesWorldWritable'] = $this->getCreatedDirectoriesWorldWritableStatus();
74 }
75 return $statuses;
76 }
77
78 /**
79 * Checks if sys_refindex is empty.
80 *
81 * @return \TYPO3\CMS\Reports\Status An object representing whether the reference index is empty or not
82 */
83 protected function getReferenceIndexStatus() {
84 $value = $GLOBALS['LANG']->getLL('status_ok');
85 $message = '';
86 $severity = \TYPO3\CMS\Reports\Status::OK;
87 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'sys_refindex');
88 $registry = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
89 $lastRefIndexUpdate = $registry->get('core', 'sys_refindex_lastUpdate');
90 if (!$count && $lastRefIndexUpdate) {
91 $value = $GLOBALS['LANG']->getLL('status_empty');
92 $severity = \TYPO3\CMS\Reports\Status::WARNING;
93 $url = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('system_dbint') . '&id=0&SET[function]=refindex';
94 $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:warning.backend_reference_index'), '<a href="' . $url . '">', '</a>', \TYPO3\CMS\Backend\Utility\BackendUtility::dateTime($lastRefIndexUpdate));
95 }
96 return GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $GLOBALS['LANG']->getLL('status_referenceIndex'), $value, $message, $severity);
97 }
98
99 /**
100 * Checks whether memcached is configured, if that's the case we assume it's also used.
101 *
102 * @return boolean TRUE if memcached is used, FALSE otherwise.
103 */
104 protected function isMemcachedUsed() {
105 $memcachedUsed = FALSE;
106 $memcachedServers = $this->getConfiguredMemcachedServers();
107 if (count($memcachedServers)) {
108 $memcachedUsed = TRUE;
109 }
110 return $memcachedUsed;
111 }
112
113 /**
114 * Gets the configured memcached server connections.
115 *
116 * @return array An array of configured memcached server connections.
117 */
118 protected function getConfiguredMemcachedServers() {
119 $memcachedServers = array();
120 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'])) {
121 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] as $table => $conf) {
122 if (is_array($conf)) {
123 foreach ($conf as $key => $value) {
124 if (!is_array($value) && $value === 'TYPO3\\CMS\\Core\\Cache\\Backend\\MemcachedBackend') {
125 $memcachedServers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$table]['options']['servers'];
126 break;
127 }
128 }
129 }
130 }
131 }
132 return $memcachedServers;
133 }
134
135 /**
136 * Checks whether TYPO3 can connect to the configured memcached servers.
137 *
138 * @return \TYPO3\CMS\Reports\Status An object representing whether TYPO3 can connect to the configured memcached servers
139 */
140 protected function getMemcachedConnectionStatus() {
141 $value = $GLOBALS['LANG']->getLL('status_ok');
142 $message = '';
143 $severity = \TYPO3\CMS\Reports\Status::OK;
144 $failedConnections = array();
145 $defaultMemcachedPort = ini_get('memcache.default_port');
146 $memcachedServers = $this->getConfiguredMemcachedServers();
147 if (function_exists('memcache_connect') && is_array($memcachedServers)) {
148 foreach ($memcachedServers as $testServer) {
149 $configuredServer = $testServer;
150 if (substr($testServer, 0, 7) == 'unix://') {
151 $host = $testServer;
152 $port = 0;
153 } else {
154 if (substr($testServer, 0, 6) === 'tcp://') {
155 $testServer = substr($testServer, 6);
156 }
157 if (strstr($testServer, ':') !== FALSE) {
158 list($host, $port) = explode(':', $testServer, 2);
159 } else {
160 $host = $testServer;
161 $port = $defaultMemcachedPort;
162 }
163 }
164 $memcachedConnection = @memcache_connect($host, $port);
165 if ($memcachedConnection != NULL) {
166 memcache_close($memcachedConnection);
167 } else {
168 $failedConnections[] = $configuredServer;
169 }
170 }
171 }
172 if (count($failedConnections)) {
173 $value = $GLOBALS['LANG']->getLL('status_connectionFailed');
174 $severity = \TYPO3\CMS\Reports\Status::WARNING;
175 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:warning.memcache_not_usable') . '<br /><br />' . '<ul><li>' . implode('</li><li>', $failedConnections) . '</li></ul>';
176 }
177 return GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $GLOBALS['LANG']->getLL('status_memcachedConfiguration'), $value, $message, $severity);
178 }
179
180 /**
181 * Provides status information on the deprecation log, whether it's enabled
182 * and if so whether certain limits in file size are reached.
183 *
184 * @return \TYPO3\CMS\Reports\Status The deprecation log status.
185 */
186 protected function getDeprecationLogStatus() {
187 $title = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLog');
188 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xlf:disabled');
189 $message = '';
190 $severity = \TYPO3\CMS\Reports\Status::OK;
191 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog']) {
192 $value = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_common.xlf:enabled');
193 $message = '<p>' . $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogEnabled') . '</p>';
194 $severity = \TYPO3\CMS\Reports\Status::NOTICE;
195 $logFile = GeneralUtility::getDeprecationLogFileName();
196 $logFileSize = 0;
197 if (@file_exists($logFile)) {
198 $logFileSize = filesize($logFile);
199 $message .= '<p>' . sprintf($GLOBALS['LANG']->getLL('status_configuration_DeprecationLogFile'), $this->getDeprecationLogFileLink()) . '</p>';
200 $removeDeprecationLogFileUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL') . '&amp;adminCmd=removeDeprecationLogFile';
201 $message .= '<p>' . sprintf($GLOBALS['LANG']->getLL('status_configuration_DeprecationLogSize'), GeneralUtility::formatSize($logFileSize)) . ' <a href="' . $removeDeprecationLogFileUrl . '">' . $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeleteLink') . '</a></p>';
202 }
203 if ($logFileSize > $this->deprecationLogFileSizeWarningThreshold) {
204 $severity = \TYPO3\CMS\Reports\Status::WARNING;
205 }
206 if ($logFileSize > $this->deprecationLogFileSizeErrorThreshold) {
207 $severity = \TYPO3\CMS\Reports\Status::ERROR;
208 }
209 }
210 return GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $title, $value, $message, $severity);
211 }
212
213 /**
214 * Warning, if fileCreateMask has write bit for 'others' set.
215 *
216 * @return \TYPO3\CMS\Reports\Status The writable status for 'others'
217 */
218 protected function getCreatedFilesWorldWritableStatus() {
219 $value = $GLOBALS['LANG']->getLL('status_ok');
220 $message = '';
221 $severity = \TYPO3\CMS\Reports\Status::OK;
222 if ((int)$GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] % 10 & 2) {
223 $value = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'];
224 $severity = \TYPO3\CMS\Reports\Status::WARNING;
225 $message = $GLOBALS['LANG']->getLL('status_CreatedFilePermissions.writable');
226 }
227 return GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $GLOBALS['LANG']->getLL('status_CreatedFilePermissions'), $value, $message, $severity);
228 }
229
230 /**
231 * Warning, if folderCreateMask has write bit for 'others' set.
232 *
233 * @return \TYPO3\CMS\Reports\Status The writable status for 'others'
234 */
235 protected function getCreatedDirectoriesWorldWritableStatus() {
236 $value = $GLOBALS['LANG']->getLL('status_ok');
237 $message = '';
238 $severity = \TYPO3\CMS\Reports\Status::OK;
239 if ((int)$GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] % 10 & 2) {
240 $value = $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'];
241 $severity = \TYPO3\CMS\Reports\Status::WARNING;
242 $message = $GLOBALS['LANG']->getLL('status_CreatedDirectoryPermissions.writable');
243 }
244 return GeneralUtility::makeInstance('TYPO3\\CMS\\Reports\\Status', $GLOBALS['LANG']->getLL('status_CreatedDirectoryPermissions'), $value, $message, $severity);
245 }
246
247 /**
248 * Creates a link to the deprecation log file with the absolute path as the
249 * link text.
250 *
251 * @return string Link to the deprecation log file
252 */
253 protected function getDeprecationLogFileLink() {
254 $logFile = GeneralUtility::getDeprecationLogFileName();
255 $relativePath = GeneralUtility::resolveBackPath($this->backPath . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($logFile));
256 $link = '<a href="' . $relativePath . '">' . $logFile . '</a>';
257 return $link;
258 }
259
260 /**
261 * Executes admin commands.
262 *
263 * Currently implemented commands are:
264 * - Remove deprecation log file
265 *
266 * @return void
267 */
268 protected function executeAdminCommand() {
269 $command = GeneralUtility::_GET('adminCmd');
270 switch ($command) {
271 case 'removeDeprecationLogFile':
272 self::removeDeprecationLogFile();
273 break;
274 default:
275 // intentionally left blank
276 }
277 }
278
279 /**
280 * Remove deprecation log file.
281 *
282 * @return void
283 */
284 static protected function removeDeprecationLogFile() {
285 if (@unlink(GeneralUtility::getDeprecationLogFileName())) {
286 $message = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeletedSuccessful');
287 $severity = \TYPO3\CMS\Core\Messaging\FlashMessage::OK;
288 } else {
289 $message = $GLOBALS['LANG']->getLL('status_configuration_DeprecationLogDeletionFailed');
290 $severity = \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR;
291 }
292 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $message, '', $severity, TRUE);
293 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
294 $flashMessageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
295 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
296 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
297 $defaultFlashMessageQueue->enqueue($flashMessage);
298 }
299
300 }