]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile.php
6e960f8b629a4a1a23bdf258a01b7602d1262d19
[friendica.git] / src / Module / Profile.php
1 <?php
2
3 namespace Friendica\Module;
4
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\Session;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile as ProfileModel;
18 use Friendica\Model\User;
19 use Friendica\Module\Security\Login;
20 use Friendica\Protocol\ActivityPub;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Util\Security;
23 use Friendica\Util\Strings;
24 use Friendica\Util\XML;
25
26 require_once 'boot.php';
27
28 class Profile extends BaseModule
29 {
30         public static $which = '';
31         public static $profile = 0;
32
33         public static function init(array $parameters = [])
34         {
35                 $a = DI::app();
36
37                 // @TODO: Replace with parameter from router
38                 if ($a->argc < 2) {
39                         throw new \Friendica\Network\HTTPException\BadRequestException();
40                 }
41
42                 self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
43
44                 // @TODO: Replace with parameter from router
45                 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
46                         self::$which = $a->user['nickname'];
47                         self::$profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
48                 }
49         }
50
51         public static function rawContent(array $parameters = [])
52         {
53                 if (ActivityPub::isRequest()) {
54                         $user = DBA::selectFirst('user', ['uid'], ['nickname' => self::$which]);
55                         if (DBA::isResult($user)) {
56                                 // The function returns an empty array when the account is removed, expired or blocked
57                                 $data = ActivityPub\Transmitter::getProfile($user['uid']);
58                                 if (!empty($data)) {
59                                         System::jsonExit($data, 'application/activity+json');
60                                 }
61                         }
62
63                         if (DBA::exists('userd', ['username' => self::$which])) {
64                                 // Known deleted user
65                                 $data = ActivityPub\Transmitter::getDeletedUser(self::$which);
66
67                                 System::jsonError(410, $data);
68                         } else {
69                                 // Any other case (unknown, blocked, unverified, expired, no profile, no self contact)
70                                 System::jsonError(404, []);
71                         }
72                 }
73         }
74
75         public static function content(array $parameters = [], $update = 0)
76         {
77                 $a = DI::app();
78
79                 if (!$update) {
80                         ProfileModel::load($a, self::$which, self::$profile);
81
82                         $page = DI::page();
83
84                         $page['htmlhead'] .= "\n";
85
86                         $blocked   = !local_user() && !Session::getRemoteContactID($a->profile['profile_uid']) && Config::get('system', 'block_public');
87                         $userblock = !local_user() && !Session::getRemoteContactID($a->profile['profile_uid']) && $a->profile['hidewall'];
88
89                         if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
90                                 $page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
91                         }
92
93                         if (!empty($a->profile['openidserver'])) {
94                                 $page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\n";
95                         }
96
97                         if (!empty($a->profile['openid'])) {
98                                 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
99                                 $page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
100                         }
101
102                         // site block
103                         if (!$blocked && !$userblock) {
104                                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $a->profile['pub_keywords'] ?? '');
105                                 if (strlen($keywords)) {
106                                         $page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
107                                 }
108                         }
109
110                         $page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
111
112                         if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
113                                 $page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
114                         }
115
116                         $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";
117                         $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";
118                         $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";
119                         $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";
120                         $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . DI::baseUrl()->getHostname() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''));
121                         $page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . DI::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\n";
122                         header('Link: <' . DI::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
123
124                         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
125                         foreach ($dfrn_pages as $dfrn) {
126                                 $page['htmlhead'] .= '<link rel="dfrn-' . $dfrn . '" href="' . DI::baseUrl() . '/dfrn_' . $dfrn . '/' . self::$which . '" />' . "\n";
127                         }
128                         $page['htmlhead'] .= '<link rel="dfrn-poco" href="' . DI::baseUrl() . '/poco/' . self::$which . '" />' . "\n";
129                 }
130
131                 $category = $datequery = $datequery2 = '';
132
133                 if ($a->argc > 2) {
134                         for ($x = 2; $x < $a->argc; $x ++) {
135                                 if (DI::dtFormat()->isYearMonth($a->argv[$x])) {
136                                         if ($datequery) {
137                                                 $datequery2 = Strings::escapeHtml($a->argv[$x]);
138                                         } else {
139                                                 $datequery = Strings::escapeHtml($a->argv[$x]);
140                                         }
141                                 } else {
142                                         $category = $a->argv[$x];
143                                 }
144                         }
145                 }
146
147                 if (empty($category)) {
148                         $category = $_GET['category'] ?? '';
149                 }
150
151                 $hashtags = $_GET['tag'] ?? '';
152
153                 if (Config::get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['profile_uid'])) {
154                         return Login::form();
155                 }
156
157                 $o = '';
158
159                 if ($update) {
160                         // Ensure we've got a profile owner if updating.
161                         $a->profile['profile_uid'] = $update;
162                 } elseif ($a->profile['profile_uid'] == local_user()) {
163                         Nav::setSelected('home');
164                 }
165
166                 $remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
167                 $is_owner = local_user() == $a->profile['profile_uid'];
168                 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . $remote_contact;
169
170                 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
171                         notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
172                         return '';
173                 }
174
175                 if (!$update) {
176                         $tab = Strings::escapeTags(trim($_GET['tab'] ?? ''));
177
178                         $o .= ProfileModel::getTabs($a, $tab, $is_owner, $a->profile['nickname']);
179
180                         if ($tab === 'profile') {
181                                 $o .= ProfileModel::getAdvanced($a);
182                                 Hook::callAll('profile_advanced', $o);
183                                 return $o;
184                         }
185
186                         $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
187
188                         $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
189                         $commvisitor = $commpage && $remote_contact;
190
191                         DI::page()['aside'] .= Widget::postedByYear(DI::baseUrl()->get(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'] ?? 0, true);
192                         DI::page()['aside'] .= Widget::categories(DI::baseUrl()->get(true) . '/profile/' . $a->profile['nickname'], XML::escape($category));
193                         DI::page()['aside'] .= Widget::tagCloud();
194
195                         if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
196                                 $x = [
197                                         'is_owner' => $is_owner,
198                                         'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
199                                         'default_location' => $is_owner ? $a->user['default-location'] : '',
200                                         'nickname' => $a->profile['nickname'],
201                                         'lockstate' => is_array($a->user)
202                                         && (strlen($a->user['allow_cid'])
203                                                 || strlen($a->user['allow_gid'])
204                                                 || strlen($a->user['deny_cid'])
205                                                 || strlen($a->user['deny_gid'])
206                                         ) ? 'lock' : 'unlock',
207                                         'acl' => $is_owner ? ACL::getFullSelectorHTML(DI::page(), $a->user, true) : '',
208                                         'bang' => '',
209                                         'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
210                                         'profile_uid' => $a->profile['profile_uid'],
211                                 ];
212
213                                 $o .= status_editor($a, $x);
214                         }
215                 }
216
217                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
218                 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
219                 $sql_extra2 = '';
220
221                 $last_updated_array = Session::get('last_updated', []);
222
223                 if ($update) {
224                         $last_updated = $last_updated_array[$last_updated_key] ?? 0;
225
226                         // If the page user is the owner of the page we should query for unseen
227                         // items. Otherwise use a timestamp of the last succesful update request.
228                         if ($is_owner || !$last_updated) {
229                                 $sql_extra4 = " AND `item`.`unseen`";
230                         } else {
231                                 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
232                                 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
233                         }
234
235                         $items_stmt = DBA::p(
236                                 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
237                                 FROM `item`
238                                 INNER JOIN `contact`
239                                 ON `contact`.`id` = `item`.`contact-id`
240                                         AND NOT `contact`.`blocked`
241                                         AND NOT `contact`.`pending`
242                                 WHERE `item`.`uid` = ?
243                                         AND `item`.`visible`
244                                         AND     (NOT `item`.`deleted` OR `item`.`gravity` = ?)
245                                         AND NOT `item`.`moderated`
246                                         AND `item`.`wall`
247                                         $sql_extra4
248                                         $sql_extra
249                                 ORDER BY `item`.`received` DESC",
250                                 $a->profile['profile_uid'],
251                                 GRAVITY_ACTIVITY
252                         );
253
254                         if (!DBA::isResult($items_stmt)) {
255                                 return '';
256                         }
257
258                         $pager = new Pager(DI::args()->getQueryString());
259                 } else {
260                         $sql_post_table = "";
261
262                         if (!empty($category)) {
263                                 $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` ",
264                                         DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
265                         }
266
267                         if (!empty($hashtags)) {
268                                 $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` ",
269                                         DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
270                         }
271
272                         if (!empty($datequery)) {
273                                 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
274                         }
275                         if (!empty($datequery2)) {
276                                 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
277                         }
278
279                         // Does the profile page belong to a forum?
280                         // If not then we can improve the performance with an additional condition
281                         $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
282                         if (!DBA::exists('user', $condition)) {
283                                 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
284                         } else {
285                                 $sql_extra3 = "";
286                         }
287
288                         //  check if we serve a mobile device and get the user settings
289                         //  accordingly
290                         if (DI::mode()->isMobile()) {
291                                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 10);
292                         } else {
293                                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 20);
294                         }
295
296                         //  now that we have the user settings, see if the theme forces
297                         //  a maximum item number which is lower then the user choice
298                         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
299                                 $itemspage_network = $a->force_max_items;
300                         }
301
302                         $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
303
304                         $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
305
306                         $items_stmt = DBA::p(
307                                 "SELECT `item`.`uri`
308                                 FROM `thread`
309                                 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
310                                 $sql_post_table
311                                 STRAIGHT_JOIN `contact`
312                                 ON `contact`.`id` = `thread`.`contact-id`
313                                         AND NOT `contact`.`blocked`
314                                         AND NOT `contact`.`pending`
315                                 WHERE `thread`.`uid` = ?
316                                         AND `thread`.`visible`
317                                         AND NOT `thread`.`deleted`
318                                         AND NOT `thread`.`moderated`
319                                         AND `thread`.`wall`
320                                         $sql_extra3
321                                         $sql_extra
322                                         $sql_extra2
323                                 ORDER BY `thread`.`received` DESC
324                                 $pager_sql",
325                                 $a->profile['profile_uid']
326                         );
327                 }
328
329                 // Set a time stamp for this page. We will make use of it when we
330                 // search for new items (update routine)
331                 $last_updated_array[$last_updated_key] = time();
332                 Session::set('last_updated', $last_updated_array);
333
334                 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
335                         $o .= ProfileModel::getBirthdays();
336                         $o .= ProfileModel::getEventsReminderHTML();
337                 }
338
339                 if ($is_owner) {
340                         $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
341                         if ($unseen) {
342                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
343                         }
344                 }
345
346                 $items = DBA::toArray($items_stmt);
347
348                 if ($pager->getStart() == 0 && !empty($a->profile['profile_uid'])) {
349                         $pinned_items = Item::selectPinned($a->profile['profile_uid'], ['uri', 'pinned'], ['true' . $sql_extra]);
350                         $pinned = Item::inArray($pinned_items);
351                         $items = array_merge($items, $pinned);
352                 }
353
354                 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'pinned_received', $a->profile['profile_uid']);
355
356                 if (!$update) {
357                         $o .= $pager->renderMinimal(count($items));
358                 }
359
360                 return $o;
361         }
362 }