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