]> git.mxchange.org Git - friendica.git/blob - include/identity.php
is empty check
[friendica.git] / include / identity.php
1 <?php
2 /**
3  * @file include/identity.php
4  */
5
6 use Friendica\App;
7
8 require_once 'include/ForumManager.php';
9 require_once 'include/bbcode.php';
10 require_once 'mod/proxy.php';
11 require_once 'include/cache.php';
12
13 /**
14  *
15  * @brief Loads a profile into the page sidebar.
16  *
17  * The function requires a writeable copy of the main App structure, and the nickname
18  * of a registered local account.
19  *
20  * If the viewer is an authenticated remote viewer, the profile displayed is the
21  * one that has been configured for his/her viewing in the Contact manager.
22  * Passing a non-zero profile ID can also allow a preview of a selected profile
23  * by the owner.
24  *
25  * Profile information is placed in the App structure for later retrieval.
26  * Honours the owner's chosen theme for display.
27  *
28  * @attention Should only be run in the _init() functions of a module. That ensures that
29  *      the theme is chosen before the _init() function of a theme is run, which will usually
30  *      load a lot of theme-specific content
31  *
32  * @param App $a
33  * @param string $nickname
34  * @param int $profile
35  * @param array $profiledata
36  */
37 function profile_load(App $a, $nickname, $profile = 0, $profiledata = array()) {
38
39         $user = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
40                 dbesc($nickname)
41         );
42
43         if (!$user && count($user) && !count($profiledata)) {
44                 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
45                 notice( t('Requested account is not available.') . EOL );
46                 $a->error = 404;
47                 return;
48         }
49
50         $pdata = get_profiledata_by_nick($nickname, $user[0]['uid'], $profile);
51
52         if (($pdata === false) || (!count($pdata)) && !count($profiledata)) {
53                 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
54                 notice( t('Requested profile is not available.') . EOL );
55                 $a->error = 404;
56                 return;
57         }
58
59         // fetch user tags if this isn't the default profile
60
61         if (!$pdata['is-default']) {
62                 $x = q("SELECT `pub_keywords` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
63                                 intval($pdata['profile_uid'])
64                 );
65                 if ($x && count($x))
66                         $pdata['pub_keywords'] = $x[0]['pub_keywords'];
67         }
68
69         $a->profile = $pdata;
70         $a->profile_uid = $pdata['profile_uid'];
71
72         $a->profile['mobile-theme'] = get_pconfig($a->profile['profile_uid'], 'system', 'mobile_theme');
73         $a->profile['network'] = NETWORK_DFRN;
74
75         $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
76
77                 if (!$profiledata  && !get_pconfig(local_user(),'system','always_my_theme'))
78                         $_SESSION['theme'] = $a->profile['theme'];
79
80         $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
81
82         /*
83          * load/reload current theme info
84          */
85
86         $a->set_template_engine(); // reset the template engine to the default in case the user's theme doesn't specify one
87
88         $theme_info_file = "view/theme/" . current_theme() . "/theme.php";
89         if (file_exists($theme_info_file)) {
90                 require_once $theme_info_file;
91         }
92
93         if (! (x($a->page,'aside')))
94                 $a->page['aside'] = '';
95
96         if (local_user() && local_user() == $a->profile['uid'] && $profiledata) {
97                 $a->page['aside'] .= replace_macros(get_markup_template('profile_edlink.tpl'),array(
98                         '$editprofile' => t('Edit profile'),
99                         '$profid' => $a->profile['id']
100                 ));
101         }
102
103         $block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
104
105         /**
106          * @todo
107          * By now, the contact block isn't shown, when a different profile is given
108          * But: When this profile was on the same server, then we could display the contacts
109          */
110         if ($profiledata)
111                 $a->page['aside'] .= profile_sidebar($profiledata, true);
112         else
113                 $a->page['aside'] .= profile_sidebar($a->profile, $block);
114
115         /*if (! $block)
116          $a->page['aside'] .= contact_block();*/
117
118         return;
119 }
120
121
122 /**
123  * @brief Get all profil data of a local user
124  *
125  * If the viewer is an authenticated remote viewer, the profile displayed is the
126  * one that has been configured for his/her viewing in the Contact manager.
127  * Passing a non-zero profile ID can also allow a preview of a selected profile
128  * by the owner
129  *
130  * @param string $nickname
131  * @param int $uid
132  * @param int $profile
133  *      ID of the profile
134  * @returns array
135  *      Includes all available profile data
136  */
137 function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0) {
138         if (remote_user() && count($_SESSION['remote'])) {
139                         foreach ($_SESSION['remote'] as $visitor) {
140                                 if ($visitor['uid'] == $uid) {
141                                         $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
142                                                 intval($visitor['cid'])
143                                         );
144                                         if (dbm::is_result($r))
145                                                 $profile = $r[0]['profile-id'];
146                                         break;
147                                 }
148                         }
149                 }
150
151         $r = null;
152
153         if ($profile) {
154                 $profile_int = intval($profile);
155                 $r = q("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` = '%s' AND `profile`.`id` = %d LIMIT 1",
163                                 dbesc($nickname),
164                                 intval($profile_int)
165                 );
166         }
167         if (!dbm::is_result($r)) {
168                 $r = q("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` = '%s' AND `profile`.`is-default` LIMIT 1",
176                                 dbesc($nickname)
177                 );
178         }
179
180         return $r[0];
181
182 }
183
184
185 /**
186  * @brief Formats a profile for display in the sidebar.
187  *
188  * It is very difficult to templatise the HTML completely
189  * because of all the conditional logic.
190  *
191  * @param array $profile
192  * @param int $block
193  *
194  * @return HTML string stuitable for sidebar inclusion
195  *
196  * @note Returns empty string if passed $profile is wrong type or not populated
197  *
198  * @hooks 'profile_sidebar_enter'
199  *      array $profile - profile data
200  * @hooks 'profile_sidebar'
201  *      array $arr
202  */
203 function profile_sidebar($profile, $block = 0) {
204         $a = get_app();
205
206         $o = '';
207         $location = false;
208         $address = false;
209         // $pdesc = true;
210
211         // This function can also use contact information in $profile
212         $is_contact = x($profile, 'cid');
213
214         if ((! is_array($profile)) && (! count($profile))) {
215                 return $o;
216         }
217
218         $profile['picdate'] = urlencode($profile['picdate']);
219
220         if (($profile['network'] != "") && ($profile['network'] != NETWORK_DFRN)) {
221                 $profile['network_name'] = format_network_name($profile['network'], $profile['url']);
222         } else {
223                 $profile['network_name'] = "";
224         }
225
226         call_hooks('profile_sidebar_enter', $profile);
227
228
229         // don't show connect link to yourself
230         $connect = (($profile['uid'] != local_user()) ? t('Connect')  : False);
231
232         // don't show connect link to authenticated visitors either
233         if (remote_user() && count($_SESSION['remote'])) {
234                 foreach ($_SESSION['remote'] as $visitor) {
235                         if ($visitor['uid'] == $profile['uid']) {
236                                 $connect = false;
237                                 break;
238                         }
239                 }
240         }
241
242         // Is the local user already connected to that user?
243         if ($connect && local_user()) {
244                 if (isset($profile["url"])) {
245                         $profile_url = normalise_link($profile["url"]);
246                 } else {
247                         $profile_url = normalise_link(App::get_baseurl()."/profile/".$profile["nickname"]);
248                 }
249
250                 $r = q("SELECT * FROM `contact` WHERE NOT `pending` AND `uid` = %d AND `nurl` = '%s'",
251                         local_user(), $profile_url);
252
253                 if (dbm::is_result($r))
254                         $connect = false;
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(App::get_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(App::get_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' => App::get_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                 $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
475                                 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
476                                 WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
477                                 ORDER BY `start` ASC ",
478                                 intval(local_user()),
479                                 dbesc(datetime_convert('UTC','UTC','now + 6 days')),
480                                 dbesc(datetime_convert('UTC','UTC','now'))
481                 );
482                 if (dbm::is_result($r)) {
483                         Cache::set($cachekey, $r, CACHE_HOUR);
484                 }
485         }
486         if (dbm::is_result($r)) {
487                 $total = 0;
488                 $now = strtotime('now');
489                 $cids = array();
490
491                 $istoday = false;
492                 foreach ($r as $rr) {
493                         if (strlen($rr['name']))
494                                 $total ++;
495                         if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
496                                 $istoday = true;
497                 }
498                 $classtoday = $istoday ? ' birthday-today ' : '';
499                 if ($total) {
500                         foreach ($r as &$rr) {
501                                 if (! strlen($rr['name']))
502                                         continue;
503
504                                 // avoid duplicates
505
506                                 if (in_array($rr['cid'],$cids))
507                                         continue;
508                                 $cids[] = $rr['cid'];
509
510                                 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
511                                 $sparkle = '';
512                                 $url = $rr['url'];
513                                 if ($rr['network'] === NETWORK_DFRN) {
514                                         $sparkle = " sparkle";
515                                         $url = App::get_baseurl() . '/redir/'  . $rr['cid'];
516                                 }
517
518                                 $rr['link'] = $url;
519                                 $rr['title'] = $rr['name'];
520                                 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ?  ' ' . t('[today]') : '');
521                                 $rr['startime'] = Null;
522                                 $rr['today'] = $today;
523
524                         }
525                 }
526         }
527         $tpl = get_markup_template("birthdays_reminder.tpl");
528         return replace_macros($tpl, array(
529                 '$baseurl' => App::get_baseurl(),
530                 '$classtoday' => $classtoday,
531                 '$count' => $total,
532                 '$event_reminders' => t('Birthday Reminders'),
533                 '$event_title' => t('Birthdays this week:'),
534                 '$events' => $r,
535                 '$lbr' => '{',  // raw brackets mess up if/endif macro processing
536                 '$rbr' => '}'
537
538         ));
539 }
540
541
542 function get_events() {
543
544         require_once 'include/bbcode.php';
545
546         $a = get_app();
547
548         if (! local_user() || $a->is_mobile || $a->is_tablet) {
549                 return $o;
550         }
551
552 //              $mobile_detect = new Mobile_Detect();
553 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
554
555 //              if ($is_mobile)
556 //                      return $o;
557
558         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
559         $bd_short = t('F d');
560
561         $r = q("SELECT `event`.* FROM `event`
562                         WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
563                         ORDER BY `start` ASC ",
564                         intval(local_user()),
565                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
566                         dbesc(datetime_convert('UTC','UTC','now - 1 days'))
567         );
568
569         if (dbm::is_result($r)) {
570                 $now = strtotime('now');
571                 $istoday = false;
572                 foreach ($r as $rr) {
573                         if (strlen($rr['name'])) {
574                                 $total ++;
575                         }
576
577                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
578                         if ($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
579                                 $istoday = true;
580                         }
581                 }
582                 $classtoday = (($istoday) ? 'event-today' : '');
583
584                 $skip = 0;
585
586                 foreach ($r as &$rr) {
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                                 $skip++;
602                                 continue;
603                         }
604
605                         $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
606
607                         $rr['title'] = $title;
608                         $rr['description'] = $desciption;
609                         $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ?  ' ' . t('[today]') : '');
610                         $rr['startime'] = $strt;
611                         $rr['today'] = $today;
612                 }
613         }
614
615         $tpl = get_markup_template("events_reminder.tpl");
616         return replace_macros($tpl, array(
617                 '$baseurl' => App::get_baseurl(),
618                 '$classtoday' => $classtoday,
619                 '$count' => count($r) - $skip,
620                 '$event_reminders' => t('Event Reminders'),
621                 '$event_title' => t('Events this week:'),
622                 '$events' => $r,
623         ));
624 }
625
626 function advanced_profile(App $a) {
627
628         $o = '';
629         $uid = $a->profile['uid'];
630
631         $o .= replace_macros(get_markup_template('section_title.tpl'),array(
632                 '$title' => t('Profile')
633         ));
634
635         if ($a->profile['name']) {
636
637                 $tpl = get_markup_template('profile_advanced.tpl');
638
639                 $profile = array();
640
641                 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
642
643                 if ($a->profile['gender']) {
644                         $profile['gender'] = array( t('Gender:'),  $a->profile['gender'] );
645                 }
646
647                 if (($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
648                         $year_bd_format = t('j F, Y');
649                         $short_bd_format = t('j F');
650
651
652                         $val = ((intval($a->profile['dob']))
653                                 ? day_translate(datetime_convert('UTC','UTC',$a->profile['dob'] . ' 00:00 +00:00',$year_bd_format))
654                                 : day_translate(datetime_convert('UTC','UTC','2001-' . substr($a->profile['dob'],5) . ' 00:00 +00:00',$short_bd_format)));
655
656                         $profile['birthday'] = array( t('Birthday:'), $val);
657
658                 }
659                 if (!empty($a->profile['dob'])
660                         && $a->profile['dob'] > '0001-01-01'
661                         && $age = age($a->profile['dob'], $a->profile['timezone'], '')
662                 ) {
663                         $profile['age'] = array( t('Age:'), $age );
664                 }
665
666                 if ($a->profile['marital']) {
667                         $profile['marital'] = array( t('Status:'), $a->profile['marital']);
668                 }
669
670                 /// @TODO Maybe use x() here, plus below?
671                 if ($a->profile['with']) {
672                         $profile['marital']['with'] = $a->profile['with'];
673                 }
674
675                 if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= NULL_DATE) {
676                         $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
677                 }
678
679                 if ($a->profile['sexual']) {
680                         $profile['sexual'] = array( t('Sexual Preference:'), $a->profile['sexual'] );
681                 }
682
683                 if ($a->profile['homepage']) {
684                         $profile['homepage'] = array( t('Homepage:'), linkify($a->profile['homepage']) );
685                 }
686
687                 if ($a->profile['hometown']) {
688                         $profile['hometown'] = array( t('Hometown:'), linkify($a->profile['hometown']) );
689                 }
690
691                 if ($a->profile['pub_keywords']) {
692                         $profile['pub_keywords'] = array( t('Tags:'), $a->profile['pub_keywords']);
693                 }
694
695                 if ($a->profile['politic']) {
696                         $profile['politic'] = array( t('Political Views:'), $a->profile['politic']);
697                 }
698
699                 if ($a->profile['religion']) {
700                         $profile['religion'] = array( t('Religion:'), $a->profile['religion']);
701                 }
702
703                 if ($txt = prepare_text($a->profile['about'])) {
704                         $profile['about'] = array( t('About:'), $txt );
705                 }
706
707                 if ($txt = prepare_text($a->profile['interest'])) {
708                         $profile['interest'] = array( t('Hobbies/Interests:'), $txt);
709                 }
710
711                 if ($txt = prepare_text($a->profile['likes'])) {
712                         $profile['likes'] = array( t('Likes:'), $txt);
713                 }
714
715                 if ($txt = prepare_text($a->profile['dislikes'])) {
716                         $profile['dislikes'] = array( t('Dislikes:'), $txt);
717                 }
718
719                 if ($txt = prepare_text($a->profile['contact'])) {
720                         $profile['contact'] = array( t('Contact information and Social Networks:'), $txt);
721                 }
722
723                 if ($txt = prepare_text($a->profile['music'])) {
724                         $profile['music'] = array( t('Musical interests:'), $txt);
725                 }
726
727                 if ($txt = prepare_text($a->profile['book'])) {
728                         $profile['book'] = array( t('Books, literature:'), $txt);
729                 }
730
731                 if ($txt = prepare_text($a->profile['tv'])) {
732                         $profile['tv'] = array( t('Television:'), $txt);
733                 }
734
735                 if ($txt = prepare_text($a->profile['film'])) {
736                         $profile['film'] = array( t('Film/dance/culture/entertainment:'), $txt);
737                 }
738
739                 if ($txt = prepare_text($a->profile['romance'])) {
740                         $profile['romance'] = array( t('Love/Romance:'), $txt);
741                 }
742
743                 if ($txt = prepare_text($a->profile['work'])) {
744                         $profile['work'] = array( t('Work/employment:'), $txt);
745                 }
746
747                 if ($txt = prepare_text($a->profile['education'])) {
748                         $profile['education'] = array( t('School/education:'), $txt );
749                 }
750
751                 //show subcribed forum if it is enabled in the usersettings
752                 if (feature_enabled($uid,'forumlist_profile')) {
753                         $profile['forumlist'] = array( t('Forums:'), ForumManager::profile_advanced($uid));
754                 }
755
756                 if ($a->profile['uid'] == local_user()) {
757                         $profile['edit'] = array(App::get_baseurl(). '/profiles/'.$a->profile['id'], t('Edit profile'),"", t('Edit profile'));
758                 }
759
760                 return replace_macros($tpl, array(
761                         '$title' => t('Profile'),
762                         '$basic' => t('Basic'),
763                         '$advanced' => t('Advanced'),
764                         '$profile' => $profile
765                 ));
766         }
767
768         return '';
769 }
770
771 function profile_tabs($a, $is_owner=False, $nickname=Null) {
772         //echo "<pre>"; var_dump($a->user); killme();
773
774         if (is_null($nickname)) {
775                 $nickname  = $a->user['nickname'];
776         }
777
778         if (x($_GET,'tab')) {
779                 $tab = notags(trim($_GET['tab']));
780         }
781
782         $url = App::get_baseurl() . '/profile/' . $nickname;
783
784         $tabs = array(
785                 array(
786                         'label'=>t('Status'),
787                         'url' => $url,
788                         'sel' => ((!isset($tab) && $a->argv[0]=='profile') ? 'active' : ''),
789                         'title' => t('Status Messages and Posts'),
790                         'id' => 'status-tab',
791                         'accesskey' => 'm',
792                 ),
793                 array(
794                         'label' => t('Profile'),
795                         'url'   => $url.'/?tab=profile',
796                         'sel'   => ((isset($tab) && $tab=='profile') ? 'active' : ''),
797                         'title' => t('Profile Details'),
798                         'id' => 'profile-tab',
799                         'accesskey' => 'r',
800                 ),
801                 array(
802                         'label' => t('Photos'),
803                         'url'   => App::get_baseurl() . '/photos/' . $nickname,
804                         'sel'   => ((!isset($tab) && $a->argv[0]=='photos') ? 'active' : ''),
805                         'title' => t('Photo Albums'),
806                         'id' => 'photo-tab',
807                         'accesskey' => 'h',
808                 ),
809                 array(
810                         'label' => t('Videos'),
811                         'url'   => App::get_baseurl() . '/videos/' . $nickname,
812                         'sel'   => ((!isset($tab) && $a->argv[0]=='videos') ? 'active' : ''),
813                         'title' => t('Videos'),
814                         'id' => 'video-tab',
815                         'accesskey' => 'v',
816                 ),
817         );
818
819         // the calendar link for the full featured events calendar
820         if ($is_owner && $a->theme_events_in_profile) {
821                         $tabs[] = array(
822                                 'label' => t('Events'),
823                                 'url'   => App::get_baseurl() . '/events',
824                                 'sel'   =>((!isset($tab) && $a->argv[0]=='events') ? 'active' : ''),
825                                 'title' => t('Events and Calendar'),
826                                 'id' => 'events-tab',
827                                 'accesskey' => 'e',
828                         );
829         // if the user is not the owner of the calendar we only show a calendar
830         // with the public events of the calendar owner
831         } elseif (! $is_owner) {
832                 $tabs[] = array(
833                                 'label' => t('Events'),
834                                 'url'   => App::get_baseurl() . '/cal/' . $nickname,
835                                 'sel'   =>((!isset($tab) && $a->argv[0]=='cal') ? 'active' : ''),
836                                 'title' => t('Events and Calendar'),
837                                 'id' => 'events-tab',
838                                 'accesskey' => 'e',
839                         );
840         }
841
842         if ($is_owner) {
843                 $tabs[] = array(
844                         'label' => t('Personal Notes'),
845                         'url'   => App::get_baseurl() . '/notes',
846                         'sel'   =>((!isset($tab) && $a->argv[0]=='notes') ? 'active' : ''),
847                         'title' => t('Only You Can See This'),
848                         'id' => 'notes-tab',
849                         'accesskey' => 't',
850                 );
851         }
852
853         if ((! $is_owner) && ((count($a->profile)) || (! $a->profile['hide-friends']))) {
854                 $tabs[] = array(
855                         'label' => t('Contacts'),
856                         'url'   => App::get_baseurl() . '/viewcontacts/' . $nickname,
857                         'sel'   => ((!isset($tab) && $a->argv[0]=='viewcontacts') ? 'active' : ''),
858                         'title' => t('Contacts'),
859                         'id' => 'viewcontacts-tab',
860                         'accesskey' => 'k',
861                 );
862         }
863
864         $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
865         call_hooks('profile_tabs', $arr);
866
867         $tpl = get_markup_template('common_tabs.tpl');
868
869         return replace_macros($tpl,array('$tabs' => $arr['tabs']));
870 }
871
872 function get_my_url() {
873         if (x($_SESSION, 'my_url')) {
874                 return $_SESSION['my_url'];
875         }
876         return false;
877 }
878
879 function zrl_init(App $a) {
880         $tmp_str = get_my_url();
881         if (validate_url($tmp_str)) {
882
883                 // Is it a DDoS attempt?
884                 // The check fetches the cached value from gprobe to reduce the load for this system
885                 $urlparts = parse_url($tmp_str);
886
887                 $result = Cache::get("gprobe:" . $urlparts["host"]);
888                 if ((!is_null($result)) && (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
889                         logger("DDoS attempt detected for " . $urlparts["host"] . " by " . $_SERVER["REMOTE_ADDR"] . ". server data: " . print_r($_SERVER, true), LOGGER_DEBUG);
890                         return;
891                 }
892
893                 proc_run(PRIORITY_LOW, 'include/gprobe.php', $tmp_str);
894                 $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
895                 call_hooks('zrl_init', $arr);
896         }
897 }
898
899 function zrl($s, $force = false) {
900         if (! strlen($s)) {
901                 return $s;
902         }
903         if ((! strpos($s, '/profile/')) && (! $force)) {
904                 return $s;
905         }
906         if ($force && substr($s, -1, 1) !== '/') {
907                 $s = $s . '/';
908         }
909         $achar = strpos($s, '?') ? '&' : '?';
910         $mine = get_my_url();
911         if ($mine && ! link_compare($mine, $s)) {
912                 return $s . $achar . 'zrl=' . urlencode($mine);
913         }
914         return $s;
915 }
916
917 /**
918  * @brief Get the user ID of the page owner
919  *
920  * Used from within PCSS themes to set theme parameters. If there's a
921  * puid request variable, that is the "page owner" and normally their theme
922  * settings take precedence; unless a local user sets the "always_my_theme"
923  * system pconfig, which means they don't want to see anybody else's theme
924  * settings except their own while on this site.
925  *
926  * @return int user ID
927  *
928  * @note Returns local_user instead of user ID if "always_my_theme"
929  *      is set to true
930  */
931 function get_theme_uid() {
932         $uid = (($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
933         if ((local_user()) && ((get_pconfig(local_user(),'system','always_my_theme')) || (! $uid))) {
934                 return local_user();
935         }
936
937         return $uid;
938 }