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\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Session;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Item;
18 use Friendica\Model\Profile as ProfileModel;
19 use Friendica\Model\User;
20 use Friendica\Module\Security\Login;
21 use Friendica\Protocol\ActivityPub;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Security;
24 use Friendica\Util\Strings;
25 use Friendica\Util\XML;
27 require_once 'boot.php';
29 class Profile extends BaseModule
31 public static $which = '';
32 public static $profile = 0;
34 public static function init(array $parameters = [])
38 // @TODO: Replace with parameter from router
40 throw new \Friendica\Network\HTTPException\BadRequestException();
43 self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
45 // @TODO: Replace with parameter from router
46 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
47 self::$which = $a->user['nickname'];
48 self::$profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
52 public static function rawContent(array $parameters = [])
54 if (ActivityPub::isRequest()) {
55 $user = DBA::selectFirst('user', ['uid'], ['nickname' => self::$which]);
56 if (DBA::isResult($user)) {
57 // The function returns an empty array when the account is removed, expired or blocked
58 $data = ActivityPub\Transmitter::getProfile($user['uid']);
60 System::jsonExit($data, 'application/activity+json');
64 if (DBA::exists('userd', ['username' => self::$which])) {
66 $data = ActivityPub\Transmitter::getDeletedUser(self::$which);
68 System::jsonError(410, $data);
70 // Any other case (unknown, blocked, unverified, expired, no profile, no self contact)
71 System::jsonError(404, []);
76 public static function content(array $parameters = [], $update = 0)
81 ProfileModel::load($a, self::$which, self::$profile);
85 $page['htmlhead'] .= "\n";
87 $blocked = !local_user() && !Session::getRemoteContactID($a->profile['profile_uid']) && Config::get('system', 'block_public');
88 $userblock = !local_user() && !Session::getRemoteContactID($a->profile['profile_uid']) && $a->profile['hidewall'];
90 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
91 $page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
94 if (!empty($a->profile['openidserver'])) {
95 $page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\n";
98 if (!empty($a->profile['openid'])) {
99 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
100 $page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
104 if (!$blocked && !$userblock) {
105 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $a->profile['pub_keywords'] ?? '');
106 if (strlen($keywords)) {
107 $page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
111 $page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
113 if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
114 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
117 $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";
118 $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";
119 $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";
120 $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";
121 $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''));
122 $page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . DI::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\n";
123 header('Link: <' . DI::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
125 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
126 foreach ($dfrn_pages as $dfrn) {
127 $page['htmlhead'] .= '<link rel="dfrn-' . $dfrn . '" href="' . DI::baseUrl() . '/dfrn_' . $dfrn . '/' . self::$which . '" />' . "\n";
129 $page['htmlhead'] .= '<link rel="dfrn-poco" href="' . DI::baseUrl() . '/poco/' . self::$which . '" />' . "\n";
132 $category = $datequery = $datequery2 = '';
135 for ($x = 2; $x < $a->argc; $x ++) {
136 if (DI::dtFormat()->isYearMonth($a->argv[$x])) {
138 $datequery2 = Strings::escapeHtml($a->argv[$x]);
140 $datequery = Strings::escapeHtml($a->argv[$x]);
143 $category = $a->argv[$x];
148 if (empty($category)) {
149 $category = $_GET['category'] ?? '';
152 $hashtags = $_GET['tag'] ?? '';
154 if (Config::get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['profile_uid'])) {
155 return Login::form();
161 // Ensure we've got a profile owner if updating.
162 $a->profile['profile_uid'] = $update;
163 } elseif ($a->profile['profile_uid'] == local_user()) {
164 Nav::setSelected('home');
167 $remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
168 $is_owner = local_user() == $a->profile['profile_uid'];
169 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . $remote_contact;
171 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
172 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
177 $tab = Strings::escapeTags(trim($_GET['tab'] ?? ''));
179 $o .= ProfileModel::getTabs($a, $tab, $is_owner, $a->profile['nickname']);
181 if ($tab === 'profile') {
182 $o .= ProfileModel::getAdvanced($a);
183 Hook::callAll('profile_advanced', $o);
187 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
189 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
190 $commvisitor = $commpage && $remote_contact;
192 DI::page()['aside'] .= Widget::postedByYear(DI::baseUrl()->get(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'] ?? 0, true);
193 DI::page()['aside'] .= Widget::categories(DI::baseUrl()->get(true) . '/profile/' . $a->profile['nickname'], XML::escape($category));
194 DI::page()['aside'] .= Widget::tagCloud();
196 if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
198 'is_owner' => $is_owner,
199 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
200 'default_location' => $is_owner ? $a->user['default-location'] : '',
201 'nickname' => $a->profile['nickname'],
202 'lockstate' => is_array($a->user)
203 && (strlen($a->user['allow_cid'])
204 || strlen($a->user['allow_gid'])
205 || strlen($a->user['deny_cid'])
206 || strlen($a->user['deny_gid'])
207 ) ? 'lock' : 'unlock',
208 'acl' => $is_owner ? ACL::getFullSelectorHTML(DI::page(), $a->user, true) : '',
210 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
211 'profile_uid' => $a->profile['profile_uid'],
214 $o .= status_editor($a, $x);
218 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
219 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
222 $last_updated_array = Session::get('last_updated', []);
225 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
227 // If the page user is the owner of the page we should query for unseen
228 // items. Otherwise use a timestamp of the last succesful update request.
229 if ($is_owner || !$last_updated) {
230 $sql_extra4 = " AND `item`.`unseen`";
232 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
233 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
236 $items_stmt = DBA::p(
237 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
240 ON `contact`.`id` = `item`.`contact-id`
241 AND NOT `contact`.`blocked`
242 AND NOT `contact`.`pending`
243 WHERE `item`.`uid` = ?
245 AND (NOT `item`.`deleted` OR `item`.`gravity` = ?)
246 AND NOT `item`.`moderated`
250 ORDER BY `item`.`received` DESC",
251 $a->profile['profile_uid'],
255 if (!DBA::isResult($items_stmt)) {
259 $pager = new Pager(DI::args()->getQueryString());
261 $sql_post_table = "";
263 if (!empty($category)) {
264 $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` ",
265 DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
268 if (!empty($hashtags)) {
269 $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` ",
270 DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
273 if (!empty($datequery)) {
274 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
276 if (!empty($datequery2)) {
277 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
280 // Does the profile page belong to a forum?
281 // If not then we can improve the performance with an additional condition
282 $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
283 if (!DBA::exists('user', $condition)) {
284 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
289 // check if we serve a mobile device and get the user settings
291 if (DI::mode()->isMobile()) {
292 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 10);
294 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 20);
297 // now that we have the user settings, see if the theme forces
298 // a maximum item number which is lower then the user choice
299 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
300 $itemspage_network = $a->force_max_items;
303 $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
305 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
307 $items_stmt = DBA::p(
310 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
312 STRAIGHT_JOIN `contact`
313 ON `contact`.`id` = `thread`.`contact-id`
314 AND NOT `contact`.`blocked`
315 AND NOT `contact`.`pending`
316 WHERE `thread`.`uid` = ?
317 AND `thread`.`visible`
318 AND NOT `thread`.`deleted`
319 AND NOT `thread`.`moderated`
324 ORDER BY `thread`.`received` DESC
326 $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 $last_updated_array[$last_updated_key] = time();
333 Session::set('last_updated', $last_updated_array);
335 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
336 $o .= ProfileModel::getBirthdays();
337 $o .= ProfileModel::getEventsReminderHTML();
341 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
343 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
347 $items = DBA::toArray($items_stmt);
349 if ($pager->getStart() == 0 && !empty($a->profile['profile_uid'])) {
350 $pinned_items = Item::selectPinned($a->profile['profile_uid'], ['uri', 'pinned'], ['true' . $sql_extra]);
351 $pinned = Item::inArray($pinned_items);
352 $items = array_merge($items, $pinned);
355 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'pinned_received', $a->profile['profile_uid']);
358 $o .= $pager->renderMinimal(count($items));