Follow-up to feature #12001: Add checks from t3lib_BEfunc::displayWarningMessages...
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / reports / status / class.tx_reports_reports_status_systemstatus.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 several checks about the system's health
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage reports
32 */
33 class tx_reports_reports_status_SystemStatus implements tx_reports_StatusProvider {
34
35 /**
36 * Determines the Install Tool's status, mainly concerning its protection.
37 *
38 * @see typo3/sysext/reports/interfaces/tx_reports_StatusProvider::getStatus()
39 */
40 public function getStatus() {
41 $statuses = array(
42 'encryptionKeyEmpty' => $this->getEncryptionKeyStatus(),
43 'fileDenyPattern' => $this->getFileDenyPatternStatus(),
44 'htaccessUpload' => $this->getHtaccessUploadStatus(),
45 'remainingUdates' => $this->getRemainingUpdatesStatus(),
46 'emptyReferenceIndex' => $this->getReferenceIndexStatus()
47 );
48
49 if ($this->isMemcachedUsed()) {
50 $statuses['memcachedConnection'] = $this->getMemcachedConnectionStatus();
51 }
52
53 return $statuses;
54 }
55
56 /**
57 * Checks whether the encryption key is empty.
58 *
59 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether the encryption key is empty or not
60 */
61 protected function getEncryptionKeyStatus() {
62 $value = $GLOBALS['LANG']->getLL('status_ok');
63 $message = '';
64 $severity = tx_reports_reports_status_Status::OK;
65
66 if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
67 $value = $GLOBALS['LANG']->getLL('status_insecure');
68 $severity = tx_reports_reports_status_Status::ERROR;
69
70 $url = 'install/index.php?redirect_url=index.php'
71 . urlencode('?TYPO3_INSTALL[type]=config#set_encryptionKey');
72
73 $message = sprintf(
74 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.install_encryption'),
75 '<a href="' . $url . '">',
76 '</a>'
77 );
78 }
79
80 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
81 'Encryption Key', $value, $message, $severity
82 );
83 }
84
85 /**
86 * Checks if fileDenyPattern was changed which is dangerous on Apache
87 *
88 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether the file deny pattern has changed
89 */
90 protected function getFileDenyPatternStatus() {
91 $value = $GLOBALS['LANG']->getLL('status_ok');
92 $message = '';
93 $severity = tx_reports_reports_status_Status::OK;
94
95 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'] != FILE_DENY_PATTERN_DEFAULT) {
96 $value = $GLOBALS['LANG']->getLL('status_insecure');
97 $severity = tx_reports_reports_status_Status::ERROR;
98
99 $url = 'install/index.php?redirect_url=index.php'
100 . urlencode('?TYPO3_INSTALL[type]=config#set_encryptionKey');
101
102 $message = sprintf(
103 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.file_deny_pattern'),
104 '<br /><pre>'
105 . htmlspecialchars(FILE_DENY_PATTERN_DEFAULT)
106 . '</pre><br />'
107 );
108 }
109
110 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
111 'File Deny Pattern', $value, $message, $severity
112 );
113 }
114
115 /**
116 * Checks if fileDenyPattern allows to upload .htaccess files which is
117 * dangerous on Apache.
118 *
119 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether it's possible to upload .htaccess files
120 */
121 protected function getHtaccessUploadStatus() {
122 $value = $GLOBALS['LANG']->getLL('status_ok');
123 $message = '';
124 $severity = tx_reports_reports_status_Status::OK;
125
126 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'] != FILE_DENY_PATTERN_DEFAULT && t3lib_div::verifyFilenameAgainstDenyPattern('.htaccess')) {
127 $value = $GLOBALS['LANG']->getLL('status_insecure');
128 $severity = tx_reports_reports_status_Status::ERROR;
129 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.file_deny_htaccess');
130 }
131
132 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
133 '.htaccess Upload Protection', $value, $message, $severity
134 );
135 }
136
137 /**
138 * Checks if there are still updates to perform
139 *
140 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether the installation is not completely updated yet
141 */
142 protected function getRemainingUpdatesStatus() {
143 $value = $GLOBALS['LANG']->getLL('status_updateComplete');
144 $message = '';
145 $severity = tx_reports_reports_status_Status::OK;
146
147 if (!t3lib_div::compat_version(TYPO3_branch)) {
148 $value = $GLOBALS['LANG']->getLL('status_updateIncomplete');
149 $severity = tx_reports_reports_status_Status::WARNING;
150
151 $url = 'install/index.php?redirect_url=index.php'
152 . urlencode('?TYPO3_INSTALL[type]=update');
153 $message = sprintf(
154 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.install_update'),
155 '<a href="' . $url . '">',
156 '</a>'
157 );
158 }
159
160 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
161 'Remaining Updates', $value, $message, $severity
162 );
163 }
164
165 /**
166 * Checks if sys_refindex is empty.
167 *
168 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether the reference index is empty or not
169 */
170 protected function getReferenceIndexStatus() {
171 $value = $GLOBALS['LANG']->getLL('status_ok');
172 $message = '';
173 $severity = tx_reports_reports_status_Status::OK;
174
175 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'sys_refindex');
176
177 if (!$count) {
178 $value = $GLOBALS['LANG']->getLL('status_empty');
179 $severity = tx_reports_reports_status_Status::WARNING;
180
181 $url = 'sysext/lowlevel/dbint/index.php?&id=0&SET[function]=refindex';
182 $message = sprintf(
183 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.backend_reference'),
184 '<a href="' . $url . '">',
185 '</a>'
186 );
187 }
188
189 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
190 'Reference Index', $value, $message, $severity
191 );
192 }
193
194 /**
195 * Checks whether memcached is configured, if that's the case we asume it's also used.
196 *
197 * @return boolean True if memcached is used, false otherwise.
198 */
199 protected function isMemcachedUsed() {
200 $memcachedUsed = false;
201
202 $memcachedServers = $this->getConfiguredMemcachedServers();
203 if (count($memcachedServers)) {
204 $memcachedUsed = true;
205 }
206
207 return $memcachedUsed;
208 }
209
210 /**
211 * Gets the configured memcached server connections.
212 *
213 * @return array An array of configured memcached server connections.
214 */
215 protected function getConfiguredMemcachedServers() {
216 $memcachedServers = array();
217
218 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'])) {
219 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] as $table => $conf) {
220 if (is_array($conf)) {
221 foreach ($conf as $key => $value) {
222 if (!is_array($value) && $value === 't3lib_cache_backend_MemcachedBackend') {
223 $memcachedServers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$table]['options']['servers'];
224 break;
225 }
226 }
227 }
228 }
229 }
230
231 return $memcachedServers;
232 }
233
234 /**
235 * Checks whether TYPO3 can connect to the configured memcached servers.
236 *
237 * @return tx_reports_reports_status_Status An tx_reports_reports_status_Status object representing whether TYPO3 can connect to the configured memcached servers
238 */
239 protected function getMemcachedConnectionStatus() {
240 $value = $GLOBALS['LANG']->getLL('status_ok');
241 $message = '';
242 $severity = tx_reports_reports_status_Status::OK;
243
244 $failedConnections = array();
245 $defaultMemcachedPort = ini_get('memcache.default_port');
246 $memcachedServers = $this->getConfiguredMemcachedServers();
247
248 if (function_exists('memcache_connect') && is_array($memcachedServers)) {
249 foreach ($memcachedServers as $testServer) {
250 $configuredServer = $testServer;
251 if (substr($testServer, 0, 7) == 'unix://') {
252 $host = $testServer;
253 $port = 0;
254 } else {
255 if (substr($testServer, 0, 6) === 'tcp://') {
256 $testServer = substr($testServer, 6);
257 }
258 if (strstr($testServer, ':') !== FALSE) {
259 list($host, $port) = explode(':', $testServer, 2);
260 } else {
261 $host = $testServer;
262 $port = $defaultPort;
263 }
264 }
265 $memcachedConnection = @memcache_connect($host, $port);
266 if ($memcachedConnection != null) {
267 memcache_close($memcachedConnection);
268 } else {
269 $failedConnections[] = $configuredServer;
270 }
271 }
272 }
273
274 if (count($failedConnections)) {
275 $value = $GLOBALS['LANG']->getLL('status_connectionFailed');
276 $severity = tx_reports_reports_status_Status::WARNING;
277
278 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:warning.memcache_not_usable')
279 . '<br /><br />'
280 . '<ul><li>'
281 . implode('</li><li>', $failedConnections)
282 . '</li></ul>';
283 }
284
285 return t3lib_div::makeInstance('tx_reports_reports_status_Status',
286 'Memcached Configuration', $value, $message, $severity
287 );
288 }
289 }
290
291
292 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/status/class.tx_reports_reports_status_systemstatus.php']) {
293 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/status/class.tx_reports_reports_status_systemstatus.php']);
294 }
295
296 ?>