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