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