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