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