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