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