]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
Docs: add a note on adding `use` on theme.php
[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() . '/feed/' . $which . '/" title="' . L10n::t('%s\'s posts', $a->profile['username']) . '"/>' . "\r\n";
79         $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";
80         $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";
81         $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->get_hostname() . ($a->path ? '/' . $a->path : ''));
82         $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\r\n";
83         header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
84
85         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
86         foreach ($dfrn_pages as $dfrn) {
87                 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"" . System::baseUrl() . "/dfrn_{$dfrn}/{$which}\" />\r\n";
88         }
89         $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . "/poco/{$which}\" />\r\n";
90 }
91
92 function profile_content(App $a, $update = 0)
93 {
94         $category = $datequery = $datequery2 = '';
95
96         if ($a->argc > 2) {
97                 for ($x = 2; $x < $a->argc; $x ++) {
98                         if (is_a_date_arg($a->argv[$x])) {
99                                 if ($datequery) {
100                                         $datequery2 = escape_tags($a->argv[$x]);
101                                 } else {
102                                         $datequery = escape_tags($a->argv[$x]);
103                                 }
104                         } else {
105                                 $category = $a->argv[$x];
106                         }
107                 }
108         }
109
110         if (!x($category)) {
111                 $category = defaults($_GET, 'category', '');
112         }
113
114         $hashtags = defaults($_GET, 'tag', '');
115
116         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
117                 return Login::form();
118         }
119
120         require_once 'include/security.php';
121         require_once 'include/conversation.php';
122         require_once 'include/items.php';
123
124         $groups = [];
125
126         $tab = 'posts';
127         $o = '';
128
129         if ($update) {
130                 // Ensure we've got a profile owner if updating.
131                 $a->profile['profile_uid'] = $update;
132         } elseif ($a->profile['profile_uid'] == local_user()) {
133                 Nav::setSelected('home');
134         }
135
136         $contact = null;
137         $remote_contact = false;
138
139         $contact_id = 0;
140
141         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
142                 foreach ($_SESSION['remote'] as $v) {
143                         if ($v['uid'] == $a->profile['profile_uid']) {
144                                 $contact_id = $v['cid'];
145                                 break;
146                         }
147                 }
148         }
149
150         if ($contact_id) {
151                 $groups = Group::getIdsByContactId($contact_id);
152                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
153                         intval($contact_id),
154                         intval($a->profile['profile_uid'])
155                 );
156                 if (DBM::is_result($r)) {
157                         $contact = $r[0];
158                         $remote_contact = true;
159                 }
160         }
161
162         if (!$remote_contact) {
163                 if (local_user()) {
164                         $contact_id = $_SESSION['cid'];
165                         $contact = $a->contact;
166                 }
167         }
168
169         $is_owner = local_user() == $a->profile['profile_uid'];
170         $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
171
172         if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) {
173                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
174                 return;
175         }
176
177         if (!$update) {
178                 $tab = false;
179                 if (x($_GET, 'tab')) {
180                         $tab = notags(trim($_GET['tab']));
181                 }
182
183                 $o .= Profile::getTabs($a, $is_owner, $a->profile['nickname']);
184
185                 if ($tab === 'profile') {
186                         $o .= Profile::getAdvanced($a);
187                         Addon::callHooks('profile_advanced', $o);
188                         return $o;
189                 }
190
191                 $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);
192
193                 if (x($_SESSION, 'new_member') && $is_owner) {
194                         $o .= '<div id="newmember-tips"><a href="newmember"><b>' . L10n::t('Tips for New Members') . '</b></a></div>';
195                 }
196
197                 $commpage = $a->profile['page-flags'] == PAGE_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'], (x($category) ? xmlify($category) : ''));
202                 $a->page['aside'] .= Widget::tagCloud();
203
204                 if (can_write_wall($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
227         // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
228         $sql_extra = item_permissions_sql($a->profile['profile_uid'], $remote_contact, $groups);
229         $sql_extra2 = '';
230
231         if ($update) {
232                 $last_updated = (x($_SESSION['last_updated'], $last_updated_key) ? $_SESSION['last_updated'][$last_updated_key] : 0);
233
234                 // If the page user is the owner of the page we should query for unseen
235                 // items. Otherwise use a timestamp of the last succesful update request.
236                 if ($is_owner || !$last_updated) {
237                         $sql_extra4 = " AND `item`.`unseen`";
238                 } else {
239                         $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
240                         $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";
241                 }
242
243                 $r = q("SELECT distinct(parent) AS `item_id`, `item`.`network` AS `item_network`, `item`.`created`
244                         FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
245                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
246                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND
247                         (`item`.`deleted` = 0 OR item.verb = '" . ACTIVITY_LIKE . "'
248                         OR item.verb = '" . ACTIVITY_DISLIKE . "' OR item.verb = '" . ACTIVITY_ATTEND . "'
249                         OR item.verb = '" . ACTIVITY_ATTENDNO . "' OR item.verb = '" . ACTIVITY_ATTENDMAYBE . "')
250                         AND `item`.`moderated` = 0
251                         AND `item`.`wall` = 1
252                         $sql_extra4
253                         $sql_extra
254                         ORDER BY `item`.`created` DESC",
255                         intval($a->profile['profile_uid'])
256                 );
257
258                 if (!DBM::is_result($r)) {
259                         return '';
260                 }
261         } else {
262                 $sql_post_table = "";
263
264                 if (x($category)) {
265                         $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` ",
266                                 dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));
267                 }
268
269                 if (x($hashtags)) {
270                         $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` ",
271                                 dbesc(protect_sprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));
272                 }
273
274                 if ($datequery) {
275                         $sql_extra2 .= protect_sprintf(sprintf(" AND `thread`.`created` <= '%s' ", dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
276                 }
277                 if ($datequery2) {
278                         $sql_extra2 .= protect_sprintf(sprintf(" AND `thread`.`created` >= '%s' ", dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
279                 }
280
281                 // Belongs the profile page to a forum?
282                 // If not then we can improve the performance with an additional condition
283                 $r = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `page-flags` IN (%d, %d)",
284                         intval($a->profile['profile_uid']),
285                         intval(PAGE_COMMUNITY),
286                         intval(PAGE_PRVGROUP)
287                 );
288
289                 if (!DBM::is_result($r)) {
290                         $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));
291                 }
292
293                 //  check if we serve a mobile device and get the user settings
294                 //  accordingly
295                 if ($a->is_mobile) {
296                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);
297                 } else {
298                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);
299                 }
300
301                 //  now that we have the user settings, see if the theme forces
302                 //  a maximum item number which is lower then the user choice
303                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
304                         $itemspage_network = $a->force_max_items;
305                 }
306
307                 $a->set_pager_itemspage($itemspage_network);
308
309                 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
310
311                 $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`
312                         FROM `thread`
313                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
314                         $sql_post_table
315                         STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
316                                 AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
317                         WHERE `thread`.`uid` = %d AND `thread`.`visible`
318                                 AND NOT `thread`.`deleted`
319                                 AND NOT `thread`.`moderated`
320                                 AND `thread`.`wall`
321                                 $sql_extra3 $sql_extra $sql_extra2
322                         ORDER BY `thread`.`created` DESC $pager_sql",
323                         intval($a->profile['profile_uid'])
324                 );
325         }
326
327         $parents_arr = [];
328         $parents_str = '';
329
330         // Set a time stamp for this page. We will make use of it when we
331         // search for new items (update routine)
332         $_SESSION['last_updated'][$last_updated_key] = time();
333
334         if (DBM::is_result($r)) {
335                 foreach ($r as $rr) {
336                         $parents_arr[] = $rr['item_id'];
337                 }
338
339                 $parents_str = implode(', ', $parents_arr);
340
341                 $items = q(item_query() . " AND `item`.`uid` = %d
342                         AND `item`.`parent` IN (%s)
343                         $sql_extra ",
344                         intval($a->profile['profile_uid']),
345                         dbesc($parents_str)
346                 );
347
348                 $items = conv_sort($items, 'created');
349         } else {
350                 $items = [];
351         }
352
353         if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {
354                 $o .= Profile::getBirthdays();
355                 $o .= Profile::getEventsReminderHTML();
356         }
357
358
359         if ($is_owner) {
360                 $unseen = dba::exists('item', ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
361                 if ($unseen) {
362                         $r = Item::update(['unseen' => false],
363                                         ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
364                 }
365         }
366
367         $o .= conversation($a, $items, 'profile', $update);
368
369         if (!$update) {
370                 $o .= alt_pager($a, count($items));
371         }
372
373         return $o;
374 }