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