]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Merge remote-tracking branch 'upstream/develop' into diasppora-delivery
[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\Protocol\ActivityPub;
23 use Friendica\Protocol\DFRN;
24
25 function display_init(App $a)
26 {
27         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
28                 return;
29         }
30
31         $nick = (($a->argc > 1) ? $a->argv[1] : '');
32         $profiledata = [];
33
34         if ($a->argc == 3) {
35                 if (substr($a->argv[2], -5) == '.atom') {
36                         $item_id = substr($a->argv[2], 0, -5);
37                         displayShowFeed($item_id, false);
38                 }
39         }
40
41         if ($a->argc == 4) {
42                 if ($a->argv[3] == 'conversation.atom') {
43                         $item_id = $a->argv[2];
44                         displayShowFeed($item_id, true);
45                 }
46         }
47
48         $item = null;
49
50         $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
51
52         // If there is only one parameter, then check if this parameter could be a guid
53         if ($a->argc == 2) {
54                 $nick = "";
55
56                 // Does the local user have this item?
57                 if (local_user()) {
58                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
59                         if (DBA::isResult($item)) {
60                                 $nick = $a->user["nickname"];
61                         }
62                 }
63
64                 // Is it an item with uid=0?
65                 if (!DBA::isResult($item)) {
66                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
67                 }
68         } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
69                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
70         }
71
72         if (!DBA::isResult($item)) {
73                 $a->error = 404;
74                 notice(L10n::t('Item not found.') . EOL);
75                 return;
76         }
77
78         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
79                 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
80                 displayShowFeed($item["id"], false);
81         }
82
83         if (ActivityPub::isRequest()) {
84                 $a->internalRedirect(str_replace('display/', 'objects/', $a->query_string));
85         }
86
87         if ($item["id"] != $item["parent"]) {
88                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
89         }
90
91         $profiledata = display_fetchauthor($a, $item);
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                         $profile = DBA::fetchFirst("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 (DBA::isResult($profile)) {
103                                 $profiledata = $profile;
104                         }
105                         $profiledata["network"] = Protocol::DFRN;
106                 } else {
107                         $profiledata = [];
108                 }
109         }
110
111         Profile::load($a, $nick, 0, $profiledata);
112 }
113
114 function display_fetchauthor($a, $item)
115 {
116         $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
117
118         $profiledata = [];
119         $profiledata['uid'] = -1;
120         $profiledata['nickname'] = $author['nick'];
121         $profiledata['name'] = $author['name'];
122         $profiledata['picdate'] = '';
123         $profiledata['photo'] = $author['photo'];
124         $profiledata['url'] = $author['url'];
125         $profiledata['network'] = $author['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 (!empty($matches[1])) {
152                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
153                 }
154                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
155                 if (!empty($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 (!empty($matches[1])) {
161                         $profiledata["url"] = $matches[1];
162                 }
163                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
164                 if (!empty($matches[1])) {
165                         $profiledata["url"] = $matches[1];
166                 }
167                 $avatar = "";
168                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
169                 if (!empty($matches[1])) {
170                         $profiledata["photo"] = $matches[1];
171                 }
172                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
173                 if (!empty($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"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
189                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
190                 }
191         } elseif ($profiledata["network"] == Protocol::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 {
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/conversation.php';
207
208         $o = '';
209
210         if ($update) {
211                 $item_id = $_REQUEST['item_id'];
212                 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
213                 if ($item['uid'] != 0) {
214                         $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
215                 } else {
216                         $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
217                 }
218                 $item_parent = $item['parent'];
219                 $item_parent_uri = $item['parent-uri'];
220         } else {
221                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
222                 $item_parent = $item_id;
223
224                 if ($a->argc == 2) {
225                         $item_parent = 0;
226                         $fields = ['id', 'parent', 'parent-uri'];
227
228                         if (local_user()) {
229                                 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
230                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
231                                 if (DBA::isResult($item)) {
232                                         $item_id = $item["id"];
233                                         $item_parent = $item["parent"];
234                                         $item_parent_uri = $item['parent-uri'];
235                                 }
236                         }
237
238                         if ($item_parent == 0) {
239                                 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
240                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
241                                 if (DBA::isResult($item)) {
242                                         $item_id = $item["id"];
243                                         $item_parent = $item["parent"];
244                                         $item_parent_uri = $item['parent-uri'];
245                                 }
246                         }
247                 }
248         }
249
250         if (!$item_id) {
251                 $a->error = 404;
252                 notice(L10n::t('Item not found.').EOL);
253                 return;
254         }
255
256         // We are displaying an "alternate" link if that post was public. See issue 2864
257         $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
258         if ($is_public) {
259                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
260                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
261                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
262         } else {
263                 $alternate = '';
264                 $conversation = '';
265         }
266
267         $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
268                                 ['$alternate' => $alternate,
269                                         '$conversation' => $conversation]);
270
271         $groups = [];
272
273         $contact = null;
274         $is_remote_contact = false;
275
276         $contact_id = 0;
277
278         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
279                 foreach ($_SESSION['remote'] as $v) {
280                         if ($v['uid'] == $a->profile['uid']) {
281                                 $contact_id = $v['cid'];
282                                 break;
283                         }
284                 }
285         }
286
287         if ($contact_id) {
288                 $groups = Group::getIdsByContactId($contact_id);
289                 $remote_contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
290                 if (DBA::isResult($remote_contact)) {
291                         $contact = $remote_contact;
292                         $is_remote_contact = true;
293                 }
294         }
295
296         if (!$is_remote_contact) {
297                 if (local_user()) {
298                         $contact_id = $_SESSION['cid'];
299                         $contact = $a->contact;
300                 }
301         }
302
303         $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
304         if (DBA::isResult($page_contact)) {
305                 $a->page_contact = $page_contact;
306         }
307         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
308
309         if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) {
310                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
311                 return;
312         }
313
314         // We need the editor here to be able to reshare an item.
315         if ($is_owner) {
316                 $x = [
317                         'is_owner' => true,
318                         'allow_location' => $a->user['allow_location'],
319                         'default_location' => $a->user['default-location'],
320                         'nickname' => $a->user['nickname'],
321                         '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'),
322                         'acl' => ACL::getFullSelectorHTML($a->user, true),
323                         'bang' => '',
324                         'visitor' => 'block',
325                         'profile_uid' => local_user(),
326                 ];
327                 $o .= status_editor($a, $x, 0, true);
328         }
329
330         $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid'], $is_remote_contact, $groups);
331
332         if (local_user() && (local_user() == $a->profile['uid'])) {
333                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
334                 $unseen = Item::exists($condition);
335         } else {
336                 $unseen = false;
337         }
338
339         if ($update && !$unseen) {
340                 return '';
341         }
342
343         $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
344         $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
345         $item = Item::selectFirstForUser(local_user(), $fields, $condition);
346
347         if (!DBA::isResult($item)) {
348                 notice(L10n::t('Item not found.') . EOL);
349                 return $o;
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', local_user());
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         killme();
429 }