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