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