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