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