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