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