]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Merge pull request #5966 from JeroenED/theme/frio/oembed/view-active-class
[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\Protocol;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Protocol\ActivityPub;
21 use Friendica\Protocol\DFRN;
22
23 function display_init(App $a)
24 {
25         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
26                 return;
27         }
28
29         $nick = (($a->argc > 1) ? $a->argv[1] : '');
30         $profiledata = [];
31
32         if ($a->argc == 3) {
33                 if (substr($a->argv[2], -5) == '.atom') {
34                         $item_id = substr($a->argv[2], 0, -5);
35                         displayShowFeed($item_id, false);
36                 }
37         }
38
39         if ($a->argc == 4) {
40                 if ($a->argv[3] == 'conversation.atom') {
41                         $item_id = $a->argv[2];
42                         displayShowFeed($item_id, true);
43                 }
44         }
45
46         $item = null;
47
48         $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
49
50         // If there is only one parameter, then check if this parameter could be a guid
51         if ($a->argc == 2) {
52                 $nick = "";
53
54                 // Does the local user have this item?
55                 if (local_user()) {
56                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
57                         if (DBA::isResult($item)) {
58                                 $nick = $a->user["nickname"];
59                         }
60                 }
61
62                 // Is it an item with uid=0?
63                 if (!DBA::isResult($item)) {
64                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
65                 }
66         } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
67                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
68         }
69
70         if (!DBA::isResult($item)) {
71                 $a->error = 404;
72                 notice(L10n::t('Item not found.') . EOL);
73                 return;
74         }
75
76         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
77                 logger('Directly serving XML for id '.$item["id"], LOGGER_DEBUG);
78                 displayShowFeed($item["id"], false);
79         }
80
81         if (ActivityPub::isRequest()) {
82                 $a->internalRedirect(str_replace('display/', 'objects/', $a->query_string));
83         }
84
85         if ($item["id"] != $item["parent"]) {
86                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
87         }
88
89         $profiledata = display_fetchauthor($a, $item);
90
91         if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
92                 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
93
94                 if (($nickname != $a->user["nickname"])) {
95                         $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
96                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
97                                 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
98                                 $nickname
99                         );
100                         if (DBA::isResult($profile)) {
101                                 $profiledata = $profile;
102                         }
103                         $profiledata["network"] = Protocol::DFRN;
104                 } else {
105                         $profiledata = [];
106                 }
107         }
108
109         Profile::load($a, $nick, 0, $profiledata);
110 }
111
112 function display_fetchauthor($a, $item)
113 {
114         $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
115
116         $profiledata = [];
117         $profiledata['uid'] = -1;
118         $profiledata['nickname'] = $author['nick'];
119         $profiledata['name'] = $author['name'];
120         $profiledata['picdate'] = '';
121         $profiledata['photo'] = $author['photo'];
122         $profiledata['url'] = $author['url'];
123         $profiledata['network'] = $author['network'];
124
125         // Check for a repeated message
126         $skip = false;
127         $body = trim($item["body"]);
128
129         // Skip if it isn't a pure repeated messages
130         // Does it start with a share?
131         if (!$skip && strpos($body, "[share") > 0) {
132                 $skip = true;
133         }
134         // Does it end with a share?
135         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
136                 $skip = true;
137         }
138         if (!$skip) {
139                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
140                 // Skip if there is no shared message in there
141                 if ($body == $attributes) {
142                         $skip = true;
143                 }
144         }
145
146         if (!$skip) {
147                 $author = "";
148                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
149                 if (!empty($matches[1])) {
150                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
151                 }
152                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
153                 if (!empty($matches[1])) {
154                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
155                 }
156                 $profile = "";
157                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
158                 if (!empty($matches[1])) {
159                         $profiledata["url"] = $matches[1];
160                 }
161                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
162                 if (!empty($matches[1])) {
163                         $profiledata["url"] = $matches[1];
164                 }
165                 $avatar = "";
166                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
167                 if (!empty($matches[1])) {
168                         $profiledata["photo"] = $matches[1];
169                 }
170                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
171                 if (!empty($matches[1])) {
172                         $profiledata["photo"] = $matches[1];
173                 }
174                 $profiledata["nickname"] = $profiledata["name"];
175                 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
176
177                 $profiledata["address"] = "";
178                 $profiledata["about"] = "";
179         }
180
181         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
182
183         $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
184
185         if (local_user()) {
186                 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
187                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
188                 }
189         } elseif ($profiledata["network"] == Protocol::DFRN) {
190                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
191                 $profiledata["remoteconnect"] = $connect;
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') && !local_user() && !remote_user()) {
200                 notice(L10n::t('Public access denied.') . EOL);
201                 return;
202         }
203
204         require_once 'include/conversation.php';
205
206         $o = '';
207
208         if ($update) {
209                 $item_id = $_REQUEST['item_id'];
210                 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
211                 if ($item['uid'] != 0) {
212                         $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
213                 } else {
214                         $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
215                 }
216                 $item_parent = $item['parent'];
217                 $item_parent_uri = $item['parent-uri'];
218         } else {
219                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
220                 $item_parent = $item_id;
221
222                 if ($a->argc == 2) {
223                         $item_parent = 0;
224                         $fields = ['id', 'parent', 'parent-uri'];
225
226                         if (local_user()) {
227                                 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
228                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
229                                 if (DBA::isResult($item)) {
230                                         $item_id = $item["id"];
231                                         $item_parent = $item["parent"];
232                                         $item_parent_uri = $item['parent-uri'];
233                                 }
234                         }
235
236                         if ($item_parent == 0) {
237                                 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
238                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
239                                 if (DBA::isResult($item)) {
240                                         $item_id = $item["id"];
241                                         $item_parent = $item["parent"];
242                                         $item_parent_uri = $item['parent-uri'];
243                                 }
244                         }
245                 }
246         }
247
248         if (!$item_id) {
249                 $a->error = 404;
250                 notice(L10n::t('Item not found.').EOL);
251                 return;
252         }
253
254         // We are displaying an "alternate" link if that post was public. See issue 2864
255         $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
256         if ($is_public) {
257                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
258                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
259                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
260         } else {
261                 $alternate = '';
262                 $conversation = '';
263         }
264
265         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
266                                 ['$alternate' => $alternate,
267                                         '$conversation' => $conversation]);
268
269         $groups = [];
270
271         $contact = null;
272         $is_remote_contact = false;
273
274         $contact_id = 0;
275
276         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
277                 foreach ($_SESSION['remote'] as $v) {
278                         if ($v['uid'] == $a->profile['uid']) {
279                                 $contact_id = $v['cid'];
280                                 break;
281                         }
282                 }
283         }
284
285         if ($contact_id) {
286                 $groups = Group::getIdsByContactId($contact_id);
287                 $remote_contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
288                 if (DBA::isResult($remote_contact)) {
289                         $contact = $remote_contact;
290                         $is_remote_contact = true;
291                 }
292         }
293
294         if (!$is_remote_contact) {
295                 if (local_user()) {
296                         $contact_id = $_SESSION['cid'];
297                         $contact = $a->contact;
298                 }
299         }
300
301         $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
302         if (DBA::isResult($page_contact)) {
303                 $a->page_contact = $page_contact;
304         }
305         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
306
307         if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) {
308                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
309                 return;
310         }
311
312         // We need the editor here to be able to reshare an item.
313         if ($is_owner) {
314                 $x = [
315                         'is_owner' => true,
316                         'allow_location' => $a->user['allow_location'],
317                         'default_location' => $a->user['default-location'],
318                         'nickname' => $a->user['nickname'],
319                         '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'),
320                         'acl' => ACL::getFullSelectorHTML($a->user, true),
321                         'bang' => '',
322                         'visitor' => 'block',
323                         'profile_uid' => local_user(),
324                 ];
325                 $o .= status_editor($a, $x, 0, true);
326         }
327
328         $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid'], $is_remote_contact, $groups);
329
330         if (local_user() && (local_user() == $a->profile['uid'])) {
331                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
332                 $unseen = Item::exists($condition);
333         } else {
334                 $unseen = false;
335         }
336
337         if ($update && !$unseen) {
338                 return '';
339         }
340
341         $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
342         $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
343         $item = Item::selectFirstForUser(local_user(), $fields, $condition);
344
345         if (!DBA::isResult($item)) {
346                 notice(L10n::t('Item not found.') . EOL);
347                 return $o;
348         }
349
350         $item['uri'] = $item['parent-uri'];
351
352         if ($unseen) {
353                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
354                 Item::update(['unseen' => false], $condition);
355         }
356
357         if (!$update) {
358                 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
359         }
360
361         $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', local_user());
362
363         // Preparing the meta header
364         $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
365         $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
366         $author_name = $item["author-name"];
367
368         $image = $a->removeBaseURL($item["author-avatar"]);
369
370         if ($title == "") {
371                 $title = $author_name;
372         }
373
374         // Limit the description to 160 characters
375         if (strlen($description) > 160) {
376                 $description = substr($description, 0, 157) . '...';
377         }
378
379         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
380         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
381         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
382
383         //<meta name="keywords" content="">
384         $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
385         $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
386         $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
387         $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
388
389         // Schema.org microdata
390         $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
391         $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
392         $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
393         $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
394
395         // Twitter cards
396         $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
397         $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
398         $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
399         $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
400         $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
401
402         // Dublin Core
403         $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
404         $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
405
406         // Open Graph
407         $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
408         $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
409         $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
410         $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
411         $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
412         $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
413         // article:tag
414
415         return $o;
416 }
417
418 function displayShowFeed($item_id, $conversation)
419 {
420         $xml = DFRN::itemFeed($item_id, $conversation);
421         if ($xml == '') {
422                 System::httpExit(500);
423         }
424         header("Content-type: application/atom+xml");
425         echo $xml;
426         killme();
427 }