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