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\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;
30 function profile_init(App $a)
32 if (empty($a->page['aside'])) {
33 $a->page['aside'] = '';
37 System::httpExit(400);
40 $which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
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);
47 DFRN::autoRedir($a, $which);
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);
60 Profile::load($a, $which, $profile);
62 $blocked = !local_user() && !remote_user() && Config::get('system', 'block_public');
63 $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
65 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == Contact::PAGE_COMMUNITY) {
66 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />';
69 if (!empty($a->profile['openidserver'])) {
70 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\r\n";
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";
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";
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);
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";
99 $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . "/poco/{$which}\" />\r\n";
102 function profile_content(App $a, $update = 0)
104 $category = $datequery = $datequery2 = '';
107 for ($x = 2; $x < $a->argc; $x ++) {
108 if (is_a_date_arg($a->argv[$x])) {
110 $datequery2 = Strings::escapeHtml($a->argv[$x]);
112 $datequery = Strings::escapeHtml($a->argv[$x]);
115 $category = $a->argv[$x];
120 if (empty($category)) {
121 $category = defaults($_GET, 'category', '');
124 $hashtags = defaults($_GET, 'tag', '');
126 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
127 return Login::form();
136 // Ensure we've got a profile owner if updating.
137 $a->profile['profile_uid'] = $update;
138 } elseif ($a->profile['profile_uid'] == local_user()) {
139 Nav::setSelected('home');
142 $remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
143 $is_owner = local_user() == $a->profile['profile_uid'];
144 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
146 if ($remote_contact) {
147 $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
148 if (!empty($cdata['user'])) {
149 $groups = Group::getIdsByContactId($cdata['user']);
150 $remote_cid = $cdata['user'];
154 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
155 notice(L10n::t('Access to this profile has been restricted.') . EOL);
161 if (!empty($_GET['tab'])) {
162 $tab = Strings::escapeTags(trim($_GET['tab']));
165 $o .= Profile::getTabs($a, $is_owner, $a->profile['nickname']);
167 if ($tab === 'profile') {
168 $o .= Profile::getAdvanced($a);
169 Hook::callAll('profile_advanced', $o);
173 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
175 $commpage = $a->profile['page-flags'] == Contact::PAGE_COMMUNITY;
176 $commvisitor = $commpage && $remote_contact;
178 $a->page['aside'] .= posted_date_widget(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'], true);
179 $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], (!empty($category) ? XML::escape($category) : ''));
180 $a->page['aside'] .= Widget::tagCloud();
182 if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
184 'is_owner' => $is_owner,
185 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
186 'default_location' => $is_owner ? $a->user['default-location'] : '',
187 'nickname' => $a->profile['nickname'],
188 'lockstate' => is_array($a->user)
189 && (strlen($a->user['allow_cid'])
190 || strlen($a->user['allow_gid'])
191 || strlen($a->user['deny_cid'])
192 || strlen($a->user['deny_gid'])
193 ) ? 'lock' : 'unlock',
194 'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',
196 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
197 'profile_uid' => $a->profile['profile_uid'],
200 $o .= status_editor($a, $x);
204 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
205 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $remote_contact, $groups, $remote_cid);
209 $last_updated = (defaults($_SESSION['last_updated'], $last_updated_key, 0));
211 // If the page user is the owner of the page we should query for unseen
212 // items. Otherwise use a timestamp of the last succesful update request.
213 if ($is_owner || !$last_updated) {
214 $sql_extra4 = " AND `item`.`unseen`";
216 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
217 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
220 $items = q("SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
221 FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
222 AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
223 WHERE `item`.`uid` = %d AND `item`.`visible` AND
224 (NOT `item`.`deleted` OR `item`.`gravity` = %d)
225 AND NOT `item`.`moderated` AND `item`.`wall`
228 ORDER BY `item`.`created` DESC",
229 intval($a->profile['profile_uid']), intval(GRAVITY_ACTIVITY)
232 if (!DBA::isResult($items)) {
236 $pager = new Pager($a->query_string);
238 $sql_post_table = "";
240 if (!empty($category)) {
241 $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` ",
242 DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
245 if (!empty($hashtags)) {
246 $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` ",
247 DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
250 if (!empty($datequery)) {
251 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
253 if (!empty($datequery2)) {
254 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
257 // Does the profile page belong to a forum?
258 // If not then we can improve the performance with an additional condition
259 $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [Contact::PAGE_COMMUNITY, Contact::PAGE_PRVGROUP]];
260 if (!DBA::exists('user', $condition)) {
261 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
266 // check if we serve a mobile device and get the user settings
269 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
271 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
274 // now that we have the user settings, see if the theme forces
275 // a maximum item number which is lower then the user choice
276 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
277 $itemspage_network = $a->force_max_items;
280 $pager = new Pager($a->query_string, $itemspage_network);
282 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
284 $items = q("SELECT `item`.`uri`
286 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
288 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
289 AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
290 WHERE `thread`.`uid` = %d AND `thread`.`visible`
291 AND NOT `thread`.`deleted`
292 AND NOT `thread`.`moderated`
294 $sql_extra3 $sql_extra $sql_extra2
295 ORDER BY `thread`.`created` DESC $pager_sql",
296 intval($a->profile['profile_uid'])
300 // Set a time stamp for this page. We will make use of it when we
301 // search for new items (update routine)
302 $_SESSION['last_updated'][$last_updated_key] = time();
304 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
305 $o .= Profile::getBirthdays();
306 $o .= Profile::getEventsReminderHTML();
310 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
312 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
316 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'created', $a->profile['profile_uid']);
319 $o .= $pager->renderMinimal(count($items));