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