]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Remove deprecated App::removeBaseURL - process methods to DI::baseUrl()->remove()
[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\Core\Session;
18 use Friendica\Database\DBA;
19 use Friendica\DI;
20 use Friendica\Model\Contact;
21 use Friendica\Model\Group;
22 use Friendica\Model\Item;
23 use Friendica\Model\Profile;
24 use Friendica\Module\Objects;
25 use Friendica\Network\HTTPException;
26 use Friendica\Protocol\ActivityPub;
27 use Friendica\Protocol\DFRN;
28 use Friendica\Util\Strings;
29
30 function display_init(App $a)
31 {
32         if (ActivityPub::isRequest()) {
33                 Objects::rawContent();
34         }
35
36         if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
37                 return;
38         }
39
40         $nick = (($a->argc > 1) ? $a->argv[1] : '');
41
42         $item = null;
43         $item_user = local_user();
44
45         $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
46
47         // If there is only one parameter, then check if this parameter could be a guid
48         if ($a->argc == 2) {
49                 $nick = "";
50
51                 // Does the local user have this item?
52                 if (local_user()) {
53                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
54                         if (DBA::isResult($item)) {
55                                 $nick = $a->user["nickname"];
56                         }
57                 }
58
59                 // Is this item private but could be visible to the remove visitor?
60                 if (!DBA::isResult($item) && remote_user()) {
61                         $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
62                         if (DBA::isResult($item)) {
63                                 if (!Contact::isFollower(remote_user(), $item['uid'])) {
64                                         $item = null;
65                                 } else {
66                                         $item_user = $item['uid'];
67                                 }
68                         }
69                 }
70
71                 // Is it an item with uid=0?
72                 if (!DBA::isResult($item)) {
73                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
74                 }
75         } elseif ($a->argc >= 3 && $nick == 'feed-item') {
76                 $item_id = $a->argv[2];
77                 if (substr($item_id, -5) == '.atom') {
78                         $item_id = substr($item_id, 0, -5);
79                 }
80                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item_id, 'private' => [0, 2], 'uid' => 0]);
81         }
82
83         if (!DBA::isResult($item)) {
84                 return;
85         }
86
87         if ($a->argc >= 3 && $nick == 'feed-item') {
88                 displayShowFeed($item['id'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
89         }
90
91         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
92                 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
93                 displayShowFeed($item["id"], false);
94         }
95
96         if ($item["id"] != $item["parent"]) {
97                 $parent = Item::selectFirstForUser($item_user, $fields, ['id' => $item["parent"]]);
98                 $item = $parent ?: $item;
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         $shared = Item::getShareArray($item);
139         if (!empty($shared) && empty($shared['comment'])) {
140                 if (!empty($shared['author'])) {
141                         $profiledata['name'] = $shared['author'];
142                 }
143
144                 if (!empty($shared['profile'])) {
145                         $profiledata['url'] = $shared['profile'];
146                 }
147
148                 if (!empty($shared['avatar'])) {
149                         $profiledata['photo'] = $shared['avatar'];
150                 }
151
152                 $profiledata["nickname"] = $profiledata["name"];
153                 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
154
155                 $profiledata["address"] = "";
156                 $profiledata["about"] = "";
157         }
158
159         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
160
161         if (!empty($profiledata["photo"])) {
162                 $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
163         }
164
165         return $profiledata;
166 }
167
168 function display_content(App $a, $update = false, $update_uid = 0)
169 {
170         if (Config::get('system','block_public') && !Session::isAuthenticated()) {
171                 throw new HTTPException\ForbiddenException(L10n::t('Public access denied.'));
172         }
173
174         $o = '';
175
176         $item = null;
177
178         if ($update) {
179                 $item_id = $_REQUEST['item_id'];
180                 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
181                 if ($item['uid'] != 0) {
182                         $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
183                 } else {
184                         $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
185                 }
186                 $item_parent = $item['parent'];
187                 $item_parent_uri = $item['parent-uri'];
188         } else {
189                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
190                 $item_parent = $item_id;
191
192                 if ($a->argc == 2) {
193                         $item_parent = 0;
194                         $fields = ['id', 'parent', 'parent-uri', 'uid'];
195
196                         if (local_user()) {
197                                 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
198                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
199                                 if (DBA::isResult($item)) {
200                                         $item_id = $item["id"];
201                                         $item_parent = $item["parent"];
202                                         $item_parent_uri = $item['parent-uri'];
203                                 }
204                         }
205
206                         if (($item_parent == 0) && remote_user()) {
207                                 $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
208                                 if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
209                                         $item_id = $item["id"];
210                                         $item_parent = $item["parent"];
211                                         $item_parent_uri = $item['parent-uri'];
212                                 }
213                         }
214
215                         if ($item_parent == 0) {
216                                 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
217                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
218                                 if (DBA::isResult($item)) {
219                                         $item_id = $item["id"];
220                                         $item_parent = $item["parent"];
221                                         $item_parent_uri = $item['parent-uri'];
222                                 }
223                         }
224                 }
225         }
226
227         if (empty($item)) {
228                 throw new HTTPException\NotFoundException(L10n::t('The requested item doesn\'t exist or has been deleted.'));
229         }
230
231         // We are displaying an "alternate" link if that post was public. See issue 2864
232         $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
233         if ($is_public) {
234                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
235                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
236                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
237         } else {
238                 $alternate = '';
239                 $conversation = '';
240         }
241
242         $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
243                                 ['$alternate' => $alternate,
244                                         '$conversation' => $conversation]);
245
246         $is_remote_contact = false;
247         $item_uid = local_user();
248
249         $parent = null;
250         if (!empty($item_parent_uri)) {
251                 $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
252         }
253
254         if (DBA::isResult($parent)) {
255                 $a->profile['uid'] = ($a->profile['uid'] ?? 0) ?: $parent['uid'];
256                 $a->profile['profile_uid'] = ($a->profile['profile_uid'] ?? 0) ?: $parent['uid'];
257                 $is_remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
258                 if ($is_remote_contact) {
259                         $item_uid = $parent['uid'];
260                 }
261         } else {
262                 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
263         }
264
265         $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
266         if (DBA::isResult($page_contact)) {
267                 $a->page_contact = $page_contact;
268         }
269
270         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
271
272         if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
273                 throw new HTTPException\ForbiddenException(L10n::t('Access to this profile has been restricted.'));
274         }
275
276         // We need the editor here to be able to reshare an item.
277         if ($is_owner) {
278                 $x = [
279                         'is_owner' => true,
280                         'allow_location' => $a->user['allow_location'],
281                         'default_location' => $a->user['default-location'],
282                         'nickname' => $a->user['nickname'],
283                         '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'),
284                         'acl' => ACL::getFullSelectorHTML($a->page, $a->user, true),
285                         'bang' => '',
286                         'visitor' => 'block',
287                         'profile_uid' => local_user(),
288                 ];
289                 $o .= status_editor($a, $x, 0, true);
290         }
291         $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
292
293         if (local_user() && (local_user() == $a->profile['profile_uid'])) {
294                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
295                 $unseen = Item::exists($condition);
296         } else {
297                 $unseen = false;
298         }
299
300         if ($update && !$unseen) {
301                 return '';
302         }
303
304         $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
305         $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
306         $item = Item::selectFirstForUser($a->profile['profile_uid'], $fields, $condition);
307
308         if (!DBA::isResult($item)) {
309                 throw new HTTPException\NotFoundException(L10n::t('The requested item doesn\'t exist or has been deleted.'));
310         }
311
312         $item['uri'] = $item['parent-uri'];
313
314         if ($unseen) {
315                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
316                 Item::update(['unseen' => false], $condition);
317         }
318
319         if (!$update) {
320                 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
321         }
322
323         $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', $item_uid);
324
325         // Preparing the meta header
326         $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
327         $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
328         $author_name = $item["author-name"];
329
330         $image = DI::baseUrl()->remove($item["author-avatar"]);
331
332         if ($title == "") {
333                 $title = $author_name;
334         }
335
336         // Limit the description to 160 characters
337         if (strlen($description) > 160) {
338                 $description = substr($description, 0, 157) . '...';
339         }
340
341         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
342         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
343         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
344
345         if (DBA::exists('contact', ['unsearchable' => true, 'id' => [$item['contact-id'], $item['author-id'], $item['owner-id']]])) {
346                 $a->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
347         }
348
349         $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
350         $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
351         $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
352         $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
353
354         // Schema.org microdata
355         $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
356         $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
357         $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
358         $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
359
360         // Twitter cards
361         $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
362         $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
363         $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
364         $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
365         $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
366
367         // Dublin Core
368         $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
369         $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
370
371         // Open Graph
372         $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
373         $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
374         $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
375         $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
376         $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
377         $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
378         // article:tag
379
380         return $o;
381 }
382
383 function displayShowFeed($item_id, $conversation)
384 {
385         $xml = DFRN::itemFeed($item_id, $conversation);
386         if ($xml == '') {
387                 throw new HTTPException\InternalServerErrorException(L10n::t('The feed for this item is unavailable.'));
388         }
389         header("Content-type: application/atom+xml");
390         echo $xml;
391         exit();
392 }