]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
Display the publish time in the local timezone
[friendica.git] / mod / follow.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Widget;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\Renderer;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Profile;
29 use Friendica\Model\Item;
30 use Friendica\Network\Probe;
31 use Friendica\Database\DBA;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Util\Strings;
35
36 function follow_post(App $a)
37 {
38         if (!local_user()) {
39                 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
40         }
41
42         if (isset($_REQUEST['cancel'])) {
43                 DI::baseUrl()->redirect('contact');
44         }
45
46         $url = Probe::cleanURI($_REQUEST['url']);
47
48         follow_process($a, $url);
49 }
50
51 function follow_content(App $a)
52 {
53         $return_path = 'contact';
54
55         if (!local_user()) {
56                 notice(DI::l10n()->t('Permission denied.'));
57                 DI::baseUrl()->redirect($return_path);
58                 // NOTREACHED
59         }
60
61         $uid = local_user();
62
63         // Issue 4815: Silently removing a prefixing @
64         $url = ltrim(Strings::escapeTags(trim($_REQUEST['url'] ?? '')), '@!');
65
66         // Issue 6874: Allow remote following from Peertube
67         if (strpos($url, 'acct:') === 0) {
68                 $url = str_replace('acct:', '', $url);
69         }
70
71         if (!$url) {
72                 DI::baseUrl()->redirect($return_path);
73         }
74
75         $submit = DI::l10n()->t('Submit Request');
76
77         // Don't try to add a pending contact
78         $user_contact = DBA::selectFirst('contact', ['pending'], ["`uid` = ? AND ((`rel` != ?) OR (`network` = ?)) AND
79                 (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `network` != ?",
80                 $uid, Contact::FOLLOWER, Protocol::DFRN, Strings::normaliseLink($url),
81                 Strings::normaliseLink($url), $url, Protocol::STATUSNET]);
82
83         if (DBA::isResult($user_contact)) {
84                 if ($user_contact['pending']) {
85                         notice(DI::l10n()->t('You already added this contact.'));
86                         $submit = '';
87                 }
88         }
89
90         $contact = Contact::getByURL($url, true);
91
92         // Possibly it is a mail contact
93         if (empty($contact)) {
94                 $contact = Probe::uri($url, Protocol::MAIL, $uid);
95         }
96
97         if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
98                 // Possibly it is a remote item and not an account
99                 follow_remote_item($url);
100
101                 notice(DI::l10n()->t("The network type couldn't be detected. Contact can't be added."));
102                 $submit = '';
103                 $contact = ['url' => $url, 'network' => Protocol::PHANTOM, 'name' => $url, 'keywords' => ''];
104         }
105
106         $protocol = Contact::getProtocol($contact['url'], $contact['network']);
107
108         if (($protocol == Protocol::DIASPORA) && !DI::config()->get('system', 'diaspora_enabled')) {
109                 notice(DI::l10n()->t("Diaspora support isn't enabled. Contact can't be added."));
110                 $submit = '';
111         }
112
113         if (($protocol == Protocol::OSTATUS) && DI::config()->get('system', 'ostatus_disabled')) {
114                 notice(DI::l10n()->t("OStatus support is disabled. Contact can't be added."));
115                 $submit = '';
116         }
117
118         if ($protocol == Protocol::MAIL) {
119                 $contact['url'] = $contact['addr'];
120         }
121
122         if (!empty($_REQUEST['auto'])) {
123                 follow_process($a, $contact['url']);
124         }
125
126         $request = DI::baseUrl() . '/follow';
127         $tpl = Renderer::getMarkupTemplate('auto_request.tpl');
128
129         $owner = User::getOwnerDataById($uid);
130         if (empty($owner)) {
131                 notice(DI::l10n()->t('Permission denied.'));
132                 DI::baseUrl()->redirect($return_path);
133                 // NOTREACHED
134         }
135
136         $myaddr = $owner['url'];
137
138         $o = Renderer::replaceMacros($tpl, [
139                 '$header'        => DI::l10n()->t('Connect/Follow'),
140                 '$pls_answer'    => DI::l10n()->t('Please answer the following:'),
141                 '$your_address'  => DI::l10n()->t('Your Identity Address:'),
142                 '$url_label'     => DI::l10n()->t('Profile URL'),
143                 '$keywords_label'=> DI::l10n()->t('Tags:'),
144                 '$submit'        => $submit,
145                 '$cancel'        => DI::l10n()->t('Cancel'),
146
147                 '$request'       => $request,
148                 '$name'          => $contact['name'],
149                 '$url'           => $contact['url'],
150                 '$zrl'           => Profile::zrl($contact['url']),
151                 '$myaddr'        => $myaddr,
152                 '$keywords'      => $contact['keywords'],
153
154                 '$does_know_you' => ['knowyou', DI::l10n()->t('%s knows you', $contact['name'])],
155                 '$addnote_field' => ['dfrn-request-message', DI::l10n()->t('Add a personal note:')],
156         ]);
157
158         DI::page()['aside'] = '';
159
160         if ($protocol != Protocol::PHANTOM) {
161                 DI::page()['aside'] = Widget\VCard::getHTML(Contact::getByURL($contact['url'], false));
162
163                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'),
164                         ['$title' => DI::l10n()->t('Status Messages and Posts')]
165                 );
166
167                 // Show last public posts
168                 $o .= Contact::getPostsFromUrl($contact['url']);
169         }
170
171         return $o;
172 }
173
174 function follow_process(App $a, string $url)
175 {
176         $return_path = 'follow?url=' . urlencode($url);
177
178         $result = Contact::createFromProbe($a->user, $url, true);
179
180         if ($result['success'] == false) {
181                 // Possibly it is a remote item and not an account
182                 follow_remote_item($url);
183
184                 if ($result['message']) {
185                         notice($result['message']);
186                 }
187                 DI::baseUrl()->redirect($return_path);
188         } elseif ($result['cid']) {
189                 DI::baseUrl()->redirect('contact/' . $result['cid']);
190         }
191
192         notice(DI::l10n()->t('The contact could not be added.'));
193
194         DI::baseUrl()->redirect($return_path);
195 }
196
197 function follow_remote_item($url)
198 {
199         $item_id = Item::fetchByLink($url, local_user());
200         if (!$item_id) {
201                 // If the user-specific search failed, we search and probe a public post
202                 $item_id = Item::fetchByLink($url);
203         }
204
205         if (!empty($item_id)) {
206                 $item = Post::selectFirst(['guid'], ['id' => $item_id]);
207                 if (DBA::isResult($item)) {
208                         DI::baseUrl()->redirect('display/' . $item['guid']);
209                 }
210         }
211 }