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