]> git.mxchange.org Git - friendica.git/blob - mod/display.php
31b6a13538b30b739d300d40358f9bd55ecf07a3
[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 `allow_cid` = ''  AND `allow_gid` = ''
56                                         AND `deny_cid`  = '' AND `deny_gid`  = ''
57                                         AND NOT `private` AND `uid` = 0
58                                         AND `guid` = ? LIMIT 1", $a->argv[1]);
59                 }
60
61                 if (!DBM::is_result($r)) {
62                         $a->error = 404;
63                         notice(t('Item not found.') . EOL);
64                         return;
65                 }
66         } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
67                 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
68                                         `author-avatar`, `network`, `body`, `uid`, `owner-link`
69                         FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
70                                 AND `allow_cid` = ''  AND `allow_gid` = ''
71                                 AND `deny_cid`  = '' AND `deny_gid`  = ''
72                                 AND NOT `private` AND `uid` = 0
73                                 AND `id` = ? LIMIT 1", $a->argv[2]);
74         }
75
76         if (DBM::is_result($r)) {
77                 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
78                         logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
79                         displayShowFeed($r["id"], false);
80                 }
81
82                 if ($r["id"] != $r["parent"]) {
83                         $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
84                                 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
85                                         AND `id` = ?", $r["parent"]);
86                 }
87
88                 $profiledata = display_fetchauthor($a, $r);
89
90                 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
91                         $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
92
93                         if (($nickname != $a->user["nickname"])) {
94                                 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
95                                         INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
96                                         WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
97                                         $nickname
98                                 );
99                                 if (DBM::is_result($r)) {
100                                         $profiledata = $r;
101                                 }
102                                 $profiledata["network"] = NETWORK_DFRN;
103                         } else {
104                                 $profiledata = array();
105                         }
106                 }
107         }
108
109         profile_load($a, $nick, 0, $profiledata);
110 }
111
112 function display_fetchauthor($a, $item) {
113         $profiledata = array();
114         $profiledata["uid"] = -1;
115         $profiledata["nickname"] = $item["author-name"];
116         $profiledata["name"] = $item["author-name"];
117         $profiledata["picdate"] = "";
118         $profiledata["photo"] = $item["author-avatar"];
119         $profiledata["url"] = $item["author-link"];
120         $profiledata["network"] = $item["network"];
121
122         // Check for a repeated message
123         $skip = false;
124         $body = trim($item["body"]);
125
126         // Skip if it isn't a pure repeated messages
127         // Does it start with a share?
128         if (!$skip && strpos($body, "[share") > 0) {
129                 $skip = true;
130         }
131         // Does it end with a share?
132         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
133                 $skip = true;
134         }
135         if (!$skip) {
136                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
137                 // Skip if there is no shared message in there
138                 if ($body == $attributes) {
139                         $skip = true;
140                 }
141         }
142
143         if (!$skip) {
144                 $author = "";
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                 preg_match('/author="(.*?)"/ism', $attributes, $matches);
150                 if ($matches[1] != "") {
151                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
152                 }
153                 $profile = "";
154                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
155                 if ($matches[1] != "") {
156                         $profiledata["url"] = $matches[1];
157                 }
158                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
159                 if ($matches[1] != "") {
160                         $profiledata["url"] = $matches[1];
161                 }
162                 $avatar = "";
163                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
164                 if ($matches[1] != "") {
165                         $profiledata["photo"] = $matches[1];
166                 }
167                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
168                 if ($matches[1] != "") {
169                         $profiledata["photo"] = $matches[1];
170                 }
171                 $profiledata["nickname"] = $profiledata["name"];
172                 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
173
174                 $profiledata["address"] = "";
175                 $profiledata["about"] = "";
176         }
177
178         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
179
180         $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
181
182         if (local_user()) {
183                 if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
184                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
185                 }
186         } elseif ($profiledata["network"] == NETWORK_DFRN) {
187                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
188                 $profiledata["remoteconnect"] = $connect;
189         }
190
191         return($profiledata);
192 }
193
194 function display_content(App $a, $update = false, $update_uid = 0) {
195         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
196                 notice(t('Public access denied.') . EOL);
197                 return;
198         }
199
200         require_once('include/security.php');
201         require_once('include/conversation.php');
202         require_once('include/acl_selectors.php');
203
204         $o = '';
205
206         if ($update) {
207                 $item_id = $_REQUEST['item_id'];
208                 $item = dba::select('item', ['uid'], ['id' => $item_id], ['limit' => 1]);
209                 $a->profile = array('uid' => intval($item['uid']), 'profile_uid' => intval($item['uid']));
210         } else {
211                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
212
213                 if ($a->argc == 2) {
214                         $item_parent = 0;
215
216                         if (local_user()) {
217                                 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
218                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
219                                                 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
220                                 if (DBM::is_result($r)) {
221                                         $item_id = $r["id"];
222                                         $item_parent = $r["parent"];
223                                 }
224                         }
225
226                         if ($item_parent == 0) {
227                                 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
228                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
229                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
230                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
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 (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 = init_groups_visitor($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 ($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                         'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
321                 );
322                 $o .= status_editor($a,$x,0,true);
323         }
324
325         $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups);
326
327         if ($update) {
328                 $r = dba::p("SELECT `id` FROM `item` WHERE
329                         `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
330                         $sql_extra AND `unseen`",
331                         $item_id
332                 );
333
334                 if (dba::num_rows($r) == 0) {
335                         return '';
336                 }
337         }
338
339         $r = dba::p(item_query()."AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
340                 $sql_extra
341                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
342                 $item_id
343         );
344
345         if (!DBM::is_result($r)) {
346                 notice(t('Item not found.') . EOL);
347                 return $o;
348         }
349
350         $s = dba::inArray($r);
351
352         if (local_user() && (local_user() == $a->profile['uid'])) {
353                 $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
354                 if (DBM::is_result($unseen)) {
355                         dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
356                 }
357         }
358
359         $items = conv_sort($s, "`commented`");
360
361         if (!$update) {
362                 $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
363         }
364         $o .= conversation($a, $items, 'display', $update_uid);
365
366         // Preparing the meta header
367         require_once('include/bbcode.php');
368         require_once("include/html2plain.php");
369         $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
370         $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
371         $author_name = $s[0]["author-name"];
372
373         $image = $a->remove_baseurl($s[0]["author-thumb"]);
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="'.$s[0]["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="'.$s[0]["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         $xml = DFRN::itemFeed($item_id, $conversation);
425         if ($xml == '') {
426                 http_status_exit(500);
427         }
428         header("Content-type: application/atom+xml");
429         echo $xml;
430         killme();
431 }