]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile.php
show pinned items
[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\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;
28
29 require_once 'boot.php';
30
31 class Profile extends BaseModule
32 {
33         public static $which = '';
34         public static $profile = 0;
35
36         public static function init(array $parameters = [])
37         {
38                 $a = self::getApp();
39
40                 // @TODO: Replace with parameter from router
41                 if ($a->argc < 2) {
42                         throw new \Friendica\Network\HTTPException\BadRequestException();
43                 }
44
45                 self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
46
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);
51                 }
52         }
53
54         public static function rawContent(array $parameters = [])
55         {
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']);
61                                 if (!empty($data)) {
62                                         System::jsonExit($data, 'application/activity+json');
63                                 }
64                         }
65
66                         if (DBA::exists('userd', ['username' => self::$which])) {
67                                 // Known deleted user
68                                 $data = ActivityPub\Transmitter::getDeletedUser(self::$which);
69
70                                 System::jsonError(410, $data);
71                         } else {
72                                 // Any other case (unknown, blocked, unverified, expired, no profile, no self contact)
73                                 System::jsonError(404, []);
74                         }
75                 }
76         }
77
78         public static function content(array $parameters = [], $update = 0)
79         {
80                 $a = self::getApp();
81
82                 if (!$update) {
83                         ProfileModel::load($a, self::$which, self::$profile);
84
85                         $a->page['htmlhead'] .= "\n";
86
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'];
89
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";
92                         }
93
94                         if (!empty($a->profile['openidserver'])) {
95                                 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\n";
96                         }
97
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";
101                         }
102
103                         // site block
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";
108                                 }
109                         }
110
111                         $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
112
113                         if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
114                                 $a->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
115                         }
116
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);
124
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";
128                         }
129                         $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . '/poco/' . self::$which . '" />' . "\n";
130                 }
131
132                 $category = $datequery = $datequery2 = '';
133
134                 /** @var DateTimeFormat $dtFormat */
135                 $dtFormat = self::getClass(DateTimeFormat::class);
136
137                 if ($a->argc > 2) {
138                         for ($x = 2; $x < $a->argc; $x ++) {
139                                 if ($dtFormat->isYearMonth($a->argv[$x])) {
140                                         if ($datequery) {
141                                                 $datequery2 = Strings::escapeHtml($a->argv[$x]);
142                                         } else {
143                                                 $datequery = Strings::escapeHtml($a->argv[$x]);
144                                         }
145                                 } else {
146                                         $category = $a->argv[$x];
147                                 }
148                         }
149                 }
150
151                 if (empty($category)) {
152                         $category = $_GET['category'] ?? '';
153                 }
154
155                 $hashtags = $_GET['tag'] ?? '';
156
157                 if (Config::get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['profile_uid'])) {
158                         return Login::form();
159                 }
160
161                 $o = '';
162
163                 if ($update) {
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');
168                 }
169
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;
173
174                 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
175                         notice(L10n::t('Access to this profile has been restricted.') . EOL);
176                         return '';
177                 }
178
179                 if (!$update) {
180                         $tab = Strings::escapeTags(trim($_GET['tab'] ?? ''));
181
182                         $o .= ProfileModel::getTabs($a, $tab, $is_owner, $a->profile['nickname']);
183
184                         if ($tab === 'profile') {
185                                 $o .= ProfileModel::getAdvanced($a);
186                                 Hook::callAll('profile_advanced', $o);
187                                 return $o;
188                         }
189
190                         $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
191
192                         $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
193                         $commvisitor = $commpage && $remote_contact;
194
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();
198
199                         if (Security::canWriteToUserWall($a->profile['profile_uid'])) {
200                                 $x = [
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) : '',
212                                         'bang' => '',
213                                         'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
214                                         'profile_uid' => $a->profile['profile_uid'],
215                                 ];
216
217                                 $o .= status_editor($a, $x);
218                         }
219                 }
220
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']);
223                 $sql_extra2 = '';
224
225                 $last_updated_array = Session::get('last_updated', []);
226
227                 if ($update) {
228                         $last_updated = $last_updated_array[$last_updated_key] ?? 0;
229
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`";
234                         } else {
235                                 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
236                                 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
237                         }
238
239                         $items_stmt = DBA::p(
240                                 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`
241                                 FROM `item`
242                                 INNER JOIN `contact`
243                                 ON `contact`.`id` = `item`.`contact-id`
244                                         AND NOT `contact`.`blocked`
245                                         AND NOT `contact`.`pending`
246                                 WHERE `item`.`uid` = ?
247                                         AND `item`.`visible`
248                                         AND     (NOT `item`.`deleted` OR `item`.`gravity` = ?)
249                                         AND NOT `item`.`moderated`
250                                         AND `item`.`wall`
251                                         $sql_extra4
252                                         $sql_extra
253                                 ORDER BY `item`.`received` DESC",
254                                 $a->profile['profile_uid'],
255                                 GRAVITY_ACTIVITY
256                         );
257
258                         if (!DBA::isResult($items_stmt)) {
259                                 return '';
260                         }
261
262                         $pager = new Pager($a->query_string);
263                 } else {
264                         $sql_post_table = "";
265
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']));
269                         }
270
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']));
274                         }
275
276                         if (!empty($datequery)) {
277                                 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
278                         }
279                         if (!empty($datequery2)) {
280                                 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
281                         }
282
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'])));
288                         } else {
289                                 $sql_extra3 = "";
290                         }
291
292                         //  check if we serve a mobile device and get the user settings
293                         //  accordingly
294                         if ($a->is_mobile) {
295                                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
296                         } else {
297                                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
298                         }
299
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;
304                         }
305
306                         $pager = new Pager($a->query_string, $itemspage_network);
307
308                         $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
309
310                         $items_stmt = DBA::p(
311                                 "SELECT `item`.`uri`
312                                 FROM `thread`
313                                 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
314                                 $sql_post_table
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`
323                                         AND `thread`.`wall`
324                                         $sql_extra3
325                                         $sql_extra
326                                         $sql_extra2
327                                 ORDER BY `thread`.`received` DESC
328                                 $pager_sql",
329                                 $a->profile['profile_uid']
330                         );
331                 }
332
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);
337
338                 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
339                         $o .= ProfileModel::getBirthdays();
340                         $o .= ProfileModel::getEventsReminderHTML();
341                 }
342
343                 if ($is_owner) {
344                         $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
345                         if ($unseen) {
346                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
347                         }
348                 }
349
350                 $items = DBA::toArray($items_stmt);
351
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);
356                 }
357
358                 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'received', $a->profile['profile_uid']);
359
360                 if (!$update) {
361                         $o .= $pager->renderMinimal(count($items));
362                 }
363
364                 return $o;
365         }
366 }