]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Merge branch 'release/3.4'
[friendica.git] / mod / display.php
1 <?php
2
3 function display_init(&$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 = q("SELECT `id`, `parent`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid` FROM `item`
20                                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
21                                         AND `guid` = '%s' AND `uid` = %d", $a->argv[1], local_user());
22                         if (count($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 = q("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent`, `item`.`author-name`,
31                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`body`
32                                 FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
33                                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
34                                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
35                                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
36                                         AND `item`.`private` = 0 AND NOT `user`.`hidewall`
37                                         AND `item`.`guid` = '%s'", $a->argv[1]);
38                                 //      AND `item`.`private` = 0 AND `item`.`wall` = 1
39                         if (count($r)) {
40                                 $nick = $r[0]["nickname"];
41                                 $itemuid = $r[0]["uid"];
42                         }
43                 }
44
45                 // Is it an item with uid=0?
46                 if ($nick == "") {
47                         $r = q("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`,
48                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`body`
49                                 FROM `item` WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
50                                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
51                                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
52                                         AND `item`.`private` = 0 AND `item`.`uid` = 0
53                                         AND `item`.`guid` = '%s'", $a->argv[1]);
54                                 //      AND `item`.`private` = 0 AND `item`.`wall` = 1
55                 }
56                 if (count($r)) {
57                         if ($r[0]["id"] != $r[0]["parent"])
58                                 $r = q("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid` FROM `item`
59                                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
60                                                 AND `id` = %d", $r[0]["parent"]);
61
62                         $profiledata = display_fetchauthor($a, $r[0]);
63
64                         if (strstr(normalise_link($profiledata["url"]), normalise_link($a->get_baseurl()))) {
65                                 $nickname = str_replace(normalise_link($a->get_baseurl())."/profile/", "", normalise_link($profiledata["url"]));
66
67                                 if (($nickname != $a->user["nickname"])) {
68                                         $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
69                                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
70                                                 WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 and `contact`.`self` = 1 LIMIT 1",
71                                                 dbesc($nickname)
72                                         );
73                                         if (count($r))
74                                                 $profiledata = $r[0];
75
76                                         $profiledata["network"] = NETWORK_DFRN;
77                                 } else
78                                         $profiledata = array();
79                         }
80                 } else {
81                         $a->error = 404;
82                         notice( t('Item not found.') . EOL);
83                         return;
84                 }
85         }
86
87         profile_load($a, $nick, 0, $profiledata);
88
89 }
90
91 function display_fetchauthor($a, $item) {
92         require_once("mod/proxy.php");
93         require_once("include/bbcode.php");
94
95         $profiledata = array();
96         $profiledata["uid"] = -1;
97         $profiledata["nickname"] = $item["author-name"];
98         $profiledata["name"] = $item["author-name"];
99         $profiledata["picdate"] = "";
100         $profiledata["photo"] = proxy_url($item["author-avatar"]);
101         $profiledata["url"] = $item["author-link"];
102         $profiledata["network"] = $item["network"];
103
104         // Fetching further contact data from the contact table
105         $r = q("SELECT `photo`, `nick`, `location`, `about` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
106                 normalise_link($profiledata["url"]), $item["uid"]);
107         if (count($r)) {
108                 $profiledata["photo"] = proxy_url($r[0]["photo"]);
109                 $profiledata["address"] = proxy_parse_html(bbcode($r[0]["location"]));
110                 $profiledata["about"] = proxy_parse_html(bbcode($r[0]["about"]));
111                 if ($r[0]["nick"] != "")
112                         $profiledata["nickname"] = $r[0]["nick"];
113         }
114
115         // Fetching profile data from unique contacts
116         $r = q("SELECT `avatar`, `nick`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'", normalise_link($profiledata["url"]));
117         if (count($r)) {
118                 if ($profiledata["photo"] == "")
119                         $profiledata["photo"] = proxy_url($r[0]["avatar"]);
120                 if ($profiledata["address"] == "")
121                         $profiledata["address"] = proxy_parse_html(bbcode($r[0]["location"]));
122                 if ($profiledata["about"] == "")
123                         $profiledata["about"] = proxy_parse_html(bbcode($r[0]["about"]));
124                 if (($profiledata["nickname"] == "") AND ($r[0]["nick"] != ""))
125                         $profiledata["nickname"] = $r[0]["nick"];
126         }
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 AND strpos($body, "[share") > 0)
135                 $skip = true;
136
137         // Does it end with a share?
138         if (!$skip AND (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         if (!$skip) {
149                 $author = "";
150                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
151                 if ($matches[1] != "")
152                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
153
154                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
155                 if ($matches[1] != "")
156                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
157
158                 $profile = "";
159                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
160                 if ($matches[1] != "")
161                         $profiledata["url"] = $matches[1];
162
163                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
164                 if ($matches[1] != "")
165                         $profiledata["url"] = $matches[1];
166
167                 $avatar = "";
168                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
169                 if ($matches[1] != "")
170                         $profiledata["photo"] = $matches[1];
171
172                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
173                 if ($matches[1] != "")
174                         $profiledata["photo"] = $matches[1];
175
176                 $profiledata["nickname"] = $profiledata["name"];
177                 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
178
179                 $profiledata["address"] = "";
180                 $profiledata["about"] = "";
181
182                 // Fetching profile data from unique contacts
183                 if ($profiledata["url"] != "") {
184                         $r = q("SELECT `avatar`, `nick`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'", normalise_link($profiledata["url"]));
185                         if (count($r)) {
186                                 $profiledata["photo"] = proxy_url($r[0]["avatar"]);
187                                 $profiledata["address"] = proxy_parse_html(bbcode($r[0]["location"]));
188                                 $profiledata["about"] = proxy_parse_html(bbcode($r[0]["about"]));
189                                 if ($r[0]["nick"] != "")
190                                         $profiledata["nickname"] = $r[0]["nick"];
191                         }
192                 }
193         }
194
195         if (local_user()) {
196                 if ($profiledata["network"] == NETWORK_DFRN) {
197                         $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"])."&addr=".bin2hex($a->get_baseurl()."/profile/".$a->user["nickname"]);
198                         $profiledata["remoteconnect"] = $connect;
199                 } elseif ($profiledata["network"] == NETWORK_DIASPORA)
200                         $profiledata["remoteconnect"] = $a->get_baseurl()."/contacts?add=".GetProfileUsername($profiledata["url"], "", true);
201         } elseif ($profiledata["network"] == NETWORK_DFRN) {
202                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
203                 $profiledata["remoteconnect"] = $connect;
204         }
205
206         return($profiledata);
207 }
208
209 function display_content(&$a, $update = 0) {
210
211         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
212                 notice( t('Public access denied.') . EOL);
213                 return;
214         }
215
216         require_once("include/bbcode.php");
217         require_once('include/security.php');
218         require_once('include/conversation.php');
219         require_once('include/acl_selectors.php');
220
221
222         $o = '';
223
224         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'), array());
225
226
227         if($update) {
228                 $nick = $_REQUEST['nick'];
229         }
230         else {
231                 $nick = (($a->argc > 1) ? $a->argv[1] : '');
232         }
233
234         if($update) {
235                 $item_id = $_REQUEST['item_id'];
236                 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
237         }
238         else {
239                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
240
241                 if ($a->argc == 2) {
242                         $nick = "";
243
244                         if (local_user()) {
245                                 $r = q("SELECT `id` FROM `item`
246                                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
247                                                 AND `guid` = '%s' AND `uid` = %d", $a->argv[1], local_user());
248                                 if (count($r)) {
249                                         $item_id = $r[0]["id"];
250                                         $nick = $a->user["nickname"];
251                                 }
252                         }
253
254                         if ($nick == "") {
255                                 $r = q("SELECT `user`.`nickname`, `item`.`id` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
256                                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
257                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
258                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
259                                                 AND `item`.`private` = 0  AND NOT `user`.`hidewall`
260                                                 AND `item`.`guid` = '%s'", $a->argv[1]);
261                                         //      AND `item`.`private` = 0 AND `item`.`wall` = 1
262                                 if (count($r)) {
263                                         $item_id = $r[0]["id"];
264                                         $nick = $r[0]["nickname"];
265                                 }
266                         }
267                         if ($nick == "") {
268                                 $r = q("SELECT `item`.`id` FROM `item`
269                                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
270                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
271                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
272                                                 AND `item`.`private` = 0  AND `item`.`uid` = 0
273                                                 AND `item`.`guid` = '%s'", $a->argv[1]);
274                                         //      AND `item`.`private` = 0 AND `item`.`wall` = 1
275                                 if (count($r)) {
276                                         $item_id = $r[0]["id"];
277                                 }
278                         }
279                 }
280         }
281
282         if(! $item_id) {
283                 $a->error = 404;
284                 notice( t('Item not found.') . EOL);
285                 return;
286         }
287
288         $groups = array();
289
290         $contact = null;
291         $remote_contact = false;
292
293         $contact_id = 0;
294
295         if(is_array($_SESSION['remote'])) {
296                 foreach($_SESSION['remote'] as $v) {
297                         if($v['uid'] == $a->profile['uid']) {
298                                 $contact_id = $v['cid'];
299                                 break;
300                         }
301                 }
302         }
303
304         if($contact_id) {
305                 $groups = init_groups_visitor($contact_id);
306                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
307                         intval($contact_id),
308                         intval($a->profile['uid'])
309                 );
310                 if(count($r)) {
311                         $contact = $r[0];
312                         $remote_contact = true;
313                 }
314         }
315
316         if(! $remote_contact) {
317                 if(local_user()) {
318                         $contact_id = $_SESSION['cid'];
319                         $contact = $a->contact;
320                 }
321         }
322
323         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
324                 intval($a->profile['uid'])
325         );
326         if(count($r))
327                 $a->page_contact = $r[0];
328
329         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
330
331         if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
332                 notice( t('Access to this profile has been restricted.') . EOL);
333                 return;
334         }
335
336         if ($is_owner) {
337                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
338
339                 $x = array(
340                         'is_owner' => true,
341                         'allow_location' => $a->user['allow_location'],
342                         'default_location' => $a->user['default-location'],
343                         'nickname' => $a->user['nickname'],
344                         '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'),
345                         'acl' => populate_acl($a->user, $celeb),
346                         'bang' => '',
347                         'visitor' => 'block',
348                         'profile_uid' => local_user(),
349                         'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
350                 );
351                 $o .= status_editor($a,$x,0,true);
352         }
353
354         $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
355
356         //              AND `item`.`parent` = ( SELECT `parent` FROM `item` FORCE INDEX (PRIMARY, `uri`) WHERE ( `id` = '%s' OR `uri` = '%s' ))
357
358         if($update) {
359
360                 $r = q("SELECT id FROM item WHERE item.uid = %d
361                         AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE (`id` = '%s' OR `uri` = '%s'))
362                         $sql_extra AND unseen = 1",
363                         intval($a->profile['uid']),
364                         dbesc($item_id),
365                         dbesc($item_id)
366                 );
367
368                 if(!$r)
369                         return '';
370         }
371
372         //      AND `item`.`parent` = ( SELECT `parent` FROM `item` FORCE INDEX (PRIMARY, `uri`) WHERE ( `id` = '%s' OR `uri` = '%s' )
373
374         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,  `item`.`network` AS `item_network`,
375                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
376                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
377                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
378                 FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
379                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
380                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
381                 and `item`.`moderated` = 0
382                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE (`id` = '%s' OR `uri` = '%s')
383                 AND uid = %d)
384                 $sql_extra
385                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
386                 intval($a->profile['uid']),
387                 dbesc($item_id),
388                 dbesc($item_id),
389                 intval($a->profile['uid'])
390         );
391
392         if(!$r && local_user()) {
393                 // Check if this is another person's link to a post that we have
394                 $r = q("SELECT `item`.uri FROM `item`
395                         WHERE (`item`.`id` = '%s' OR `item`.`uri` = '%s' )
396                         LIMIT 1",
397                         dbesc($item_id),
398                         dbesc($item_id)
399                 );
400                 if($r) {
401                         $item_uri = $r[0]['uri'];
402                         //      AND `item`.`parent` = ( SELECT `parent` FROM `item` FORCE INDEX (PRIMARY, `uri`) WHERE `uri` = '%s' AND uid = %d )
403
404                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,  `item`.`network` AS `item_network`,
405                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
406                                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, 
407                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
408                                 FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
409                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
410                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
411                                 and `item`.`moderated` = 0
412                                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = '%s' AND uid = %d)
413                                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC ",
414                                 intval(local_user()),
415                                 dbesc($item_uri),
416                                 intval(local_user())
417                         );
418                 }
419         }
420
421
422         if($r) {
423
424                 if((local_user()) && (local_user() == $a->profile['uid'])) {
425                         q("UPDATE `item` SET `unseen` = 0
426                                 WHERE `parent` = %d AND `unseen` = 1",
427                                 intval($r[0]['parent'])
428                         );
429                 }
430
431                 $items = conv_sort($r,"`commented`");
432
433                 if(!$update)
434                         $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
435                 $o .= conversation($a,$items,'display', $update);
436
437                 // Preparing the meta header
438                 require_once('include/bbcode.php');
439                 require_once("include/html2plain.php");
440                 $description = trim(html2plain(bbcode($r[0]["body"], false, false), 0, true));
441                 $title = trim(html2plain(bbcode($r[0]["title"], false, false), 0, true));
442                 $author_name = $r[0]["author-name"];
443
444                 $image = "";
445                 if ($image == "")
446                         $image = $r[0]["thumb"];
447
448                 if ($title == "")
449                         $title = $author_name;
450
451                 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
452                 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
453                 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
454
455                 //<meta name="keywords" content="">
456                 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
457                 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
458                 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
459                 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
460
461                 // Schema.org microdata
462                 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
463                 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
464                 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
465                 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
466
467                 // Twitter cards
468                 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
469                 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
470                 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
471                 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
472                 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$r[0]["plink"].'" />'."\n";
473
474                 // Dublin Core
475                 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
476                 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
477
478                 // Open Graph
479                 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
480                 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
481                 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
482                 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$r[0]["plink"].'" />'."\n";
483                 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
484                 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
485                 // article:tag
486
487                 return $o;
488         }
489
490         $r = q("SELECT `id`,`deleted` FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
491                 dbesc($item_id),
492                 dbesc($item_id)
493         );
494         if($r) {
495                 if($r[0]['deleted']) {
496                         notice( t('Item has been removed.') . EOL );
497                 }
498                 else {
499                         notice( t('Permission denied.') . EOL );
500                 }
501         }
502         else {
503                 notice( t('Item not found.') . EOL );
504         }
505
506         return $o;
507 }
508