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