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