]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/RemoteFollow.php
Merge pull request #12228 from MrPetovan/task/4090-move-mod-photos
[friendica.git] / src / Module / Profile / RemoteFollow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Profile;
23
24 use Friendica\App;
25 use Friendica\App\Page;
26 use Friendica\BaseModule;
27 use Friendica\Content\Widget;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Protocol;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Search;
33 use Friendica\Core\Session\Capability\IHandleUserSessions;
34 use Friendica\Core\System;
35 use Friendica\DI;
36 use Friendica\Model\Contact;
37 use Friendica\Model\Profile;
38 use Friendica\Model\User;
39 use Friendica\Module\Response;
40 use Friendica\Navigation\SystemMessages;
41 use Friendica\Network\HTTPException;
42 use Friendica\Network\Probe;
43 use Friendica\Util\Profiler;
44 use Psr\Log\LoggerInterface;
45
46 /**
47  * Remotely follow the account on this system by the provided account
48  */
49 class RemoteFollow extends BaseModule
50 {
51         /** @var SystemMessages */
52         private $systemMessages;
53         /** @var Page */
54         protected $page;
55         /** @var IHandleUserSessions */
56         private $userSession;
57
58         /** @var array */
59         protected $owner;
60
61         public function __construct(IHandleUserSessions $userSession, SystemMessages $systemMessages, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, App\Page $page, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
62         {
63                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
64
65                 $this->systemMessages = $systemMessages;
66                 $this->page           = $page;
67                 $this->userSession    = $userSession;
68
69                 $this->owner = User::getOwnerDataByNick($this->parameters['nickname']);
70                 if (!$this->owner) {
71                         throw new HTTPException\NotFoundException($this->t('User not found.'));
72                 }
73         }
74
75         protected function post(array $request = [])
76         {
77                 if (!empty($request['cancel']) || empty($request['dfrn_url'])) {
78                         $this->baseUrl->redirect('profile/' . $this->parameters['nickname']);
79                 }
80         
81                 if (empty($this->owner)) {
82                         $this->systemMessages->addNotice($this->t('Profile unavailable.'));
83                         return;
84                 }
85
86                 $url = Probe::cleanURI($request['dfrn_url']);
87                 if (!strlen($url)) {
88                         $this->systemMessages->addNotice($this->t('Invalid locator'));
89                         return;
90                 }
91
92                 // Detect the network, make sure the provided URL is valid
93                 $data = Contact::getByURL($url);
94                 if (!$data) {
95                         $this->systemMessages->addNotice($this->t("The provided profile link doesn't seem to be valid"));
96                         return;
97                 }
98
99                 if (empty($data['subscribe'])) {
100                         $this->systemMessages->addNotice($this->t("Remote subscription can't be done for your network. Please subscribe directly on your system."));
101                         return;
102                 }
103
104                 $this->logger->notice('Remote request', ['url' => $url, 'follow' => $this->owner['url'], 'remote' => $data['subscribe']]);
105
106                 // Substitute our user's feed URL into $data['subscribe']
107                 // Send the subscriber home to subscribe
108                 // Diaspora needs the uri in the format user@domain.tld
109                 if ($data['network'] == Protocol::DIASPORA) {
110                         $uri = urlencode($this->owner['addr']);
111                 } else {
112                         $uri = urlencode($this->owner['url']);
113                 }
114
115                 $follow_link = str_replace('{uri}', $uri, $data['subscribe']);
116                 System::externalRedirect($follow_link);
117         }
118
119         protected function content(array $request = []): string
120         {
121                 $this->page['aside'] = Widget\VCard::getHTML($this->owner);
122
123                 $target_addr = $this->owner['addr'];
124                 $target_url  = $this->owner['url'];
125
126                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
127                 return Renderer::replaceMacros($tpl, [
128                         '$header'        => $this->t('Friend/Connection Request'),
129                         '$page_desc'     => $this->t('Enter your Webfinger address (user@domain.tld) or profile URL here. If this isn\'t supported by your system, you have to subscribe to <strong>%s</strong> or <strong>%s</strong> directly on your system.', $target_addr, $target_url),
130                         '$invite_desc'   => $this->t('If you are not yet a member of the free social web, <a href="%s">follow this link to find a public Friendica node and join us today</a>.', Search::getGlobalDirectory() . '/servers'),
131                         '$your_address'  => $this->t('Your Webfinger address or profile URL:'),
132                         '$pls_answer'    => $this->t('Please answer the following:'),
133                         '$submit'        => $this->t('Submit Request'),
134                         '$cancel'        => $this->t('Cancel'),
135
136                         '$action'        => 'profile/' . $this->parameters['nickname'] . '/remote_follow',
137                         '$name'          => $this->owner['name'],
138                         '$myaddr'        => $this->userSession->getMyUrl(),
139                 ]);
140         }
141 }