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