Fix: In combination with "showmore" long posts aren't shrinked anymore every few...
[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\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Protocol;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Group;
15 use Friendica\Model\Profile;
16 use Friendica\Protocol\DFRN;
17
18 function display_init(App $a)
19 {
20         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
21                 return;
22         }
23
24         $nick = (($a->argc > 1) ? $a->argv[1] : '');
25         $profiledata = [];
26
27         if ($a->argc == 3) {
28                 if (substr($a->argv[2], -5) == '.atom') {
29                         $item_id = substr($a->argv[2], 0, -5);
30                         displayShowFeed($item_id, false);
31                 }
32         }
33
34         if ($a->argc == 4) {
35                 if ($a->argv[3] == 'conversation.atom') {
36                         $item_id = $a->argv[2];
37                         displayShowFeed($item_id, true);
38                 }
39         }
40
41         $r = false;
42
43         // If there is only one parameter, then check if this parameter could be a guid
44         if ($a->argc == 2) {
45                 $nick = "";
46                 $r = false;
47
48                 // Does the local user have this item?
49                 if (local_user()) {
50                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
51                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
52                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
53                                         AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
54                         if (DBM::is_result($r)) {
55                                 $nick = $a->user["nickname"];
56                         }
57                 }
58
59                 // Is it an item with uid=0?
60                 if (!DBM::is_result($r)) {
61                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
62                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
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-name`, `author-link`,
75                                         `author-avatar`, `network`, `body`, `uid`, `owner-link`
76                         FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
77                                 AND NOT `private` AND `uid` = 0
78                                 AND `id` = ? LIMIT 1", $a->argv[2]);
79         }
80
81         if (DBM::is_result($r)) {
82                 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
83                         logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
84                         displayShowFeed($r["id"], false);
85                 }
86
87                 if ($r["id"] != $r["parent"]) {
88                         $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
89                                 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
90                                         AND `id` = ?", $r["parent"]);
91                 }
92
93                 $profiledata = display_fetchauthor($a, $r);
94
95                 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
96                         $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
97
98                         if (($nickname != $a->user["nickname"])) {
99                                 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
100                                         INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
101                                         WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
102                                         $nickname
103                                 );
104                                 if (DBM::is_result($r)) {
105                                         $profiledata = $r;
106                                 }
107                                 $profiledata["network"] = NETWORK_DFRN;
108                         } else {
109                                 $profiledata = [];
110                         }
111                 }
112         }
113
114         Profile::load($a, $nick, 0, $profiledata);
115 }
116
117 function display_fetchauthor($a, $item) {
118         $profiledata = [];
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"] = Protocol::matchByProfileUrl($profiledata["url"]);
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"], [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 = false, $update_uid = 0) {
200         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
201                 notice(L10n::t('Public access denied.') . EOL);
202                 return;
203         }
204
205         require_once 'include/security.php';
206         require_once 'include/conversation.php';
207         require_once 'include/acl_selectors.php';
208
209         $o = '';
210
211         if ($update) {
212                 $item_id = $_REQUEST['item_id'];
213                 $item = dba::selectFirst('item', ['uid', 'parent'], ['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         } else {
221                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
222
223                 if ($a->argc == 2) {
224                         $item_parent = 0;
225
226                         if (local_user()) {
227                                 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
228                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
229                                                 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
230                                 if (DBM::is_result($r)) {
231                                         $item_id = $r["id"];
232                                         $item_parent = $r["parent"];
233                                 }
234                         }
235
236                         if ($item_parent == 0) {
237                                 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
238                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
239                                                 AND NOT `item`.`private` AND `item`.`uid` = 0
240                                                 AND `item`.`guid` = ?", $a->argv[1]);
241                                 if (DBM::is_result($r)) {
242                                         $item_id = $r["id"];
243                                         $item_parent = $r["parent"];
244                                 }
245                         }
246                 }
247         }
248
249         if (!$item_id) {
250                 $a->error = 404;
251                 notice(L10n::t('Item not found.').EOL);
252                 return;
253         }
254
255         // We are displaying an "alternate" link if that post was public. See issue 2864
256         $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
257         if ($is_public) {
258                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
259                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
260                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
261         } else {
262                 $alternate = '';
263                 $conversation = '';
264         }
265
266         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
267                                 ['$alternate' => $alternate,
268                                         '$conversation' => $conversation]);
269
270         $groups = [];
271
272         $contact = null;
273         $remote_contact = false;
274
275         $contact_id = 0;
276
277         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
278                 foreach ($_SESSION['remote'] as $v) {
279                         if ($v['uid'] == $a->profile['uid']) {
280                                 $contact_id = $v['cid'];
281                                 break;
282                         }
283                 }
284         }
285
286         if ($contact_id) {
287                 $groups = Group::getIdsByContactId($contact_id);
288                 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
289                         $contact_id,
290                         $a->profile['uid']
291                 );
292                 if (DBM::is_result($r)) {
293                         $contact = $r;
294                         $remote_contact = true;
295                 }
296         }
297
298         if (!$remote_contact) {
299                 if (local_user()) {
300                         $contact_id = $_SESSION['cid'];
301                         $contact = $a->contact;
302                 }
303         }
304
305         $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
306         if (DBM::is_result($r)) {
307                 $a->page_contact = $r;
308         }
309         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
310
311         if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
312                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
313                 return;
314         }
315
316         // We need the editor here to be able to reshare an item.
317         if ($is_owner) {
318                 $x = [
319                         'is_owner' => true,
320                         'allow_location' => $a->user['allow_location'],
321                         'default_location' => $a->user['default-location'],
322                         'nickname' => $a->user['nickname'],
323                         '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'),
324                         'acl' => populate_acl($a->user, true),
325                         'bang' => '',
326                         'visitor' => 'block',
327                         'profile_uid' => local_user(),
328                 ];
329                 $o .= status_editor($a, $x, 0, true);
330         }
331
332         $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups);
333
334         if ($update) {
335                 $r = dba::p("SELECT `id` FROM `item` WHERE
336                         `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
337                         $sql_extra AND `unseen`",
338                         $item_id
339                 );
340
341                 if (dba::num_rows($r) == 0) {
342                         return '';
343                 }
344         }
345
346         $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
347                 $sql_extra
348                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
349                 $item_id
350         );
351
352         if (!DBM::is_result($r)) {
353                 notice(L10n::t('Item not found.') . EOL);
354                 return $o;
355         }
356
357         $s = dba::inArray($r);
358
359         if (local_user() && (local_user() == $a->profile['uid'])) {
360                 $unseen = dba::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
361                 if (DBM::is_result($unseen)) {
362                         dba::update('item', ['unseen' => false], ['parent' => $s[0]['parent'], 'unseen' => true]);
363                 }
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);
372
373         // Preparing the meta header
374         require_once 'include/html2plain.php';
375
376         $description = trim(html2plain(BBCode::convert($s[0]["body"], false), 0, true));
377         $title = trim(html2plain(BBCode::convert($s[0]["title"], false), 0, true));
378         $author_name = $s[0]["author-name"];
379
380         $image = $a->remove_baseurl($s[0]["author-thumb"]);
381
382         if ($title == "") {
383                 $title = $author_name;
384         }
385
386         // Limit the description to 160 characters
387         if (strlen($description) > 160) {
388                 $description = substr($description, 0, 157) . '...';
389         }
390
391         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
392         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
393         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
394
395         //<meta name="keywords" content="">
396         $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
397         $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
398         $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
399         $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
400
401         // Schema.org microdata
402         $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
403         $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
404         $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
405         $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
406
407         // Twitter cards
408         $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
409         $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
410         $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
411         $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
412         $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
413
414         // Dublin Core
415         $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
416         $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
417
418         // Open Graph
419         $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
420         $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
421         $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
422         $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
423         $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
424         $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
425         // article:tag
426
427         return $o;
428 }
429
430 function displayShowFeed($item_id, $conversation) {
431         $xml = DFRN::itemFeed($item_id, $conversation);
432         if ($xml == '') {
433                 System::httpExit(500);
434         }
435         header("Content-type: application/atom+xml");
436         echo $xml;
437         killme();
438 }