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