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