]> git.mxchange.org Git - friendica.git/blob - src/Module/Friendica.php
Merge remote-tracking branch 'upstream/develop' into more-privacy
[friendica.git] / src / Module / Friendica.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;
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\Capabilities\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                 $register_policy_int = $this->config->get('config', 'register_policy');
98                 if (!empty($blockList) && ($register_policy_int !== Register::CLOSED || $this->session->isAuthenticated())) {
99                         $blocked = [
100                                 'title'    => $this->t('On this server the following remote servers are blocked.'),
101                                 'header'   => [
102                                         $this->t('Blocked domain'),
103                                         $this->t('Reason for the block'),
104                                 ],
105                                 'download' => $this->t('Download this list in CSV format'),
106                                 'list'     => $blockList,
107                         ];
108                 } else {
109                         $blocked = null;
110                 }
111
112                 $hooked = '';
113
114                 Hook::callAll('about_hook', $hooked);
115
116                 $tpl = Renderer::getMarkupTemplate('friendica.tpl');
117
118                 return Renderer::replaceMacros($tpl, [
119                         '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.',
120                                 '<strong>' . App::VERSION . '</strong>',
121                                 $this->baseUrl,
122                                 '<strong>' . $this->config->get('system', 'build') . '/' . DB_UPDATE_VERSION . '</strong>',
123                                 '<strong>' . $this->keyValue->get('post_update_version') . '/' . PostUpdate::VERSION . '</strong>'),
124                         'friendica' => $this->t('Please visit <a href="https://friendi.ca">Friendi.ca</a> to learn more about the Friendica project.'),
125                         '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>',
126                         'info'      => $this->t('Suggestions, praise, etc. - please email "info" at "friendi - dot - ca'),
127
128                         'visible_addons' => $addon,
129                         'tos'            => $tos,
130                         'block_list'     => $blocked,
131                         'hooked'         => $hooked,
132                 ]);
133         }
134
135         protected function rawContent(array $request = [])
136         {
137                 if (empty($this->parameters['format']) || $this->parameters['format'] !== 'json') {
138                         if (!ActivityPub::isRequest()) {
139                                 return;
140                         }
141
142                         try {
143                                 $data = ActivityPub\Transmitter::getProfile(0);
144                                 header('Access-Control-Allow-Origin: *');
145                                 header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
146                                 System::jsonExit($data, 'application/activity+json');
147                         } catch (HTTPException\NotFoundException $e) {
148                                 System::jsonError(404, ['error' => 'Record not found']);
149                         }
150                 }
151
152                 $register_policies = [
153                         Register::CLOSED  => 'REGISTER_CLOSED',
154                         Register::APPROVE => 'REGISTER_APPROVE',
155                         Register::OPEN    => 'REGISTER_OPEN'
156                 ];
157
158                 $register_policy_int = $this->config->get('config', 'register_policy');
159                 if ($register_policy_int !== Register::CLOSED && $this->config->get('config', 'invitation_only')) {
160                         $register_policy = 'REGISTER_INVITATION';
161                 } else {
162                         $register_policy = $register_policies[$register_policy_int];
163                 }
164
165                 $admin = [];
166                 $administrator = User::getFirstAdmin(['username', 'nickname']);
167                 if (!empty($administrator)) {
168                         $admin = [
169                                 'name'    => $administrator['username'],
170                                 'profile' => $this->baseUrl . '/profile/' . $administrator['nickname'],
171                         ];
172                 }
173
174                 $visible_addons = Addon::getVisibleList();
175
176                 $this->config->reload();
177                 $locked_features = [];
178                 $featureLocks = $this->config->get('config', 'feature_lock');
179                 if (isset($featureLocks)) {
180                         foreach ($featureLocks as $feature => $lock) {
181                                 if ($feature === 'config_loaded') {
182                                         continue;
183                                 }
184
185                                 $locked_features[$feature] = intval($lock);
186                         }
187                 }
188
189                 $data = [
190                         'version'          => App::VERSION,
191                         'url'              => (string)$this->baseUrl,
192                         'addons'           => $visible_addons,
193                         'locked_features'  => $locked_features,
194                         'explicit_content' => intval($this->config->get('system', 'explicit_content', 0)),
195                         'language'         => $this->config->get('system', 'language'),
196                         'register_policy'  => $register_policy,
197                         'admin'            => $admin,
198                         'site_name'        => $this->config->get('config', 'sitename'),
199                         'platform'         => strtolower(App::PLATFORM),
200                         'info'             => $this->config->get('config', 'info'),
201                         'no_scrape_url'    => $this->baseUrl . '/noscrape',
202                 ];
203
204                 System::jsonExit($data);
205         }
206 }