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