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