6 * Function : profile_load
8 * @parameter string $nickname
9 * @parameter int $profile
11 * Summary: Loads a profile into the page sidebar.
12 * The function requires a writeable copy of the main App structure, and the nickname
13 * of a registered local account.
15 * If the viewer is an authenticated remote viewer, the profile displayed is the
16 * one that has been configured for his/her viewing in the Contact manager.
17 * Passing a non-zero profile ID can also allow a preview of a selected profile
20 * Profile information is placed in the App structure for later retrieval.
21 * Honours the owner's chosen theme for display.
23 * IMPORTANT: Should only be run in the _init() functions of a module. That ensures that
24 * the theme is chosen before the _init() function of a theme is run, which will usually
25 * load a lot of theme-specific content
29 if(! function_exists('profile_load')) {
30 function profile_load(&$a, $nickname, $profile = 0, $profiledata = array()) {
32 $user = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
36 if(!$user && count($user) && !count($profiledata)) {
37 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
38 notice( t('Requested account is not available.') . EOL );
43 if(remote_user() && count($_SESSION['remote'])) {
44 foreach($_SESSION['remote'] as $visitor) {
45 if($visitor['uid'] == $user[0]['uid']) {
46 $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
47 intval($visitor['cid'])
50 $profile = $r[0]['profile-id'];
59 $profile_int = intval($profile);
60 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
61 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
62 WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d and `contact`.`self` = 1 LIMIT 1",
67 if((!$r) && (!count($r))) {
68 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
69 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
70 WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 and `contact`.`self` = 1 LIMIT 1",
75 if(($r === false) || (!count($r)) && !count($profiledata)) {
76 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
77 notice( t('Requested profile is not available.') . EOL );
82 // fetch user tags if this isn't the default profile
84 if(!$r[0]['is-default']) {
85 $x = q("select `pub_keywords` from `profile` where uid = %d and `is-default` = 1 limit 1",
86 intval($r[0]['profile_uid'])
89 $r[0]['pub_keywords'] = $x[0]['pub_keywords'];
93 $a->profile_uid = $r[0]['profile_uid'];
95 $a->profile['mobile-theme'] = get_pconfig($a->profile['profile_uid'], 'system', 'mobile_theme');
96 $a->profile['network'] = NETWORK_DFRN;
98 $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
100 // if (!$profiledata)
101 // $_SESSION['theme'] = $a->profile['theme'];
103 $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
106 * load/reload current theme info
109 $a->set_template_engine(); // reset the template engine to the default in case the user's theme doesn't specify one
111 $theme_info_file = "view/theme/".current_theme()."/theme.php";
112 if (file_exists($theme_info_file)){
113 require_once($theme_info_file);
116 if(! (x($a->page,'aside')))
117 $a->page['aside'] = '';
119 if(local_user() && local_user() == $a->profile['uid'] && $profiledata) {
120 $a->page['aside'] .= replace_macros(get_markup_template('profile_edlink.tpl'),array(
121 '$editprofile' => t('Edit profile'),
122 '$profid' => $a->profile['id']
126 $block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
129 // By now, the contact block isn't shown, when a different profile is given
130 // But: When this profile was on the same server, then we could display the contacts
132 $a->page['aside'] .= profile_sidebar($profiledata, true);
134 $a->page['aside'] .= profile_sidebar($a->profile, $block);
138 $a->page['aside'] .= contact_block();*/
147 * Function: profile_sidebar
149 * Formats a profile for display in the sidebar.
150 * It is very difficult to templatise the HTML completely
151 * because of all the conditional logic.
153 * @parameter: array $profile
155 * Returns HTML string stuitable for sidebar inclusion
156 * Exceptions: Returns empty string if passed $profile is wrong type or not populated
161 if(! function_exists('profile_sidebar')) {
162 function profile_sidebar($profile, $block = 0) {
170 if((! is_array($profile)) && (! count($profile)))
173 $profile['picdate'] = urlencode($profile['picdate']);
175 if (($profile['network'] != "") AND ($profile['network'] != NETWORK_DFRN)) {
176 require_once('include/contact_selectors.php');
177 if ($profile['url'] != "")
178 $profile['network_name'] = '<a href="'.$profile['url'].'">'.network_to_name($profile['network'], $profile['url'])."</a>";
180 $profile['network_name'] = network_to_name($profile['network']);
182 $profile['network_name'] = "";
184 call_hooks('profile_sidebar_enter', $profile);
187 // don't show connect link to yourself
188 $connect = (($profile['uid'] != local_user()) ? t('Connect') : False);
190 // don't show connect link to authenticated visitors either
191 if(remote_user() && count($_SESSION['remote'])) {
192 foreach($_SESSION['remote'] as $visitor) {
193 if($visitor['uid'] == $profile['uid']) {
200 // Is the local user already connected to that user?
201 if ($connect AND local_user()) {
202 if (isset($profile["url"]))
203 $profile_url = normalise_link($profile["url"]);
205 $profile_url = normalise_link($a->get_baseurl()."/profile/".$profile["nickname"]);
207 $r = q("SELECT * FROM `contact` WHERE NOT `pending` AND `uid` = %d AND `nurl` = '%s'",
208 local_user(), $profile_url);
213 if ($connect AND ($profile['network'] != NETWORK_DFRN) AND !isset($profile['remoteconnect']))
216 if (isset($profile['remoteconnect']))
217 $remoteconnect = $profile['remoteconnect'];
219 if( get_my_url() && $profile['unkmail'] && ($profile['uid'] != local_user()) )
220 $wallmessage = t('Message');
222 $wallmessage = false;
224 // show edit profile to yourself
225 if ($profile['uid'] == local_user() && feature_enabled(local_user(),'multi_profiles')) {
226 $profile['edit'] = array($a->get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
227 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
230 $profile['menu'] = array(
231 'chg_photo' => t('Change profile photo'),
232 'cr_new' => t('Create New Profile'),
233 'entries' => array(),
239 $profile['menu']['entries'][] = array(
240 'photo' => $rr['thumb'],
242 'alt' => t('Profile Image'),
243 'profile_name' => $rr['profile-name'],
244 'isdefault' => $rr['is-default'],
245 'visibile_to_everybody' => t('visible to everybody'),
246 'edit_visibility' => t('Edit visibility'),
254 if ($profile['uid'] == local_user() && !feature_enabled(local_user(),'multi_profiles')) {
255 $profile['edit'] = array($a->get_baseurl(). '/profiles/'.$profile['id'], t('Edit profile'),"", t('Edit profile'));
256 $profile['menu'] = array(
257 'chg_photo' => t('Change profile photo'),
259 'entries' => array(),
263 if((x($profile,'address') == 1)
264 || (x($profile,'locality') == 1)
265 || (x($profile,'region') == 1)
266 || (x($profile,'postal-code') == 1)
267 || (x($profile,'country-name') == 1))
268 $location = t('Location:');
270 $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
273 $marital = ((x($profile,'marital') == 1) ? t('Status:') : False);
275 $homepage = ((x($profile,'homepage') == 1) ? t('Homepage:') : False);
277 $about = ((x($profile,'about') == 1) ? t('About:') : False);
279 if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
280 $location = $pdesc = $gender = $marital = $homepage = $about = False;
283 $firstname = ((strpos($profile['name'],' '))
284 ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
285 $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
288 'guid' => $profile['guid'],
289 'podloc' => $a->get_baseurl(),
290 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
291 'nickname' => $profile['nickname'],
292 'fullname' => $profile['name'],
293 'firstname' => $firstname,
294 'lastname' => $lastname,
295 'photo300' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg'),
296 'photo100' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg'),
297 'photo50' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/50/' . $profile['uid'] . '.jpg'),
301 $contact_block = contact_block();
303 if(is_array($a->profile) AND !$a->profile['hide-friends']) {
304 $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
305 intval($a->profile['uid']));
307 $updated = date("c", strtotime($r[0]['updated']));
309 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0 AND `archive` = 0
310 AND `network` IN ('%s', '%s', '%s', '')",
311 intval($profile['uid']),
313 dbesc(NETWORK_DIASPORA),
314 dbesc(NETWORK_OSTATUS)
317 $contacts = intval($r[0]['total']);
322 foreach($profile as $k => $v) {
323 $k = str_replace('-','_',$k);
327 if($a->theme['template_engine'] === 'internal')
328 $location = template_escape($location);
330 $tpl = get_markup_template('profile_vcard.tpl');
331 $o .= replace_macros($tpl, array(
333 '$connect' => $connect,
334 '$remoteconnect' => $remoteconnect,
335 '$wallmessage' => $wallmessage,
336 '$location' => $location,
337 '$gender' => $gender,
339 '$marital' => $marital,
340 '$homepage' => $homepage,
342 '$network' => t('Network:'),
343 '$contacts' => $contacts,
344 '$updated' => $updated,
345 '$diaspora' => $diaspora,
346 '$contact_block' => $contact_block,
350 $arr = array('profile' => &$profile, 'entry' => &$o);
352 call_hooks('profile_sidebar', $arr);
359 if(! function_exists('get_birthdays')) {
360 function get_birthdays() {
365 if(! local_user() || $a->is_mobile || $a->is_tablet)
368 // $mobile_detect = new Mobile_Detect();
369 // $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
374 $bd_format = t('g A l F d') ; // 8 AM Friday January 18
375 $bd_short = t('F d');
377 $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
378 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
379 WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
380 ORDER BY `start` ASC ",
381 intval(local_user()),
382 dbesc(datetime_convert('UTC','UTC','now + 6 days')),
383 dbesc(datetime_convert('UTC','UTC','now'))
386 if($r && count($r)) {
388 $now = strtotime('now');
393 if(strlen($rr['name']))
395 if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
398 $classtoday = $istoday ? ' birthday-today ' : '';
400 foreach($r as &$rr) {
401 if(! strlen($rr['name']))
406 if(in_array($rr['cid'],$cids))
408 $cids[] = $rr['cid'];
410 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
413 if($rr['network'] === NETWORK_DFRN) {
414 $sparkle = " sparkle";
415 $url = $a->get_baseurl() . '/redir/' . $rr['cid'];
419 $rr['title'] = $rr['name'];
420 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . t('[today]') : '');
421 $rr['startime'] = Null;
422 $rr['today'] = $today;
427 $tpl = get_markup_template("birthdays_reminder.tpl");
428 return replace_macros($tpl, array(
429 '$baseurl' => $a->get_baseurl(),
430 '$classtoday' => $classtoday,
432 '$event_reminders' => t('Birthday Reminders'),
433 '$event_title' => t('Birthdays this week:'),
435 '$lbr' => '{', // raw brackets mess up if/endif macro processing
443 if(! function_exists('get_events')) {
444 function get_events() {
446 require_once('include/bbcode.php');
450 if(! local_user() || $a->is_mobile || $a->is_tablet)
454 // $mobile_detect = new Mobile_Detect();
455 // $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
460 $bd_format = t('g A l F d') ; // 8 AM Friday January 18
461 $bd_short = t('F d');
463 $r = q("SELECT `event`.* FROM `event`
464 WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
465 ORDER BY `start` ASC ",
466 intval(local_user()),
467 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
468 dbesc(datetime_convert('UTC','UTC','now - 1 days'))
471 if($r && count($r)) {
472 $now = strtotime('now');
475 if(strlen($rr['name']))
478 $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
479 if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
482 $classtoday = (($istoday) ? 'event-today' : '');
486 foreach($r as &$rr) {
487 $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
489 if(strlen($title) > 35)
490 $title = substr($title,0,32) . '... ';
492 $description = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
494 $description = t('[No description]');
496 $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
498 if(substr($strt,0,10) < datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
503 $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
505 $rr['title'] = $title;
506 $rr['description'] = $desciption;
507 $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
508 $rr['startime'] = $strt;
509 $rr['today'] = $today;
513 $tpl = get_markup_template("events_reminder.tpl");
514 return replace_macros($tpl, array(
515 '$baseurl' => $a->get_baseurl(),
516 '$classtoday' => $classtoday,
517 '$count' => count($r) - $skip,
518 '$event_reminders' => t('Event Reminders'),
519 '$event_title' => t('Events this week:'),
525 function advanced_profile(&$a) {
529 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
530 '$title' => t('Profile')
533 if($a->profile['name']) {
535 $tpl = get_markup_template('profile_advanced.tpl');
539 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
541 if($a->profile['gender']) $profile['gender'] = array( t('Gender:'), $a->profile['gender'] );
544 if(($a->profile['dob']) && ($a->profile['dob'] != '0000-00-00')) {
546 $year_bd_format = t('j F, Y');
547 $short_bd_format = t('j F');
550 $val = ((intval($a->profile['dob']))
551 ? day_translate(datetime_convert('UTC','UTC',$a->profile['dob'] . ' 00:00 +00:00',$year_bd_format))
552 : day_translate(datetime_convert('UTC','UTC','2001-' . substr($a->profile['dob'],5) . ' 00:00 +00:00',$short_bd_format)));
554 $profile['birthday'] = array( t('Birthday:'), $val);
558 if($age = age($a->profile['dob'],$a->profile['timezone'],'')) $profile['age'] = array( t('Age:'), $age );
561 if($a->profile['marital']) $profile['marital'] = array( t('Status:'), $a->profile['marital']);
564 if($a->profile['with']) $profile['marital']['with'] = $a->profile['with'];
566 if(strlen($a->profile['howlong']) && $a->profile['howlong'] !== '0000-00-00 00:00:00') {
567 $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
570 if($a->profile['sexual']) $profile['sexual'] = array( t('Sexual Preference:'), $a->profile['sexual'] );
572 if($a->profile['homepage']) $profile['homepage'] = array( t('Homepage:'), linkify($a->profile['homepage']) );
574 if($a->profile['hometown']) $profile['hometown'] = array( t('Hometown:'), linkify($a->profile['hometown']) );
576 if($a->profile['pub_keywords']) $profile['pub_keywords'] = array( t('Tags:'), $a->profile['pub_keywords']);
578 if($a->profile['politic']) $profile['politic'] = array( t('Political Views:'), $a->profile['politic']);
580 if($a->profile['religion']) $profile['religion'] = array( t('Religion:'), $a->profile['religion']);
582 if($txt = prepare_text($a->profile['about'])) $profile['about'] = array( t('About:'), $txt );
584 if($txt = prepare_text($a->profile['interest'])) $profile['interest'] = array( t('Hobbies/Interests:'), $txt);
586 if($txt = prepare_text($a->profile['likes'])) $profile['likes'] = array( t('Likes:'), $txt);
588 if($txt = prepare_text($a->profile['dislikes'])) $profile['dislikes'] = array( t('Dislikes:'), $txt);
591 if($txt = prepare_text($a->profile['contact'])) $profile['contact'] = array( t('Contact information and Social Networks:'), $txt);
593 if($txt = prepare_text($a->profile['music'])) $profile['music'] = array( t('Musical interests:'), $txt);
595 if($txt = prepare_text($a->profile['book'])) $profile['book'] = array( t('Books, literature:'), $txt);
597 if($txt = prepare_text($a->profile['tv'])) $profile['tv'] = array( t('Television:'), $txt);
599 if($txt = prepare_text($a->profile['film'])) $profile['film'] = array( t('Film/dance/culture/entertainment:'), $txt);
601 if($txt = prepare_text($a->profile['romance'])) $profile['romance'] = array( t('Love/Romance:'), $txt);
603 if($txt = prepare_text($a->profile['work'])) $profile['work'] = array( t('Work/employment:'), $txt);
605 if($txt = prepare_text($a->profile['education'])) $profile['education'] = array( t('School/education:'), $txt );
607 if ($a->profile['uid'] == local_user())
608 $profile['edit'] = array($a->get_baseurl(). '/profiles/'.$a->profile['id'], t('Edit profile'),"", t('Edit profile'));
610 return replace_macros($tpl, array(
611 '$title' => t('Profile'),
612 '$profile' => $profile
619 if(! function_exists('profile_tabs')){
620 function profile_tabs($a, $is_owner=False, $nickname=Null){
621 //echo "<pre>"; var_dump($a->user); killme();
623 if (is_null($nickname))
624 $nickname = $a->user['nickname'];
627 $tab = notags(trim($_GET['tab']));
629 $url = $a->get_baseurl() . '/profile/' . $nickname;
633 'label'=>t('Status'),
635 'sel' => ((!isset($tab)&&$a->argv[0]=='profile')?'active':''),
636 'title' => t('Status Messages and Posts'),
637 'id' => 'status-tab',
641 'label' => t('Profile'),
642 'url' => $url.'/?tab=profile',
643 'sel' => ((isset($tab) && $tab=='profile')?'active':''),
644 'title' => t('Profile Details'),
645 'id' => 'profile-tab',
649 'label' => t('Photos'),
650 'url' => $a->get_baseurl() . '/photos/' . $nickname,
651 'sel' => ((!isset($tab)&&$a->argv[0]=='photos')?'active':''),
652 'title' => t('Photo Albums'),
657 'label' => t('Videos'),
658 'url' => $a->get_baseurl() . '/videos/' . $nickname,
659 'sel' => ((!isset($tab)&&$a->argv[0]=='videos')?'active':''),
660 'title' => t('Videos'),
667 if ($a->theme_events_in_profile)
669 'label' => t('Events'),
670 'url' => $a->get_baseurl() . '/events',
671 'sel' =>((!isset($tab)&&$a->argv[0]=='events')?'active':''),
672 'title' => t('Events and Calendar'),
673 'id' => 'events-tab',
677 'label' => t('Personal Notes'),
678 'url' => $a->get_baseurl() . '/notes',
679 'sel' =>((!isset($tab)&&$a->argv[0]=='notes')?'active':''),
680 'title' => t('Only You Can See This'),
687 $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
688 call_hooks('profile_tabs', $arr);
690 $tpl = get_markup_template('common_tabs.tpl');
692 return replace_macros($tpl,array('$tabs' => $arr['tabs']));
696 function get_my_url() {
697 if(x($_SESSION,'my_url'))
698 return $_SESSION['my_url'];
702 function zrl_init(&$a) {
703 $tmp_str = get_my_url();
704 if(validate_url($tmp_str)) {
706 // Is it a DDoS attempt?
707 // The check fetches the cached value from gprobe to reduce the load for this system
708 $urlparts = parse_url($tmp_str);
710 $result = Cache::get("gprobe:".$urlparts["host"]);
711 if (!is_null($result)) {
712 $result = unserialize($result);
713 if (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
714 logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
719 proc_run('php','include/gprobe.php',bin2hex($tmp_str));
720 $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
721 call_hooks('zrl_init',$arr);
725 function zrl($s,$force = false) {
728 if((! strpos($s,'/profile/')) && (! $force))
730 if($force && substr($s,-1,1) !== '/')
732 $achar = strpos($s,'?') ? '&' : '?';
733 $mine = get_my_url();
734 if($mine and ! link_compare($mine,$s))
735 return $s . $achar . 'zrl=' . urlencode($mine);
739 // Used from within PCSS themes to set theme parameters. If there's a
740 // puid request variable, that is the "page owner" and normally their theme
741 // settings take precedence; unless a local user sets the "always_my_theme"
742 // system pconfig, which means they don't want to see anybody else's theme
743 // settings except their own while on this site.
745 function get_theme_uid() {
746 $uid = (($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
748 if((get_pconfig(local_user(),'system','always_my_theme')) || (! $uid))