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