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\PConfig;
14 use Friendica\Core\Session;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact as ContactModel;
18 use Friendica\Model\Group;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile as ProfileModel;
21 use Friendica\Model\User;
22 use Friendica\Protocol\ActivityPub;
23 use Friendica\Protocol\DFRN;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Security;
26 use Friendica\Util\Strings;
27 use Friendica\Util\XML;
29 require_once 'boot.php';
31 class Profile extends BaseModule
33 public static $which = '';
34 public static $profile = 0;
36 public static function init(array $parameters = [])
40 // @TODO: Replace with parameter from router
42 throw new \Friendica\Network\HTTPException\BadRequestException();
45 self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
47 // @TODO: Replace with parameter from router
48 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
49 self::$which = $a->user['nickname'];
50 self::$profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
54 public static function rawContent(array $parameters = [])
56 if (ActivityPub::isRequest()) {
57 $user = DBA::selectFirst('user', ['uid'], ['nickname' => self::$which]);
58 if (DBA::isResult($user)) {
59 // The function returns an empty array when the account is removed, expired or blocked
60 $data = ActivityPub\Transmitter::getProfile($user['uid']);
62 System::jsonExit($data, 'application/activity+json');
66 if (DBA::exists('userd', ['username' => self::$which])) {
68 $data = ActivityPub\Transmitter::getDeletedUser(self::$which);
70 System::jsonError(410, $data);
72 // Any other case (unknown, blocked, unverified, expired, no profile, no self contact)
73 System::jsonError(404, []);
78 public static function content(array $parameters = [], $update = 0)
83 ProfileModel::load($a, self::$which, self::$profile);
85 $a->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 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
94 if (!empty($a->profile['openidserver'])) {
95 $a->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 $a->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 $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
111 $a->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 $a->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
117 $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/dfrn_poll/' . self::$which . '" title="DFRN: ' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\n";
118 $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . self::$which . '/" title="' . L10n::t('%s\'s posts', $a->profile['username']) . '"/>' . "\n";
119 $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . self::$which . '/comments" title="' . L10n::t('%s\'s comments', $a->profile['username']) . '"/>' . "\n";
120 $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . self::$which . '/activity" title="' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\n";
121 $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''));
122 $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\n";
123 header('Link: <' . System::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 $a->page['htmlhead'] .= '<link rel="dfrn-' . $dfrn . '" href="' . System::baseUrl() . '/dfrn_' . $dfrn . '/' . self::$which . '" />' . "\n";
129 $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . '/poco/' . self::$which . '" />' . "\n";
132 $category = $datequery = $datequery2 = '';
134 /** @var DateTimeFormat $dtFormat */
135 $dtFormat = self::getClass(DateTimeFormat::class);
138 for ($x = 2; $x < $a->argc; $x ++) {
139 if ($dtFormat->isYearMonth($a->argv[$x])) {
141 $datequery2 = Strings::escapeHtml($a->argv[$x]);
143 $datequery = Strings::escapeHtml($a->argv[$x]);
146 $category = $a->argv[$x];
151 if (empty($category)) {
152 $category = $_GET['category'] ?? '';
155 $hashtags = $_GET['tag'] ?? '';
157 if (Config::get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['profile_uid'])) {
158 return Login::form();
164 // Ensure we've got a profile owner if updating.
165 $a->profile['profile_uid'] = $update;
166 } elseif ($a->profile['profile_uid'] == local_user()) {
167 Nav::setSelected('home');
170 $remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
171 $is_owner = local_user() == $a->profile['profile_uid'];
172 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . $remote_contact;
174 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
175 notice(L10n::t('Access to this profile has been restricted.') . EOL);
180 $tab = Strings::escapeTags(trim($_GET['tab'] ?? ''));
182 $o .= ProfileModel::getTabs($a, $tab, $is_owner, $a->profile['nickname']);
184 if ($tab === 'profile') {
185 $o .= ProfileModel::getAdvanced($a);
186 Hook::callAll('profile_advanced', $o);
190 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
192 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
193 $commvisitor = $commpage && $remote_contact;
195 $a->page['aside'] .= Widget::postedByYear(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'] ?? 0, true);
196 $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], XML::escape($category));
197 $a->page['aside'] .= Widget::tagCloud();
199 if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
201 'is_owner' => $is_owner,
202 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
203 'default_location' => $is_owner ? $a->user['default-location'] : '',
204 'nickname' => $a->profile['nickname'],
205 'lockstate' => is_array($a->user)
206 && (strlen($a->user['allow_cid'])
207 || strlen($a->user['allow_gid'])
208 || strlen($a->user['deny_cid'])
209 || strlen($a->user['deny_gid'])
210 ) ? 'lock' : 'unlock',
211 'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',
213 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
214 'profile_uid' => $a->profile['profile_uid'],
217 $o .= status_editor($a, $x);
221 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
222 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
225 $last_updated_array = Session::get('last_updated', []);
228 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
230 // If the page user is the owner of the page we should query for unseen
231 // items. Otherwise use a timestamp of the last succesful update request.
232 if ($is_owner || !$last_updated) {
233 $sql_extra4 = " AND `item`.`unseen`";
235 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
236 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
239 $items_stmt = DBA::p(
240 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
243 ON `contact`.`id` = `item`.`contact-id`
244 AND NOT `contact`.`blocked`
245 AND NOT `contact`.`pending`
246 WHERE `item`.`uid` = ?
248 AND (NOT `item`.`deleted` OR `item`.`gravity` = ?)
249 AND NOT `item`.`moderated`
253 ORDER BY `item`.`received` DESC",
254 $a->profile['profile_uid'],
258 if (!DBA::isResult($items_stmt)) {
262 $pager = new Pager($a->query_string);
264 $sql_post_table = "";
266 if (!empty($category)) {
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($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
271 if (!empty($hashtags)) {
272 $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` ",
273 DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
276 if (!empty($datequery)) {
277 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
279 if (!empty($datequery2)) {
280 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
283 // Does the profile page belong to a forum?
284 // If not then we can improve the performance with an additional condition
285 $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
286 if (!DBA::exists('user', $condition)) {
287 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
292 // check if we serve a mobile device and get the user settings
295 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
297 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
300 // now that we have the user settings, see if the theme forces
301 // a maximum item number which is lower then the user choice
302 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
303 $itemspage_network = $a->force_max_items;
306 $pager = new Pager($a->query_string, $itemspage_network);
308 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
310 $items_stmt = DBA::p(
313 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
315 STRAIGHT_JOIN `contact`
316 ON `contact`.`id` = `thread`.`contact-id`
317 AND NOT `contact`.`blocked`
318 AND NOT `contact`.`pending`
319 WHERE `thread`.`uid` = ?
320 AND `thread`.`visible`
321 AND NOT `thread`.`deleted`
322 AND NOT `thread`.`moderated`
327 ORDER BY `thread`.`received` DESC
329 $a->profile['profile_uid']
333 // Set a time stamp for this page. We will make use of it when we
334 // search for new items (update routine)
335 $last_updated_array[$last_updated_key] = time();
336 Session::set('last_updated', $last_updated_array);
338 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
339 $o .= ProfileModel::getBirthdays();
340 $o .= ProfileModel::getEventsReminderHTML();
344 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
346 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
350 $items = DBA::toArray($items_stmt);
352 if ($pager->getStart() == 0) {
353 $pinned_items = Item::selectPinned($a->profile['profile_uid'], ['uri']);
354 $pinned = Item::inArray($pinned_items);
355 $items = array_merge($items, $pinned);
358 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'pinned_received', $a->profile['profile_uid']);
361 $o .= $pager->renderMinimal(count($items));