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