]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Summary.php
Merge pull request #12679 from nupplaphil/feat/reduce_config_dependency
[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\Renderer;
29 use Friendica\Core\Update;
30 use Friendica\Database\DBA;
31 use Friendica\Database\DBStructure;
32 use Friendica\DI;
33 use Friendica\Core\Config\Factory\Config;
34 use Friendica\Module\BaseAdmin;
35 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
36 use Friendica\Network\HTTPException\ServiceUnavailableException;
37 use Friendica\Network\Probe;
38 use Friendica\Util\DateTimeFormat;
39
40 class Summary extends BaseAdmin
41 {
42         protected function content(array $request = []): string
43         {
44                 parent::content();
45
46                 $a = DI::app();
47
48                 // are there MyISAM tables in the DB? If so, trigger a warning message
49                 $warningtext = [];
50
51                 $templateEngine = Renderer::getTemplateEngine();
52                 $errors = [];
53                 $templateEngine->testInstall($errors);
54                 foreach ($errors as $error) {
55                         $warningtext[] = DI::l10n()->t('Template engine (%s) error: %s', $templateEngine::$name, $error);
56                 }
57
58                 if (DBA::count('information_schema.tables', ['engine' => 'myisam', 'table_schema' => DBA::databaseName()])) {
59                         $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');
60                 }
61
62                 // are there InnoDB tables in Antelope in the DB? If so, trigger a warning message
63                 if (DBA::count('information_schema.tables', ['ENGINE' => 'InnoDB', 'ROW_FORMAT' => ['COMPACT', 'REDUNDANT'], 'table_schema' => DBA::databaseName()])) {
64                         $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');
65                 }
66
67                 // Avoid the database error 1615 "Prepared statement needs to be re-prepared", see https://github.com/friendica/friendica/issues/8550
68                 if (!DI::config()->get('database', 'pdo_emulate_prepares')) {
69                         $table_definition_cache = DBA::getVariable('table_definition_cache');
70                         $table_open_cache = DBA::getVariable('table_open_cache');
71                         if (!empty($table_definition_cache) && !empty($table_open_cache)) {
72                                 $suggested_definition_cache = min(400 + round($table_open_cache / 2, 1), 2000);
73                                 if ($suggested_definition_cache > $table_definition_cache) {
74                                         $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');
75                                 }
76                         }
77                 }
78
79                 // Check if github.com/friendica/stable/VERSION is higher then
80                 // the local version of Friendica. Check is opt-in, source may be stable or develop branch
81                 if (DI::config()->get('system', 'check_new_version_url', 'none') != 'none') {
82                         $gitversion = DI::config()->get('system', 'git_friendica_version');
83                         if (version_compare(App::VERSION, $gitversion) < 0) {
84                                 $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);
85                         }
86                 }
87
88                 if (DI::config()->get('system', 'dbupdate', DBStructure::UPDATE_NOT_CHECKED) == DBStructure::UPDATE_NOT_CHECKED) {
89                         DBStructure::performUpdate();
90                 }
91
92                 if (DI::config()->get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) {
93                         $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.');
94                 }
95
96                 if (DI::config()->get('system', 'update') == Update::FAILED) {
97                         $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.)');
98                 }
99
100                 $last_worker_call = DI::keyValue()->get('last_worker_execution');
101                 if (!$last_worker_call) {
102                         $warningtext[] = DI::l10n()->t('The worker was never executed. Please check your database structure!');
103                 } elseif ((strtotime(DateTimeFormat::utcNow()) - strtotime($last_worker_call)) > 60 * 60) {
104                         $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);
105                 }
106
107                 // Legacy config file warning
108                 if (file_exists('.htconfig.php')) {
109                         $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');
110                 }
111
112                 if (file_exists('config/local.ini.php')) {
113                         $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');
114                 }
115
116                 if (!DI::configFileManager()->dataIsWritable()) {
117                         $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);
118                 }
119
120                 // Check server vitality
121                 if (!self::checkSelfHostMeta()) {
122                         $well_known = DI::baseUrl()->get() . Probe::HOST_META;
123                         $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.',
124                                 $well_known, $well_known, DI::baseUrl()->get() . '/help/Install');
125                 }
126
127                 // Check logfile permission
128                 if (DI::config()->get('system', 'debugging')) {
129                         $file = DI::config()->get('system', 'logfile');
130
131                         $fileSystem = DI::fs();
132
133                         try {
134                                 $stream = $fileSystem->createStream($file);
135
136                                 if (!isset($stream)) {
137                                         throw new ServiceUnavailableException('Stream is null.');
138                                 }
139
140                         } catch (\Throwable $exception) {
141                                 $warningtext[] = DI::l10n()->t('The logfile \'%s\' is not usable. No logging possible (error: \'%s\')', $file, $exception->getMessage());
142                         }
143
144                         $file = DI::config()->get('system', 'dlogfile');
145
146                         try {
147                                 if (!empty($file)) {
148                                         $stream = $fileSystem->createStream($file);
149
150                                         if (!isset($stream)) {
151                                                 throw new ServiceUnavailableException('Stream is null.');
152                                         }
153                                 }
154                         } catch (\Throwable $exception) {
155                                 $warningtext[] = DI::l10n()->t('The debug logfile \'%s\' is not usable. No logging possible (error: \'%s\')', $file, $exception->getMessage());
156                         }
157                 }
158
159                 // check legacy basepath settings
160                 $configLoader = (new Config())->createConfigFileManager($a->getBasePath(), $_SERVER);
161                 $configCache = new Cache();
162                 $configLoader->setupCache($configCache);
163                 $confBasepath = $configCache->get('system', 'basepath');
164                 $currBasepath = DI::config()->get('system', 'basepath');
165                 if ($confBasepath !== $currBasepath || !is_dir($currBasepath)) {
166                         if (is_dir($confBasepath) && DI::config()->set('system', 'basepath', $confBasepath)) {
167                                 DI::logger()->info('Friendica\'s system.basepath was updated successfully.', [
168                                         'from' => $currBasepath,
169                                         'to'   => $confBasepath,
170                                 ]);
171                                 $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.',
172                                         $currBasepath,
173                                         $confBasepath);
174                         } elseif (!is_dir($currBasepath)) {
175                                 DI::logger()->alert('Friendica\'s system.basepath is wrong.', [
176                                         'from' => $currBasepath,
177                                         'to'   => $confBasepath,
178                                 ]);
179                                 $warningtext[] = DI::l10n()->t('Friendica\'s current system.basepath \'%s\' is wrong and the config file \'%s\' isn\'t used.',
180                                         $currBasepath,
181                                         $confBasepath);
182                         } else {
183                                 DI::logger()->alert('Friendica\'s system.basepath is wrong.', [
184                                         'from' => $currBasepath,
185                                         'to'   => $confBasepath,
186                                 ]);
187                                 $warningtext[] = DI::l10n()->t('Friendica\'s current system.basepath \'%s\' is not equal to the config file \'%s\'. Please fix your configuration.',
188                                         $currBasepath,
189                                         $confBasepath);
190                         }
191                 }
192
193                 $deferred = DBA::count('workerqueue', ['NOT `done` AND `retrial` > ?', 0]);
194
195                 $workerqueue = DBA::count('workerqueue', ['NOT `done` AND `retrial` = ?', 0]);
196
197                 // We can do better, but this is a quick queue status
198                 $queues = ['label' => DI::l10n()->t('Message queues'), 'deferred' => $deferred, 'workerq' => $workerqueue];
199
200                 $variables = DBA::toArray(DBA::p('SHOW variables LIKE "max_allowed_packet"'));
201                 $max_allowed_packet = $variables ? $variables[0]['Value'] : 0;
202
203                 $server_settings = [
204                         'label' => DI::l10n()->t('Server Settings'),
205                         'php'   => [
206                                 'version'             => phpversion(),
207                                 'php.ini'             => php_ini_loaded_file(),
208                                 'upload_max_filesize' => ini_get('upload_max_filesize'),
209                                 'post_max_size'       => ini_get('post_max_size'),
210                                 'memory_limit'        => ini_get('memory_limit')
211                         ],
212                         'mysql' => [
213                                 'max_allowed_packet' => $max_allowed_packet
214                         ]
215                 ];
216
217                 $t = Renderer::getMarkupTemplate('admin/summary.tpl');
218                 return Renderer::replaceMacros($t, [
219                         '$title'          => DI::l10n()->t('Administration'),
220                         '$page'           => DI::l10n()->t('Summary'),
221                         '$queues'         => $queues,
222                         '$version'        => [DI::l10n()->t('Version'), App::VERSION],
223                         '$platform'       => App::PLATFORM,
224                         '$codename'       => App::CODENAME,
225                         '$build'          => DI::config()->get('system', 'build'),
226                         '$addons'         => [DI::l10n()->t('Active addons'), Addon::getEnabledList()],
227                         '$serversettings' => $server_settings,
228                         '$warningtext'    => $warningtext,
229                 ]);
230         }
231
232         private static function checkSelfHostMeta()
233         {
234                 // Fetch the host-meta to check if this really is a vital server
235                 return DI::httpClient()->get(DI::baseUrl()->get() . Probe::HOST_META, HttpClientAccept::XRD_XML)->isSuccess();
236         }
237
238 }