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