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