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