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