]> git.mxchange.org Git - friendica.git/blob - mod/display.php
Logger Levels
[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\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Group;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Protocol\ActivityPub;
22 use Friendica\Protocol\DFRN;
23
24 function display_init(App $a)
25 {
26         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
27                 return;
28         }
29
30         $nick = (($a->argc > 1) ? $a->argv[1] : '');
31         $profiledata = [];
32
33         if ($a->argc == 3) {
34                 if (substr($a->argv[2], -5) == '.atom') {
35                         $item_id = substr($a->argv[2], 0, -5);
36                         displayShowFeed($item_id, false);
37                 }
38         }
39
40         if ($a->argc == 4) {
41                 if ($a->argv[3] == 'conversation.atom') {
42                         $item_id = $a->argv[2];
43                         displayShowFeed($item_id, true);
44                 }
45         }
46
47         $item = null;
48
49         $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
50
51         // If there is only one parameter, then check if this parameter could be a guid
52         if ($a->argc == 2) {
53                 $nick = "";
54
55                 // Does the local user have this item?
56                 if (local_user()) {
57                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
58                         if (DBA::isResult($item)) {
59                                 $nick = $a->user["nickname"];
60                         }
61                 }
62
63                 // Is it an item with uid=0?
64                 if (!DBA::isResult($item)) {
65                         $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
66                 }
67         } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
68                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
69         }
70
71         if (!DBA::isResult($item)) {
72                 $a->error = 404;
73                 notice(L10n::t('Item not found.') . EOL);
74                 return;
75         }
76
77         if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
78                 Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
79                 displayShowFeed($item["id"], false);
80         }
81
82         if (ActivityPub::isRequest()) {
83                 $a->internalRedirect(str_replace('display/', 'objects/', $a->query_string));
84         }
85
86         if ($item["id"] != $item["parent"]) {
87                 $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
88         }
89
90         $profiledata = display_fetchauthor($a, $item);
91
92         if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
93                 $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
94
95                 if (($nickname != $a->user["nickname"])) {
96                         $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
97                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
98                                 WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
99                                 $nickname
100                         );
101                         if (DBA::isResult($profile)) {
102                                 $profiledata = $profile;
103                         }
104                         $profiledata["network"] = Protocol::DFRN;
105                 } else {
106                         $profiledata = [];
107                 }
108         }
109
110         Profile::load($a, $nick, 0, $profiledata);
111 }
112
113 function display_fetchauthor($a, $item)
114 {
115         $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
116
117         $profiledata = [];
118         $profiledata['uid'] = -1;
119         $profiledata['nickname'] = $author['nick'];
120         $profiledata['name'] = $author['name'];
121         $profiledata['picdate'] = '';
122         $profiledata['photo'] = $author['photo'];
123         $profiledata['url'] = $author['url'];
124         $profiledata['network'] = $author['network'];
125
126         // Check for a repeated message
127         $skip = false;
128         $body = trim($item["body"]);
129
130         // Skip if it isn't a pure repeated messages
131         // Does it start with a share?
132         if (!$skip && strpos($body, "[share") > 0) {
133                 $skip = true;
134         }
135         // Does it end with a share?
136         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
137                 $skip = true;
138         }
139         if (!$skip) {
140                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
141                 // Skip if there is no shared message in there
142                 if ($body == $attributes) {
143                         $skip = true;
144                 }
145         }
146
147         if (!$skip) {
148                 $author = "";
149                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
150                 if (!empty($matches[1])) {
151                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
152                 }
153                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
154                 if (!empty($matches[1])) {
155                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
156                 }
157                 $profile = "";
158                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
159                 if (!empty($matches[1])) {
160                         $profiledata["url"] = $matches[1];
161                 }
162                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
163                 if (!empty($matches[1])) {
164                         $profiledata["url"] = $matches[1];
165                 }
166                 $avatar = "";
167                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
168                 if (!empty($matches[1])) {
169                         $profiledata["photo"] = $matches[1];
170                 }
171                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
172                 if (!empty($matches[1])) {
173                         $profiledata["photo"] = $matches[1];
174                 }
175                 $profiledata["nickname"] = $profiledata["name"];
176                 $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
177
178                 $profiledata["address"] = "";
179                 $profiledata["about"] = "";
180         }
181
182         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
183
184         $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
185
186         if (local_user()) {
187                 if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
188                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
189                 }
190         } elseif ($profiledata["network"] == Protocol::DFRN) {
191                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
192                 $profiledata["remoteconnect"] = $connect;
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                 notice(L10n::t('Public access denied.') . EOL);
202                 return;
203         }
204
205         require_once 'include/conversation.php';
206
207         $o = '';
208
209         if ($update) {
210                 $item_id = $_REQUEST['item_id'];
211                 $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
212                 if ($item['uid'] != 0) {
213                         $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
214                 } else {
215                         $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
216                 }
217                 $item_parent = $item['parent'];
218                 $item_parent_uri = $item['parent-uri'];
219         } else {
220                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
221                 $item_parent = $item_id;
222
223                 if ($a->argc == 2) {
224                         $item_parent = 0;
225                         $fields = ['id', 'parent', 'parent-uri'];
226
227                         if (local_user()) {
228                                 $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
229                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
230                                 if (DBA::isResult($item)) {
231                                         $item_id = $item["id"];
232                                         $item_parent = $item["parent"];
233                                         $item_parent_uri = $item['parent-uri'];
234                                 }
235                         }
236
237                         if ($item_parent == 0) {
238                                 $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
239                                 $item = Item::selectFirstForUser(local_user(), $fields, $condition);
240                                 if (DBA::isResult($item)) {
241                                         $item_id = $item["id"];
242                                         $item_parent = $item["parent"];
243                                         $item_parent_uri = $item['parent-uri'];
244                                 }
245                         }
246                 }
247         }
248
249         if (!$item_id) {
250                 $a->error = 404;
251                 notice(L10n::t('Item not found.').EOL);
252                 return;
253         }
254
255         // We are displaying an "alternate" link if that post was public. See issue 2864
256         $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
257         if ($is_public) {
258                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
259                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
260                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
261         } else {
262                 $alternate = '';
263                 $conversation = '';
264         }
265
266         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
267                                 ['$alternate' => $alternate,
268                                         '$conversation' => $conversation]);
269
270         $groups = [];
271
272         $contact = null;
273         $is_remote_contact = false;
274
275         $contact_id = 0;
276
277         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
278                 foreach ($_SESSION['remote'] as $v) {
279                         if ($v['uid'] == $a->profile['uid']) {
280                                 $contact_id = $v['cid'];
281                                 break;
282                         }
283                 }
284         }
285
286         if ($contact_id) {
287                 $groups = Group::getIdsByContactId($contact_id);
288                 $remote_contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
289                 if (DBA::isResult($remote_contact)) {
290                         $contact = $remote_contact;
291                         $is_remote_contact = true;
292                 }
293         }
294
295         if (!$is_remote_contact) {
296                 if (local_user()) {
297                         $contact_id = $_SESSION['cid'];
298                         $contact = $a->contact;
299                 }
300         }
301
302         $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
303         if (DBA::isResult($page_contact)) {
304                 $a->page_contact = $page_contact;
305         }
306         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
307
308         if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) {
309                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
310                 return;
311         }
312
313         // We need the editor here to be able to reshare an item.
314         if ($is_owner) {
315                 $x = [
316                         'is_owner' => true,
317                         'allow_location' => $a->user['allow_location'],
318                         'default_location' => $a->user['default-location'],
319                         'nickname' => $a->user['nickname'],
320                         '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'),
321                         'acl' => ACL::getFullSelectorHTML($a->user, true),
322                         'bang' => '',
323                         'visitor' => 'block',
324                         'profile_uid' => local_user(),
325                 ];
326                 $o .= status_editor($a, $x, 0, true);
327         }
328
329         $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid'], $is_remote_contact, $groups);
330
331         if (local_user() && (local_user() == $a->profile['uid'])) {
332                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
333                 $unseen = Item::exists($condition);
334         } else {
335                 $unseen = false;
336         }
337
338         if ($update && !$unseen) {
339                 return '';
340         }
341
342         $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
343         $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
344         $item = Item::selectFirstForUser(local_user(), $fields, $condition);
345
346         if (!DBA::isResult($item)) {
347                 notice(L10n::t('Item not found.') . EOL);
348                 return $o;
349         }
350
351         $item['uri'] = $item['parent-uri'];
352
353         if ($unseen) {
354                 $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
355                 Item::update(['unseen' => false], $condition);
356         }
357
358         if (!$update) {
359                 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
360         }
361
362         $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', local_user());
363
364         // Preparing the meta header
365         $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
366         $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
367         $author_name = $item["author-name"];
368
369         $image = $a->removeBaseURL($item["author-avatar"]);
370
371         if ($title == "") {
372                 $title = $author_name;
373         }
374
375         // Limit the description to 160 characters
376         if (strlen($description) > 160) {
377                 $description = substr($description, 0, 157) . '...';
378         }
379
380         $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
381         $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
382         $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
383
384         //<meta name="keywords" content="">
385         $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
386         $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
387         $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
388         $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
389
390         // Schema.org microdata
391         $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
392         $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
393         $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
394         $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
395
396         // Twitter cards
397         $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
398         $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
399         $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
400         $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
401         $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
402
403         // Dublin Core
404         $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
405         $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
406
407         // Open Graph
408         $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
409         $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
410         $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
411         $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
412         $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
413         $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
414         // article:tag
415
416         return $o;
417 }
418
419 function displayShowFeed($item_id, $conversation)
420 {
421         $xml = DFRN::itemFeed($item_id, $conversation);
422         if ($xml == '') {
423                 System::httpExit(500);
424         }
425         header("Content-type: application/atom+xml");
426         echo $xml;
427         killme();
428 }