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