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