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