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