]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Merge branch 'develop' into rewrites/dbm_is_result
[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
112 function display_fetchauthor($a, $item) {
113
114         require_once("include/Contact.php");
115
116         $profiledata = array();
117         $profiledata["uid"] = -1;
118         $profiledata["nickname"] = $item["author-name"];
119         $profiledata["name"] = $item["author-name"];
120         $profiledata["picdate"] = "";
121         $profiledata["photo"] = $item["author-avatar"];
122         $profiledata["url"] = $item["author-link"];
123         $profiledata["network"] = $item["network"];
124
125         // Check for a repeated message
126         $skip = false;
127         $body = trim($item["body"]);
128
129         // Skip if it isn't a pure repeated messages
130         // Does it start with a share?
131         if (!$skip AND strpos($body, "[share") > 0) {
132                 $skip = true;
133         }
134         // Does it end with a share?
135         if (!$skip AND (strlen($body) > (strrpos($body, "[/share]") + 8))) {
136                 $skip = true;
137         }
138         if (!$skip) {
139                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
140                 // Skip if there is no shared message in there
141                 if ($body == $attributes) {
142                         $skip = true;
143                 }
144         }
145
146         if (!$skip) {
147                 $author = "";
148                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
149                 if ($matches[1] != "") {
150                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
151                 }
152                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
153                 if ($matches[1] != "") {
154                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
155                 }
156                 $profile = "";
157                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
158                 if ($matches[1] != "") {
159                         $profiledata["url"] = $matches[1];
160                 }
161                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
162                 if ($matches[1] != "") {
163                         $profiledata["url"] = $matches[1];
164                 }
165                 $avatar = "";
166                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
167                 if ($matches[1] != "") {
168                         $profiledata["photo"] = $matches[1];
169                 }
170                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
171                 if ($matches[1] != "") {
172                         $profiledata["photo"] = $matches[1];
173                 }
174                 $profiledata["nickname"] = $profiledata["name"];
175                 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
176
177                 $profiledata["address"] = "";
178                 $profiledata["about"] = "";
179         }
180
181         $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
182
183         $profiledata["photo"] = App::remove_baseurl($profiledata["photo"]);
184
185         if (local_user()) {
186                 if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
187                         $profiledata["remoteconnect"] = App::get_baseurl()."/follow?url=".urlencode($profiledata["url"]);
188                 }
189         } elseif ($profiledata["network"] == NETWORK_DFRN) {
190                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
191                 $profiledata["remoteconnect"] = $connect;
192         }
193
194         return($profiledata);
195 }
196
197 function display_content(&$a, $update = 0) {
198
199         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
200                 notice(t('Public access denied.') . EOL);
201                 return;
202         }
203
204         require_once('include/security.php');
205         require_once('include/conversation.php');
206         require_once('include/acl_selectors.php');
207
208
209         $o = '';
210
211         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'), array());
212
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 = qu("SELECT `id` FROM `item`
231                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
232                                                 AND `guid` = '%s' AND `uid` = %d", dbesc($a->argv[1]), local_user());
233                                 if (dbm::is_result($r)) {
234                                         $item_id = $r[0]["id"];
235                                         $nick = $a->user["nickname"];
236                                 }
237                         }
238
239                         if ($nick == "") {
240                                 $r = qu("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
241                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
242                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
243                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
244                                                 AND NOT `item`.`private` AND NOT `user`.`hidewall`
245                                                 AND `item`.`guid` = '%s'", dbesc($a->argv[1]));
246                                         //      AND NOT `item`.`private` AND `item`.`wall`
247                                 if (dbm::is_result($r)) {
248                                         $item_id = $r[0]["id"];
249                                         $nick = $r[0]["nickname"];
250                                 }
251                         }
252                         if ($nick == "") {
253                                 $r = qu("SELECT `item`.`id` FROM `item`
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 `item`.`uid` = 0
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                                 }
263                         }
264                 }
265         }
266
267         if ($item_id AND !is_numeric($item_id)) {
268                 $r = qu("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
269                         dbesc($item_id), intval($a->profile['uid']));
270                 if (dbm::is_result($r)) {
271                         $item_id = $r[0]["id"];
272                 } else {
273                         $item_id = false;
274                 }
275         }
276
277         if (!$item_id) {
278                 $a->error = 404;
279                 notice(t('Item not found.').EOL);
280                 return;
281         }
282
283
284         $groups = array();
285
286         $contact = null;
287         $remote_contact = false;
288
289         $contact_id = 0;
290
291         if (is_array($_SESSION['remote'])) {
292                 foreach ($_SESSION['remote'] as $v) {
293                         if ($v['uid'] == $a->profile['uid']) {
294                                 $contact_id = $v['cid'];
295                                 break;
296                         }
297                 }
298         }
299
300         if ($contact_id) {
301                 $groups = init_groups_visitor($contact_id);
302                 $r = qu("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
303                         intval($contact_id),
304                         intval($a->profile['uid'])
305                 );
306                 if (dbm::is_result($r)) {
307                         $contact = $r[0];
308                         $remote_contact = true;
309                 }
310         }
311
312         if (!$remote_contact) {
313                 if (local_user()) {
314                         $contact_id = $_SESSION['cid'];
315                         $contact = $a->contact;
316                 }
317         }
318
319         $r = qu("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
320                 intval($a->profile['uid'])
321         );
322         if (dbm::is_result($r)) {
323                 $a->page_contact = $r[0];
324         }
325         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
326
327         if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
328                 notice(t('Access to this profile has been restricted.') . EOL);
329                 return;
330         }
331
332         // We need the editor here to be able to reshare an item.
333
334         if ($is_owner) {
335                 $x = array(
336                         'is_owner' => true,
337                         'allow_location' => $a->user['allow_location'],
338                         'default_location' => $a->user['default-location'],
339                         'nickname' => $a->user['nickname'],
340                         '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'),
341                         'acl' => populate_acl($a->user, true),
342                         'bang' => '',
343                         'visitor' => 'block',
344                         'profile_uid' => local_user(),
345                         'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
346                 );
347                 $o .= status_editor($a,$x,0,true);
348         }
349
350         $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
351
352         if ($update) {
353
354                 $r = qu("SELECT `id` FROM `item` WHERE `item`.`uid` = %d
355                         AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = %d)
356                         $sql_extra AND `unseen`",
357                         intval($a->profile['uid']),
358                         intval($item_id)
359                 );
360
361                 if (!$r) {
362                         return '';
363                 }
364         }
365
366         $r = qu(item_query()." AND `item`.`uid` = %d
367                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = %d)
368                 $sql_extra
369                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
370                 intval($a->profile['uid']),
371                 intval($item_id)
372         );
373
374
375         if (!$r && local_user()) {
376                 // Check if this is another person's link to a post that we have
377                 $r = qu("SELECT `item`.uri FROM `item`
378                         WHERE (`item`.`id` = %d OR `item`.`uri` = '%s')
379                         LIMIT 1",
380                         intval($item_id),
381                         dbesc($item_id)
382                 );
383                 if (dbm::is_result($r)) {
384                         $item_uri = $r[0]['uri'];
385
386                         $r = qu(item_query()." AND `item`.`uid` = %d
387                                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = '%s' AND uid = %d)
388                                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC ",
389                                 intval(local_user()),
390                                 dbesc($item_uri),
391                                 intval(local_user())
392                         );
393                 }
394         }
395
396         if ($r) {
397
398                 if ((local_user()) && (local_user() == $a->profile['uid'])) {
399                         $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `parent` = %d",
400                                         intval($r[0]['parent']));
401
402                         if ($unseen) {
403                                 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d AND `unseen`",
404                                         intval($r[0]['parent'])
405                                 );
406                         }
407                 }
408
409                 $items = conv_sort($r,"`commented`");
410
411                 if (!$update) {
412                         $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
413                 }
414                 $o .= conversation($a,$items,'display', $update);
415
416                 // Preparing the meta header
417                 require_once('include/bbcode.php');
418                 require_once("include/html2plain.php");
419                 $description = trim(html2plain(bbcode($r[0]["body"], false, false), 0, true));
420                 $title = trim(html2plain(bbcode($r[0]["title"], false, false), 0, true));
421                 $author_name = $r[0]["author-name"];
422
423                 $image = $a->remove_baseurl($r[0]["thumb"]);
424
425                 if ($title == "") {
426                         $title = $author_name;
427                 }
428                 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
429                 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
430                 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
431
432                 //<meta name="keywords" content="">
433                 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
434                 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
435                 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
436                 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
437
438                 // Schema.org microdata
439                 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
440                 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
441                 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
442                 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
443
444                 // Twitter cards
445                 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
446                 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
447                 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
448                 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
449                 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$r[0]["plink"].'" />'."\n";
450
451                 // Dublin Core
452                 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
453                 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
454
455                 // Open Graph
456                 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
457                 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
458                 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
459                 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$r[0]["plink"].'" />'."\n";
460                 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
461                 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
462                 // article:tag
463
464                 return $o;
465         }
466
467         $r = qu("SELECT `id`,`deleted` FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
468                 dbesc($item_id),
469                 dbesc($item_id)
470         );
471         if ($r) {
472                 if ($r[0]['deleted']) {
473                         notice(t('Item has been removed.') . EOL );
474                 } else {
475                         notice(t('Permission denied.') . EOL );
476                 }
477         } else {
478                 notice(t('Item not found.') . EOL );
479         }
480
481         return $o;
482 }
483