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