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