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