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