3 * @file mod/profile.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Widget;
9 use Friendica\Core\ACL;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\System;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Group;
17 use Friendica\Model\Item;
18 use Friendica\Model\Profile;
19 use Friendica\Module\Login;
20 use Friendica\Protocol\DFRN;
21 use Friendica\Util\DateTimeFormat;
23 function profile_init(App $a)
25 if (!x($a->page, 'aside')) {
26 $a->page['aside'] = '';
30 $which = htmlspecialchars($a->argv[1]);
32 $r = q("SELECT `nickname` FROM `user` WHERE `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 ORDER BY RAND() LIMIT 1");
33 if (DBM::is_result($r)) {
34 goaway(System::baseUrl() . '/profile/' . $r[0]['nickname']);
36 logger('profile error: mod_profile ' . $a->query_string, LOGGER_DEBUG);
37 notice(L10n::t('Requested profile is not available.') . EOL);
44 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
45 $which = $a->user['nickname'];
46 $profile = htmlspecialchars($a->argv[1]);
48 DFRN::autoRedir($a, $which);
51 Profile::load($a, $which, $profile);
53 $blocked = !local_user() && !remote_user() && Config::get('system', 'block_public');
54 $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
56 if (x($a->profile, 'page-flags') && $a->profile['page-flags'] == PAGE_COMMUNITY) {
57 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />';
60 if (x($a->profile, 'openidserver')) {
61 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\r\n";
64 if (x($a->profile, 'openid')) {
65 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
66 $a->page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\r\n";
70 if (!$blocked && !$userblock) {
71 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], defaults($a->profile, 'pub_keywords', ''));
72 if (strlen($keywords)) {
73 $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\r\n";
77 $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\r\n";
78 $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";
79 $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";
80 $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";
81 $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->get_hostname() . ($a->path ? '/' . $a->path : ''));
82 $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\r\n";
83 header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
85 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
86 foreach ($dfrn_pages as $dfrn) {
87 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"" . System::baseUrl() . "/dfrn_{$dfrn}/{$which}\" />\r\n";
89 $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . "/poco/{$which}\" />\r\n";
92 function profile_content(App $a, $update = 0)
94 $category = $datequery = $datequery2 = '';
97 for ($x = 2; $x < $a->argc; $x ++) {
98 if (is_a_date_arg($a->argv[$x])) {
100 $datequery2 = escape_tags($a->argv[$x]);
102 $datequery = escape_tags($a->argv[$x]);
105 $category = $a->argv[$x];
111 $category = defaults($_GET, 'category', '');
114 $hashtags = defaults($_GET, 'tag', '');
116 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
117 return Login::form();
120 require_once 'include/security.php';
121 require_once 'include/conversation.php';
122 require_once 'include/items.php';
130 // Ensure we've got a profile owner if updating.
131 $a->profile['profile_uid'] = $update;
132 } elseif ($a->profile['profile_uid'] == local_user()) {
133 Nav::setSelected('home');
137 $remote_contact = false;
141 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
142 foreach ($_SESSION['remote'] as $v) {
143 if ($v['uid'] == $a->profile['profile_uid']) {
144 $contact_id = $v['cid'];
151 $groups = Group::getIdsByContactId($contact_id);
152 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
154 intval($a->profile['profile_uid'])
156 if (DBM::is_result($r)) {
158 $remote_contact = true;
162 if (!$remote_contact) {
164 $contact_id = $_SESSION['cid'];
165 $contact = $a->contact;
169 $is_owner = local_user() == $a->profile['profile_uid'];
170 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
172 if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
173 notice(L10n::t('Access to this profile has been restricted.') . EOL);
179 if (x($_GET, 'tab')) {
180 $tab = notags(trim($_GET['tab']));
183 $o .= Profile::getTabs($a, $is_owner, $a->profile['nickname']);
185 if ($tab === 'profile') {
186 $o .= Profile::getAdvanced($a);
187 Addon::callHooks('profile_advanced', $o);
191 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
193 if (x($_SESSION, 'new_member') && $is_owner) {
194 $o .= '<a href="newmember" id="newmember-tips" style="font-size: 1.2em;"><b>' . L10n::t('Tips for New Members') . '</b></a>' . EOL;
197 $commpage = $a->profile['page-flags'] == PAGE_COMMUNITY;
198 $commvisitor = $commpage && $remote_contact;
200 $a->page['aside'] .= posted_date_widget(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'], true);
201 $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], (x($category) ? xmlify($category) : ''));
202 $a->page['aside'] .= Widget::tagCloud();
204 if (can_write_wall($a->profile['profile_uid'])) {
206 'is_owner' => $is_owner,
207 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
208 'default_location' => $is_owner ? $a->user['default-location'] : '',
209 'nickname' => $a->profile['nickname'],
210 'lockstate' => is_array($a->user)
211 && (strlen($a->user['allow_cid'])
212 || strlen($a->user['allow_gid'])
213 || strlen($a->user['deny_cid'])
214 || strlen($a->user['deny_gid'])
215 ) ? 'lock' : 'unlock',
216 'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',
218 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
219 'profile_uid' => $a->profile['profile_uid'],
222 $o .= status_editor($a, $x);
227 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
228 $sql_extra = item_permissions_sql($a->profile['profile_uid'], $remote_contact, $groups);
232 $last_updated = (x($_SESSION['last_updated'], $last_updated_key) ? $_SESSION['last_updated'][$last_updated_key] : 0);
234 // If the page user is the owner of the page we should query for unseen
235 // items. Otherwise use a timestamp of the last succesful update request.
236 if ($is_owner || !$last_updated) {
237 $sql_extra4 = " AND `item`.`unseen`";
239 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
240 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
243 $r = q("SELECT distinct(parent) AS `item_id`, `item`.`network` AS `item_network`, `item`.`created`
244 FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
245 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
246 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND
247 (`item`.`deleted` = 0 OR item.verb = '" . ACTIVITY_LIKE . "'
248 OR item.verb = '" . ACTIVITY_DISLIKE . "' OR item.verb = '" . ACTIVITY_ATTEND . "'
249 OR item.verb = '" . ACTIVITY_ATTENDNO . "' OR item.verb = '" . ACTIVITY_ATTENDMAYBE . "')
250 AND `item`.`moderated` = 0
251 AND `item`.`wall` = 1
254 ORDER BY `item`.`created` DESC",
255 intval($a->profile['profile_uid'])
258 if (!DBM::is_result($r)) {
262 $sql_post_table = "";
265 $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` ",
266 dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
270 $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` ",
271 dbesc(protect_sprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
275 $sql_extra2 .= protect_sprintf(sprintf(" AND `thread`.`created` <= '%s' ", dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
278 $sql_extra2 .= protect_sprintf(sprintf(" AND `thread`.`created` >= '%s' ", dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
281 // Belongs the profile page to a forum?
282 // If not then we can improve the performance with an additional condition
283 $r = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `page-flags` IN (%d, %d)",
284 intval($a->profile['profile_uid']),
285 intval(PAGE_COMMUNITY),
286 intval(PAGE_PRVGROUP)
289 if (!DBM::is_result($r)) {
290 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
293 // check if we serve a mobile device and get the user settings
296 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
298 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
301 // now that we have the user settings, see if the theme forces
302 // a maximum item number which is lower then the user choice
303 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
304 $itemspage_network = $a->force_max_items;
307 $a->set_pager_itemspage($itemspage_network);
309 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
311 $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`
313 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
315 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
316 AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
317 WHERE `thread`.`uid` = %d AND `thread`.`visible`
318 AND NOT `thread`.`deleted`
319 AND NOT `thread`.`moderated`
321 $sql_extra3 $sql_extra $sql_extra2
322 ORDER BY `thread`.`created` DESC $pager_sql",
323 intval($a->profile['profile_uid'])
330 // Set a time stamp for this page. We will make use of it when we
331 // search for new items (update routine)
332 $_SESSION['last_updated'][$last_updated_key] = time();
334 if (DBM::is_result($r)) {
335 foreach ($r as $rr) {
336 $parents_arr[] = $rr['item_id'];
339 $parents_str = implode(', ', $parents_arr);
341 $items = q(item_query() . " AND `item`.`uid` = %d
342 AND `item`.`parent` IN (%s)
344 intval($a->profile['profile_uid']),
348 $items = conv_sort($items, 'created');
353 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
354 $o .= Profile::getBirthdays();
355 $o .= Profile::getEventsReminderHTML();
360 $unseen = dba::exists('item', ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
362 $r = Item::update(['unseen' => false],
363 ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
367 $o .= conversation($a, $items, 'profile', $update);
370 $o .= alt_pager($a, count($items));