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