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