]> git.mxchange.org Git - friendica.git/blob - src/Module/Friendica.php
Changes:
[friendica.git] / src / Module / Friendica.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\Addon;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\Hook;
29 use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
30 use Friendica\Core\L10n;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session\Capability\IHandleUserSessions;
33 use Friendica\Core\System;
34 use Friendica\Database\PostUpdate;
35 use Friendica\Model\User;
36 use Friendica\Network\HTTPException;
37 use Friendica\Protocol\ActivityPub;
38 use Friendica\Util\Profiler;
39 use Psr\Log\LoggerInterface;
40
41 /**
42  * Prints information about the current node
43  * Either in human-readable form or in JSON
44  */
45 class Friendica extends BaseModule
46 {
47         /** @var IManageConfigValues */
48         private $config;
49         /** @var IManageKeyValuePairs */
50         private $keyValue;
51         /** @var IHandleUserSessions */
52         private $session;
53
54         public function __construct(IHandleUserSessions $session, IManageKeyValuePairs $keyValue, IManageConfigValues $config, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
55         {
56                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
57
58                 $this->config = $config;
59                 $this->keyValue = $keyValue;
60                 $this->session = $session;
61         }
62
63         protected function content(array $request = []): string
64         {
65                 $visibleAddonList = Addon::getVisibleList();
66                 if (!empty($visibleAddonList)) {
67
68                         $sorted = $visibleAddonList;
69                         sort($sorted);
70
71                         $sortedAddonList = '';
72
73                         foreach ($sorted as $addon) {
74                                 if (strlen($addon)) {
75                                         if (strlen($sortedAddonList)) {
76                                                 $sortedAddonList .= ', ';
77                                         }
78                                         $sortedAddonList .= $addon;
79                                 }
80                         }
81                         $addon = [
82                                 'title' => $this->t('Installed addons/apps:'),
83                                 'list'  => $sortedAddonList,
84                         ];
85                 } else {
86                         $addon = [
87                                 'title' => $this->t('No installed addons/apps'),
88                         ];
89                 }
90
91                 $tos = ($this->config->get('system', 'tosdisplay')) ?
92                         $this->t('Read about the <a href="%1$s/tos">Terms of Service</a> of this node.', $this->baseUrl) :
93                         '';
94
95                 $blockList = $this->config->get('system', 'blocklist') ?? [];
96
97                 if (!empty($blockList) && ($this->config->get('blocklist', 'public') || $this->session->isAuthenticated())) {
98                         $blocked = [
99                                 'title'    => $this->t('On this server the following remote servers are blocked.'),
100                                 'header'   => [
101                                         $this->t('Blocked domain'),
102                                         $this->t('Reason for the block'),
103                                 ],
104                                 'download' => $this->t('Download this list in CSV format'),
105                                 'list'     => $blockList,
106                         ];
107                 } else {
108                         $blocked = null;
109                 }
110
111                 $hooked = '';
112
113                 Hook::callAll('about_hook', $hooked);
114
115                 $tpl = Renderer::getMarkupTemplate('friendica.tpl');
116
117                 return Renderer::replaceMacros($tpl, [
118                         'about'     => $this->t('This is Friendica, version %s that is running at the web location %s. The database version is %s, the post update version is %s.',
119                                 '<strong>' . App::VERSION . '</strong>',
120                                 $this->baseUrl,
121                                 '<strong>' . $this->config->get('system', 'build') . '/' . DB_UPDATE_VERSION . '</strong>',
122                                 '<strong>' . $this->keyValue->get('post_update_version') . '/' . PostUpdate::VERSION . '</strong>'),
123                         'friendica' => $this->t('Please visit <a href="https://friendi.ca">Friendi.ca</a> to learn more about the Friendica project.'),
124                         'bugs'      => $this->t('Bug reports and issues: please visit') . ' ' . '<a href="https://github.com/friendica/friendica/issues?state=open">' . $this->t('the bugtracker at github') . '</a>',
125                         'info'      => $this->t('Suggestions, praise, etc. - please email "info" at "friendi - dot - ca'),
126
127                         'visible_addons' => $addon,
128                         'tos'            => $tos,
129                         'block_list'     => $blocked,
130                         'hooked'         => $hooked,
131                 ]);
132         }
133
134         protected function rawContent(array $request = [])
135         {
136                 if (empty($this->parameters['format']) || $this->parameters['format'] !== 'json') {
137                         if (!ActivityPub::isRequest()) {
138                                 return;
139                         }
140
141                         try {
142                                 $data = ActivityPub\Transmitter::getProfile(0);
143                                 header('Access-Control-Allow-Origin: *');
144                                 header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
145                                 $this->jsonExit($data, 'application/activity+json');
146                         } catch (HTTPException\NotFoundException $e) {
147                                 $this->jsonError(404, ['error' => 'Record not found']);
148                         }
149                 }
150
151                 $register_policies = [
152                         Register::CLOSED  => 'REGISTER_CLOSED',
153                         Register::APPROVE => 'REGISTER_APPROVE',
154                         Register::OPEN    => 'REGISTER_OPEN'
155                 ];
156
157                 $register_policy_int = $this->config->get('config', 'register_policy');
158                 if ($register_policy_int !== Register::CLOSED && $this->config->get('config', 'invitation_only')) {
159                         $register_policy = 'REGISTER_INVITATION';
160                 } else {
161                         $register_policy = $register_policies[$register_policy_int];
162                 }
163
164                 $admin = [];
165                 $administrator = User::getFirstAdmin(['username', 'nickname']);
166                 if (!empty($administrator)) {
167                         $admin = [
168                                 'name'    => $administrator['username'],
169                                 'profile' => $this->baseUrl . '/profile/' . $administrator['nickname'],
170                         ];
171                 }
172
173                 $visible_addons = Addon::getVisibleList();
174
175                 $this->config->reload();
176                 $locked_features = [];
177                 $featureLocks = $this->config->get('config', 'feature_lock');
178                 if (isset($featureLocks)) {
179                         foreach ($featureLocks as $feature => $lock) {
180                                 if ($feature === 'config_loaded') {
181                                         continue;
182                                 }
183
184                                 $locked_features[$feature] = intval($lock);
185                         }
186                 }
187
188                 $data = [
189                         'version'          => App::VERSION,
190                         'url'              => (string)$this->baseUrl,
191                         'addons'           => $visible_addons,
192                         'locked_features'  => $locked_features,
193                         'explicit_content' => intval($this->config->get('system', 'explicit_content', 0)),
194                         'language'         => $this->config->get('system', 'language'),
195                         'register_policy'  => $register_policy,
196                         'admin'            => $admin,
197                         'site_name'        => $this->config->get('config', 'sitename'),
198                         'platform'         => strtolower(App::PLATFORM),
199                         'info'             => $this->config->get('config', 'info'),
200                         'no_scrape_url'    => $this->baseUrl . '/noscrape',
201                 ];
202
203                 $this->jsonExit($data);
204         }
205 }