]> git.mxchange.org Git - friendica.git/blob - mod/display.php
1ecff36182197a0e4b9392638c48e86912cbb924
[friendica.git] / mod / display.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5
6 require_once('include/dfrn.php');
7
8 function display_init(App $a) {
9
10         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
11                 return;
12         }
13
14         $nick = (($a->argc > 1) ? $a->argv[1] : '');
15         $profiledata = array();
16
17         if ($a->argc == 3) {
18                 if (substr($a->argv[2], -5) == '.atom') {
19                         $item_id = substr($a->argv[2], 0, -5);
20                         displayShowFeed($item_id);
21                 }
22         }
23
24         // If there is only one parameter, then check if this parameter could be a guid
25         if ($a->argc == 2) {
26                 $nick = "";
27                 $itemuid = 0;
28                 $r = false;
29
30                 // Does the local user have this item?
31                 if (local_user()) {
32                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
33                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
34                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
35                                         AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
36                         if (dbm::is_result($r)) {
37                                 $nick = $a->user["nickname"];
38                                 $itemuid = local_user();
39                         }
40                 }
41
42                 // Is it an item with uid=0?
43                 if (!dbm::is_result($r)) {
44                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
45                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
46                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
47                                         AND `allow_cid` = ''  AND `allow_gid` = ''
48                                         AND `deny_cid`  = '' AND `deny_gid`  = ''
49                                         AND NOT `private` AND `uid` = 0
50                                         AND `guid` = ? LIMIT 1", $a->argv[1]);
51                 }
52
53                 // Or is it anywhere on the server?
54                 if (!dbm::is_result($r)) {
55                         $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
56                                 `item`.`author-avatar`, `item`.`network`, `item`.`body`, `item`.`uid`, `item`.`owner-link`
57                                 FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
58                                 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
59                                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
60                                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
61                                         AND NOT `item`.`private` AND NOT `user`.`hidewall`
62                                         AND `item`.`guid` = ? LIMIT 1", $a->argv[1]);
63                 }
64
65                 if (dbm::is_result($r)) {
66
67                         if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
68                                 logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
69                                 displayShowFeed($r["id"]);
70                         }
71
72                         if ($r["id"] != $r["parent"]) {
73                                 $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
74                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
75                                                 AND `id` = ?", $r["parent"]);
76                         }
77
78                         $profiledata = display_fetchauthor($a, $r);
79
80                         if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
81                                 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
82
83                                 if (($nickname != $a->user["nickname"])) {
84                                         $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
85                                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
86                                                 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
87                                                 $nickname
88                                         );
89                                         if (dbm::is_result($r)) {
90                                                 $profiledata = $r;
91                                         }
92                                         $profiledata["network"] = NETWORK_DFRN;
93                                 } else {
94                                         $profiledata = array();
95                                 }
96                         }
97                 } else {
98                         $a->error = 404;
99                         notice(t('Item not found.') . EOL);
100                         return;
101                 }
102         }
103
104         profile_load($a, $nick, 0, $profiledata);
105 }
106
107 function display_fetchauthor($a, $item) {
108
109         require_once("include/Contact.php");
110
111         $profiledata = array();
112         $profiledata["uid"] = -1;
113         $profiledata["nickname"] = $item["author-name"];
114         $profiledata["name"] = $item["author-name"];
115         $profiledata["picdate"] = "";
116         $profiledata["photo"] = $item["author-avatar"];
117         $profiledata["url"] = $item["author-link"];
118         $profiledata["network"] = $item["network"];
119
120         // Check for a repeated message
121         $skip = false;
122         $body = trim($item["body"]);
123
124         // Skip if it isn't a pure repeated messages
125         // Does it start with a share?
126         if (!$skip && strpos($body, "[share") > 0) {
127                 $skip = true;
128         }
129         // Does it end with a share?
130         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
131                 $skip = true;
132         }
133         if (!$skip) {
134                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
135                 // Skip if there is no shared message in there
136                 if ($body == $attributes) {
137                         $skip = true;
138                 }
139         }
140
141         if (!$skip) {
142                 $author = "";
143                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
144                 if ($matches[1] != "") {
145                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
146                 }
147                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
148                 if ($matches[1] != "") {
149                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
150                 }
151                 $profile = "";
152                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
153                 if ($matches[1] != "") {
154                         $profiledata["url"] = $matches[1];
155                 }
156                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
157                 if ($matches[1] != "") {
158                         $profiledata["url"] = $matches[1];
159                 }
160                 $avatar = "";
161                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
162                 if ($matches[1] != "") {
163                         $profiledata["photo"] = $matches[1];
164                 }
165                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
166                 if ($matches[1] != "") {
167                         $profiledata["photo"] = $matches[1];
168                 }
169                 $profiledata["nickname"] = $profiledata["name"];
170                 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
171
172                 $profiledata["address"] = "";
173                 $profiledata["about"] = "";
174         }
175
176         $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
177
178         $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
179
180         if (local_user()) {
181                 if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
182                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
183                 }
184         } elseif ($profiledata["network"] == NETWORK_DFRN) {
185                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
186                 $profiledata["remoteconnect"] = $connect;
187         }
188
189         return($profiledata);
190 }
191
192 function display_content(App $a, $update = 0) {
193
194         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
195                 notice(t('Public access denied.') . EOL);
196                 return;
197         }
198
199         require_once('include/security.php');
200         require_once('include/conversation.php');
201         require_once('include/acl_selectors.php');
202
203
204         $o = '';
205
206         if ($update) {
207                 $nick = $_REQUEST['nick'];
208         } else {
209                 $nick = (($a->argc > 1) ? $a->argv[1] : '');
210         }
211
212         if ($update) {
213                 $item_id = $_REQUEST['item_id'];
214                 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
215         } else {
216                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
217
218                 if ($a->argc == 2) {
219                         $nick = "";
220
221                         if (local_user()) {
222                                 $r = dba::fetch_first("SELECT `id` FROM `item`
223                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
224                                                 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
225                                 if (dbm::is_result($r)) {
226                                         $item_id = $r["id"];
227                                         $nick = $a->user["nickname"];
228                                 }
229                         }
230
231                         if ($nick == "") {
232                                 $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
233                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
234                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
235                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
236                                                 AND NOT `item`.`private` AND NOT `user`.`hidewall`
237                                                 AND `item`.`guid` = ?", $a->argv[1]);
238                                 if (dbm::is_result($r)) {
239                                         $item_id = $r["id"];
240                                         $nick = $r["nickname"];
241                                 }
242                         }
243                         if ($nick == "") {
244                                 $r = dba::fetch_first("SELECT `item`.`id` FROM `item`
245                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
246                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
247                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
248                                                 AND NOT `item`.`private` AND `item`.`uid` = 0
249                                                 AND `item`.`guid` = ?", $a->argv[1]);
250                                 if (dbm::is_result($r)) {
251                                         $item_id = $r["id"];
252                                 }
253                         }
254                 }
255         }
256
257         if ($item_id && !is_numeric($item_id)) {
258                 $r = dba::select('item', array('id'), array('uri' => $item_id, 'uid' => $a->profile['uid']), array('limit' => 1));
259                 if (dbm::is_result($r)) {
260                         $item_id = $r["id"];
261                 } else {
262                         $item_id = false;
263                 }
264         }
265
266         if (!$item_id) {
267                 $a->error = 404;
268                 notice(t('Item not found.').EOL);
269                 return;
270         }
271
272         // We are displaying an "alternate" link if that post was public. See issue 2864
273         $is_public = dba::exists('item', array('id' => $item_id, 'private' => false));
274         if ($is_public) {
275                 $alternate = System::baseUrl().'/display/'.$nick.'/'.$item_id.'.atom';
276         } else {
277                 $alternate = '';
278         }
279
280         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
281                                 array('$alternate' => $alternate));
282
283         $groups = array();
284
285         $contact = null;
286         $remote_contact = false;
287
288         $contact_id = 0;
289
290         if (is_array($_SESSION['remote'])) {
291                 foreach ($_SESSION['remote'] as $v) {
292                         if ($v['uid'] == $a->profile['uid']) {
293                                 $contact_id = $v['cid'];
294                                 break;
295                         }
296                 }
297         }
298
299         if ($contact_id) {
300                 $groups = init_groups_visitor($contact_id);
301                 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
302                         $contact_id,
303                         $a->profile['uid']
304                 );
305                 if (dbm::is_result($r)) {
306                         $contact = $r;
307                         $remote_contact = true;
308                 }
309         }
310
311         if (!$remote_contact) {
312                 if (local_user()) {
313                         $contact_id = $_SESSION['cid'];
314                         $contact = $a->contact;
315                 }
316         }
317
318         $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
319         if (dbm::is_result($r)) {
320                 $a->page_contact = $r;
321         }
322         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
323
324         if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
325                 notice(t('Access to this profile has been restricted.') . EOL);
326                 return;
327         }
328
329         // We need the editor here to be able to reshare an item.
330
331         if ($is_owner) {
332                 $x = array(
333                         'is_owner' => true,
334                         'allow_location' => $a->user['allow_location'],
335                         'default_location' => $a->user['default-location'],
336                         'nickname' => $a->user['nickname'],
337                         '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'),
338                         'acl' => populate_acl($a->user, true),
339                         'bang' => '',
340                         'visitor' => 'block',
341                         'profile_uid' => local_user(),
342                         'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
343                 );
344                 $o .= status_editor($a,$x,0,true);
345         }
346
347         $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
348
349         if ($update) {
350                 $r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
351                         AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
352                         $sql_extra AND `unseen`",
353                         $a->profile['uid'],
354                         $item_id
355                 );
356
357                 if (dba::num_rows($r) == 0) {
358                         return '';
359                 }
360         }
361
362         $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
363                 $sql_extra
364                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
365                 $item_id
366         );
367
368         if (!dbm::is_result($r) && local_user()) {
369                 // Check if this is another person's link to a post that we have
370                 $r = dba::fetch_first("SELECT `item`.uri FROM `item`
371                         WHERE (`item`.`id` = ? OR `item`.`uri` = ?)
372                         LIMIT 1",
373                         $item_id,
374                         $item_id
375                 );
376                 if (dbm::is_result($r)) {
377                         $item_uri = $r['uri'];
378
379                         $r = dba::p(item_query()." AND `item`.`uid` = ?
380                                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = ? AND uid = ?)
381                                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
382                                 local_user(),
383                                 $item_uri,
384                                 local_user()
385                         );
386                 }
387         }
388
389         if (dbm::is_result($r)) {
390                 $s = dba::inArray($r);
391
392                 if ((local_user()) && (local_user() == $a->profile['uid'])) {
393                         $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
394                         if (dbm::is_result($unseen)) {
395                                 dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
396                         }
397                 }
398
399                 $items = conv_sort($s, "`commented`");
400
401                 if (!$update) {
402                         $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
403                 }
404                 $o .= conversation($a, $items, 'display', $update);
405
406                 // Preparing the meta header
407                 require_once('include/bbcode.php');
408                 require_once("include/html2plain.php");
409                 $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
410                 $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
411                 $author_name = $s[0]["author-name"];
412
413                 $image = $a->remove_baseurl($s[0]["author-thumb"]);
414
415                 if ($title == "") {
416                         $title = $author_name;
417                 }
418
419                 // Limit the description to 160 characters
420                 if (strlen($description) > 160) {
421                         $description = substr($description, 0, 157) . '...';
422                 }
423
424                 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
425                 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
426                 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
427
428                 //<meta name="keywords" content="">
429                 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
430                 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
431                 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
432                 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
433
434                 // Schema.org microdata
435                 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
436                 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
437                 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
438                 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
439
440                 // Twitter cards
441                 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
442                 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
443                 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
444                 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
445                 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
446
447                 // Dublin Core
448                 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
449                 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
450
451                 // Open Graph
452                 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
453                 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
454                 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
455                 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
456                 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
457                 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
458                 // article:tag
459
460                 return $o;
461         }
462         $r = dba::fetch_first("SELECT `id`,`deleted` FROM `item` WHERE `id` = ? OR `uri` = ? LIMIT 1",
463                 $item_id,
464                 $item_id
465         );
466         if (dbm::is_result($r)) {
467                 if ($r['deleted']) {
468                         notice(t('Item has been removed.') . EOL);
469                 } else {
470                         notice(t('Permission denied.') . EOL);
471                 }
472         } else {
473                 notice(t('Item not found.') . EOL);
474         }
475
476         return $o;
477 }
478
479 function displayShowFeed($item_id) {
480         $xml = dfrn::itemFeed($item_id);
481         if ($xml == '') {
482                 http_status_exit(500);
483         }
484         header("Content-type: application/atom+xml");
485         echo $xml;
486         killme();
487 }