]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
2a5241a0ca47673f853fb25c94a742421887a719
[friendica.git] / mod / follow.php
1 <?php
2 /**
3  * @file mod/follow.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\Renderer;
8 use Friendica\DI;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Profile;
11 use Friendica\Model\Item;
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                 // Possibly it is a remote item and not an account
38                 follow_remote_item($url);
39
40                 if ($result['message']) {
41                         notice($result['message']);
42                 }
43                 DI::baseUrl()->redirect($return_path);
44         } elseif ($result['cid']) {
45                 DI::baseUrl()->redirect('contact/' . $result['cid']);
46         }
47
48         info(DI::l10n()->t('The contact could not be added.'));
49
50         DI::baseUrl()->redirect($return_path);
51         // NOTREACHED
52 }
53
54 function follow_content(App $a)
55 {
56         $return_path = 'contact';
57
58         if (!local_user()) {
59                 notice(DI::l10n()->t('Permission denied.'));
60                 DI::baseUrl()->redirect($return_path);
61                 // NOTREACHED
62         }
63
64         $uid = local_user();
65
66         // Issue 4815: Silently removing a prefixing @
67         $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
68
69         // Issue 6874: Allow remote following from Peertube
70         if (strpos($url, 'acct:') === 0) {
71                 $url = str_replace('acct:', '', $url);
72         }
73
74         if (!$url) {
75                 DI::baseUrl()->redirect($return_path);
76         }
77
78         $submit = DI::l10n()->t('Submit Request');
79
80         // Don't try to add a pending contact
81         $r = q("SELECT `pending` FROM `contact` WHERE `uid` = %d AND ((`rel` != %d) OR (`network` = '%s')) AND
82                 (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND
83                 `network` != '%s' LIMIT 1",
84                 intval(local_user()), DBA::escape(Contact::FOLLOWER), DBA::escape(Protocol::DFRN), DBA::escape(Strings::normaliseLink($url)),
85                 DBA::escape(Strings::normaliseLink($url)), DBA::escape($url), DBA::escape(Protocol::STATUSNET));
86
87         if ($r) {
88                 if ($r[0]['pending']) {
89                         notice(DI::l10n()->t('You already added this contact.'));
90                         $submit = '';
91                         //$a->internalRedirect($_SESSION['return_path']);
92                         // NOTREACHED
93                 }
94         }
95
96         $ret = Probe::uri($url);
97
98         $protocol = Contact::getProtocol($ret['url'], $ret['network']);
99
100         if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
101                 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
102                 $submit = '';
103                 //$a->internalRedirect($_SESSION['return_path']);
104                 // NOTREACHED
105         }
106
107         if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
108                 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
109                 $submit = '';
110                 //$a->internalRedirect($_SESSION['return_path']);
111                 // NOTREACHED
112         }
113
114         if ($protocol == Protocol::PHANTOM) {
115                 // Possibly it is a remote item and not an account
116                 follow_remote_item($url);
117
118                 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
119                 $submit = '';
120                 //$a->internalRedirect($_SESSION['return_path']);
121                 // NOTREACHED
122         }
123
124         if ($protocol == Protocol::MAIL) {
125                 $ret['url'] = $ret['addr'];
126         }
127
128         if (($protocol === Protocol::DFRN) && !DBA::isResult($r)) {
129                 $request = $ret['request'];
130                 $tpl = Renderer::getMarkupTemplate('dfrn_request.tpl');
131         } else {
132                 $request = DI::baseUrl() . '/follow';
133                 $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
134         }
135
136         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
137
138         if (!$r) {
139                 notice(DI::l10n()->t('Permission denied.'));
140                 DI::baseUrl()->redirect($return_path);
141                 // NOTREACHED
142         }
143
144         $myaddr = $r[0]['url'];
145         $gcontact_id = 0;
146
147         // Makes the connection request for friendica contacts easier
148         $_SESSION['fastlane'] = $ret['url'];
149
150         $r = q("SELECT `id`, `location`, `about`, `keywords` FROM `gcontact` WHERE `nurl` = '%s'",
151                 Strings::normaliseLink($ret['url']));
152
153         if (!$r) {
154                 $r = [['location' => '', 'about' => '', 'keywords' => '']];
155         } else {
156                 $gcontact_id = $r[0]['id'];
157         }
158
159         if ($protocol === Protocol::DIASPORA) {
160                 $r[0]['location'] = '';
161                 $r[0]['about'] = '';
162         }
163
164         $o = Renderer::replaceMacros($tpl, [
165                 '$header'        => DI::l10n()->t('Connect/Follow'),
166                 '$desc'          => '',
167                 '$pls_answer'    => DI::l10n()->t('Please answer the following:'),
168                 '$does_know_you' => ['knowyou', DI::l10n()->t('Does %s know you?', $ret['name'])],
169                 '$add_note'      => DI::l10n()->t('Add a personal note:'),
170                 '$page_desc'     => '',
171                 '$friendica'     => '',
172                 '$statusnet'     => '',
173                 '$diaspora'      => '',
174                 '$diasnote'      => '',
175                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
176                 '$invite_desc'   => '',
177                 '$emailnet'      => '',
178                 '$submit'        => $submit,
179                 '$cancel'        => DI::l10n()->t('Cancel'),
180                 '$nickname'      => '',
181                 '$name'          => $ret['name'],
182                 '$url'           => $ret['url'],
183                 '$zrl'           => Profile::zrl($ret['url']),
184                 '$url_label'     => DI::l10n()->t('Profile URL'),
185                 '$myaddr'        => $myaddr,
186                 '$request'       => $request,
187                 '$keywords'      => $r[0]['keywords'],
188                 '$keywords_label'=> DI::l10n()->t('Tags:')
189         ]);
190
191         DI::page()['aside'] = '';
192
193         $profiledata = Contact::getDetailsByURL($ret['url']);
194         if ($profiledata) {
195                 Profile::load($a, '', $profiledata, false);
196         }
197
198         if ($gcontact_id <> 0) {
199                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
200                         ['$title' => DI::l10n()->t('Status Messages and Posts')]
201                 );
202
203                 // Show last public posts
204                 $o .= Contact::getPostsFromUrl($ret['url']);
205         }
206
207         return $o;
208 }
209
210 function follow_remote_item($url)
211 {
212         $item_id = Item::fetchByLink($url, local_user());
213         if (!$item_id) {
214                 // If the user-specific search failed, we search and probe a public post
215                 $item_id = Item::fetchByLink($url);
216         }
217
218         if (!empty($item_id)) {
219                 $item = Item::selectFirst(['guid'], ['id' => $item_id]);
220                 if (DBA::isResult($item)) {
221                         DI::baseUrl()->redirect('display/' . $item['guid']);
222                 }
223         }
224 }