]> git.mxchange.org Git - friendica.git/blob - mod/display.php
54a54be1678fa615db4311afb735818faa6a257b
[friendica.git] / mod / display.php
1 <?php
2 /**
3  * @file mod/display.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Config;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Group;
12 use Friendica\Model\Profile;
13 use Friendica\Protocol\DFRN;
14 use Friendica\Util\Network;
15
16 function display_init(App $a)
17 {
18         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
19                 return;
20         }
21
22         $nick = (($a->argc > 1) ? $a->argv[1] : '');
23         $profiledata = [];
24
25         if ($a->argc == 3) {
26                 if (substr($a->argv[2], -5) == '.atom') {
27                         $item_id = substr($a->argv[2], 0, -5);
28                         displayShowFeed($item_id, false);
29                 }
30         }
31
32         if ($a->argc == 4) {
33                 if ($a->argv[3] == 'conversation.atom') {
34                         $item_id = $a->argv[2];
35                         displayShowFeed($item_id, true);
36                 }
37         }
38
39         $r = false;
40
41         // If there is only one parameter, then check if this parameter could be a guid
42         if ($a->argc == 2) {
43                 $nick = "";
44                 $r = false;
45
46                 // Does the local user have this item?
47                 if (local_user()) {
48                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
49                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
50                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
51                                         AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
52                         if (DBM::is_result($r)) {
53                                 $nick = $a->user["nickname"];
54                         }
55                 }
56
57                 // Is it an item with uid=0?
58                 if (!DBM::is_result($r)) {
59                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
60                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
61                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
62                                         AND NOT `private` AND `uid` = 0
63                                         AND `guid` = ? LIMIT 1", $a->argv[1]);
64                 }
65
66                 if (!DBM::is_result($r)) {
67                         $a->error = 404;
68                         notice(L10n::t('Item not found.') . EOL);
69                         return;
70                 }
71         } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
72                 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
73                                         `author-avatar`, `network`, `body`, `uid`, `owner-link`
74                         FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
75                                 AND NOT `private` AND `uid` = 0
76                                 AND `id` = ? LIMIT 1", $a->argv[2]);
77         }
78
79         if (DBM::is_result($r)) {
80                 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
81                         logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
82                         displayShowFeed($r["id"], false);
83                 }
84
85                 if ($r["id"] != $r["parent"]) {
86                         $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
87                                 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
88                                         AND `id` = ?", $r["parent"]);
89                 }
90
91                 $profiledata = display_fetchauthor($a, $r);
92
93                 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
94                         $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
95
96                         if (($nickname != $a->user["nickname"])) {
97                                 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
98                                         INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
99                                         WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
100                                         $nickname
101                                 );
102                                 if (DBM::is_result($r)) {
103                                         $profiledata = $r;
104                                 }
105                                 $profiledata["network"] = NETWORK_DFRN;
106                         } else {
107                                 $profiledata = [];
108                         }
109                 }
110         }
111
112         Profile::load($a, $nick, 0, $profiledata);
113 }
114
115 function display_fetchauthor($a, $item) {
116         $profiledata = [];
117         $profiledata["uid"] = -1;
118         $profiledata["nickname"] = $item["author-name"];
119         $profiledata["name"] = $item["author-name"];
120         $profiledata["picdate"] = "";
121         $profiledata["photo"] = $item["author-avatar"];
122         $profiledata["url"] = $item["author-link"];
123         $profiledata["network"] = $item["network"];
124
125         // Check for a repeated message
126         $skip = false;
127         $body = trim($item["body"]);
128
129         // Skip if it isn't a pure repeated messages
130         // Does it start with a share?
131         if (!$skip && strpos($body, "[share") > 0) {
132                 $skip = true;
133         }
134         // Does it end with a share?
135         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
136                 $skip = true;
137         }
138         if (!$skip) {
139                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
140                 // Skip if there is no shared message in there
141                 if ($body == $attributes) {
142                         $skip = true;
143                 }
144         }
145
146         if (!$skip) {
147                 $author = "";
148                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
149                 if ($matches[1] != "") {
150                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
151                 }
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                 $profile = "";
157                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
158                 if ($matches[1] != "") {
159                         $profiledata["url"] = $matches[1];
160                 }
161                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
162                 if ($matches[1] != "") {
163                         $profiledata["url"] = $matches[1];
164                 }
165                 $avatar = "";
166                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
167                 if ($matches[1] != "") {
168                         $profiledata["photo"] = $matches[1];
169                 }
170                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
171                 if ($matches[1] != "") {
172                         $profiledata["photo"] = $matches[1];
173                 }
174                 $profiledata["nickname"] = $profiledata["name"];
175                 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
176
177                 $profiledata["address"] = "";
178                 $profiledata["about"] = "";
179         }
180
181         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
182
183         $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
184
185         if (local_user()) {
186                 if (in_array($profiledata["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
187                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
188                 }
189         } elseif ($profiledata["network"] == NETWORK_DFRN) {
190                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
191                 $profiledata["remoteconnect"] = $connect;
192         }
193
194         return($profiledata);
195 }
196
197 function display_content(App $a, $update = false, $update_uid = 0) {
198         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
199                 notice(L10n::t('Public access denied.') . EOL);
200                 return;
201         }
202
203         require_once 'include/security.php';
204         require_once 'include/conversation.php';
205         require_once 'include/acl_selectors.php';
206
207         $o = '';
208
209         if ($update) {
210                 $item_id = $_REQUEST['item_id'];
211                 $item = dba::selectFirst('item', ['uid', 'parent'], ['id' => $item_id]);
212                 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
213                 $item_parent = $item['parent'];
214         } else {
215                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
216
217                 if ($a->argc == 2) {
218                         $item_parent = 0;
219
220                         if (local_user()) {
221                                 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
222                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
223                                                 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
224                                 if (DBM::is_result($r)) {
225                                         $item_id = $r["id"];
226                                         $item_parent = $r["parent"];
227                                 }
228                         }
229
230                         if ($item_parent == 0) {
231                                 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
232                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
233                                                 AND NOT `item`.`private` AND `item`.`uid` = 0
234                                                 AND `item`.`guid` = ?", $a->argv[1]);
235                                 if (DBM::is_result($r)) {
236                                         $item_id = $r["id"];
237                                         $item_parent = $r["parent"];
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' => populate_acl($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 ($update) {
329                 $r = dba::p("SELECT `id` FROM `item` WHERE
330                         `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
331                         $sql_extra AND `unseen`",
332                         $item_id
333                 );
334
335                 if (dba::num_rows($r) == 0) {
336                         return '';
337                 }
338         }
339
340         $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
341                 $sql_extra
342                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
343                 $item_id
344         );
345
346         if (!DBM::is_result($r)) {
347                 notice(L10n::t('Item not found.') . EOL);
348                 return $o;
349         }
350
351         $s = dba::inArray($r);
352
353         if (local_user() && (local_user() == $a->profile['uid'])) {
354                 $unseen = dba::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
355                 if (DBM::is_result($unseen)) {
356                         dba::update('item', ['unseen' => false], ['parent' => $s[0]['parent'], 'unseen' => true]);
357                 }
358         }
359
360         $items = conv_sort($s, "`commented`");
361
362         if (!$update) {
363                 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
364         }
365         $o .= conversation($a, $items, 'display', $update_uid);
366
367         // Preparing the meta header
368         require_once 'include/bbcode.php';
369         require_once 'include/html2plain.php';
370         $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
371         $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
372         $author_name = $s[0]["author-name"];
373
374         $image = $a->remove_baseurl($s[0]["author-thumb"]);
375
376         if ($title == "") {
377                 $title = $author_name;
378         }
379
380         // Limit the description to 160 characters
381         if (strlen($description) > 160) {
382                 $description = substr($description, 0, 157) . '...';
383         }
384
385         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
386         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
387         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
388
389         //<meta name="keywords" content="">
390         $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
391         $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
392         $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
393         $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
394
395         // Schema.org microdata
396         $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
397         $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
398         $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
399         $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
400
401         // Twitter cards
402         $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
403         $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
404         $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
405         $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
406         $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
407
408         // Dublin Core
409         $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
410         $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
411
412         // Open Graph
413         $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
414         $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
415         $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
416         $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
417         $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
418         $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
419         // article:tag
420
421         return $o;
422 }
423
424 function displayShowFeed($item_id, $conversation) {
425         $xml = DFRN::itemFeed($item_id, $conversation);
426         if ($xml == '') {
427                 Network::httpStatusExit(500);
428         }
429         header("Content-type: application/atom+xml");
430         echo $xml;
431         killme();
432 }