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