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