]> git.mxchange.org Git - friendica.git/blob - src/Module/RemoteFollow.php
Merge pull request #11012 from annando/api-relations
[friendica.git] / src / Module / RemoteFollow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\BaseURL;
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\System;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Profile;
36 use Friendica\Model\User;
37 use Friendica\Network\HTTPException;
38 use Friendica\Network\Probe;
39
40 /**
41  * Remotely follow the account on this system by the provided account
42  */
43 class RemoteFollow extends BaseModule
44 {
45         /** @var array */
46         protected $owner;
47         /** @var Page */
48         protected $page;
49         /** @var BaseURL */
50         protected $baseUrl;
51
52         public function __construct(L10n $l10n, Page $page, BaseURL $baseUrl, array $parameters = [])
53         {
54                 parent::__construct($l10n, $parameters);
55
56                 $this->owner = User::getOwnerDataByNick($this->parameters['profile']);
57                 if (!$this->owner) {
58                         throw new HTTPException\NotFoundException($this->t('User not found.'));
59                 }
60
61                 $this->baseUrl = $baseUrl;
62                 $this->page    = $page;
63         }
64
65         public function post()
66         {
67                 if (!empty($_POST['cancel']) || empty($_POST['dfrn_url'])) {
68                         $this->baseUrl->redirect();
69                 }
70         
71                 if (empty($this->owner)) {
72                         notice($this->t('Profile unavailable.'));
73                         return;
74                 }
75                 
76                 $url = Probe::cleanURI($_POST['dfrn_url']);
77                 if (!strlen($url)) {
78                         notice($this->t("Invalid locator"));
79                         return;
80                 }
81
82                 // Detect the network, make sure the provided URL is valid
83                 $data = Contact::getByURL($url);
84                 if (!$data) {
85                         notice($this->t("The provided profile link doesn't seem to be valid"));
86                         return;
87                 }
88
89                 if (empty($data['subscribe'])) {
90                         notice($this->t("Remote subscription can't be done for your network. Please subscribe directly on your system."));
91                         return;
92                 }
93
94                 Logger::notice('Remote request', ['url' => $url, 'follow' => $this->owner['url'], 'remote' => $data['subscribe']]);
95                 
96                 // Substitute our user's feed URL into $data['subscribe']
97                 // Send the subscriber home to subscribe
98                 // Diaspora needs the uri in the format user@domain.tld
99                 if ($data['network'] == Protocol::DIASPORA) {
100                         $uri = urlencode($this->owner['addr']);
101                 } else {
102                         $uri = urlencode($this->owner['url']);
103                 }
104         
105                 $follow_link = str_replace('{uri}', $uri, $data['subscribe']);
106                 System::externalRedirect($follow_link);
107         }
108
109         public function content(): string
110         {
111                 if (empty($this->owner)) {
112                         return '';
113                 }
114
115                 $this->page['aside'] = Widget\VCard::getHTML($this->owner);
116
117                 $target_addr = $this->owner['addr'];
118                 $target_url = $this->owner['url'];
119
120                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
121                 $o = Renderer::replaceMacros($tpl, [
122                         '$header'        => $this->t('Friend/Connection Request'),
123                         '$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),
124                         '$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'),
125                         '$your_address'  => $this->t('Your Webfinger address or profile URL:'),
126                         '$pls_answer'    => $this->t('Please answer the following:'),
127                         '$submit'        => $this->t('Submit Request'),
128                         '$cancel'        => $this->t('Cancel'),
129
130                         '$request'       => 'remote_follow/' . $this->parameters['profile'],
131                         '$name'          => $this->owner['name'],
132                         '$myaddr'        => Profile::getMyURL(),
133                 ]);
134                 return $o;
135         }
136 }