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