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()
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);
52 DFRN::autoRedir($a, self::$which);
56 public static function rawContent()
58 if (ActivityPub::isRequest()) {
59 $user = DBA::selectFirst('user', ['uid'], ['nickname' => self::$which]);
60 if (DBA::isResult($user)) {
61 // The function returns an empty array when the account is removed, expired or blocked
62 $data = ActivityPub\Transmitter::getProfile($user['uid']);
64 System::jsonExit($data, 'application/activity+json');
68 if (DBA::exists('userd', ['username' => self::$which])) {
70 $data = ActivityPub\Transmitter::getDeletedUser(self::$which);
72 System::jsonError(410, $data);
74 // Any other case (unknown, blocked, unverified, expired, no profile, no self contact)
75 System::jsonError(404, []);
80 public static function content($update = 0)
85 ProfileModel::load($a, self::$which, self::$profile);
87 $a->page['htmlhead'] .= "\n";
89 $blocked = !local_user() && !remote_user() && Config::get('system', 'block_public');
90 $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
92 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
93 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
96 if (!empty($a->profile['openidserver'])) {
97 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\n";
100 if (!empty($a->profile['openid'])) {
101 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
102 $a->page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
106 if (!$blocked && !$userblock) {
107 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], defaults($a->profile, 'pub_keywords', ''));
108 if (strlen($keywords)) {
109 $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
113 $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
115 if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
116 $a->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
119 $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";
120 $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";
121 $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";
122 $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";
123 $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''));
124 $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\n";
125 header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
127 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
128 foreach ($dfrn_pages as $dfrn) {
129 $a->page['htmlhead'] .= '<link rel="dfrn-' . $dfrn . '" href="' . System::baseUrl() . '/dfrn_' . $dfrn . '/' . self::$which . '" />' . "\n";
131 $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . '/poco/' . self::$which . '" />' . "\n";
134 $category = $datequery = $datequery2 = '';
137 for ($x = 2; $x < $a->argc; $x ++) {
138 if (is_a_date_arg($a->argv[$x])) {
140 $datequery2 = Strings::escapeHtml($a->argv[$x]);
142 $datequery = Strings::escapeHtml($a->argv[$x]);
145 $category = $a->argv[$x];
150 if (empty($category)) {
151 $category = defaults($_GET, 'category', '');
154 $hashtags = defaults($_GET, 'tag', '');
156 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
157 return Login::form();
166 // Ensure we've got a profile owner if updating.
167 $a->profile['profile_uid'] = $update;
168 } elseif ($a->profile['profile_uid'] == local_user()) {
169 Nav::setSelected('home');
172 $remote_contact = ContactModel::isFollower(remote_user(), $a->profile['profile_uid']);
173 $is_owner = local_user() == $a->profile['profile_uid'];
174 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
176 if ($remote_contact) {
177 $cdata = ContactModel::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
178 if (!empty($cdata['user'])) {
179 $groups = Group::getIdsByContactId($cdata['user']);
180 $remote_cid = $cdata['user'];
184 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
185 notice(L10n::t('Access to this profile has been restricted.') . EOL);
190 $tab = Strings::escapeTags(trim(defaults($_GET, 'tab', '')));
192 $o .= ProfileModel::getTabs($a, $tab, $is_owner, $a->profile['nickname']);
194 if ($tab === 'profile') {
195 $o .= ProfileModel::getAdvanced($a);
196 Hook::callAll('profile_advanced', $o);
200 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
202 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
203 $commvisitor = $commpage && $remote_contact;
205 $a->page['aside'] .= Widget::postedByYear(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'] ?? 0, true);
206 $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], XML::escape($category));
207 $a->page['aside'] .= Widget::tagCloud();
209 if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
211 'is_owner' => $is_owner,
212 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
213 'default_location' => $is_owner ? $a->user['default-location'] : '',
214 'nickname' => $a->profile['nickname'],
215 'lockstate' => is_array($a->user)
216 && (strlen($a->user['allow_cid'])
217 || strlen($a->user['allow_gid'])
218 || strlen($a->user['deny_cid'])
219 || strlen($a->user['deny_gid'])
220 ) ? 'lock' : 'unlock',
221 'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',
223 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
224 'profile_uid' => $a->profile['profile_uid'],
227 $o .= status_editor($a, $x);
231 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
232 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $remote_contact, $groups, $remote_cid);
235 $last_updated_array = Session::get('last_updated', []);
238 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
240 // If the page user is the owner of the page we should query for unseen
241 // items. Otherwise use a timestamp of the last succesful update request.
242 if ($is_owner || !$last_updated) {
243 $sql_extra4 = " AND `item`.`unseen`";
245 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
246 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
249 $items_stmt = DBA::p(
250 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
253 ON `contact`.`id` = `item`.`contact-id`
254 AND NOT `contact`.`blocked`
255 AND NOT `contact`.`pending`
256 WHERE `item`.`uid` = ?
258 AND (NOT `item`.`deleted` OR `item`.`gravity` = ?)
259 AND NOT `item`.`moderated`
263 ORDER BY `item`.`received` DESC",
264 $a->profile['profile_uid'],
268 if (!DBA::isResult($items_stmt)) {
272 $pager = new Pager($a->query_string);
274 $sql_post_table = "";
276 if (!empty($category)) {
277 $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` ",
278 DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
281 if (!empty($hashtags)) {
282 $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` ",
283 DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
286 if (!empty($datequery)) {
287 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
289 if (!empty($datequery2)) {
290 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
293 // Does the profile page belong to a forum?
294 // If not then we can improve the performance with an additional condition
295 $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
296 if (!DBA::exists('user', $condition)) {
297 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
302 // check if we serve a mobile device and get the user settings
305 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
307 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
310 // now that we have the user settings, see if the theme forces
311 // a maximum item number which is lower then the user choice
312 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
313 $itemspage_network = $a->force_max_items;
316 $pager = new Pager($a->query_string, $itemspage_network);
318 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
320 $items_stmt = DBA::p(
323 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
325 STRAIGHT_JOIN `contact`
326 ON `contact`.`id` = `thread`.`contact-id`
327 AND NOT `contact`.`blocked`
328 AND NOT `contact`.`pending`
329 WHERE `thread`.`uid` = ?
330 AND `thread`.`visible`
331 AND NOT `thread`.`deleted`
332 AND NOT `thread`.`moderated`
337 ORDER BY `thread`.`received` DESC
339 $a->profile['profile_uid']
343 // Set a time stamp for this page. We will make use of it when we
344 // search for new items (update routine)
345 $last_updated_array[$last_updated_key] = time();
346 Session::set('last_updated', $last_updated_array);
348 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
349 $o .= ProfileModel::getBirthdays();
350 $o .= ProfileModel::getEventsReminderHTML();
354 $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
356 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
360 $items = DBA::toArray($items_stmt);
362 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'received', $a->profile['profile_uid']);
365 $o .= $pager->renderMinimal(count($items));