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