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