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