Merge pull request #4340 from MrPetovan/task/4265-mov-jgrowl-to-composer
[friendica.git/.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\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Group;
12 use Friendica\Model\Profile;
13 use Friendica\Protocol\DFRN;
14
15 function display_init(App $a)
16 {
17         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
18                 return;
19         }
20
21         $nick = (($a->argc > 1) ? $a->argv[1] : '');
22         $profiledata = [];
23
24         if ($a->argc == 3) {
25                 if (substr($a->argv[2], -5) == '.atom') {
26                         $item_id = substr($a->argv[2], 0, -5);
27                         displayShowFeed($item_id, false);
28                 }
29         }
30
31         if ($a->argc == 4) {
32                 if ($a->argv[3] == 'conversation.atom') {
33                         $item_id = $a->argv[2];
34                         displayShowFeed($item_id, true);
35                 }
36         }
37
38         $r = false;
39
40         // If there is only one parameter, then check if this parameter could be a guid
41         if ($a->argc == 2) {
42                 $nick = "";
43                 $r = false;
44
45                 // Does the local user have this item?
46                 if (local_user()) {
47                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
48                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
49                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
50                                         AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
51                         if (DBM::is_result($r)) {
52                                 $nick = $a->user["nickname"];
53                         }
54                 }
55
56                 // Is it an item with uid=0?
57                 if (!DBM::is_result($r)) {
58                         $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
59                                                 `author-avatar`, `network`, `body`, `uid`, `owner-link`
60                                 FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
61                                         AND NOT `private` AND `uid` = 0
62                                         AND `guid` = ? LIMIT 1", $a->argv[1]);
63                 }
64
65                 if (!DBM::is_result($r)) {
66                         $a->error = 404;
67                         notice(L10n::t('Item not found.') . EOL);
68                         return;
69                 }
70         } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
71                 $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`,
72                                         `author-avatar`, `network`, `body`, `uid`, `owner-link`
73                         FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
74                                 AND NOT `private` AND `uid` = 0
75                                 AND `id` = ? LIMIT 1", $a->argv[2]);
76         }
77
78         if (DBM::is_result($r)) {
79                 if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
80                         logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG);
81                         displayShowFeed($r["id"], false);
82                 }
83
84                 if ($r["id"] != $r["parent"]) {
85                         $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
86                                 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
87                                         AND `id` = ?", $r["parent"]);
88                 }
89
90                 $profiledata = display_fetchauthor($a, $r);
91
92                 if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
93                         $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
94
95                         if (($nickname != $a->user["nickname"])) {
96                                 $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
97                                         INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
98                                         WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
99                                         $nickname
100                                 );
101                                 if (DBM::is_result($r)) {
102                                         $profiledata = $r;
103                                 }
104                                 $profiledata["network"] = NETWORK_DFRN;
105                         } else {
106                                 $profiledata = [];
107                         }
108                 }
109         }
110
111         Profile::load($a, $nick, 0, $profiledata);
112 }
113
114 function display_fetchauthor($a, $item) {
115         $profiledata = [];
116         $profiledata["uid"] = -1;
117         $profiledata["nickname"] = $item["author-name"];
118         $profiledata["name"] = $item["author-name"];
119         $profiledata["picdate"] = "";
120         $profiledata["photo"] = $item["author-avatar"];
121         $profiledata["url"] = $item["author-link"];
122         $profiledata["network"] = $item["network"];
123
124         // Check for a repeated message
125         $skip = false;
126         $body = trim($item["body"]);
127
128         // Skip if it isn't a pure repeated messages
129         // Does it start with a share?
130         if (!$skip && strpos($body, "[share") > 0) {
131                 $skip = true;
132         }
133         // Does it end with a share?
134         if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
135                 $skip = true;
136         }
137         if (!$skip) {
138                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
139                 // Skip if there is no shared message in there
140                 if ($body == $attributes) {
141                         $skip = true;
142                 }
143         }
144
145         if (!$skip) {
146                 $author = "";
147                 preg_match("/author='(.*?)'/ism", $attributes, $matches);
148                 if ($matches[1] != "") {
149                         $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
150                 }
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                 $profile = "";
156                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
157                 if ($matches[1] != "") {
158                         $profiledata["url"] = $matches[1];
159                 }
160                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
161                 if ($matches[1] != "") {
162                         $profiledata["url"] = $matches[1];
163                 }
164                 $avatar = "";
165                 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
166                 if ($matches[1] != "") {
167                         $profiledata["photo"] = $matches[1];
168                 }
169                 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
170                 if ($matches[1] != "") {
171                         $profiledata["photo"] = $matches[1];
172                 }
173                 $profiledata["nickname"] = $profiledata["name"];
174                 $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
175
176                 $profiledata["address"] = "";
177                 $profiledata["about"] = "";
178         }
179
180         $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
181
182         $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
183
184         if (local_user()) {
185                 if (in_array($profiledata["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
186                         $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
187                 }
188         } elseif ($profiledata["network"] == NETWORK_DFRN) {
189                 $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
190                 $profiledata["remoteconnect"] = $connect;
191         }
192
193         return($profiledata);
194 }
195
196 function display_content(App $a, $update = false, $update_uid = 0) {
197         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
198                 notice(L10n::t('Public access denied.') . EOL);
199                 return;
200         }
201
202         require_once 'include/security.php';
203         require_once 'include/conversation.php';
204         require_once 'include/acl_selectors.php';
205
206         $o = '';
207
208         if ($update) {
209                 $item_id = $_REQUEST['item_id'];
210                 $item = dba::selectFirst('item', ['uid', 'parent'], ['id' => $item_id]);
211                 $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
212                 $item_parent = $item['parent'];
213         } else {
214                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
215
216                 if ($a->argc == 2) {
217                         $item_parent = 0;
218
219                         if (local_user()) {
220                                 $r = dba::fetch_first("SELECT `id`, `parent` FROM `item`
221                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
222                                                 AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
223                                 if (DBM::is_result($r)) {
224                                         $item_id = $r["id"];
225                                         $item_parent = $r["parent"];
226                                 }
227                         }
228
229                         if ($item_parent == 0) {
230                                 $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent` FROM `item`
231                                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
232                                                 AND NOT `item`.`private` AND `item`.`uid` = 0
233                                                 AND `item`.`guid` = ?", $a->argv[1]);
234                                 if (DBM::is_result($r)) {
235                                         $item_id = $r["id"];
236                                         $item_parent = $r["parent"];
237                                 }
238                         }
239                 }
240         }
241
242         if (!$item_id) {
243                 $a->error = 404;
244                 notice(L10n::t('Item not found.').EOL);
245                 return;
246         }
247
248         // We are displaying an "alternate" link if that post was public. See issue 2864
249         $is_public = dba::exists('item', ['id' => $item_id, 'private' => false]);
250         if ($is_public) {
251                 // For the atom feed the nickname doesn't matter at all, we only need the item id.
252                 $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
253                 $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
254         } else {
255                 $alternate = '';
256                 $conversation = '';
257         }
258
259         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
260                                 ['$alternate' => $alternate,
261                                         '$conversation' => $conversation]);
262
263         $groups = [];
264
265         $contact = null;
266         $remote_contact = false;
267
268         $contact_id = 0;
269
270         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
271                 foreach ($_SESSION['remote'] as $v) {
272                         if ($v['uid'] == $a->profile['uid']) {
273                                 $contact_id = $v['cid'];
274                                 break;
275                         }
276                 }
277         }
278
279         if ($contact_id) {
280                 $groups = Group::getIdsByContactId($contact_id);
281                 $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
282                         $contact_id,
283                         $a->profile['uid']
284                 );
285                 if (DBM::is_result($r)) {
286                         $contact = $r;
287                         $remote_contact = true;
288                 }
289         }
290
291         if (!$remote_contact) {
292                 if (local_user()) {
293                         $contact_id = $_SESSION['cid'];
294                         $contact = $a->contact;
295                 }
296         }
297
298         $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
299         if (DBM::is_result($r)) {
300                 $a->page_contact = $r;
301         }
302         $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
303
304         if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
305                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
306                 return;
307         }
308
309         // We need the editor here to be able to reshare an item.
310         if ($is_owner) {
311                 $x = [
312                         'is_owner' => true,
313                         'allow_location' => $a->user['allow_location'],
314                         'default_location' => $a->user['default-location'],
315                         'nickname' => $a->user['nickname'],
316                         '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'),
317                         'acl' => populate_acl($a->user, true),
318                         'bang' => '',
319                         'visitor' => 'block',
320                         'profile_uid' => local_user(),
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(L10n::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::selectFirst('item', ['id'], ['parent' => $s[0]['parent'], 'unseen' => true]);
354                 if (DBM::is_result($unseen)) {
355                         dba::update('item', ['unseen' => false], ['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                 System::httpExit(500);
427         }
428         header("Content-type: application/atom+xml");
429         echo $xml;
430         killme();
431 }