]> git.mxchange.org Git - friendica.git/blob - include/identity.php
Merge remote-tracking branch 'upstream/develop' into 1701-performance
[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         if((! is_array($profile)) && (! count($profile)))
206                 return $o;
207
208         $profile['picdate'] = urlencode($profile['picdate']);
209
210         if (($profile['network'] != "") AND ($profile['network'] != NETWORK_DFRN)) {
211                 $profile['network_name'] = format_network_name($profile['network'],$profile['url']);
212         } else
213                 $profile['network_name'] = "";
214
215         call_hooks('profile_sidebar_enter', $profile);
216
217
218         // don't show connect link to yourself
219         $connect = (($profile['uid'] != local_user()) ? t('Connect')  : False);
220
221         // don't show connect link to authenticated visitors either
222         if(remote_user() && count($_SESSION['remote'])) {
223                 foreach($_SESSION['remote'] as $visitor) {
224                         if($visitor['uid'] == $profile['uid']) {
225                                 $connect = false;
226                                 break;
227                         }
228                 }
229         }
230
231         // Is the local user already connected to that user?
232         if ($connect AND local_user()) {
233                 if (isset($profile["url"])) {
234                         $profile_url = normalise_link($profile["url"]);
235                 } else {
236                         $profile_url = normalise_link(App::get_baseurl()."/profile/".$profile["nickname"]);
237                 }
238
239                 $r = q("SELECT * FROM `contact` WHERE NOT `pending` AND `uid` = %d AND `nurl` = '%s'",
240                         local_user(), $profile_url);
241
242                 if (dbm::is_result($r))
243                         $connect = false;
244         }
245
246         if ($connect AND ($profile['network'] != NETWORK_DFRN) AND !isset($profile['remoteconnect']))
247                 $connect = false;
248
249         $remoteconnect = NULL;
250         if (isset($profile['remoteconnect']))
251                 $remoteconnect = $profile['remoteconnect'];
252
253         if ($connect AND ($profile['network'] == NETWORK_DFRN) AND !isset($remoteconnect))
254                 $subscribe_feed = t("Atom feed");
255         else
256                 $subscribe_feed = false;
257
258         if (remote_user() OR (get_my_url() && $profile['unkmail'] && ($profile['uid'] != local_user()))) {
259                 $wallmessage = t('Message');
260                 $wallmessage_link = "wallmessage/".$profile["nickname"];
261
262                 if (remote_user()) {
263                         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
264                                 intval($profile['uid']),
265                                 intval(remote_user()),
266                                 intval(CONTACT_IS_FRIEND));
267                 } else {
268                         $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
269                                 intval($profile['uid']),
270                                 dbesc(normalise_link(get_my_url())),
271                                 intval(CONTACT_IS_FRIEND));
272                 }
273                 if ($r) {
274                         $remote_url = $r[0]["url"];
275                         $message_path = preg_replace("=(.*)/profile/(.*)=ism", "$1/message/new/", $remote_url);
276                         $wallmessage_link = $message_path.base64_encode($profile["addr"]);
277                 }
278         } else {
279                 $wallmessage = false;
280                 $wallmessage_link = false;
281         }
282
283         // show edit profile to yourself
284         if ($profile['uid'] == local_user() && feature_enabled(local_user(),'multi_profiles')) {
285                 $profile['edit'] = array(App::get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
286                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
287                                 local_user());
288
289                 $profile['menu'] = array(
290                         'chg_photo' => t('Change profile photo'),
291                         'cr_new' => t('Create New Profile'),
292                         'entries' => array(),
293                 );
294
295                 if (dbm::is_result($r)) {
296
297                         foreach ($r as $rr) {
298                                 $profile['menu']['entries'][] = array(
299                                         'photo' => $rr['thumb'],
300                                         'id' => $rr['id'],
301                                         'alt' => t('Profile Image'),
302                                         'profile_name' => $rr['profile-name'],
303                                         'isdefault' => $rr['is-default'],
304                                         'visibile_to_everybody' =>  t('visible to everybody'),
305                                         'edit_visibility' => t('Edit visibility'),
306
307                                 );
308                         }
309
310
311                 }
312         }
313         if ($profile['uid'] == local_user() && !feature_enabled(local_user(),'multi_profiles')) {
314                 $profile['edit'] = array(App::get_baseurl(). '/profiles/'.$profile['id'], t('Edit profile'),"", t('Edit profile'));
315                 $profile['menu'] = array(
316                         'chg_photo' => t('Change profile photo'),
317                         'cr_new' => null,
318                         'entries' => array(),
319                 );
320         }
321
322         // Fetch the account type
323         $account_type = account_type($profile);
324
325         if((x($profile,'address') == 1)
326                         || (x($profile,'location') == 1)
327                         || (x($profile,'locality') == 1)
328                         || (x($profile,'region') == 1)
329                         || (x($profile,'postal-code') == 1)
330                         || (x($profile,'country-name') == 1))
331                 $location = t('Location:');
332
333         $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
334
335
336         $marital = ((x($profile,'marital') == 1) ?  t('Status:') : False);
337
338         $homepage = ((x($profile,'homepage') == 1) ?  t('Homepage:') : False);
339
340         $about = ((x($profile,'about') == 1) ?  t('About:') : False);
341
342         $xmpp = ((x($profile,'xmpp') == 1) ?  t('XMPP:') : False);
343
344         if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
345                 $location = $pdesc = $gender = $marital = $homepage = $about = False;
346         }
347
348         $firstname = ((strpos($profile['name'],' '))
349                         ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
350         $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
351
352         if ($profile['guid'] != "")
353                 $diaspora = array(
354                         'guid' => $profile['guid'],
355                         'podloc' => App::get_baseurl(),
356                         'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
357                         'nickname' => $profile['nickname'],
358                         'fullname' => $profile['name'],
359                         'firstname' => $firstname,
360                         'lastname' => $lastname,
361                         'photo300' => App::get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
362                         'photo100' => App::get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
363                         'photo50' => App::get_baseurl() . '/photo/custom/50/'  . $profile['uid'] . '.jpg',
364                 );
365         else
366                 $diaspora = false;
367
368         if (!$block){
369                 $contact_block = contact_block();
370
371                 if(is_array($a->profile) AND !$a->profile['hide-friends']) {
372                         $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
373                                 intval($a->profile['uid']));
374                         if (dbm::is_result($r))
375                                 $updated =  date("c", strtotime($r[0]['updated']));
376
377                         $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `hidden` AND NOT `archive`
378                                         AND `network` IN ('%s', '%s', '%s', '')",
379                                 intval($profile['uid']),
380                                 dbesc(NETWORK_DFRN),
381                                 dbesc(NETWORK_DIASPORA),
382                                 dbesc(NETWORK_OSTATUS)
383                         );
384                         if (dbm::is_result($r))
385                                 $contacts = intval($r[0]['total']);
386                 }
387         }
388
389         $p = array();
390         foreach($profile as $k => $v) {
391                 $k = str_replace('-','_',$k);
392                 $p[$k] = $v;
393         }
394
395         if (isset($p["about"]))
396                 $p["about"] = bbcode($p["about"]);
397
398         if (isset($p["address"]))
399                 $p["address"] = bbcode($p["address"]);
400         else
401                 $p["address"] = bbcode($p["location"]);
402
403         if (isset($p["photo"]))
404                 $p["photo"] = proxy_url($p["photo"], false, PROXY_SIZE_SMALL);
405
406         if($a->theme['template_engine'] === 'internal')
407                 $location = template_escape($location);
408
409         $tpl = get_markup_template('profile_vcard.tpl');
410         $o .= replace_macros($tpl, array(
411                 '$profile' => $p,
412                 '$xmpp' => $xmpp,
413                 '$connect'  => $connect,
414                 '$remoteconnect'  => $remoteconnect,
415                 '$subscribe_feed' => $subscribe_feed,
416                 '$wallmessage' => $wallmessage,
417                 '$wallmessage_link' => $wallmessage_link,
418                 '$account_type' => $account_type,
419                 '$location' => $location,
420                 '$gender'   => $gender,
421 //                      '$pdesc'        => $pdesc,
422                 '$marital'  => $marital,
423                 '$homepage' => $homepage,
424                 '$about' => $about,
425                 '$network' =>  t('Network:'),
426                 '$contacts' => $contacts,
427                 '$updated' => $updated,
428                 '$diaspora' => $diaspora,
429                 '$contact_block' => $contact_block,
430         ));
431
432         $arr = array('profile' => &$profile, 'entry' => &$o);
433
434         call_hooks('profile_sidebar', $arr);
435
436         return $o;
437 }
438
439
440 function get_birthdays() {
441
442         $a = get_app();
443         $o = '';
444
445         if(! local_user() || $a->is_mobile || $a->is_tablet)
446                 return $o;
447
448 //              $mobile_detect = new Mobile_Detect();
449 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
450
451 //              if($is_mobile)
452 //                      return $o;
453
454         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
455         $bd_short = t('F d');
456
457         $cachekey = "get_birthdays:".local_user();
458         $r = Cache::get($cachekey);
459         if (is_null($r)) {
460                 $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
461                                 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
462                                 WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
463                                 ORDER BY `start` ASC ",
464                                 intval(local_user()),
465                                 dbesc(datetime_convert('UTC','UTC','now + 6 days')),
466                                 dbesc(datetime_convert('UTC','UTC','now'))
467                 );
468                 if (dbm::is_result($r)) {
469                         Cache::set($cachekey, $r, CACHE_HOUR);
470                 }
471         }
472         if (dbm::is_result($r)) {
473                 $total = 0;
474                 $now = strtotime('now');
475                 $cids = array();
476
477                 $istoday = false;
478                 foreach ($r as $rr) {
479                         if(strlen($rr['name']))
480                                 $total ++;
481                         if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
482                                 $istoday = true;
483                 }
484                 $classtoday = $istoday ? ' birthday-today ' : '';
485                 if($total) {
486                         foreach($r as &$rr) {
487                                 if(! strlen($rr['name']))
488                                         continue;
489
490                                 // avoid duplicates
491
492                                 if(in_array($rr['cid'],$cids))
493                                         continue;
494                                 $cids[] = $rr['cid'];
495
496                                 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
497                                 $sparkle = '';
498                                 $url = $rr['url'];
499                                 if($rr['network'] === NETWORK_DFRN) {
500                                         $sparkle = " sparkle";
501                                         $url = App::get_baseurl() . '/redir/'  . $rr['cid'];
502                                 }
503
504                                 $rr['link'] = $url;
505                                 $rr['title'] = $rr['name'];
506                                 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ?  ' ' . t('[today]') : '');
507                                 $rr['startime'] = Null;
508                                 $rr['today'] = $today;
509
510                         }
511                 }
512         }
513         $tpl = get_markup_template("birthdays_reminder.tpl");
514         return replace_macros($tpl, array(
515                 '$baseurl' => App::get_baseurl(),
516                 '$classtoday' => $classtoday,
517                 '$count' => $total,
518                 '$event_reminders' => t('Birthday Reminders'),
519                 '$event_title' => t('Birthdays this week:'),
520                 '$events' => $r,
521                 '$lbr' => '{',  // raw brackets mess up if/endif macro processing
522                 '$rbr' => '}'
523
524         ));
525 }
526
527
528 function get_events() {
529
530         require_once('include/bbcode.php');
531
532         $a = get_app();
533
534         if(! local_user() || $a->is_mobile || $a->is_tablet)
535                 return $o;
536
537
538 //              $mobile_detect = new Mobile_Detect();
539 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
540
541 //              if($is_mobile)
542 //                      return $o;
543
544         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
545         $bd_short = t('F d');
546
547         $r = q("SELECT `event`.* FROM `event`
548                         WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
549                         ORDER BY `start` ASC ",
550                         intval(local_user()),
551                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
552                         dbesc(datetime_convert('UTC','UTC','now - 1 days'))
553         );
554
555         if (dbm::is_result($r)) {
556                 $now = strtotime('now');
557                 $istoday = false;
558                 foreach ($r as $rr) {
559                         if(strlen($rr['name']))
560                                 $total ++;
561
562                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
563                         if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
564                                 $istoday = true;
565                 }
566                 $classtoday = (($istoday) ? 'event-today' : '');
567
568                 $skip = 0;
569
570                 foreach($r as &$rr) {
571                         $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
572
573                         if(strlen($title) > 35)
574                                 $title = substr($title,0,32) . '... ';
575
576                         $description = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
577                         if(! $description)
578                                 $description = t('[No description]');
579
580                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
581
582                         if(substr($strt,0,10) < datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
583                                 $skip++;
584                                 continue;
585                         }
586
587                         $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
588
589                         $rr['title'] = $title;
590                         $rr['description'] = $desciption;
591                         $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ?  ' ' . t('[today]') : '');
592                         $rr['startime'] = $strt;
593                         $rr['today'] = $today;
594                 }
595         }
596
597         $tpl = get_markup_template("events_reminder.tpl");
598         return replace_macros($tpl, array(
599                 '$baseurl' => App::get_baseurl(),
600                 '$classtoday' => $classtoday,
601                 '$count' => count($r) - $skip,
602                 '$event_reminders' => t('Event Reminders'),
603                 '$event_title' => t('Events this week:'),
604                 '$events' => $r,
605         ));
606 }
607
608 function advanced_profile(App $a) {
609
610         $o = '';
611         $uid = $a->profile['uid'];
612
613         $o .= replace_macros(get_markup_template('section_title.tpl'),array(
614                 '$title' => t('Profile')
615         ));
616
617         if($a->profile['name']) {
618
619                 $tpl = get_markup_template('profile_advanced.tpl');
620
621                 $profile = array();
622
623                 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
624
625                 if($a->profile['gender']) $profile['gender'] = array( t('Gender:'),  $a->profile['gender'] );
626
627
628                 if(($a->profile['dob']) && ($a->profile['dob'] != '0000-00-00')) {
629
630                         $year_bd_format = t('j F, Y');
631                         $short_bd_format = t('j F');
632
633
634                         $val = ((intval($a->profile['dob']))
635                                 ? day_translate(datetime_convert('UTC','UTC',$a->profile['dob'] . ' 00:00 +00:00',$year_bd_format))
636                                 : day_translate(datetime_convert('UTC','UTC','2001-' . substr($a->profile['dob'],5) . ' 00:00 +00:00',$short_bd_format)));
637
638                         $profile['birthday'] = array( t('Birthday:'), $val);
639
640                 }
641
642                 if($age = age($a->profile['dob'],$a->profile['timezone'],''))  $profile['age'] = array( t('Age:'), $age );
643
644
645                 if($a->profile['marital']) $profile['marital'] = array( t('Status:'), $a->profile['marital']);
646
647                 /// @TODO Maybe use x() here, plus below?
648                 if ($a->profile['with']) {
649                         $profile['marital']['with'] = $a->profile['with'];
650                 }
651
652                 if (strlen($a->profile['howlong']) && $a->profile['howlong'] !== '0000-00-00 00:00:00') {
653                         $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
654                 }
655
656                 if ($a->profile['sexual']) {
657                         $profile['sexual'] = array( t('Sexual Preference:'), $a->profile['sexual'] );
658                 }
659
660                 if ($a->profile['homepage']) {
661                         $profile['homepage'] = array( t('Homepage:'), linkify($a->profile['homepage']) );
662                 }
663
664                 if ($a->profile['hometown']) {
665                         $profile['hometown'] = array( t('Hometown:'), linkify($a->profile['hometown']) );
666                 }
667
668                 if ($a->profile['pub_keywords']) {
669                         $profile['pub_keywords'] = array( t('Tags:'), $a->profile['pub_keywords']);
670                 }
671
672                 if ($a->profile['politic']) {
673                         $profile['politic'] = array( t('Political Views:'), $a->profile['politic']);
674                 }
675
676                 if ($a->profile['religion']) {
677                         $profile['religion'] = array( t('Religion:'), $a->profile['religion']);
678                 }
679
680                 if ($txt = prepare_text($a->profile['about'])) {
681                         $profile['about'] = array( t('About:'), $txt );
682                 }
683
684                 if ($txt = prepare_text($a->profile['interest'])) {
685                         $profile['interest'] = array( t('Hobbies/Interests:'), $txt);
686                 }
687
688                 if ($txt = prepare_text($a->profile['likes'])) {
689                         $profile['likes'] = array( t('Likes:'), $txt);
690                 }
691
692                 if ($txt = prepare_text($a->profile['dislikes'])) {
693                         $profile['dislikes'] = array( t('Dislikes:'), $txt);
694                 }
695
696                 if ($txt = prepare_text($a->profile['contact'])) {
697                         $profile['contact'] = array( t('Contact information and Social Networks:'), $txt);
698                 }
699
700                 if ($txt = prepare_text($a->profile['music'])) {
701                         $profile['music'] = array( t('Musical interests:'), $txt);
702                 }
703
704                 if ($txt = prepare_text($a->profile['book'])) {
705                         $profile['book'] = array( t('Books, literature:'), $txt);
706                 }
707
708                 if ($txt = prepare_text($a->profile['tv'])) {
709                         $profile['tv'] = array( t('Television:'), $txt);
710                 }
711
712                 if ($txt = prepare_text($a->profile['film'])) {
713                         $profile['film'] = array( t('Film/dance/culture/entertainment:'), $txt);
714                 }
715
716                 if ($txt = prepare_text($a->profile['romance'])) {
717                         $profile['romance'] = array( t('Love/Romance:'), $txt);
718                 }
719
720                 if ($txt = prepare_text($a->profile['work'])) {
721                         $profile['work'] = array( t('Work/employment:'), $txt);
722                 }
723
724                 if ($txt = prepare_text($a->profile['education'])) {
725                         $profile['education'] = array( t('School/education:'), $txt );
726                 }
727
728                 //show subcribed forum if it is enabled in the usersettings
729                 if (feature_enabled($uid,'forumlist_profile')) {
730                         $profile['forumlist'] = array( t('Forums:'), ForumManager::profile_advanced($uid));
731                 }
732
733                 if ($a->profile['uid'] == local_user()) {
734                         $profile['edit'] = array(App::get_baseurl(). '/profiles/'.$a->profile['id'], t('Edit profile'),"", t('Edit profile'));
735                 }
736
737                 return replace_macros($tpl, array(
738                         '$title' => t('Profile'),
739                         '$basic' => t('Basic'),
740                         '$advanced' => t('Advanced'),
741                         '$profile' => $profile
742                 ));
743         }
744
745         return '';
746 }
747
748 function profile_tabs($a, $is_owner=False, $nickname=Null){
749         //echo "<pre>"; var_dump($a->user); killme();
750
751         if (is_null($nickname)) {
752                 $nickname  = $a->user['nickname'];
753         }
754
755         if (x($_GET,'tab')) {
756                 $tab = notags(trim($_GET['tab']));
757         }
758
759         $url = App::get_baseurl() . '/profile/' . $nickname;
760
761         $tabs = array(
762                 array(
763                         'label'=>t('Status'),
764                         'url' => $url,
765                         'sel' => ((!isset($tab) && $a->argv[0]=='profile')?'active':''),
766                         'title' => t('Status Messages and Posts'),
767                         'id' => 'status-tab',
768                         'accesskey' => 'm',
769                 ),
770                 array(
771                         'label' => t('Profile'),
772                         'url'   => $url.'/?tab=profile',
773                         'sel'   => ((isset($tab) && $tab=='profile')?'active':''),
774                         'title' => t('Profile Details'),
775                         'id' => 'profile-tab',
776                         'accesskey' => 'r',
777                 ),
778                 array(
779                         'label' => t('Photos'),
780                         'url'   => App::get_baseurl() . '/photos/' . $nickname,
781                         'sel'   => ((!isset($tab) && $a->argv[0]=='photos')?'active':''),
782                         'title' => t('Photo Albums'),
783                         'id' => 'photo-tab',
784                         'accesskey' => 'h',
785                 ),
786                 array(
787                         'label' => t('Videos'),
788                         'url'   => App::get_baseurl() . '/videos/' . $nickname,
789                         'sel'   => ((!isset($tab) && $a->argv[0]=='videos')?'active':''),
790                         'title' => t('Videos'),
791                         'id' => 'video-tab',
792                         'accesskey' => 'v',
793                 ),
794         );
795
796         // the calendar link for the full featured events calendar
797         if ($is_owner && $a->theme_events_in_profile) {
798                         $tabs[] = array(
799                                 'label' => t('Events'),
800                                 'url'   => App::get_baseurl() . '/events',
801                                 'sel'   =>((!isset($tab) && $a->argv[0]=='events')?'active':''),
802                                 'title' => t('Events and Calendar'),
803                                 'id' => 'events-tab',
804                                 'accesskey' => 'e',
805                         );
806         // if the user is not the owner of the calendar we only show a calendar
807         // with the public events of the calendar owner
808         } elseif (! $is_owner) {
809                 $tabs[] = array(
810                                 'label' => t('Events'),
811                                 'url'   => App::get_baseurl() . '/cal/' . $nickname,
812                                 'sel'   =>((!isset($tab) && $a->argv[0]=='cal')?'active':''),
813                                 'title' => t('Events and Calendar'),
814                                 'id' => 'events-tab',
815                                 'accesskey' => 'e',
816                         );
817         }
818
819         if ($is_owner){
820                 $tabs[] = array(
821                         'label' => t('Personal Notes'),
822                         'url'   => App::get_baseurl() . '/notes',
823                         'sel'   =>((!isset($tab) && $a->argv[0]=='notes')?'active':''),
824                         'title' => t('Only You Can See This'),
825                         'id' => 'notes-tab',
826                         'accesskey' => 't',
827                 );
828         }
829
830         if ((! $is_owner) && ((count($a->profile)) || (! $a->profile['hide-friends']))) {
831                 $tabs[] = array(
832                         'label' => t('Contacts'),
833                         'url'   => App::get_baseurl() . '/viewcontacts/' . $nickname,
834                         'sel'   => ((!isset($tab) && $a->argv[0]=='viewcontacts')?'active':''),
835                         'title' => t('Contacts'),
836                         'id' => 'viewcontacts-tab',
837                         'accesskey' => 'k',
838                 );
839         }
840
841         $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
842         call_hooks('profile_tabs', $arr);
843
844         $tpl = get_markup_template('common_tabs.tpl');
845
846         return replace_macros($tpl,array('$tabs' => $arr['tabs']));
847 }
848
849 function get_my_url() {
850         if(x($_SESSION,'my_url'))
851                 return $_SESSION['my_url'];
852         return false;
853 }
854
855 function zrl_init(App $a) {
856         $tmp_str = get_my_url();
857         if(validate_url($tmp_str)) {
858
859                 // Is it a DDoS attempt?
860                 // The check fetches the cached value from gprobe to reduce the load for this system
861                 $urlparts = parse_url($tmp_str);
862
863                 $result = Cache::get("gprobe:".$urlparts["host"]);
864                 if (!is_null($result)) {
865                         if (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
866                                 logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
867                                 return;
868                         }
869                 }
870
871                 proc_run(PRIORITY_LOW, 'include/gprobe.php',bin2hex($tmp_str));
872                 $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
873                 call_hooks('zrl_init',$arr);
874         }
875 }
876
877 function zrl($s,$force = false) {
878         if(! strlen($s))
879                 return $s;
880         if((! strpos($s,'/profile/')) && (! $force))
881                 return $s;
882         if($force && substr($s,-1,1) !== '/')
883                 $s = $s . '/';
884         $achar = strpos($s,'?') ? '&' : '?';
885         $mine = get_my_url();
886         if($mine and ! link_compare($mine,$s))
887                 return $s . $achar . 'zrl=' . urlencode($mine);
888         return $s;
889 }
890
891 /**
892  * @brief Get the user ID of the page owner
893  *
894  * Used from within PCSS themes to set theme parameters. If there's a
895  * puid request variable, that is the "page owner" and normally their theme
896  * settings take precedence; unless a local user sets the "always_my_theme"
897  * system pconfig, which means they don't want to see anybody else's theme
898  * settings except their own while on this site.
899  *
900  * @return int user ID
901  *
902  * @note Returns local_user instead of user ID if "always_my_theme"
903  *      is set to true
904  */
905 function get_theme_uid() {
906         $uid = (($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
907         if(local_user()) {
908                 if((get_pconfig(local_user(),'system','always_my_theme')) || (! $uid))
909                         return local_user();
910         }
911
912         return $uid;
913 }