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