3 * @file mod/profile.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Widget;
10 use Friendica\Core\ACL;
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
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\Model\User;
23 use Friendica\Module\Login;
24 use Friendica\Protocol\ActivityPub;
25 use Friendica\Protocol\DFRN;
26 use Friendica\Util\DateTimeFormat;
27 use Friendica\Util\Security;
28 use Friendica\Util\Strings;
29 use Friendica\Util\XML;
31 function profile_init(App $a)
33 if (empty($a->page['aside'])) {
34 $a->page['aside'] = '';
38 System::httpExit(400);
41 $which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
44 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
45 $which = $a->user['nickname'];
46 $profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
48 DFRN::autoRedir($a, $which);
51 if (ActivityPub::isRequest()) {
52 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $which]);
53 if (DBA::isResult($user)) {
54 $data = ActivityPub\Transmitter::getProfile($user['uid']);
55 header('Content-Type: application/activity+json');
56 echo json_encode($data);
61 Profile::load($a, $which, $profile);
63 $blocked = !local_user() && !remote_user() && Config::get('system', 'block_public');
64 $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
66 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
67 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />';
70 if (!empty($a->profile['openidserver'])) {
71 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\r\n";
74 if (!empty($a->profile['openid'])) {
75 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
76 $a->page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\r\n";
80 if (!$blocked && !$userblock) {
81 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], defaults($a->profile, 'pub_keywords', ''));
82 if (strlen($keywords)) {
83 $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\r\n";
87 $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\r\n";
88 $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";
89 $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";
90 $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";
91 $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";
92 $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''));
93 $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\r\n";
94 header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
96 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
97 foreach ($dfrn_pages as $dfrn) {
98 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"" . System::baseUrl() . "/dfrn_{$dfrn}/{$which}\" />\r\n";
100 $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . "/poco/{$which}\" />\r\n";
103 function profile_content(App $a, $update = 0)
105 $category = $datequery = $datequery2 = '';
108 for ($x = 2; $x < $a->argc; $x ++) {
109 if (is_a_date_arg($a->argv[$x])) {
111 $datequery2 = Strings::escapeHtml($a->argv[$x]);
113 $datequery = Strings::escapeHtml($a->argv[$x]);
116 $category = $a->argv[$x];
121 if (empty($category)) {
122 $category = defaults($_GET, 'category', '');
125 $hashtags = defaults($_GET, 'tag', '');
127 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
128 return Login::form();
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');
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();
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'];
155 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
156 notice(L10n::t('Access to this profile has been restricted.') . EOL);
162 if (!empty($_GET['tab'])) {
163 $tab = Strings::escapeTags(trim($_GET['tab']));
166 $o .= Profile::getTabs($a, $is_owner, $a->profile['nickname']);
168 if ($tab === 'profile') {
169 $o .= Profile::getAdvanced($a);
170 Hook::callAll('profile_advanced', $o);
174 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
176 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
177 $commvisitor = $commpage && $remote_contact;
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();
183 if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
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) : '',
197 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
198 'profile_uid' => $a->profile['profile_uid'],
201 $o .= status_editor($a, $x);
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);
210 $last_updated = (defaults($_SESSION['last_updated'], $last_updated_key, 0));
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`";
217 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
218 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
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`
229 ORDER BY `item`.`created` DESC",
230 intval($a->profile['profile_uid']), intval(GRAVITY_ACTIVITY)
233 if (!DBA::isResult($items)) {
237 $pager = new Pager($a->query_string);
239 $sql_post_table = "";
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']));
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']));
251 if (!empty($datequery)) {
252 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
254 if (!empty($datequery2)) {
255 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
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' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
261 if (!DBA::exists('user', $condition)) {
262 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
267 // check if we serve a mobile device and get the user settings
270 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
272 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
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;
281 $pager = new Pager($a->query_string, $itemspage_network);
283 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
285 $items = q("SELECT `item`.`uri`
287 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
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`
295 $sql_extra3 $sql_extra $sql_extra2
296 ORDER BY `thread`.`created` DESC $pager_sql",
297 intval($a->profile['profile_uid'])
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();
305 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
306 $o .= Profile::getBirthdays();
307 $o .= Profile::getEventsReminderHTML();
311 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
313 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
317 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'created', $a->profile['profile_uid']);
320 $o .= $pager->renderMinimal(count($items));