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