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