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