]> git.mxchange.org Git - friendica.git/blob - include/identity.php
9e34e061c35d5fae2a49695b9de768bf9895ec08
[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         var_dump($profile);
287
288         // show edit profile to yourself
289         if (!$is_contact && $profile['uid'] == local_user() && feature_enabled(local_user(),'multi_profiles')) {
290                 $profile['edit'] = array(App::get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
291                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
292                                 local_user());
293
294                 $profile['menu'] = array(
295                         'chg_photo' => t('Change profile photo'),
296                         'cr_new' => t('Create New Profile'),
297                         'entries' => array(),
298                 );
299
300                 if (dbm::is_result($r)) {
301
302                         foreach ($r as $rr) {
303                                 $profile['menu']['entries'][] = array(
304                                         'photo' => $rr['thumb'],
305                                         'id' => $rr['id'],
306                                         'alt' => t('Profile Image'),
307                                         'profile_name' => $rr['profile-name'],
308                                         'isdefault' => $rr['is-default'],
309                                         'visibile_to_everybody' =>  t('visible to everybody'),
310                                         'edit_visibility' => t('Edit visibility'),
311
312                                 );
313                         }
314
315
316                 }
317         }
318         if (!$is_contact && $profile['uid'] == local_user() && !feature_enabled(local_user(),'multi_profiles')) {
319                 $profile['edit'] = array(App::get_baseurl(). '/profiles/'.$profile['id'], t('Edit profile'),"", t('Edit profile'));
320                 $profile['menu'] = array(
321                         'chg_photo' => t('Change profile photo'),
322                         'cr_new' => null,
323                         'entries' => array(),
324                 );
325         }
326
327         // Fetch the account type
328         $account_type = account_type($profile);
329
330         if((x($profile,'address') == 1)
331                         || (x($profile,'location') == 1)
332                         || (x($profile,'locality') == 1)
333                         || (x($profile,'region') == 1)
334                         || (x($profile,'postal-code') == 1)
335                         || (x($profile,'country-name') == 1))
336                 $location = t('Location:');
337
338         $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
339
340
341         $marital = ((x($profile,'marital') == 1) ?  t('Status:') : False);
342
343         $homepage = ((x($profile,'homepage') == 1) ?  t('Homepage:') : False);
344
345         $about = ((x($profile,'about') == 1) ?  t('About:') : False);
346
347         $xmpp = ((x($profile,'xmpp') == 1) ?  t('XMPP:') : False);
348
349         if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
350                 $location = $pdesc = $gender = $marital = $homepage = $about = False;
351         }
352
353         $firstname = ((strpos($profile['name'],' '))
354                         ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
355         $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
356
357         if ($profile['guid'] != "")
358                 $diaspora = array(
359                         'guid' => $profile['guid'],
360                         'podloc' => App::get_baseurl(),
361                         'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
362                         'nickname' => $profile['nickname'],
363                         'fullname' => $profile['name'],
364                         'firstname' => $firstname,
365                         'lastname' => $lastname,
366                         'photo300' => App::get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
367                         'photo100' => App::get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
368                         'photo50' => App::get_baseurl() . '/photo/custom/50/'  . $profile['uid'] . '.jpg',
369                 );
370         else
371                 $diaspora = false;
372
373         if (!$block){
374                 $contact_block = contact_block();
375
376                 if(is_array($a->profile) AND !$a->profile['hide-friends']) {
377                         $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
378                                 intval($a->profile['uid']));
379                         if (dbm::is_result($r))
380                                 $updated =  date("c", strtotime($r[0]['updated']));
381
382                         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
383                                 WHERE `uid` = %d
384                                         AND NOT `self` AND NOT `blocked` AND NOT `pending`
385                                         AND NOT `hidden` AND NOT `archive`
386                                         AND `network` IN ('%s', '%s', '%s', '')",
387                                 intval($profile['uid']),
388                                 dbesc(NETWORK_DFRN),
389                                 dbesc(NETWORK_DIASPORA),
390                                 dbesc(NETWORK_OSTATUS)
391                         );
392                         if (dbm::is_result($r))
393                                 $contacts = intval($r[0]['total']);
394                 }
395         }
396
397         $p = array();
398         foreach($profile as $k => $v) {
399                 $k = str_replace('-','_',$k);
400                 $p[$k] = $v;
401         }
402
403         if (isset($p["about"]))
404                 $p["about"] = bbcode($p["about"]);
405
406         if (isset($p["address"]))
407                 $p["address"] = bbcode($p["address"]);
408         else
409                 $p["address"] = bbcode($p["location"]);
410
411         if (isset($p["photo"]))
412                 $p["photo"] = proxy_url($p["photo"], false, PROXY_SIZE_SMALL);
413
414         if($a->theme['template_engine'] === 'internal')
415                 $location = template_escape($location);
416
417         $tpl = get_markup_template('profile_vcard.tpl');
418         $o .= replace_macros($tpl, array(
419                 '$profile' => $p,
420                 '$xmpp' => $xmpp,
421                 '$connect'  => $connect,
422                 '$remoteconnect'  => $remoteconnect,
423                 '$subscribe_feed' => $subscribe_feed,
424                 '$wallmessage' => $wallmessage,
425                 '$wallmessage_link' => $wallmessage_link,
426                 '$account_type' => $account_type,
427                 '$location' => $location,
428                 '$gender'   => $gender,
429 //                      '$pdesc'        => $pdesc,
430                 '$marital'  => $marital,
431                 '$homepage' => $homepage,
432                 '$about' => $about,
433                 '$network' =>  t('Network:'),
434                 '$contacts' => $contacts,
435                 '$updated' => $updated,
436                 '$diaspora' => $diaspora,
437                 '$contact_block' => $contact_block,
438         ));
439
440         $arr = array('profile' => &$profile, 'entry' => &$o);
441
442         call_hooks('profile_sidebar', $arr);
443
444         return $o;
445 }
446
447
448 function get_birthdays() {
449
450         $a = get_app();
451         $o = '';
452
453         if(! local_user() || $a->is_mobile || $a->is_tablet)
454                 return $o;
455
456 //              $mobile_detect = new Mobile_Detect();
457 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
458
459 //              if($is_mobile)
460 //                      return $o;
461
462         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
463         $bd_short = t('F d');
464
465         $cachekey = "get_birthdays:".local_user();
466         $r = Cache::get($cachekey);
467         if (is_null($r)) {
468                 $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
469                                 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
470                                 WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
471                                 ORDER BY `start` ASC ",
472                                 intval(local_user()),
473                                 dbesc(datetime_convert('UTC','UTC','now + 6 days')),
474                                 dbesc(datetime_convert('UTC','UTC','now'))
475                 );
476                 if (dbm::is_result($r)) {
477                         Cache::set($cachekey, $r, CACHE_HOUR);
478                 }
479         }
480         if (dbm::is_result($r)) {
481                 $total = 0;
482                 $now = strtotime('now');
483                 $cids = array();
484
485                 $istoday = false;
486                 foreach ($r as $rr) {
487                         if(strlen($rr['name']))
488                                 $total ++;
489                         if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
490                                 $istoday = true;
491                 }
492                 $classtoday = $istoday ? ' birthday-today ' : '';
493                 if($total) {
494                         foreach($r as &$rr) {
495                                 if(! strlen($rr['name']))
496                                         continue;
497
498                                 // avoid duplicates
499
500                                 if(in_array($rr['cid'],$cids))
501                                         continue;
502                                 $cids[] = $rr['cid'];
503
504                                 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
505                                 $sparkle = '';
506                                 $url = $rr['url'];
507                                 if($rr['network'] === NETWORK_DFRN) {
508                                         $sparkle = " sparkle";
509                                         $url = App::get_baseurl() . '/redir/'  . $rr['cid'];
510                                 }
511
512                                 $rr['link'] = $url;
513                                 $rr['title'] = $rr['name'];
514                                 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ?  ' ' . t('[today]') : '');
515                                 $rr['startime'] = Null;
516                                 $rr['today'] = $today;
517
518                         }
519                 }
520         }
521         $tpl = get_markup_template("birthdays_reminder.tpl");
522         return replace_macros($tpl, array(
523                 '$baseurl' => App::get_baseurl(),
524                 '$classtoday' => $classtoday,
525                 '$count' => $total,
526                 '$event_reminders' => t('Birthday Reminders'),
527                 '$event_title' => t('Birthdays this week:'),
528                 '$events' => $r,
529                 '$lbr' => '{',  // raw brackets mess up if/endif macro processing
530                 '$rbr' => '}'
531
532         ));
533 }
534
535
536 function get_events() {
537
538         require_once('include/bbcode.php');
539
540         $a = get_app();
541
542         if(! local_user() || $a->is_mobile || $a->is_tablet)
543                 return $o;
544
545
546 //              $mobile_detect = new Mobile_Detect();
547 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
548
549 //              if($is_mobile)
550 //                      return $o;
551
552         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
553         $bd_short = t('F d');
554
555         $r = q("SELECT `event`.* FROM `event`
556                         WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
557                         ORDER BY `start` ASC ",
558                         intval(local_user()),
559                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
560                         dbesc(datetime_convert('UTC','UTC','now - 1 days'))
561         );
562
563         if (dbm::is_result($r)) {
564                 $now = strtotime('now');
565                 $istoday = false;
566                 foreach ($r as $rr) {
567                         if(strlen($rr['name']))
568                                 $total ++;
569
570                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
571                         if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
572                                 $istoday = true;
573                 }
574                 $classtoday = (($istoday) ? 'event-today' : '');
575
576                 $skip = 0;
577
578                 foreach($r as &$rr) {
579                         $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
580
581                         if(strlen($title) > 35)
582                                 $title = substr($title,0,32) . '... ';
583
584                         $description = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
585                         if(! $description)
586                                 $description = t('[No description]');
587
588                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
589
590                         if(substr($strt,0,10) < datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
591                                 $skip++;
592                                 continue;
593                         }
594
595                         $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
596
597                         $rr['title'] = $title;
598                         $rr['description'] = $desciption;
599                         $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ?  ' ' . t('[today]') : '');
600                         $rr['startime'] = $strt;
601                         $rr['today'] = $today;
602                 }
603         }
604
605         $tpl = get_markup_template("events_reminder.tpl");
606         return replace_macros($tpl, array(
607                 '$baseurl' => App::get_baseurl(),
608                 '$classtoday' => $classtoday,
609                 '$count' => count($r) - $skip,
610                 '$event_reminders' => t('Event Reminders'),
611                 '$event_title' => t('Events this week:'),
612                 '$events' => $r,
613         ));
614 }
615
616 function advanced_profile(App $a) {
617
618         $o = '';
619         $uid = $a->profile['uid'];
620
621         $o .= replace_macros(get_markup_template('section_title.tpl'),array(
622                 '$title' => t('Profile')
623         ));
624
625         if($a->profile['name']) {
626
627                 $tpl = get_markup_template('profile_advanced.tpl');
628
629                 $profile = array();
630
631                 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
632
633                 if($a->profile['gender']) $profile['gender'] = array( t('Gender:'),  $a->profile['gender'] );
634
635
636                 if(($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
637
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         if((! strpos($s,'/profile/')) && (! $force))
889                 return $s;
890         if($force && substr($s,-1,1) !== '/')
891                 $s = $s . '/';
892         $achar = strpos($s,'?') ? '&' : '?';
893         $mine = get_my_url();
894         if($mine and ! link_compare($mine,$s))
895                 return $s . $achar . 'zrl=' . urlencode($mine);
896         return $s;
897 }
898
899 /**
900  * @brief Get the user ID of the page owner
901  *
902  * Used from within PCSS themes to set theme parameters. If there's a
903  * puid request variable, that is the "page owner" and normally their theme
904  * settings take precedence; unless a local user sets the "always_my_theme"
905  * system pconfig, which means they don't want to see anybody else's theme
906  * settings except their own while on this site.
907  *
908  * @return int user ID
909  *
910  * @note Returns local_user instead of user ID if "always_my_theme"
911  *      is set to true
912  */
913 function get_theme_uid() {
914         $uid = (($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
915         if(local_user()) {
916                 if((get_pconfig(local_user(),'system','always_my_theme')) || (! $uid))
917                         return local_user();
918         }
919
920         return $uid;
921 }