]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Replaced argv/argc with replacement functions
[friendica.git] / mod / display.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\Text\BBCode;
24 use Friendica\Content\Widget;
25 use Friendica\Core\ACL;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Item;
34 use Friendica\Model\Post;
35 use Friendica\Model\User;
36 use Friendica\Module\ActivityPub\Objects;
37 use Friendica\Network\HTTPException;
38 use Friendica\Protocol\ActivityPub;
39 use Friendica\Protocol\DFRN;
40
41 function display_init(App $a)
42 {
43         if (ActivityPub::isRequest()) {
44                 Objects::rawContent(['guid' => DI::args()->getArgv()[1] ?? null]);
45         }
46
47         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
48                 return;
49         }
50
51         $nick = ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : '');
52
53         $item = null;
54         $item_user = local_user();
55
56         $fields = ['uri-id', 'parent-uri-id', 'author-id', 'author-link', 'body', 'uid', 'guid', 'gravity'];
57
58         // If there is only one parameter, then check if this parameter could be a guid
59         if (DI::args()->getArgc() == 2) {
60                 $nick = '';
61
62                 // Does the local user have this item?
63                 if (local_user()) {
64                         $item = Post::selectFirstForUser(local_user(), $fields, ['guid' => DI::args()->getArgv()[1], 'uid' => local_user()]);
65                         if (DBA::isResult($item)) {
66                                 $nick = $a->user['nickname'];
67                         }
68                 }
69
70                 // Is this item private but could be visible to the remove visitor?
71                 if (!DBA::isResult($item) && remote_user()) {
72                         $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
73                         if (DBA::isResult($item)) {
74                                 if (!Contact::isFollower(remote_user(), $item['uid'])) {
75                                         $item = null;
76                                 } else {
77                                         $item_user = $item['uid'];
78                                 }
79                         }
80                 }
81
82                 // Is it an item with uid=0?
83                 if (!DBA::isResult($item)) {
84                         $item = Post::selectFirstForUser(local_user(), $fields, ['guid' => DI::args()->getArgv()[1], 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
85                 }
86         } elseif (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
87                 $uri_id = DI::args()->getArgv()[2];
88                 if (substr($uri_id, -5) == '.atom') {
89                         $uri_id = substr($uri_id, 0, -5);
90                 }
91                 $item = Post::selectFirstForUser(local_user(), $fields, ['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
92         }
93
94         if (!DBA::isResult($item)) {
95                 return;
96         }
97
98         if (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
99                 displayShowFeed($item['uri-id'], $item['uid'], DI::args()->getArgc() > 3 && DI::args()->getArgv()[3] == 'conversation.atom');
100         }
101
102         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
103                 Logger::log('Directly serving XML for uri-id '.$item['uri-id'], Logger::DEBUG);
104                 displayShowFeed($item['uri-id'], $item['uid'], false);
105         }
106
107         if ($item['gravity'] != GRAVITY_PARENT) {
108                 $parent = Post::selectFirstForUser($item_user, $fields, ['uid' => $item['uid'], 'uri-id' => $item['parent-uri-id']]);
109                 $item = $parent ?: $item;
110         }
111
112         $profiledata = display_fetchauthor($item);
113
114         DI::page()['aside'] = Widget\VCard::getHTML($profiledata);
115 }
116
117 function display_fetchauthor($item)
118 {
119         $profiledata = Contact::getByURLForUser($item['author-link'], local_user());
120
121         // Check for a repeated message
122         $shared = Item::getShareArray($item);
123         if (!empty($shared) && empty($shared['comment'])) {
124                 $profiledata = [
125                         'uid' => -1,
126                         'id' => -1,
127                         'nickname' => '',
128                         'name' => '',
129                         'picdate' => '',
130                         'photo' => '',
131                         'url' => '',
132                         'network' => '',
133                 ];
134
135                 if (!empty($shared['author'])) {
136                         $profiledata['name'] = $shared['author'];
137                 }
138
139                 if (!empty($shared['profile'])) {
140                         $profiledata['url'] = $shared['profile'];
141                 }
142
143                 if (!empty($shared['avatar'])) {
144                         $profiledata['photo'] = $shared['avatar'];
145                 }
146
147                 $profiledata['nickname'] = $profiledata['name'];
148                 $profiledata['network'] = Protocol::matchByProfileUrl($profiledata['url']);
149
150                 $profiledata['address'] = '';
151                 $profiledata['about'] = '';
152
153                 $profiledata = Contact::getByURLForUser($profiledata['url'], local_user()) ?: $profiledata;
154         }
155
156         if (!empty($profiledata['photo'])) {
157                 $profiledata['photo'] = DI::baseUrl()->remove($profiledata['photo']);
158         }
159
160         return $profiledata;
161 }
162
163 function display_content(App $a, $update = false, $update_uid = 0)
164 {
165         if (DI::config()->get('system','block_public') && !Session::isAuthenticated()) {
166                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
167         }
168
169         $o = '';
170
171         $item = null;
172
173         $force = (bool)($_REQUEST['force'] ?? false);
174
175         if ($update) {
176                 $uri_id = $_REQUEST['uri_id'];
177                 $item = Post::selectFirst(['uid', 'parent-uri-id'], ['uri-id' => $uri_id, 'uid' => $update_uid]);
178                 if (!empty($item)) {
179                         if ($item['uid'] != 0) {
180                                 $a->setProfileOwner($item['uid']);
181                         } else {
182                                 $a->setProfileOwner($update_uid);
183                         }
184                         $parent_uri_id = $item['parent-uri-id'];
185                 }
186         } else {
187                 $uri_id = ((DI::args()->getArgc() > 2) ? DI::args()->getArgv()[2] : 0);
188                 $parent_uri_id = $uri_id;
189
190                 if (DI::args()->getArgc() == 2) {
191                         $fields = ['uri-id', 'parent-uri-id', 'uid'];
192
193                         if (local_user()) {
194                                 $condition = ['guid' => DI::args()->getArgv()[1], 'uid' => local_user()];
195                                 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
196                                 if (DBA::isResult($item)) {
197                                         $uri_id = $item['uri-id'];
198                                         $parent_uri_id = $item['parent-uri-id'];
199                                 }
200                         }
201
202                         if (($parent_uri_id == 0) && remote_user()) {
203                                 $item = Post::selectFirst($fields, ['guid' => DI::args()->getArgv()[1], 'private' => Item::PRIVATE, 'origin' => true]);
204                                 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
205                                         $uri_id = $item['uri-id'];
206                                         $parent_uri_id = $item['parent-uri-id'];
207                                 }
208                         }
209
210                         if ($parent_uri_id == 0) {
211                                 $condition = ['private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => DI::args()->getArgv()[1], 'uid' => 0];
212                                 $item = Post::selectFirstForUser(local_user(), $fields, $condition);
213                                 if (DBA::isResult($item)) {
214                                         $uri_id = $item['uri-id'];
215                                         $parent_uri_id = $item['parent-uri-id'];
216                                 }
217                         }
218                 }
219         }
220
221         if (empty($item)) {
222                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
223         }
224
225         if (!DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
226                 DBA::update('notification', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
227                 DBA::update('notify', ['seen' => true], ['parent-uri-id' => $item['parent-uri-id'], 'uid' => local_user()]);
228         }
229
230         // We are displaying an "alternate" link if that post was public. See issue 2864
231         $is_public = Post::exists(['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
232         if ($is_public) {
233                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
234                 $alternate = DI::baseUrl().'/display/feed-item/'.$uri_id.'.atom';
235                 $conversation = DI::baseUrl().'/display/feed-item/' . $parent_uri_id . '/conversation.atom';
236         } else {
237                 $alternate = '';
238                 $conversation = '';
239         }
240
241         DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
242                                 ['$alternate' => $alternate,
243                                         '$conversation' => $conversation]);
244
245         $is_remote_contact = false;
246         $item_uid = local_user();
247         $page_uid = 0;
248
249         $parent = null;
250         if (!empty($parent_uri_id)) {
251                 $parent = Post::selectFirst(['uid'], ['uri-id' => $parent_uri_id, 'wall' => true]);
252         }
253
254         if (DBA::isResult($parent)) {
255                 $page_uid = $page_uid ?? 0 ?: $parent['uid'];
256                 $is_remote_contact = Session::getRemoteContactID($page_uid);
257                 if ($is_remote_contact) {
258                         $item_uid = $parent['uid'];
259                 }
260         } else {
261                 $page_uid = $item['uid'];
262         }
263
264         if (!empty($page_uid) && ($page_uid != local_user())) {
265                 $page_user = User::getById($page_uid);
266         }
267
268         $is_owner = local_user() && (in_array($page_uid, [local_user(), 0]));
269
270         if (!empty($page_user['hidewall']) && !$is_owner && !$is_remote_contact) {
271                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
272         }
273
274         // We need the editor here to be able to reshare an item.
275         if ($is_owner && !$update) {
276                 $x = [
277                         'is_owner' => true,
278                         'allow_location' => $a->user['allow_location'],
279                         'default_location' => $a->user['default-location'],
280                         'nickname' => $a->user['nickname'],
281                         'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
282                         'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
283                         'bang' => '',
284                         'visitor' => 'block',
285                         'profile_uid' => local_user(),
286                 ];
287                 $o .= status_editor($a, $x, 0, true);
288         }
289         $sql_extra = Item::getPermissionsSQLByUserId($page_uid);
290
291         if (local_user() && (local_user() == $page_uid)) {
292                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
293                 $unseen = Post::exists($condition);
294         } else {
295                 $unseen = false;
296         }
297
298         if ($update && !$unseen && !$force) {
299                 return '';
300         }
301
302         $condition = ["`uri-id` = ? AND `uid` IN (0, ?) " . $sql_extra, $uri_id, $item_uid];
303         $fields = ['parent-uri-id', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
304         $item = Post::selectFirstForUser($page_uid, $fields, $condition);
305
306         if (!DBA::isResult($item)) {
307                 throw new HTTPException\NotFoundException(DI::l10n()->t('The requested item doesn\'t exist or has been deleted.'));
308         }
309
310         $item['uri-id'] = $item['parent-uri-id'];
311
312         if ($unseen) {
313                 $condition = ['parent-uri-id' => $parent_uri_id, 'uid' => local_user(), 'unseen' => true];
314                 Item::update(['unseen' => false], $condition);
315         }
316
317         if (!$update && local_user()) {
318                 $o .= "<script> var netargs = '?uri_id=" . $item['uri-id'] . "'; </script>";
319         }
320
321         $o .= conversation($a, [$item], 'display', $update_uid, false, 'commented', $item_uid);
322
323         // Preparing the meta header
324         $description = trim(BBCode::toPlaintext($item['body']));
325         $title = trim(BBCode::toPlaintext($item['title']));
326         $author_name = $item['author-name'];
327
328         $image = DI::baseUrl()->remove($item['author-avatar']);
329
330         if ($title == '') {
331                 $title = $author_name;
332         }
333
334         // Limit the description to 160 characters
335         if (strlen($description) > 160) {
336                 $description = substr($description, 0, 157) . '...';
337         }
338
339         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
340         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
341         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
342
343         $page = DI::page();
344
345         if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
346                 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
347         }
348
349         DI::page()['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
350         $page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
351         $page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
352         $page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
353
354         // Schema.org microdata
355         $page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
356         $page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
357         $page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
358         $page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
359
360         // Twitter cards
361         $page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
362         $page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
363         $page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
364         $page['htmlhead'] .= '<meta name="twitter:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
365         $page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
366
367         // Dublin Core
368         $page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
369         $page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
370
371         // Open Graph
372         $page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
373         $page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
374         $page['htmlhead'] .= '<meta property="og:image" content="'.DI::baseUrl().'/'.$image.'" />'."\n";
375         $page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
376         $page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
377         $page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
378         // article:tag
379
380         return $o;
381 }
382
383 function displayShowFeed(int $uri_id, int $uid, bool $conversation)
384 {
385         $xml = DFRN::itemFeed($uri_id, $uid, $conversation);
386         if ($xml == '') {
387                 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('The feed for this item is unavailable.'));
388         }
389         header("Content-type: application/atom+xml");
390         echo $xml;
391         exit();
392 }