3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Content\Nav;
7 use Friendica\Content\Pager;
8 use Friendica\Content\Widget;
9 use Friendica\Core\ACL;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Session;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
15 use Friendica\Model\Item;
16 use Friendica\Model\Profile as ProfileModel;
17 use Friendica\Model\User;
18 use Friendica\Module\Security\Login;
19 use Friendica\Protocol\ActivityPub;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Security;
22 use Friendica\Util\Strings;
23 use Friendica\Util\XML;
25 require_once 'boot.php';
27 class Profile extends BaseModule
29 public static $which = '';
30 public static $profile = 0;
32 public static function init(array $parameters = [])
36 // @TODO: Replace with parameter from router
38 throw new \Friendica\Network\HTTPException\BadRequestException();
41 self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
43 // @TODO: Replace with parameter from router
44 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
45 self::$which = $a->user['nickname'];
46 self::$profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
50 public static function rawContent(array $parameters = [])
52 if (ActivityPub::isRequest()) {
53 $user = DBA::selectFirst('user', ['uid'], ['nickname' => self::$which]);
54 if (DBA::isResult($user)) {
55 // The function returns an empty array when the account is removed, expired or blocked
56 $data = ActivityPub\Transmitter::getProfile($user['uid']);
58 System::jsonExit($data, 'application/activity+json');
62 if (DBA::exists('userd', ['username' => self::$which])) {
64 $data = ActivityPub\Transmitter::getDeletedUser(self::$which);
66 System::jsonError(410, $data);
68 // Any other case (unknown, blocked, unverified, expired, no profile, no self contact)
69 System::jsonError(404, []);
74 public static function content(array $parameters = [], $update = 0)
79 ProfileModel::load($a, self::$which, self::$profile);
83 $page['htmlhead'] .= "\n";
85 $blocked = !local_user() && !Session::getRemoteContactID($a->profile['uid']) && DI::config()->get('system', 'block_public');
86 $userblock = !local_user() && !Session::getRemoteContactID($a->profile['uid']) && $a->profile['hidewall'];
88 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
89 $page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
92 if (!empty($a->profile['openidserver'])) {
93 $page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\n";
96 if (!empty($a->profile['openid'])) {
97 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
98 $page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
102 if (!$blocked && !$userblock) {
103 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $a->profile['pub_keywords'] ?? '');
104 if (strlen($keywords)) {
105 $page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
109 $page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
111 if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
112 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
115 $page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/dfrn_poll/' . self::$which . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $a->profile['username']) . '"/>' . "\n";
116 $page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . self::$which . '/" title="' . DI::l10n()->t('%s\'s posts', $a->profile['username']) . '"/>' . "\n";
117 $page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . self::$which . '/comments" title="' . DI::l10n()->t('%s\'s comments', $a->profile['username']) . '"/>' . "\n";
118 $page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . self::$which . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $a->profile['username']) . '"/>' . "\n";
119 $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''));
120 $page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . DI::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\n";
121 header('Link: <' . DI::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
123 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
124 foreach ($dfrn_pages as $dfrn) {
125 $page['htmlhead'] .= '<link rel="dfrn-' . $dfrn . '" href="' . DI::baseUrl() . '/dfrn_' . $dfrn . '/' . self::$which . '" />' . "\n";
127 $page['htmlhead'] .= '<link rel="dfrn-poco" href="' . DI::baseUrl() . '/poco/' . self::$which . '" />' . "\n";
130 $category = $datequery = $datequery2 = '';
133 for ($x = 2; $x < $a->argc; $x ++) {
134 if (DI::dtFormat()->isYearMonth($a->argv[$x])) {
136 $datequery2 = Strings::escapeHtml($a->argv[$x]);
138 $datequery = Strings::escapeHtml($a->argv[$x]);
141 $category = $a->argv[$x];
146 if (empty($category)) {
147 $category = $_GET['category'] ?? '';
150 $hashtags = $_GET['tag'] ?? '';
152 if (DI::config()->get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['uid'])) {
153 return Login::form();
159 // Ensure we've got a profile owner if updating.
160 $a->profile['uid'] = $update;
161 } elseif ($a->profile['uid'] == local_user()) {
162 Nav::setSelected('home');
165 $remote_contact = Session::getRemoteContactID($a->profile['uid']);
166 $is_owner = local_user() == $a->profile['uid'];
167 $last_updated_key = "profile:" . $a->profile['uid'] . ":" . local_user() . ":" . $remote_contact;
169 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
170 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
175 $tab = Strings::escapeTags(trim($_GET['tab'] ?? ''));
177 $o .= ProfileModel::getTabs($a, $tab, $is_owner, $a->profile['nickname']);
179 if ($tab === 'profile') {
180 $o .= ProfileModel::getAdvanced($a);
181 Hook::callAll('profile_advanced', $o);
185 $o .= Widget::commonFriendsVisitor($a->profile['uid']);
187 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
188 $commvisitor = $commpage && $remote_contact;
190 DI::page()['aside'] .= Widget::postedByYear(DI::baseUrl()->get(true) . '/profile/' . $a->profile['nickname'], $a->profile['uid'] ?? 0, true);
191 DI::page()['aside'] .= Widget::categories(DI::baseUrl()->get(true) . '/profile/' . $a->profile['nickname'], XML::escape($category));
192 DI::page()['aside'] .= Widget::tagCloud();
194 if (Security::canWriteToUserWall($a->profile['uid'])) {
196 'is_owner' => $is_owner,
197 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
198 'default_location' => $is_owner ? $a->user['default-location'] : '',
199 'nickname' => $a->profile['nickname'],
200 'lockstate' => is_array($a->user)
201 && (strlen($a->user['allow_cid'])
202 || strlen($a->user['allow_gid'])
203 || strlen($a->user['deny_cid'])
204 || strlen($a->user['deny_gid'])
205 ) ? 'lock' : 'unlock',
206 'acl' => $is_owner ? ACL::getFullSelectorHTML(DI::page(), $a->user, true) : '',
208 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
209 'profile_uid' => $a->profile['uid'],
212 $o .= status_editor($a, $x);
216 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
217 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid']);
220 $last_updated_array = Session::get('last_updated', []);
223 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
225 // If the page user is the owner of the page we should query for unseen
226 // items. Otherwise use a timestamp of the last succesful update request.
227 if ($is_owner || !$last_updated) {
228 $sql_extra4 = " AND `item`.`unseen`";
230 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
231 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
234 $items_stmt = DBA::p(
235 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
238 ON `contact`.`id` = `item`.`contact-id`
239 AND NOT `contact`.`blocked`
240 AND NOT `contact`.`pending`
241 WHERE `item`.`uid` = ?
243 AND (NOT `item`.`deleted` OR `item`.`gravity` = ?)
244 AND NOT `item`.`moderated`
248 ORDER BY `item`.`received` DESC",
253 if (!DBA::isResult($items_stmt)) {
257 $pager = new Pager(DI::args()->getQueryString());
259 $sql_post_table = "";
261 if (!empty($category)) {
262 $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` ",
263 DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['uid']));
266 if (!empty($hashtags)) {
267 $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` ",
268 DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['uid']));
271 if (!empty($datequery)) {
272 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
274 if (!empty($datequery2)) {
275 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
278 // Does the profile page belong to a forum?
279 // If not then we can improve the performance with an additional condition
280 $condition = ['uid' => $a->profile['uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
281 if (!DBA::exists('user', $condition)) {
282 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
287 // check if we serve a mobile device and get the user settings
289 if (DI::mode()->isMobile()) {
290 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 10);
292 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 20);
295 // now that we have the user settings, see if the theme forces
296 // a maximum item number which is lower then the user choice
297 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
298 $itemspage_network = $a->force_max_items;
301 $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
303 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
305 $items_stmt = DBA::p(
308 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
310 STRAIGHT_JOIN `contact`
311 ON `contact`.`id` = `thread`.`contact-id`
312 AND NOT `contact`.`blocked`
313 AND NOT `contact`.`pending`
314 WHERE `thread`.`uid` = ?
315 AND `thread`.`visible`
316 AND NOT `thread`.`deleted`
317 AND NOT `thread`.`moderated`
322 ORDER BY `thread`.`received` DESC
328 // Set a time stamp for this page. We will make use of it when we
329 // search for new items (update routine)
330 $last_updated_array[$last_updated_key] = time();
331 Session::set('last_updated', $last_updated_array);
333 if ($is_owner && !$update && !DI::config()->get('theme', 'hide_eventlist')) {
334 $o .= ProfileModel::getBirthdays();
335 $o .= ProfileModel::getEventsReminderHTML();
339 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
341 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
345 $items = DBA::toArray($items_stmt);
347 if ($pager->getStart() == 0 && !empty($a->profile['uid'])) {
348 $pinned_items = Item::selectPinned($a->profile['uid'], ['uri', 'pinned'], ['true' . $sql_extra]);
349 $pinned = Item::inArray($pinned_items);
350 $items = array_merge($items, $pinned);
353 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'pinned_received', $a->profile['uid']);
356 $o .= $pager->renderMinimal(count($items));