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