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