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