]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Merge pull request #8141 from annando/brief2
[friendica.git] / mod / follow.php
1 <?php
2 /**
3  * @file mod/follow.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Config;
7 use Friendica\Core\Protocol;
8 use Friendica\Core\Renderer;
9 use Friendica\DI;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Profile;
12 use Friendica\Network\Probe;
13 use Friendica\Database\DBA;
14 use Friendica\Util\Strings;
15
16 function follow_post(App $a)
17 {
18         if (!local_user()) {
19                 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
20         }
21
22         if (isset($_REQUEST['cancel'])) {
23                 DI::baseUrl()->redirect('contact');
24         }
25
26         $uid = local_user();
27         $url = Strings::escapeTags(trim($_REQUEST['url']));
28         $return_path = 'follow?url=' . urlencode($url);
29
30         // Makes the connection request for friendica contacts easier
31         // This is just a precaution if maybe this page is called somewhere directly via POST
32         $_SESSION['fastlane'] = $url;
33
34         $result = Contact::createFromProbe($uid, $url, true);
35
36         if ($result['success'] == false) {
37                 if ($result['message']) {
38                         notice($result['message']);
39                 }
40                 DI::baseUrl()->redirect($return_path);
41         } elseif ($result['cid']) {
42                 DI::baseUrl()->redirect('contact/' . $result['cid']);
43         }
44
45         info(DI::l10n()->t('The contact could not be added.'));
46
47         DI::baseUrl()->redirect($return_path);
48         // NOTREACHED
49 }
50
51 function follow_content(App $a)
52 {
53         $return_path = 'contact';
54
55         if (!local_user()) {
56                 notice(DI::l10n()->t('Permission denied.'));
57                 DI::baseUrl()->redirect($return_path);
58                 // NOTREACHED
59         }
60
61         $uid = local_user();
62
63         // Issue 4815: Silently removing a prefixing @
64         $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
65
66         // Issue 6874: Allow remote following from Peertube
67         if (strpos($url, 'acct:') === 0) {
68                 $url = str_replace('acct:', '', $url);
69         }
70
71         if (!$url) {
72                 DI::baseUrl()->redirect($return_path);
73         }
74
75         $submit = DI::l10n()->t('Submit Request');
76
77         // Don't try to add a pending contact
78         $r = q("SELECT `pending` FROM `contact` WHERE `uid` = %d AND ((`rel` != %d) OR (`network` = '%s')) AND
79                 (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND
80                 `network` != '%s' LIMIT 1",
81                 intval(local_user()), DBA::escape(Contact::FOLLOWER), DBA::escape(Protocol::DFRN), DBA::escape(Strings::normaliseLink($url)),
82                 DBA::escape(Strings::normaliseLink($url)), DBA::escape($url), DBA::escape(Protocol::STATUSNET));
83
84         if ($r) {
85                 if ($r[0]['pending']) {
86                         notice(DI::l10n()->t('You already added this contact.'));
87                         $submit = '';
88                         //$a->internalRedirect($_SESSION['return_path']);
89                         // NOTREACHED
90                 }
91         }
92
93         $ret = Probe::uri($url);
94
95         $protocol = Contact::getProtocol($ret['url'], $ret['network']);
96
97         if (($protocol == Protocol::DIASPORA) && !Config::get('system', 'diaspora_enabled')) {
98                 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
99                 $submit = '';
100                 //$a->internalRedirect($_SESSION['return_path']);
101                 // NOTREACHED
102         }
103
104         if (($protocol == Protocol::OSTATUS) && Config::get('system', 'ostatus_disabled')) {
105                 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
106                 $submit = '';
107                 //$a->internalRedirect($_SESSION['return_path']);
108                 // NOTREACHED
109         }
110
111         if ($protocol == Protocol::PHANTOM) {
112                 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
113                 $submit = '';
114                 //$a->internalRedirect($_SESSION['return_path']);
115                 // NOTREACHED
116         }
117
118         if ($protocol == Protocol::MAIL) {
119                 $ret['url'] = $ret['addr'];
120         }
121
122         if (($protocol === Protocol::DFRN) && !DBA::isResult($r)) {
123                 $request = $ret['request'];
124                 $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
125         } else {
126                 $request = DI::baseUrl() . '/follow';
127                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
128         }
129
130         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
131
132         if (!$r) {
133                 notice(DI::l10n()->t('Permission denied.'));
134                 DI::baseUrl()->redirect($return_path);
135                 // NOTREACHED
136         }
137
138         $myaddr = $r[0]['url'];
139         $gcontact_id = 0;
140
141         // Makes the connection request for friendica contacts easier
142         $_SESSION['fastlane'] = $ret['url'];
143
144         $r = q("SELECT `id`, `location`, `about`, `keywords` FROM `gcontact` WHERE `nurl` = '%s'",
145                 Strings::normaliseLink($ret['url']));
146
147         if (!$r) {
148                 $r = [['location' => '', 'about' => '', 'keywords' => '']];
149         } else {
150                 $gcontact_id = $r[0]['id'];
151         }
152
153         if ($protocol === Protocol::DIASPORA) {
154                 $r[0]['location'] = '';
155                 $r[0]['about'] = '';
156         }
157
158         $o = Renderer::replaceMacros($tpl, [
159                 '$header'        => DI::l10n()->t('Connect/Follow'),
160                 '$desc'          => '',
161                 '$pls_answer'    => DI::l10n()->t('Please answer the following:'),
162                 '$does_know_you' => ['knowyou', DI::l10n()->t('Does %s know you?', $ret['name']), false, '', [DI::l10n()->t('No'), DI::l10n()->t('Yes')]],
163                 '$add_note'      => DI::l10n()->t('Add a personal note:'),
164                 '$page_desc'     => '',
165                 '$friendica'     => '',
166                 '$statusnet'     => '',
167                 '$diaspora'      => '',
168                 '$diasnote'      => '',
169                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
170                 '$invite_desc'   => '',
171                 '$emailnet'      => '',
172                 '$submit'        => $submit,
173                 '$cancel'        => DI::l10n()->t('Cancel'),
174                 '$nickname'      => '',
175                 '$name'          => $ret['name'],
176                 '$url'           => $ret['url'],
177                 '$zrl'           => Profile::zrl($ret['url']),
178                 '$url_label'     => DI::l10n()->t('Profile URL'),
179                 '$myaddr'        => $myaddr,
180                 '$request'       => $request,
181                 '$keywords'      => $r[0]['keywords'],
182                 '$keywords_label'=> DI::l10n()->t('Tags:')
183         ]);
184
185         DI::page()['aside'] = '';
186
187         $profiledata = Contact::getDetailsByURL($ret['url']);
188         if ($profiledata) {
189                 Profile::load($a, '', 0, $profiledata, false);
190         }
191
192         if ($gcontact_id <> 0) {
193                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
194                         ['$title' => DI::l10n()->t('Status Messages and Posts')]
195                 );
196
197                 // Show last public posts
198                 $o .= Contact::getPostsFromUrl($ret['url']);
199         }
200
201         return $o;
202 }