]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Summary.php
Merge pull request #12627 from nupplaphil/feat/config_warning
[friendica.git] / src / Module / Admin / Summary.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Admin;
23
24 use Friendica\App;
25 use Friendica\Core\Addon;
26 use Friendica\Core\Config\Util\ConfigFileManager;
27 use Friendica\Core\Config\ValueObject\Cache;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Update;
31 use Friendica\Database\DBA;
32 use Friendica\Database\DBStructure;
33 use Friendica\DI;
34 use Friendica\Core\Config\Factory\Config;
35 use Friendica\Model\Register;
36 use Friendica\Module\BaseAdmin;
37 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
38 use Friendica\Network\HTTPException\ServiceUnavailableException;
39 use Friendica\Network\Probe;
40 use Friendica\Util\DateTimeFormat;
41
42 class Summary extends BaseAdmin
43 {
44         protected function content(array $request = []): string
45         {
46                 parent::content();
47
48                 $a = DI::app();
49
50                 // are there MyISAM tables in the DB? If so, trigger a warning message
51                 $warningtext = [];
52
53                 $templateEngine = Renderer::getTemplateEngine();
54                 $errors = [];
55                 $templateEngine->testInstall($errors);
56                 foreach ($errors as $error) {
57                         $warningtext[] = DI::l10n()->t('Template engine (%s) error: %s', $templateEngine::$name, $error);
58                 }
59
60                 if (DBA::count('information_schema.tables', ['engine' => 'myisam', 'table_schema' => DBA::databaseName()])) {
61                         $warningtext[] = DI::l10n()->t('Your DB still runs with MyISAM tables. You should change the engine type to InnoDB. As Friendica will use InnoDB only features in the future, you should change this! See <a href="%s">here</a> for a guide that may be helpful converting the table engines. You may also use the command <tt>php bin/console.php dbstructure toinnodb</tt> of your Friendica installation for an automatic conversion.<br />', 'https://dev.mysql.com/doc/refman/5.7/en/converting-tables-to-innodb.html');
62                 }
63
64                 // are there InnoDB tables in Antelope in the DB? If so, trigger a warning message
65                 if (DBA::count('information_schema.tables', ['ENGINE' => 'InnoDB', 'ROW_FORMAT' => ['COMPACT', 'REDUNDANT'], 'table_schema' => DBA::databaseName()])) {
66                         $warningtext[] = DI::l10n()->t('Your DB still runs with InnoDB tables in the Antelope file format. You should change the file format to Barracuda. Friendica is using features that are not provided by the Antelope format. See <a href="%s">here</a> for a guide that may be helpful converting the table engines. You may also use the command <tt>php bin/console.php dbstructure toinnodb</tt> of your Friendica installation for an automatic conversion.<br />', 'https://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html');
67                 }
68
69                 // Avoid the database error 1615 "Prepared statement needs to be re-prepared", see https://github.com/friendica/friendica/issues/8550
70                 if (!DI::config()->get('database', 'pdo_emulate_prepares')) {
71                         $table_definition_cache = DBA::getVariable('table_definition_cache');
72                         $table_open_cache = DBA::getVariable('table_open_cache');
73                         if (!empty($table_definition_cache) && !empty($table_open_cache)) {
74                                 $suggested_definition_cache = min(400 + round($table_open_cache / 2, 1), 2000);
75                                 if ($suggested_definition_cache > $table_definition_cache) {
76                                         $warningtext[] = DI::l10n()->t('Your table_definition_cache is too low (%d). This can lead to the database error "Prepared statement needs to be re-prepared". Please set it at least to %d. See <a href="%s">here</a> for more information.<br />', $table_definition_cache, $suggested_definition_cache, 'https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_table_definition_cache');
77                                 }
78                         }
79                 }
80
81                 // Check if github.com/friendica/stable/VERSION is higher then
82                 // the local version of Friendica. Check is opt-in, source may be stable or develop branch
83                 if (DI::config()->get('system', 'check_new_version_url', 'none') != 'none') {
84                         $gitversion = DI::config()->get('system', 'git_friendica_version');
85                         if (version_compare(App::VERSION, $gitversion) < 0) {
86                                 $warningtext[] = DI::l10n()->t('There is a new version of Friendica available for download. Your current version is %1$s, upstream version is %2$s', App::VERSION, $gitversion);
87                         }
88                 }
89
90                 if (DI::config()->get('system', 'dbupdate', DBStructure::UPDATE_NOT_CHECKED) == DBStructure::UPDATE_NOT_CHECKED) {
91                         DBStructure::performUpdate();
92                 }
93
94                 if (DI::config()->get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) {
95                         $warningtext[] = DI::l10n()->t('The database update failed. Please run "php bin/console.php dbstructure update" from the command line and have a look at the errors that might appear.');
96                 }
97
98                 if (DI::config()->get('system', 'update') == Update::FAILED) {
99                         $warningtext[] = DI::l10n()->t('The last update failed. Please run "php bin/console.php dbstructure update" from the command line and have a look at the errors that might appear. (Some of the errors are possibly inside the logfile.)');
100                 }
101
102                 $last_worker_call = DI::keyValue()->get('last_worker_execution');
103                 if (!$last_worker_call) {
104                         $warningtext[] = DI::l10n()->t('The worker was never executed. Please check your database structure!');
105                 } elseif ((strtotime(DateTimeFormat::utcNow()) - strtotime($last_worker_call)) > 60 * 60) {
106                         $warningtext[] = DI::l10n()->t('The last worker execution was on %s UTC. This is older than one hour. Please check your crontab settings.', $last_worker_call);
107                 }
108
109                 // Legacy config file warning
110                 if (file_exists('.htconfig.php')) {
111                         $warningtext[] = DI::l10n()->t('Friendica\'s configuration now is stored in config/local.config.php, please copy config/local-sample.config.php and move your config from <code>.htconfig.php</code>. See <a href="%s">the Config help page</a> for help with the transition.', DI::baseUrl()->get() . '/help/Config');
112                 }
113
114                 if (file_exists('config/local.ini.php')) {
115                         $warningtext[] = DI::l10n()->t('Friendica\'s configuration now is stored in config/local.config.php, please copy config/local-sample.config.php and move your config from <code>config/local.ini.php</code>. See <a href="%s">the Config help page</a> for help with the transition.', DI::baseUrl()->get() . '/help/Config');
116                 }
117
118                 if (!DI::configFileManager()->dataIsWritable()) {
119                         $warningtext[] = DI::l10n()->t('Friendica\'s configuration store "%s" isn\'t writable. Until then database updates won\'t be applied automatically, admin settings and console configuration changes won\'t be saved.', ConfigFileManager::CONFIG_DATA_FILE);
120                 }
121
122                 // Check server vitality
123                 if (!self::checkSelfHostMeta()) {
124                         $well_known = DI::baseUrl()->get() . Probe::HOST_META;
125                         $warningtext[] = DI::l10n()->t('<a href="%s">%s</a> is not reachable on your system. This is a severe configuration issue that prevents server to server communication. See <a href="%s">the installation page</a> for help.',
126                                 $well_known, $well_known, DI::baseUrl()->get() . '/help/Install');
127                 }
128
129                 // Check logfile permission
130                 if (DI::config()->get('system', 'debugging')) {
131                         $file = DI::config()->get('system', 'logfile');
132
133                         $fileSystem = DI::fs();
134
135                         try {
136                                 $stream = $fileSystem->createStream($file);
137
138                                 if (!isset($stream)) {
139                                         throw new ServiceUnavailableException('Stream is null.');
140                                 }
141
142                         } catch (\Throwable $exception) {
143                                 $warningtext[] = DI::l10n()->t('The logfile \'%s\' is not usable. No logging possible (error: \'%s\')', $file, $exception->getMessage());
144                         }
145
146                         $file = DI::config()->get('system', 'dlogfile');
147
148                         try {
149                                 if (!empty($file)) {
150                                         $stream = $fileSystem->createStream($file);
151
152                                         if (!isset($stream)) {
153                                                 throw new ServiceUnavailableException('Stream is null.');
154                                         }
155                                 }
156                         } catch (\Throwable $exception) {
157                                 $warningtext[] = DI::l10n()->t('The debug logfile \'%s\' is not usable. No logging possible (error: \'%s\')', $file, $exception->getMessage());
158                         }
159                 }
160
161                 // check legacy basepath settings
162                 $configLoader = (new Config())->createConfigFileManager($a->getBasePath(), $_SERVER);
163                 $configCache = new Cache();
164                 $configLoader->setupCache($configCache);
165                 $confBasepath = $configCache->get('system', 'basepath');
166                 $currBasepath = DI::config()->get('system', 'basepath');
167                 if ($confBasepath !== $currBasepath || !is_dir($currBasepath)) {
168                         if (is_dir($confBasepath) && DI::config()->set('system', 'basepath', $confBasepath)) {
169                                 DI::logger()->info('Friendica\'s system.basepath was updated successfully.', [
170                                         'from' => $currBasepath,
171                                         'to'   => $confBasepath,
172                                 ]);
173                                 $warningtext[] = DI::l10n()->t('Friendica\'s system.basepath was updated from \'%s\' to \'%s\'. Please remove the system.basepath from your db to avoid differences.',
174                                         $currBasepath,
175                                         $confBasepath);
176                         } elseif (!is_dir($currBasepath)) {
177                                 DI::logger()->alert('Friendica\'s system.basepath is wrong.', [
178                                         'from' => $currBasepath,
179                                         'to'   => $confBasepath,
180                                 ]);
181                                 $warningtext[] = DI::l10n()->t('Friendica\'s current system.basepath \'%s\' is wrong and the config file \'%s\' isn\'t used.',
182                                         $currBasepath,
183                                         $confBasepath);
184                         } else {
185                                 DI::logger()->alert('Friendica\'s system.basepath is wrong.', [
186                                         'from' => $currBasepath,
187                                         'to'   => $confBasepath,
188                                 ]);
189                                 $warningtext[] = DI::l10n()->t('Friendica\'s current system.basepath \'%s\' is not equal to the config file \'%s\'. Please fix your configuration.',
190                                         $currBasepath,
191                                         $confBasepath);
192                         }
193                 }
194
195                 $deferred = DBA::count('workerqueue', ['NOT `done` AND `retrial` > ?', 0]);
196
197                 $workerqueue = DBA::count('workerqueue', ['NOT `done` AND `retrial` = ?', 0]);
198
199                 // We can do better, but this is a quick queue status
200                 $queues = ['label' => DI::l10n()->t('Message queues'), 'deferred' => $deferred, 'workerq' => $workerqueue];
201
202                 $variables = DBA::toArray(DBA::p('SHOW variables LIKE "max_allowed_packet"'));
203                 $max_allowed_packet = $variables ? $variables[0]['Value'] : 0;
204
205                 $server_settings = [
206                         'label' => DI::l10n()->t('Server Settings'),
207                         'php'   => [
208                                 'version'             => phpversion(),
209                                 'php.ini'             => php_ini_loaded_file(),
210                                 'upload_max_filesize' => ini_get('upload_max_filesize'),
211                                 'post_max_size'       => ini_get('post_max_size'),
212                                 'memory_limit'        => ini_get('memory_limit')
213                         ],
214                         'mysql' => [
215                                 'max_allowed_packet' => $max_allowed_packet
216                         ]
217                 ];
218
219                 $t = Renderer::getMarkupTemplate('admin/summary.tpl');
220                 return Renderer::replaceMacros($t, [
221                         '$title'          => DI::l10n()->t('Administration'),
222                         '$page'           => DI::l10n()->t('Summary'),
223                         '$queues'         => $queues,
224                         '$version'        => [DI::l10n()->t('Version'), App::VERSION],
225                         '$platform'       => App::PLATFORM,
226                         '$codename'       => App::CODENAME,
227                         '$build'          => DI::config()->get('system', 'build'),
228                         '$addons'         => [DI::l10n()->t('Active addons'), Addon::getEnabledList()],
229                         '$serversettings' => $server_settings,
230                         '$warningtext'    => $warningtext,
231                 ]);
232         }
233
234         private static function checkSelfHostMeta()
235         {
236                 // Fetch the host-meta to check if this really is a vital server
237                 return DI::httpClient()->get(DI::baseUrl()->get() . Probe::HOST_META, HttpClientAccept::XRD_XML)->isSuccess();
238         }
239
240 }