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