]> git.mxchange.org Git - friendica.git/blob - include/identity.php
Standards stuff should make Hypolite happy.
[friendica.git] / include / identity.php
1 <?php
2 /**
3  * @file include/identity.php
4  */
5
6 require_once('include/ForumManager.php');
7 require_once('include/bbcode.php');
8 require_once("mod/proxy.php");
9
10 /**
11  *
12  * @brief Loads a profile into the page sidebar.
13  *
14  * The function requires a writeable copy of the main App structure, and the nickname
15  * of a registered local account.
16  *
17  * If the viewer is an authenticated remote viewer, the profile displayed is the
18  * one that has been configured for his/her viewing in the Contact manager.
19  * Passing a non-zero profile ID can also allow a preview of a selected profile
20  * by the owner.
21  *
22  * Profile information is placed in the App structure for later retrieval.
23  * Honours the owner's chosen theme for display.
24  *
25  * @attention Should only be run in the _init() functions of a module. That ensures that
26  *      the theme is chosen before the _init() function of a theme is run, which will usually
27  *      load a lot of theme-specific content
28  *
29  * @param App $a
30  * @param string $nickname
31  * @param int $profile
32  * @param array $profiledata
33  */
34 function profile_load(&$a, $nickname, $profile = 0, $profiledata = array()) {
35
36         $user = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
37                 dbesc($nickname)
38         );
39
40         if(!$user && count($user) && !count($profiledata)) {
41                 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
42                 notice( t('Requested account is not available.') . EOL );
43                 $a->error = 404;
44                 return;
45         }
46
47         $pdata = get_profiledata_by_nick($nickname, $user[0]['uid'], $profile);
48
49         if(($pdata === false) || (!count($pdata)) && !count($profiledata)) {
50                 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
51                 notice( t('Requested profile is not available.') . EOL );
52                 $a->error = 404;
53                 return;
54         }
55
56         // fetch user tags if this isn't the default profile
57
58         if(!$pdata['is-default']) {
59                 $x = q("SELECT `pub_keywords` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
60                                 intval($pdata['profile_uid'])
61                 );
62                 if($x && count($x))
63                         $pdata['pub_keywords'] = $x[0]['pub_keywords'];
64         }
65
66         $a->profile = $pdata;
67         $a->profile_uid = $pdata['profile_uid'];
68
69         $a->profile['mobile-theme'] = get_pconfig($a->profile['profile_uid'], 'system', 'mobile_theme');
70         $a->profile['network'] = NETWORK_DFRN;
71
72         $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
73
74                 if (!$profiledata  && !get_pconfig(local_user(),'system','always_my_theme'))
75                         $_SESSION['theme'] = $a->profile['theme'];
76
77         $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
78
79         /*
80          * load/reload current theme info
81          */
82
83         $a->set_template_engine(); // reset the template engine to the default in case the user's theme doesn't specify one
84
85         $theme_info_file = "view/theme/".current_theme()."/theme.php";
86         if (file_exists($theme_info_file)){
87                 require_once($theme_info_file);
88         }
89
90         if(! (x($a->page,'aside')))
91                 $a->page['aside'] = '';
92
93         if(local_user() && local_user() == $a->profile['uid'] && $profiledata) {
94                 $a->page['aside'] .= replace_macros(get_markup_template('profile_edlink.tpl'),array(
95                         '$editprofile' => t('Edit profile'),
96                         '$profid' => $a->profile['id']
97                 ));
98         }
99
100         $block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
101
102         /**
103          * @todo
104          * By now, the contact block isn't shown, when a different profile is given
105          * But: When this profile was on the same server, then we could display the contacts
106          */
107         if ($profiledata)
108                 $a->page['aside'] .= profile_sidebar($profiledata, true);
109         else
110                 $a->page['aside'] .= profile_sidebar($a->profile, $block);
111
112         /*if(! $block)
113          $a->page['aside'] .= contact_block();*/
114
115         return;
116 }
117
118
119 /**
120  * @brief Get all profil data of a local user
121  * 
122  * If the viewer is an authenticated remote viewer, the profile displayed is the
123  * one that has been configured for his/her viewing in the Contact manager.
124  * Passing a non-zero profile ID can also allow a preview of a selected profile
125  * by the owner
126  * 
127  * @param string $nickname
128  * @param int $uid
129  * @param int $profile
130  *      ID of the profile
131  * @returns array
132  *      Includes all available profile data
133  */
134 function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0) {
135         if(remote_user() && count($_SESSION['remote'])) {
136                         foreach($_SESSION['remote'] as $visitor) {
137                                 if($visitor['uid'] == $uid) {
138                                         $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
139                                                 intval($visitor['cid'])
140                                         );
141                                         if (dbm::is_result($r))
142                                                 $profile = $r[0]['profile-id'];
143                                         break;
144                                 }
145                         }
146                 }
147
148         $r = null;
149
150         if($profile) {
151                 $profile_int = intval($profile);
152                 $r = q("SELECT `contact`.`id` AS `contact_id`, `profile`.`uid` AS `profile_uid`, `profile`.*,
153                                 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.*
154                         FROM `profile`
155                         INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
156                         INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
157                         WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
158                                 dbesc($nickname),
159                                 intval($profile_int)
160                 );
161         }
162         if (!dbm::is_result($r)) {
163                 $r = q("SELECT `contact`.`id` AS `contact_id`, `profile`.`uid` AS `profile_uid`, `profile`.*,
164                                 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.*
165                         FROM `profile`
166                         INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
167                         INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
168                         WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` LIMIT 1",
169                                 dbesc($nickname)
170                 );
171         }
172
173         return $r[0];
174
175 }
176
177
178 /**
179  * @brief Formats a profile for display in the sidebar.
180  * 
181  * It is very difficult to templatise the HTML completely
182  * because of all the conditional logic.
183  * 
184  * @param array $profile
185  * @param int $block
186  * 
187  * @return HTML string stuitable for sidebar inclusion
188  * 
189  * @note Returns empty string if passed $profile is wrong type or not populated
190  * 
191  * @hooks 'profile_sidebar_enter'
192  *      array $profile - profile data
193  * @hooks 'profile_sidebar'
194  *      array $arr
195  */
196 function profile_sidebar($profile, $block = 0) {
197         $a = get_app();
198
199         $o = '';
200         $location = false;
201         $address = false;
202 //              $pdesc = true;
203
204         if((! is_array($profile)) && (! count($profile)))
205                 return $o;
206
207         $profile['picdate'] = urlencode($profile['picdate']);
208
209         if (($profile['network'] != "") AND ($profile['network'] != NETWORK_DFRN)) {
210                 $profile['network_name'] = format_network_name($profile['network'],$profile['url']);
211         } else
212                 $profile['network_name'] = "";
213
214         call_hooks('profile_sidebar_enter', $profile);
215
216
217         // don't show connect link to yourself
218         $connect = (($profile['uid'] != local_user()) ? t('Connect')  : False);
219
220         // don't show connect link to authenticated visitors either
221         if(remote_user() && count($_SESSION['remote'])) {
222                 foreach($_SESSION['remote'] as $visitor) {
223                         if($visitor['uid'] == $profile['uid']) {
224                                 $connect = false;
225                                 break;
226                         }
227                 }
228         }
229
230         // Is the local user already connected to that user?
231         if ($connect AND local_user()) {
232                 if (isset($profile["url"])) {
233                         $profile_url = normalise_link($profile["url"]);
234                 } else {
235                         $profile_url = normalise_link(App::get_baseurl()."/profile/".$profile["nickname"]);
236                 }
237
238                 $r = q("SELECT * FROM `contact` WHERE NOT `pending` AND `uid` = %d AND `nurl` = '%s'",
239                         local_user(), $profile_url);
240
241                 if (dbm::is_result($r))
242                         $connect = false;
243         }
244
245         if ($connect AND ($profile['network'] != NETWORK_DFRN) AND !isset($profile['remoteconnect']))
246                 $connect = false;
247
248         $remoteconnect = NULL;
249         if (isset($profile['remoteconnect']))
250                 $remoteconnect = $profile['remoteconnect'];
251
252         if ($connect AND ($profile['network'] == NETWORK_DFRN) AND !isset($remoteconnect))
253                 $subscribe_feed = t("Atom feed");
254         else
255                 $subscribe_feed = false;
256
257         if (remote_user() OR (get_my_url() && $profile['unkmail'] && ($profile['uid'] != local_user()))) {
258                 $wallmessage = t('Message');
259                 $wallmessage_link = "wallmessage/".$profile["nickname"];
260
261                 if (remote_user()) {
262                         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
263                                 intval($profile['uid']),
264                                 intval(remote_user()),
265                                 intval(CONTACT_IS_FRIEND));
266                 } else {
267                         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
268                                 intval($profile['uid']),
269                                 dbesc(normalise_link(get_my_url())),
270                                 intval(CONTACT_IS_FRIEND));
271                 }
272                 if ($r) {
273                         $remote_url = $r[0]["url"];
274                         $message_path = preg_replace("=(.*)/profile/(.*)=ism", "$1/message/new/", $remote_url);
275                         $wallmessage_link = $message_path.base64_encode($profile["addr"]);
276                 }
277         } else {
278                 $wallmessage = false;
279                 $wallmessage_link = false;
280         }
281
282         // show edit profile to yourself
283         if ($profile['uid'] == local_user() && feature_enabled(local_user(),'multi_profiles')) {
284                 $profile['edit'] = array(App::get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
285                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
286                                 local_user());
287
288                 $profile['menu'] = array(
289                         'chg_photo' => t('Change profile photo'),
290                         'cr_new' => t('Create New Profile'),
291                         'entries' => array(),
292                 );
293
294                 if (dbm::is_result($r)) {
295
296                         foreach ($r as $rr) {
297                                 $profile['menu']['entries'][] = array(
298                                         'photo' => $rr['thumb'],
299                                         'id' => $rr['id'],
300                                         'alt' => t('Profile Image'),
301                                         'profile_name' => $rr['profile-name'],
302                                         'isdefault' => $rr['is-default'],
303                                         'visibile_to_everybody' =>  t('visible to everybody'),
304                                         'edit_visibility' => t('Edit visibility'),
305
306                                 );
307                         }
308
309
310                 }
311         }
312         if ($profile['uid'] == local_user() && !feature_enabled(local_user(),'multi_profiles')) {
313                 $profile['edit'] = array(App::get_baseurl(). '/profiles/'.$profile['id'], t('Edit profile'),"", t('Edit profile'));
314                 $profile['menu'] = array(
315                         'chg_photo' => t('Change profile photo'),
316                         'cr_new' => null,
317                         'entries' => array(),
318                 );
319         }
320
321         // Fetch the account type
322         $account_type = account_type($profile);
323
324         if((x($profile,'address') == 1)
325                         || (x($profile,'location') == 1)
326                         || (x($profile,'locality') == 1)
327                         || (x($profile,'region') == 1)
328                         || (x($profile,'postal-code') == 1)
329                         || (x($profile,'country-name') == 1))
330                 $location = t('Location:');
331
332         $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
333
334
335         $marital = ((x($profile,'marital') == 1) ?  t('Status:') : False);
336
337         $homepage = ((x($profile,'homepage') == 1) ?  t('Homepage:') : False);
338
339         $about = ((x($profile,'about') == 1) ?  t('About:') : False);
340
341         $xmpp = ((x($profile,'xmpp') == 1) ?  t('XMPP:') : False);
342
343         if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
344                 $location = $pdesc = $gender = $marital = $homepage = $about = False;
345         }
346
347         $firstname = ((strpos($profile['name'],' '))
348                         ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
349         $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
350
351         if ($profile['guid'] != "")
352                 $diaspora = array(
353                         'guid' => $profile['guid'],
354                         'podloc' => App::get_baseurl(),
355                         'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
356                         'nickname' => $profile['nickname'],
357                         'fullname' => $profile['name'],
358                         'firstname' => $firstname,
359                         'lastname' => $lastname,
360                         'photo300' => App::get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
361                         'photo100' => App::get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
362                         'photo50' => App::get_baseurl() . '/photo/custom/50/'  . $profile['uid'] . '.jpg',
363                 );
364         else
365                 $diaspora = false;
366
367         if (!$block){
368                 $contact_block = contact_block();
369
370                 if(is_array($a->profile) AND !$a->profile['hide-friends']) {
371                         $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
372                                 intval($a->profile['uid']));
373                         if (dbm::is_result($r))
374                                 $updated =  date("c", strtotime($r[0]['updated']));
375
376                         $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `hidden` AND NOT `archive`
377                                         AND `network` IN ('%s', '%s', '%s', '')",
378                                 intval($profile['uid']),
379                                 dbesc(NETWORK_DFRN),
380                                 dbesc(NETWORK_DIASPORA),
381                                 dbesc(NETWORK_OSTATUS)
382                         );
383                         if (dbm::is_result($r))
384                                 $contacts = intval($r[0]['total']);
385                 }
386         }
387
388         $p = array();
389         foreach($profile as $k => $v) {
390                 $k = str_replace('-','_',$k);
391                 $p[$k] = $v;
392         }
393
394         if (isset($p["about"]))
395                 $p["about"] = bbcode($p["about"]);
396
397         if (isset($p["address"]))
398                 $p["address"] = bbcode($p["address"]);
399         else
400                 $p["address"] = bbcode($p["location"]);
401
402         if (isset($p["photo"]))
403                 $p["photo"] = proxy_url($p["photo"], false, PROXY_SIZE_SMALL);
404
405         if($a->theme['template_engine'] === 'internal')
406                 $location = template_escape($location);
407
408         $tpl = get_markup_template('profile_vcard.tpl');
409         $o .= replace_macros($tpl, array(
410                 '$profile' => $p,
411                 '$xmpp' => $xmpp,
412                 '$connect'  => $connect,
413                 '$remoteconnect'  => $remoteconnect,
414                 '$subscribe_feed' => $subscribe_feed,
415                 '$wallmessage' => $wallmessage,
416                 '$wallmessage_link' => $wallmessage_link,
417                 '$account_type' => $account_type,
418                 '$location' => $location,
419                 '$gender'   => $gender,
420 //                      '$pdesc'        => $pdesc,
421                 '$marital'  => $marital,
422                 '$homepage' => $homepage,
423                 '$about' => $about,
424                 '$network' =>  t('Network:'),
425                 '$contacts' => $contacts,
426                 '$updated' => $updated,
427                 '$diaspora' => $diaspora,
428                 '$contact_block' => $contact_block,
429         ));
430
431         $arr = array('profile' => &$profile, 'entry' => &$o);
432
433         call_hooks('profile_sidebar', $arr);
434
435         return $o;
436 }
437
438
439 function get_birthdays() {
440
441         $a = get_app();
442         $o = '';
443
444         if(! local_user() || $a->is_mobile || $a->is_tablet)
445                 return $o;
446
447 //              $mobile_detect = new Mobile_Detect();
448 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
449
450 //              if($is_mobile)
451 //                      return $o;
452
453         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
454         $bd_short = t('F d');
455
456         $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
457                         INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
458                         WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
459                         ORDER BY `start` ASC ",
460                         intval(local_user()),
461                         dbesc(datetime_convert('UTC','UTC','now + 6 days')),
462                         dbesc(datetime_convert('UTC','UTC','now'))
463         );
464
465         if (dbm::is_result($r)) {
466                 $total = 0;
467                 $now = strtotime('now');
468                 $cids = array();
469
470                 $istoday = false;
471                 foreach ($r as $rr) {
472                         if(strlen($rr['name']))
473                                 $total ++;
474                         if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
475                                 $istoday = true;
476                 }
477                 $classtoday = $istoday ? ' birthday-today ' : '';
478                 if($total) {
479                         foreach($r as &$rr) {
480                                 if(! strlen($rr['name']))
481                                         continue;
482
483                                 // avoid duplicates
484
485                                 if(in_array($rr['cid'],$cids))
486                                         continue;
487                                 $cids[] = $rr['cid'];
488
489                                 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
490                                 $sparkle = '';
491                                 $url = $rr['url'];
492                                 if($rr['network'] === NETWORK_DFRN) {
493                                         $sparkle = " sparkle";
494                                         $url = App::get_baseurl() . '/redir/'  . $rr['cid'];
495                                 }
496
497                                 $rr['link'] = $url;
498                                 $rr['title'] = $rr['name'];
499                                 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ?  ' ' . t('[today]') : '');
500                                 $rr['startime'] = Null;
501                                 $rr['today'] = $today;
502
503                         }
504                 }
505         }
506         $tpl = get_markup_template("birthdays_reminder.tpl");
507         return replace_macros($tpl, array(
508                 '$baseurl' => App::get_baseurl(),
509                 '$classtoday' => $classtoday,
510                 '$count' => $total,
511                 '$event_reminders' => t('Birthday Reminders'),
512                 '$event_title' => t('Birthdays this week:'),
513                 '$events' => $r,
514                 '$lbr' => '{',  // raw brackets mess up if/endif macro processing
515                 '$rbr' => '}'
516
517         ));
518 }
519
520
521 function get_events() {
522
523         require_once('include/bbcode.php');
524
525         $a = get_app();
526
527         if(! local_user() || $a->is_mobile || $a->is_tablet)
528                 return $o;
529
530
531 //              $mobile_detect = new Mobile_Detect();
532 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
533
534 //              if($is_mobile)
535 //                      return $o;
536
537         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
538         $bd_short = t('F d');
539
540         $r = q("SELECT `event`.* FROM `event`
541                         WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
542                         ORDER BY `start` ASC ",
543                         intval(local_user()),
544                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
545                         dbesc(datetime_convert('UTC','UTC','now - 1 days'))
546         );
547
548         if (dbm::is_result($r)) {
549                 $now = strtotime('now');
550                 $istoday = false;
551                 foreach ($r as $rr) {
552                         if(strlen($rr['name']))
553                                 $total ++;
554
555                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
556                         if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
557                                 $istoday = true;
558                 }
559                 $classtoday = (($istoday) ? 'event-today' : '');
560
561                 $skip = 0;
562
563                 foreach($r as &$rr) {
564                         $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
565
566                         if(strlen($title) > 35)
567                                 $title = substr($title,0,32) . '... ';
568
569                         $description = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
570                         if(! $description)
571                                 $description = t('[No description]');
572
573                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
574
575                         if(substr($strt,0,10) < datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
576                                 $skip++;
577                                 continue;
578                         }
579
580                         $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
581
582                         $rr['title'] = $title;
583                         $rr['description'] = $desciption;
584                         $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ?  ' ' . t('[today]') : '');
585                         $rr['startime'] = $strt;
586                         $rr['today'] = $today;
587                 }
588         }
589
590         $tpl = get_markup_template("events_reminder.tpl");
591         return replace_macros($tpl, array(
592                 '$baseurl' => App::get_baseurl(),
593                 '$classtoday' => $classtoday,
594                 '$count' => count($r) - $skip,
595                 '$event_reminders' => t('Event Reminders'),
596                 '$event_title' => t('Events this week:'),
597                 '$events' => $r,
598         ));
599 }
600
601 function advanced_profile(App &$a) {
602
603         $o = '';
604         $uid = $a->profile['uid'];
605
606         $o .= replace_macros(get_markup_template('section_title.tpl'),array(
607                 '$title' => t('Profile')
608         ));
609
610         if($a->profile['name']) {
611
612                 $tpl = get_markup_template('profile_advanced.tpl');
613
614                 $profile = array();
615
616                 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
617
618                 if($a->profile['gender']) $profile['gender'] = array( t('Gender:'),  $a->profile['gender'] );
619
620
621                 if(($a->profile['dob']) && ($a->profile['dob'] != '0000-00-00')) {
622
623                         $year_bd_format = t('j F, Y');
624                         $short_bd_format = t('j F');
625
626
627                         $val = ((intval($a->profile['dob']))
628                                 ? day_translate(datetime_convert('UTC','UTC',$a->profile['dob'] . ' 00:00 +00:00',$year_bd_format))
629                                 : day_translate(datetime_convert('UTC','UTC','2001-' . substr($a->profile['dob'],5) . ' 00:00 +00:00',$short_bd_format)));
630
631                         $profile['birthday'] = array( t('Birthday:'), $val);
632
633                 }
634
635                 if($age = age($a->profile['dob'],$a->profile['timezone'],''))  $profile['age'] = array( t('Age:'), $age );
636
637
638                 if($a->profile['marital']) $profile['marital'] = array( t('Status:'), $a->profile['marital']);
639
640                 /// @TODO Maybe use x() here, plus below?
641                 if ($a->profile['with']) {
642                         $profile['marital']['with'] = $a->profile['with'];
643                 }
644
645                 if (strlen($a->profile['howlong']) && $a->profile['howlong'] !== '0000-00-00 00:00:00') {
646                         $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
647                 }
648
649                 if ($a->profile['sexual']) {
650                         $profile['sexual'] = array( t('Sexual Preference:'), $a->profile['sexual'] );
651                 }
652
653                 if ($a->profile['homepage']) {
654                         $profile['homepage'] = array( t('Homepage:'), linkify($a->profile['homepage']) );
655                 }
656
657                 if ($a->profile['hometown']) {
658                         $profile['hometown'] = array( t('Hometown:'), linkify($a->profile['hometown']) );
659                 }
660
661                 if ($a->profile['pub_keywords']) {
662                         $profile['pub_keywords'] = array( t('Tags:'), $a->profile['pub_keywords']);
663                 }
664
665                 if ($a->profile['politic']) {
666                         $profile['politic'] = array( t('Political Views:'), $a->profile['politic']);
667                 }
668
669                 if ($a->profile['religion']) {
670                         $profile['religion'] = array( t('Religion:'), $a->profile['religion']);
671                 }
672
673                 if ($txt = prepare_text($a->profile['about'])) {
674                         $profile['about'] = array( t('About:'), $txt );
675                 }
676
677                 if ($txt = prepare_text($a->profile['interest'])) {
678                         $profile['interest'] = array( t('Hobbies/Interests:'), $txt);
679                 }
680
681                 if ($txt = prepare_text($a->profile['likes'])) {
682                         $profile['likes'] = array( t('Likes:'), $txt);
683                 }
684
685                 if ($txt = prepare_text($a->profile['dislikes'])) {
686                         $profile['dislikes'] = array( t('Dislikes:'), $txt);
687                 }
688
689                 if ($txt = prepare_text($a->profile['contact'])) {
690                         $profile['contact'] = array( t('Contact information and Social Networks:'), $txt);
691                 }
692
693                 if ($txt = prepare_text($a->profile['music'])) {
694                         $profile['music'] = array( t('Musical interests:'), $txt);
695                 }
696
697                 if ($txt = prepare_text($a->profile['book'])) {
698                         $profile['book'] = array( t('Books, literature:'), $txt);
699                 }
700
701                 if ($txt = prepare_text($a->profile['tv'])) {
702                         $profile['tv'] = array( t('Television:'), $txt);
703                 }
704
705                 if ($txt = prepare_text($a->profile['film'])) {
706                         $profile['film'] = array( t('Film/dance/culture/entertainment:'), $txt);
707                 }
708
709                 if ($txt = prepare_text($a->profile['romance'])) {
710                         $profile['romance'] = array( t('Love/Romance:'), $txt);
711                 }
712
713                 if ($txt = prepare_text($a->profile['work'])) {
714                         $profile['work'] = array( t('Work/employment:'), $txt);
715                 }
716
717                 if ($txt = prepare_text($a->profile['education'])) {
718                         $profile['education'] = array( t('School/education:'), $txt );
719                 }
720
721                 //show subcribed forum if it is enabled in the usersettings
722                 if (feature_enabled($uid,'forumlist_profile')) {
723                         $profile['forumlist'] = array( t('Forums:'), ForumManager::profile_advanced($uid));
724                 }
725
726                 if ($a->profile['uid'] == local_user()) {
727                         $profile['edit'] = array(App::get_baseurl(). '/profiles/'.$a->profile['id'], t('Edit profile'),"", t('Edit profile'));
728                 }
729
730                 return replace_macros($tpl, array(
731                         '$title' => t('Profile'),
732                         '$basic' => t('Basic'),
733                         '$advanced' => t('Advanced'),
734                         '$profile' => $profile
735                 ));
736         }
737
738         return '';
739 }
740
741 function profile_tabs($a, $is_owner=False, $nickname=Null){
742         //echo "<pre>"; var_dump($a->user); killme();
743
744         if (is_null($nickname)) {
745                 $nickname  = $a->user['nickname'];
746         }
747
748         if (x($_GET,'tab')) {
749                 $tab = notags(trim($_GET['tab']));
750         }
751
752         $url = App::get_baseurl() . '/profile/' . $nickname;
753
754         $tabs = array(
755                 array(
756                         'label'=>t('Status'),
757                         'url' => $url,
758                         'sel' => ((!isset($tab)&&$a->argv[0]=='profile')?'active':''),
759                         'title' => t('Status Messages and Posts'),
760                         'id' => 'status-tab',
761                         'accesskey' => 'm',
762                 ),
763                 array(
764                         'label' => t('Profile'),
765                         'url'   => $url.'/?tab=profile',
766                         'sel'   => ((isset($tab) && $tab=='profile')?'active':''),
767                         'title' => t('Profile Details'),
768                         'id' => 'profile-tab',
769                         'accesskey' => 'r',
770                 ),
771                 array(
772                         'label' => t('Photos'),
773                         'url'   => App::get_baseurl() . '/photos/' . $nickname,
774                         'sel'   => ((!isset($tab)&&$a->argv[0]=='photos')?'active':''),
775                         'title' => t('Photo Albums'),
776                         'id' => 'photo-tab',
777                         'accesskey' => 'h',
778                 ),
779                 array(
780                         'label' => t('Videos'),
781                         'url'   => App::get_baseurl() . '/videos/' . $nickname,
782                         'sel'   => ((!isset($tab)&&$a->argv[0]=='videos')?'active':''),
783                         'title' => t('Videos'),
784                         'id' => 'video-tab',
785                         'accesskey' => 'v',
786                 ),
787         );
788
789         // the calendar link for the full featured events calendar
790         if ($is_owner && $a->theme_events_in_profile) {
791                         $tabs[] = array(
792                                 'label' => t('Events'),
793                                 'url'   => App::get_baseurl() . '/events',
794                                 'sel'   =>((!isset($tab)&&$a->argv[0]=='events')?'active':''),
795                                 'title' => t('Events and Calendar'),
796                                 'id' => 'events-tab',
797                                 'accesskey' => 'e',
798                         );
799         // if the user is not the owner of the calendar we only show a calendar
800         // with the public events of the calendar owner
801         } elseif (! $is_owner) {
802                 $tabs[] = array(
803                                 'label' => t('Events'),
804                                 'url'   => App::get_baseurl() . '/cal/' . $nickname,
805                                 'sel'   =>((!isset($tab)&&$a->argv[0]=='cal')?'active':''),
806                                 'title' => t('Events and Calendar'),
807                                 'id' => 'events-tab',
808                                 'accesskey' => 'e',
809                         );
810         }
811
812         if ($is_owner){
813                 $tabs[] = array(
814                         'label' => t('Personal Notes'),
815                         'url'   => App::get_baseurl() . '/notes',
816                         'sel'   =>((!isset($tab)&&$a->argv[0]=='notes')?'active':''),
817                         'title' => t('Only You Can See This'),
818                         'id' => 'notes-tab',
819                         'accesskey' => 't',
820                 );
821         }
822
823         if ((! $is_owner) && ((count($a->profile)) || (! $a->profile['hide-friends']))) {
824                 $tabs[] = array(
825                         'label' => t('Contacts'),
826                         'url'   => App::get_baseurl() . '/viewcontacts/' . $nickname,
827                         'sel'   => ((!isset($tab)&&$a->argv[0]=='viewcontacts')?'active':''),
828                         'title' => t('Contacts'),
829                         'id' => 'viewcontacts-tab',
830                         'accesskey' => 'k',
831                 );
832         }
833
834         $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
835         call_hooks('profile_tabs', $arr);
836
837         $tpl = get_markup_template('common_tabs.tpl');
838
839         return replace_macros($tpl,array('$tabs' => $arr['tabs']));
840 }
841
842 function get_my_url() {
843         if(x($_SESSION,'my_url'))
844                 return $_SESSION['my_url'];
845         return false;
846 }
847
848 function zrl_init(App &$a) {
849         $tmp_str = get_my_url();
850         if(validate_url($tmp_str)) {
851
852                 // Is it a DDoS attempt?
853                 // The check fetches the cached value from gprobe to reduce the load for this system
854                 $urlparts = parse_url($tmp_str);
855
856                 $result = Cache::get("gprobe:".$urlparts["host"]);
857                 if (!is_null($result)) {
858                         if (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
859                                 logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
860                                 return;
861                         }
862                 }
863
864                 proc_run(PRIORITY_LOW, 'include/gprobe.php',bin2hex($tmp_str));
865                 $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
866                 call_hooks('zrl_init',$arr);
867         }
868 }
869
870 function zrl($s,$force = false) {
871         if(! strlen($s))
872                 return $s;
873         if((! strpos($s,'/profile/')) && (! $force))
874                 return $s;
875         if($force && substr($s,-1,1) !== '/')
876                 $s = $s . '/';
877         $achar = strpos($s,'?') ? '&' : '?';
878         $mine = get_my_url();
879         if($mine and ! link_compare($mine,$s))
880                 return $s . $achar . 'zrl=' . urlencode($mine);
881         return $s;
882 }
883
884 /**
885  * @brief Get the user ID of the page owner
886  *
887  * Used from within PCSS themes to set theme parameters. If there's a
888  * puid request variable, that is the "page owner" and normally their theme
889  * settings take precedence; unless a local user sets the "always_my_theme"
890  * system pconfig, which means they don't want to see anybody else's theme
891  * settings except their own while on this site.
892  *
893  * @return int user ID
894  * 
895  * @note Returns local_user instead of user ID if "always_my_theme"
896  *      is set to true
897  */
898 function get_theme_uid() {
899         $uid = (($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
900         if(local_user()) {
901                 if((get_pconfig(local_user(),'system','always_my_theme')) || (! $uid))
902                         return local_user();
903         }
904
905         return $uid;
906 }