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