]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
Catch HTTPExceptions in App::runFrontend()
[friendica.git] / mod / profile.php
1 <?php
2 /**
3  * @file mod/profile.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Widget;
10 use Friendica\Core\ACL;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\PConfig;
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\Login;
23 use Friendica\Protocol\ActivityPub;
24 use Friendica\Protocol\DFRN;
25 use Friendica\Util\DateTimeFormat;
26 use Friendica\Util\Security;
27 use Friendica\Util\Strings;
28 use Friendica\Util\XML;
29
30 function profile_init(App $a)
31 {
32         if (empty($a->page['aside'])) {
33                 $a->page['aside'] = '';
34         }
35
36         if ($a->argc < 2) {
37                 System::httpExit(400);
38         }
39
40         $which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
41
42         $profile = 0;
43         if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
44                 $which = $a->user['nickname'];
45                 $profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
46         } else {
47                 DFRN::autoRedir($a, $which);
48         }
49
50         if (ActivityPub::isRequest()) {
51                 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $which]);
52                 if (DBA::isResult($user)) {
53                         $data = ActivityPub\Transmitter::getProfile($user['uid']);
54                         header('Content-Type: application/activity+json');
55                         echo json_encode($data);
56                         exit();
57                 }
58         }
59
60         Profile::load($a, $which, $profile);
61
62         $blocked   = !local_user() && !remote_user() && Config::get('system', 'block_public');
63         $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
64
65         if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == Contact::PAGE_COMMUNITY) {
66                 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />';
67         }
68
69         if (!empty($a->profile['openidserver'])) {
70                 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\r\n";
71         }
72
73         if (!empty($a->profile['openid'])) {
74                 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
75                 $a->page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\r\n";
76         }
77
78         // site block
79         if (!$blocked && !$userblock) {
80                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], defaults($a->profile, 'pub_keywords', ''));
81                 if (strlen($keywords)) {
82                         $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\r\n";
83                 }
84         }
85
86         $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\r\n";
87         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/dfrn_poll/' . $which . '" title="DFRN: ' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\r\n";
88         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . $which . '/" title="' . L10n::t('%s\'s posts', $a->profile['username']) . '"/>' . "\r\n";
89         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . $which . '/comments" title="' . L10n::t('%s\'s comments', $a->profile['username']) . '"/>' . "\r\n";
90         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . $which . '/activity" title="' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\r\n";
91         $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''));
92         $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\r\n";
93         header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
94
95         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
96         foreach ($dfrn_pages as $dfrn) {
97                 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"" . System::baseUrl() . "/dfrn_{$dfrn}/{$which}\" />\r\n";
98         }
99         $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . "/poco/{$which}\" />\r\n";
100 }
101
102 function profile_content(App $a, $update = 0)
103 {
104         $category = $datequery = $datequery2 = '';
105
106         if ($a->argc > 2) {
107                 for ($x = 2; $x < $a->argc; $x ++) {
108                         if (is_a_date_arg($a->argv[$x])) {
109                                 if ($datequery) {
110                                         $datequery2 = Strings::escapeHtml($a->argv[$x]);
111                                 } else {
112                                         $datequery = Strings::escapeHtml($a->argv[$x]);
113                                 }
114                         } else {
115                                 $category = $a->argv[$x];
116                         }
117                 }
118         }
119
120         if (empty($category)) {
121                 $category = defaults($_GET, 'category', '');
122         }
123
124         $hashtags = defaults($_GET, 'tag', '');
125
126         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
127                 return Login::form();
128         }
129
130         $groups = [];
131         $remote_cid = null;
132
133         $tab = 'posts';
134         $o = '';
135
136         if ($update) {
137                 // Ensure we've got a profile owner if updating.
138                 $a->profile['profile_uid'] = $update;
139         } elseif ($a->profile['profile_uid'] == local_user()) {
140                 Nav::setSelected('home');
141         }
142
143         $remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
144         $is_owner = local_user() == $a->profile['profile_uid'];
145         $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
146
147         if ($remote_contact) {
148                 $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
149                 if (!empty($cdata['user'])) {
150                         $groups = Group::getIdsByContactId($cdata['user']);
151                         $remote_cid = $cdata['user'];
152                 }
153         }
154
155         if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
156                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
157                 return;
158         }
159
160         if (!$update) {
161                 $tab = false;
162                 if (!empty($_GET['tab'])) {
163                         $tab = Strings::escapeTags(trim($_GET['tab']));
164                 }
165
166                 $o .= Profile::getTabs($a, $is_owner, $a->profile['nickname']);
167
168                 if ($tab === 'profile') {
169                         $o .= Profile::getAdvanced($a);
170                         Addon::callHooks('profile_advanced', $o);
171                         return $o;
172                 }
173
174                 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
175
176                 $commpage = $a->profile['page-flags'] == Contact::PAGE_COMMUNITY;
177                 $commvisitor = $commpage && $remote_contact;
178
179                 $a->page['aside'] .= posted_date_widget(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'], true);
180                 $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], (!empty($category) ? XML::escape($category) : ''));
181                 $a->page['aside'] .= Widget::tagCloud();
182
183                 if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
184                         $x = [
185                                 'is_owner' => $is_owner,
186                                 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
187                                 'default_location' => $is_owner ? $a->user['default-location'] : '',
188                                 'nickname' => $a->profile['nickname'],
189                                 'lockstate' => is_array($a->user)
190                                         && (strlen($a->user['allow_cid'])
191                                                 || strlen($a->user['allow_gid'])
192                                                 || strlen($a->user['deny_cid'])
193                                                 || strlen($a->user['deny_gid'])
194                                         ) ? 'lock' : 'unlock',
195                                 'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',
196                                 'bang' => '',
197                                 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
198                                 'profile_uid' => $a->profile['profile_uid'],
199                         ];
200
201                         $o .= status_editor($a, $x);
202                 }
203         }
204
205         // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
206         $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $remote_contact, $groups, $remote_cid);
207         $sql_extra2 = '';
208
209         if ($update) {
210                 $last_updated = (defaults($_SESSION['last_updated'], $last_updated_key, 0));
211
212                 // If the page user is the owner of the page we should query for unseen
213                 // items. Otherwise use a timestamp of the last succesful update request.
214                 if ($is_owner || !$last_updated) {
215                         $sql_extra4 = " AND `item`.`unseen`";
216                 } else {
217                         $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
218                         $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
219                 }
220
221                 $items = q("SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
222                         FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
223                         AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
224                         WHERE `item`.`uid` = %d AND `item`.`visible` AND
225                         (NOT `item`.`deleted` OR `item`.`gravity` = %d)
226                         AND NOT `item`.`moderated` AND `item`.`wall`
227                         $sql_extra4
228                         $sql_extra
229                         ORDER BY `item`.`created` DESC",
230                         intval($a->profile['profile_uid']), intval(GRAVITY_ACTIVITY)
231                 );
232
233                 if (!DBA::isResult($items)) {
234                         return '';
235                 }
236
237                 $pager = new Pager($a->query_string);
238         } else {
239                 $sql_post_table = "";
240
241                 if (!empty($category)) {
242                         $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
243                                 DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
244                 }
245
246                 if (!empty($hashtags)) {
247                         $sql_post_table .= sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
248                                 DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
249                 }
250
251                 if (!empty($datequery)) {
252                         $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
253                 }
254                 if (!empty($datequery2)) {
255                         $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
256                 }
257
258                 // Does the profile page belong to a forum?
259                 // If not then we can improve the performance with an additional condition
260                 $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [Contact::PAGE_COMMUNITY, Contact::PAGE_PRVGROUP]];
261                 if (!DBA::exists('user', $condition)) {
262                         $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
263                 } else {
264                         $sql_extra3 = "";
265                 }
266
267                 //  check if we serve a mobile device and get the user settings
268                 //  accordingly
269                 if ($a->is_mobile) {
270                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
271                 } else {
272                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
273                 }
274
275                 //  now that we have the user settings, see if the theme forces
276                 //  a maximum item number which is lower then the user choice
277                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
278                         $itemspage_network = $a->force_max_items;
279                 }
280
281                 $pager = new Pager($a->query_string, $itemspage_network);
282
283                 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
284
285                 $items = q("SELECT `item`.`uri`
286                         FROM `thread`
287                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
288                         $sql_post_table
289                         STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
290                                 AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
291                         WHERE `thread`.`uid` = %d AND `thread`.`visible`
292                                 AND NOT `thread`.`deleted`
293                                 AND NOT `thread`.`moderated`
294                                 AND `thread`.`wall`
295                                 $sql_extra3 $sql_extra $sql_extra2
296                         ORDER BY `thread`.`created` DESC $pager_sql",
297                         intval($a->profile['profile_uid'])
298                 );
299         }
300
301         // Set a time stamp for this page. We will make use of it when we
302         // search for new items (update routine)
303         $_SESSION['last_updated'][$last_updated_key] = time();
304
305         if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
306                 $o .= Profile::getBirthdays();
307                 $o .= Profile::getEventsReminderHTML();
308         }
309
310         if ($is_owner) {
311                 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
312                 if ($unseen) {
313                         $r = Item::update(['unseen' => false],
314                                         ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
315                 }
316         }
317
318         $o .= conversation($a, $items, $pager, 'profile', $update, false, 'created', $a->profile['profile_uid']);
319
320         if (!$update) {
321                 $o .= $pager->renderMinimal(count($items));
322         }
323
324         return $o;
325 }