]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
Improve Console/Config display for array values
[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\Widget;
9 use Friendica\Core\ACL;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\System;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Group;
17 use Friendica\Model\Item;
18 use Friendica\Model\Profile;
19 use Friendica\Module\Login;
20 use Friendica\Protocol\DFRN;
21 use Friendica\Util\DateTimeFormat;
22
23 function profile_init(App $a)
24 {
25         if (!x($a->page, 'aside')) {
26                 $a->page['aside'] = '';
27         }
28
29         if ($a->argc > 1) {
30                 $which = htmlspecialchars($a->argv[1]);
31         } else {
32                 $r = q("SELECT `nickname` FROM `user` WHERE `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 ORDER BY RAND() LIMIT 1");
33                 if (DBM::is_result($r)) {
34                         goaway(System::baseUrl() . '/profile/' . $r[0]['nickname']);
35                 } else {
36                         logger('profile error: mod_profile ' . $a->query_string, LOGGER_DEBUG);
37                         notice(L10n::t('Requested profile is not available.') . EOL);
38                         $a->error = 404;
39                         return;
40                 }
41         }
42
43         $profile = 0;
44         if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
45                 $which = $a->user['nickname'];
46                 $profile = htmlspecialchars($a->argv[1]);
47         } else {
48                 DFRN::autoRedir($a, $which);
49         }
50
51         Profile::load($a, $which, $profile);
52
53         $blocked   = !local_user() && !remote_user() && Config::get('system', 'block_public');
54         $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
55
56         if (x($a->profile, 'page-flags') && $a->profile['page-flags'] == PAGE_COMMUNITY) {
57                 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />';
58         }
59
60         if (x($a->profile, 'openidserver')) {
61                 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\r\n";
62         }
63
64         if (x($a->profile, 'openid')) {
65                 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];
66                 $a->page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\r\n";
67         }
68
69         // site block
70         if (!$blocked && !$userblock) {
71                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], defaults($a->profile, 'pub_keywords', ''));
72                 if (strlen($keywords)) {
73                         $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\r\n";
74                 }
75         }
76
77         $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\r\n";
78         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/dfrn_poll/' . $which . '" title="' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\r\n";
79         $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";
80         $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";
81         $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";
82         $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->get_hostname() . ($a->urlpath ? '/' . $a->urlpath : ''));
83         $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\r\n";
84         header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
85
86         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
87         foreach ($dfrn_pages as $dfrn) {
88                 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"" . System::baseUrl() . "/dfrn_{$dfrn}/{$which}\" />\r\n";
89         }
90         $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . "/poco/{$which}\" />\r\n";
91 }
92
93 function profile_content(App $a, $update = 0)
94 {
95         $category = $datequery = $datequery2 = '';
96
97         if ($a->argc > 2) {
98                 for ($x = 2; $x < $a->argc; $x ++) {
99                         if (is_a_date_arg($a->argv[$x])) {
100                                 if ($datequery) {
101                                         $datequery2 = escape_tags($a->argv[$x]);
102                                 } else {
103                                         $datequery = escape_tags($a->argv[$x]);
104                                 }
105                         } else {
106                                 $category = $a->argv[$x];
107                         }
108                 }
109         }
110
111         if (!x($category)) {
112                 $category = defaults($_GET, 'category', '');
113         }
114
115         $hashtags = defaults($_GET, 'tag', '');
116
117         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
118                 return Login::form();
119         }
120
121         require_once 'include/security.php';
122         require_once 'include/conversation.php';
123         require_once 'include/items.php';
124
125         $groups = [];
126
127         $tab = 'posts';
128         $o = '';
129
130         if ($update) {
131                 // Ensure we've got a profile owner if updating.
132                 $a->profile['profile_uid'] = $update;
133         } elseif ($a->profile['profile_uid'] == local_user()) {
134                 Nav::setSelected('home');
135         }
136
137         $contact = null;
138         $remote_contact = false;
139
140         $contact_id = 0;
141
142         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
143                 foreach ($_SESSION['remote'] as $v) {
144                         if ($v['uid'] == $a->profile['profile_uid']) {
145                                 $contact_id = $v['cid'];
146                                 break;
147                         }
148                 }
149         }
150
151         if ($contact_id) {
152                 $groups = Group::getIdsByContactId($contact_id);
153                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
154                         intval($contact_id),
155                         intval($a->profile['profile_uid'])
156                 );
157                 if (DBM::is_result($r)) {
158                         $contact = $r[0];
159                         $remote_contact = true;
160                 }
161         }
162
163         if (!$remote_contact) {
164                 if (local_user()) {
165                         $contact_id = $_SESSION['cid'];
166                         $contact = $a->contact;
167                 }
168         }
169
170         $is_owner = local_user() == $a->profile['profile_uid'];
171         $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
172
173         if (x($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 = false;
180                 if (x($_GET, 'tab')) {
181                         $tab = notags(trim($_GET['tab']));
182                 }
183
184                 $o .= Profile::getTabs($a, $is_owner, $a->profile['nickname']);
185
186                 if ($tab === 'profile') {
187                         $o .= Profile::getAdvanced($a);
188                         Addon::callHooks('profile_advanced', $o);
189                         return $o;
190                 }
191
192                 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
193
194                 $commpage = $a->profile['page-flags'] == PAGE_COMMUNITY;
195                 $commvisitor = $commpage && $remote_contact;
196
197                 $a->page['aside'] .= posted_date_widget(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'], true);
198                 $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], (x($category) ? xmlify($category) : ''));
199                 $a->page['aside'] .= Widget::tagCloud();
200
201                 if (can_write_wall($a->profile['profile_uid'])) {
202                         $x = [
203                                 'is_owner' => $is_owner,
204                                 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
205                                 'default_location' => $is_owner ? $a->user['default-location'] : '',
206                                 'nickname' => $a->profile['nickname'],
207                                 'lockstate' => is_array($a->user)
208                                         && (strlen($a->user['allow_cid'])
209                                                 || strlen($a->user['allow_gid'])
210                                                 || strlen($a->user['deny_cid'])
211                                                 || strlen($a->user['deny_gid'])
212                                         ) ? 'lock' : 'unlock',
213                                 'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',
214                                 'bang' => '',
215                                 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
216                                 'profile_uid' => $a->profile['profile_uid'],
217                         ];
218
219                         $o .= status_editor($a, $x);
220                 }
221         }
222
223
224         // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
225         $sql_extra = item_permissions_sql($a->profile['profile_uid'], $remote_contact, $groups);
226         $sql_extra2 = '';
227
228         if ($update) {
229                 $last_updated = (x($_SESSION['last_updated'], $last_updated_key) ? $_SESSION['last_updated'][$last_updated_key] : 0);
230
231                 // If the page user is the owner of the page we should query for unseen
232                 // items. Otherwise use a timestamp of the last succesful update request.
233                 if ($is_owner || !$last_updated) {
234                         $sql_extra4 = " AND `item`.`unseen`";
235                 } else {
236                         $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
237                         $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
238                 }
239
240                 $r = q("SELECT distinct(parent) AS `item_id`, `item`.`network` AS `item_network`, `item`.`created`
241                         FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
242                         AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
243                         WHERE `item`.`uid` = %d AND `item`.`visible` AND
244                         (NOT `item`.`deleted` OR `item`.`gravity` = %d)
245                         AND NOT `item`.`moderated` AND `item`.`wall`
246                         $sql_extra4
247                         $sql_extra
248                         ORDER BY `item`.`created` DESC",
249                         intval($a->profile['profile_uid']), intval(GRAVITY_ACTIVITY)
250                 );
251
252                 if (!DBM::is_result($r)) {
253                         return '';
254                 }
255         } else {
256                 $sql_post_table = "";
257
258                 if (x($category)) {
259                         $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` ",
260                                 dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
261                 }
262
263                 if (x($hashtags)) {
264                         $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` ",
265                                 dbesc(protect_sprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
266                 }
267
268                 if ($datequery) {
269                         $sql_extra2 .= protect_sprintf(sprintf(" AND `thread`.`created` <= '%s' ", dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
270                 }
271                 if ($datequery2) {
272                         $sql_extra2 .= protect_sprintf(sprintf(" AND `thread`.`created` >= '%s' ", dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
273                 }
274
275                 // Belongs the profile page to a forum?
276                 // If not then we can improve the performance with an additional condition
277                 $r = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `page-flags` IN (%d, %d)",
278                         intval($a->profile['profile_uid']),
279                         intval(PAGE_COMMUNITY),
280                         intval(PAGE_PRVGROUP)
281                 );
282
283                 if (!DBM::is_result($r)) {
284                         $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
285                 } else {
286                         $sql_extra3 = "";
287                 }
288
289                 //  check if we serve a mobile device and get the user settings
290                 //  accordingly
291                 if ($a->is_mobile) {
292                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
293                 } else {
294                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
295                 }
296
297                 //  now that we have the user settings, see if the theme forces
298                 //  a maximum item number which is lower then the user choice
299                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
300                         $itemspage_network = $a->force_max_items;
301                 }
302
303                 $a->set_pager_itemspage($itemspage_network);
304
305                 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
306
307                 $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`
308                         FROM `thread`
309                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
310                         $sql_post_table
311                         STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
312                                 AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
313                         WHERE `thread`.`uid` = %d AND `thread`.`visible`
314                                 AND NOT `thread`.`deleted`
315                                 AND NOT `thread`.`moderated`
316                                 AND `thread`.`wall`
317                                 $sql_extra3 $sql_extra $sql_extra2
318                         ORDER BY `thread`.`created` DESC $pager_sql",
319                         intval($a->profile['profile_uid'])
320                 );
321         }
322
323         $parents_arr = [];
324         $parents_str = '';
325
326         // Set a time stamp for this page. We will make use of it when we
327         // search for new items (update routine)
328         $_SESSION['last_updated'][$last_updated_key] = time();
329
330         if (DBM::is_result($r)) {
331                 foreach ($r as $rr) {
332                         $parents_arr[] = $rr['item_id'];
333                 }
334
335                 $condition = ['uid' => $a->profile['profile_uid'], 'parent' => $parents_arr];
336                 $result = Item::selectForUser($a->profile['profile_uid'], [], $condition);
337                 $items = conv_sort(Item::inArray($result), 'created');
338         } else {
339                 $items = [];
340         }
341
342         if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
343                 $o .= Profile::getBirthdays();
344                 $o .= Profile::getEventsReminderHTML();
345         }
346
347
348         if ($is_owner) {
349                 $unseen = dba::exists('item', ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
350                 if ($unseen) {
351                         $r = Item::update(['unseen' => false],
352                                         ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
353                 }
354         }
355
356         $o .= conversation($a, $items, 'profile', $update, false, 'commented', local_user());
357
358         if (!$update) {
359                 $o .= alt_pager($a, count($items));
360         }
361
362         return $o;
363 }