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