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