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