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