]> git.mxchange.org Git - friendica.git/blob - mod/display.php
add missing frio template
[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                 $item = Item::selectFirstForUser($item_user, $fields, ['id' => $item["parent"]]);
97         }
98
99         $profiledata = display_fetchauthor($a, $item);
100
101         if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(System::baseUrl()))) {
102                 $nickname = str_replace(Strings::normaliseLink(System::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
103
104                 if ($nickname != $a->user["nickname"]) {
105                         $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
106                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
107                                 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
108                                 $nickname
109                         );
110                         if (DBA::isResult($profile)) {
111                                 $profiledata = $profile;
112                         }
113                         $profiledata["network"] = Protocol::DFRN;
114                 } else {
115                         $profiledata = [];
116                 }
117         }
118
119         Profile::load($a, $nick, 0, $profiledata);
120 }
121
122 function display_fetchauthor($a, $item)
123 {
124         $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
125
126         $profiledata = [];
127         $profiledata['uid'] = -1;
128         $profiledata['nickname'] = $author['nick'];
129         $profiledata['name'] = $author['name'];
130         $profiledata['picdate'] = '';
131         $profiledata['photo'] = $author['photo'];
132         $profiledata['url'] = $author['url'];
133         $profiledata['network'] = $author['network'];
134
135         // Check for a repeated message
136         $skip = false;
137         $body = trim($item["body"]);
138
139         // Skip if it isn't a pure repeated messages
140         // Does it start with a share?
141         if (!$skip && strpos($body, "[share") > 0) {
142                 $skip = true;
143         }
144         // Does it end with a share?
145         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
146                 $skip = true;
147         }
148         if (!$skip) {
149                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
150                 // Skip if there is no shared message in there
151                 if ($body == $attributes) {
152                         $skip = true;
153                 }
154         }
155
156         if (!$skip) {
157                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
158                 if (!empty($matches[1])) {
159                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
160                 }
161                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
162                 if (!empty($matches[1])) {
163                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
164                 }
165                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
166                 if (!empty($matches[1])) {
167                         $profiledata["url"] = $matches[1];
168                 }
169                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
170                 if (!empty($matches[1])) {
171                         $profiledata["url"] = $matches[1];
172                 }
173                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
174                 if (!empty($matches[1])) {
175                         $profiledata["photo"] = $matches[1];
176                 }
177                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
178                 if (!empty($matches[1])) {
179                         $profiledata["photo"] = $matches[1];
180                 }
181                 $profiledata["nickname"] = $profiledata["name"];
182                 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
183
184                 $profiledata["address"] = "";
185                 $profiledata["about"] = "";
186         }
187
188         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
189
190         if (!empty($profiledata["photo"])) {
191                 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
192         }
193
194         return $profiledata;
195 }
196
197 function display_content(App $a, $update = false, $update_uid = 0)
198 {
199         if (Config::get('system','block_public') && !Session::isAuthenticated()) {
200                 throw new HTTPException\ForbiddenException(L10n::t('Public access denied.'));
201         }
202
203         $o = '';
204
205         if ($update) {
206                 $item_id = $_REQUEST['item_id'];
207                 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
208                 if ($item['uid'] != 0) {
209                         $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
210                 } else {
211                         $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
212                 }
213                 $item_parent = $item['parent'];
214                 $item_parent_uri = $item['parent-uri'];
215         } else {
216                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
217                 $item_parent = $item_id;
218
219                 if ($a->argc == 2) {
220                         $item_parent = 0;
221                         $fields = ['id', 'parent', 'parent-uri', 'uid'];
222
223                         if (local_user()) {
224                                 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
225                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
226                                 if (DBA::isResult($item)) {
227                                         $item_id = $item["id"];
228                                         $item_parent = $item["parent"];
229                                         $item_parent_uri = $item['parent-uri'];
230                                 }
231                         }
232
233                         if (($item_parent == 0) && remote_user()) {
234                                 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
235                                 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
236                                         $item_id = $item["id"];
237                                         $item_parent = $item["parent"];
238                                         $item_parent_uri = $item['parent-uri'];
239                                 }
240                         }
241
242                         if ($item_parent == 0) {
243                                 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
244                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
245                                 if (DBA::isResult($item)) {
246                                         $item_id = $item["id"];
247                                         $item_parent = $item["parent"];
248                                         $item_parent_uri = $item['parent-uri'];
249                                 }
250                         }
251                 }
252         }
253
254         if (!$item_id) {
255                 throw new HTTPException\NotFoundException(L10n::t('The requested item doesn\'t exist or has been deleted.'));
256         }
257
258         // We are displaying an "alternate" link if that post was public. See issue 2864
259         $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
260         if ($is_public) {
261                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
262                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
263                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
264         } else {
265                 $alternate = '';
266                 $conversation = '';
267         }
268
269         $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
270                                 ['$alternate' => $alternate,
271                                         '$conversation' => $conversation]);
272
273         $is_remote_contact = false;
274         $item_uid = local_user();
275
276         if (isset($item_parent_uri)) {
277                 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
278                 if (DBA::isResult($parent)) {
279                         $a->profile['uid'] = ($a->profile['uid'] ?? 0) ?: $parent['uid'];
280                         $a->profile['profile_uid'] = ($a->profile['profile_uid'] ?? 0) ?: $parent['uid'];
281                         $is_remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
282                         if ($is_remote_contact) {
283                                 $item_uid = $parent['uid'];
284                         }
285                 }
286         }
287
288         $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
289         if (DBA::isResult($page_contact)) {
290                 $a->page_contact = $page_contact;
291         }
292
293         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
294
295         if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
296                 throw new HTTPException\ForbiddenException(L10n::t('Access to this profile has been restricted.'));
297         }
298
299         // We need the editor here to be able to reshare an item.
300         if ($is_owner) {
301                 $x = [
302                         'is_owner' => true,
303                         'allow_location' => $a->user['allow_location'],
304                         'default_location' => $a->user['default-location'],
305                         'nickname' => $a->user['nickname'],
306                         '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'),
307                         'acl' => ACL::getFullSelectorHTML($a->user, true),
308                         'bang' => '',
309                         'visitor' => 'block',
310                         'profile_uid' => local_user(),
311                 ];
312                 $o .= status_editor($a, $x, 0, true);
313         }
314         $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
315
316         if (local_user() && (local_user() == $a->profile['profile_uid'])) {
317                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
318                 $unseen = Item::exists($condition);
319         } else {
320                 $unseen = false;
321         }
322
323         if ($update && !$unseen) {
324                 return '';
325         }
326
327         $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
328         $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
329         $item = Item::selectFirstForUser($a->profile['profile_uid'], $fields, $condition);
330
331         if (!DBA::isResult($item)) {
332                 throw new HTTPException\NotFoundException(L10n::t('The requested item doesn\'t exist or has been deleted.'));
333         }
334
335         $item['uri'] = $item['parent-uri'];
336
337         if ($unseen) {
338                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
339                 Item::update(['unseen' => false], $condition);
340         }
341
342         if (!$update) {
343                 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
344         }
345
346         $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', $item_uid);
347
348         // Preparing the meta header
349         $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
350         $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
351         $author_name = $item["author-name"];
352
353         $image = $a->removeBaseURL($item["author-avatar"]);
354
355         if ($title == "") {
356                 $title = $author_name;
357         }
358
359         // Limit the description to 160 characters
360         if (strlen($description) > 160) {
361                 $description = substr($description, 0, 157) . '...';
362         }
363
364         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
365         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
366         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
367
368         if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
369                 $a->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
370         }
371
372         $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
373         $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
374         $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
375         $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
376
377         // Schema.org microdata
378         $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
379         $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
380         $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
381         $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
382
383         // Twitter cards
384         $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
385         $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
386         $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
387         $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
388         $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
389
390         // Dublin Core
391         $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
392         $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
393
394         // Open Graph
395         $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
396         $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
397         $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
398         $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
399         $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
400         $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
401         // article:tag
402
403         return $o;
404 }
405
406 function displayShowFeed($item_id, $conversation)
407 {
408         $xml = DFRN::itemFeed($item_id, $conversation);
409         if ($xml == '') {
410                 throw new HTTPException\InternalServerErrorException(L10n::t('The feed for this item is unavailable.'));
411         }
412         header("Content-type: application/atom+xml");
413         echo $xml;
414         exit();
415 }