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