]> git.mxchange.org Git - friendica.git/blob - mod/display.php
e0a7912a28d4d25dd365b4930613c952f05cc9e0
[friendica.git] / mod / display.php
1 <?php
2
3 function display_init(&$a) {
4
5         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
6                 return;
7         }
8
9         $nick = (($a->argc > 1) ? $a->argv[1] : '');
10
11         // If there is only one parameter, then check if this parameter could be a guid
12         if ($a->argc == 2) {
13                 $nick = "";
14
15                 // Does the local user have this item?
16                 if (local_user()) {
17                         $r = q("SELECT `id` FROM `item`
18                                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
19                                         AND `guid` = '%s' AND `uid` = %d", $a->argv[1], local_user());
20                         if (count($r))
21                                 $nick = $a->user["nickname"];
22                 }
23
24                 // Or is it anywhere on the server?
25                 if ($nick == "") {
26                         $r = q("SELECT `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
27                                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
28                                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
29                                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
30                                         AND `item`.`private` = 0 AND `item`.`wall` = 1
31                                         AND `item`.`guid` = '%s'", $a->argv[1]);
32                         if (count($r))
33                                 $nick = $r[0]["nickname"];
34                 }
35         }
36
37         profile_load($a,$nick);
38
39 }
40
41
42 function display_content(&$a, $update = 0) {
43
44         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
45                 notice( t('Public access denied.') . EOL);
46                 return;
47         }
48
49         require_once("include/bbcode.php");
50         require_once('include/security.php');
51         require_once('include/conversation.php');
52         require_once('include/acl_selectors.php');
53
54
55         $o = '';
56
57         $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'), array());
58
59
60         if($update) {
61                 $nick = $_REQUEST['nick'];
62         }
63         else {
64                 $nick = (($a->argc > 1) ? $a->argv[1] : '');
65         }
66
67         if($update) {
68                 $item_id = $_REQUEST['item_id'];
69                 $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
70         }
71         else {
72                 $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
73
74                 if ($a->argc == 2) {
75                         $nick = "";
76
77                         if (local_user()) {
78                                 $r = q("SELECT `id` FROM `item`
79                                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
80                                                 AND `guid` = '%s' AND `uid` = %d", $a->argv[1], local_user());
81                                 if (count($r)) {
82                                         $item_id = $r[0]["id"];
83                                         $nick = $a->user["nickname"];
84                                 }
85                         }
86
87                         if ($nick == "") {
88                                 $r = q("SELECT `user`.`nickname`, `item`.`id` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
89                                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
90                                                 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
91                                                 AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
92                                                 AND `item`.`private` = 0 AND `item`.`wall` = 1
93                                                 AND `item`.`guid` = '%s'", $a->argv[1]);
94                                 if (count($r)) {
95                                         $item_id = $r[0]["id"];
96                                         $nick = $r[0]["nickname"];
97                                 }
98                         }
99                 }
100         }
101
102         if(! $item_id) {
103                 $a->error = 404;
104                 notice( t('Item not found.') . EOL);
105                 return;
106         }
107
108         $groups = array();
109
110         $contact = null;
111         $remote_contact = false;
112
113         $contact_id = 0;
114
115         if(is_array($_SESSION['remote'])) {
116                 foreach($_SESSION['remote'] as $v) {
117                         if($v['uid'] == $a->profile['uid']) {
118                                 $contact_id = $v['cid'];
119                                 break;
120                         }
121                 }
122         }
123
124         if($contact_id) {
125                 $groups = init_groups_visitor($contact_id);
126                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
127                         intval($contact_id),
128                         intval($a->profile['uid'])
129                 );
130                 if(count($r)) {
131                         $contact = $r[0];
132                         $remote_contact = true;
133                 }
134         }
135
136         if(! $remote_contact) {
137                 if(local_user()) {
138                         $contact_id = $_SESSION['cid'];
139                         $contact = $a->contact;
140                 }
141         }
142
143         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
144                 intval($a->profile['uid'])
145         );
146         if(count($r))
147                 $a->page_contact = $r[0];
148
149         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
150
151         if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
152                 notice( t('Access to this profile has been restricted.') . EOL);
153                 return;
154         }
155
156         if ($is_owner) {
157                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
158
159                 $x = array(
160                         'is_owner' => true,
161                         'allow_location' => $a->user['allow_location'],
162                         'default_location' => $a->user['default-location'],
163                         'nickname' => $a->user['nickname'],
164                         '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'),
165                         'acl' => populate_acl($a->user, $celeb),
166                         'bang' => '',
167                         'visitor' => 'block',
168                         'profile_uid' => local_user(),
169                         'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
170                 );
171                 $o .= status_editor($a,$x,0,true);
172         }
173
174         $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
175
176         //              AND `item`.`parent` = ( SELECT `parent` FROM `item` FORCE INDEX (PRIMARY, `uri`) WHERE ( `id` = '%s' OR `uri` = '%s' ))
177
178         if($update) {
179
180                 $r = q("SELECT id FROM item WHERE item.uid = %d
181                         AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE (`id` = '%s' OR `uri` = '%s'))
182                         $sql_extra AND unseen = 1",
183                         intval($a->profile['uid']),
184                         dbesc($item_id),
185                         dbesc($item_id)
186                 );
187
188                 if(!$r)
189                         return '';
190         }
191
192         //      AND `item`.`parent` = ( SELECT `parent` FROM `item` FORCE INDEX (PRIMARY, `uri`) WHERE ( `id` = '%s' OR `uri` = '%s' )
193
194         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,  `item`.`network` AS `item_network`,
195                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
196                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
197                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
198                 FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
199                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
200                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
201                 and `item`.`moderated` = 0
202                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE (`id` = '%s' OR `uri` = '%s')
203                 AND uid = %d)
204                 $sql_extra
205                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
206                 intval($a->profile['uid']),
207                 dbesc($item_id),
208                 dbesc($item_id),
209                 intval($a->profile['uid'])
210         );
211
212         if(!$r && local_user()) {
213                 // Check if this is another person's link to a post that we have
214                 $r = q("SELECT `item`.uri FROM `item`
215                         WHERE (`item`.`id` = '%s' OR `item`.`uri` = '%s' )
216                         LIMIT 1",
217                         dbesc($item_id),
218                         dbesc($item_id)
219                 );
220                 if($r) {
221                         $item_uri = $r[0]['uri'];
222                         //      AND `item`.`parent` = ( SELECT `parent` FROM `item` FORCE INDEX (PRIMARY, `uri`) WHERE `uri` = '%s' AND uid = %d )
223
224                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,  `item`.`network` AS `item_network`,
225                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
226                                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, 
227                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
228                                 FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
229                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
230                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
231                                 and `item`.`moderated` = 0
232                                 AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = '%s' AND uid = %d)
233                                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC ",
234                                 intval(local_user()),
235                                 dbesc($item_uri),
236                                 intval(local_user())
237                         );
238                 }
239         }
240
241
242         if($r) {
243
244                 if((local_user()) && (local_user() == $a->profile['uid'])) {
245                         q("UPDATE `item` SET `unseen` = 0
246                                 WHERE `parent` = %d AND `unseen` = 1",
247                                 intval($r[0]['parent'])
248                         );
249                 }
250
251                 $items = conv_sort($r,"`commented`");
252
253                 if(!$update)
254                         $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
255                 $o .= conversation($a,$items,'display', $update);
256
257                 // Preparing the meta header
258                 require_once('include/bbcode.php');
259                 require_once("include/html2plain.php");
260                 $description = trim(html2plain(bbcode($r[0]["body"], false, false), 0, true));
261                 $title = trim(html2plain(bbcode($r[0]["title"], false, false), 0, true));
262                 $author_name = $r[0]["author-name"];
263
264                 $image = "";
265                 if ($image == "")
266                         $image = $r[0]["thumb"];
267
268                 if ($title == "")
269                         $title = $author_name;
270
271                 $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
272                 $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
273                 $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
274
275                 //<meta name="keywords" content="">
276                 $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
277                 $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
278                 $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
279                 $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
280
281                 // Schema.org microdata
282                 $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
283                 $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
284                 $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
285                 $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
286
287                 // Twitter cards
288                 $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
289                 $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
290                 $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
291                 $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
292                 $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$r[0]["plink"].'" />'."\n";
293
294                 // Dublin Core
295                 $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
296                 $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
297
298                 // Open Graph
299                 $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
300                 $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
301                 $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
302                 $a->page['htmlhead'] .= '<meta property="og:url" content="'.$r[0]["plink"].'" />'."\n";
303                 $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
304                 $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
305                 // article:tag
306
307                 return $o;
308         }
309
310         $r = q("SELECT `id`,`deleted` FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
311                 dbesc($item_id),
312                 dbesc($item_id)
313         );
314         if($r) {
315                 if($r[0]['deleted']) {
316                         notice( t('Item has been removed.') . EOL );
317                 }
318                 else {
319                         notice( t('Permission denied.') . EOL );
320                 }
321         }
322         else {
323                 notice( t('Item not found.') . EOL );
324         }
325
326         return $o;
327 }
328