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'];
94 $a->profile['mobile-theme'] = get_pconfig($a->profile['profile_uid'], 'system', 'mobile_theme');
95 $a->profile['network'] = NETWORK_DFRN;
97 $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
100 $_SESSION['theme'] = $a->profile['theme'];
102 $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
105 * load/reload current theme info
108 $a->set_template_engine(); // reset the template engine to the default in case the user's theme doesn't specify one
110 $theme_info_file = "view/theme/".current_theme()."/theme.php";
111 if (file_exists($theme_info_file)){
112 require_once($theme_info_file);
115 if(! (x($a->page,'aside')))
116 $a->page['aside'] = '';
118 if(local_user() && local_user() == $a->profile['uid'] && $profiledata) {
119 $a->page['aside'] .= replace_macros(get_markup_template('profile_edlink.tpl'),array(
120 '$editprofile' => t('Edit profile'),
121 '$profid' => $a->profile['id']
125 $block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
128 // By now, the contact block isn't shown, when a different profile is given
129 // But: When this profile was on the same server, then we could display the contacts
131 $a->page['aside'] .= profile_sidebar($profiledata, true);
133 $a->page['aside'] .= profile_sidebar($a->profile, $block);
137 $a->page['aside'] .= contact_block();*/
146 * Function: profile_sidebar
148 * Formats a profile for display in the sidebar.
149 * It is very difficult to templatise the HTML completely
150 * because of all the conditional logic.
152 * @parameter: array $profile
154 * Returns HTML string stuitable for sidebar inclusion
155 * Exceptions: Returns empty string if passed $profile is wrong type or not populated
160 if(! function_exists('profile_sidebar')) {
161 function profile_sidebar($profile, $block = 0) {
169 if((! is_array($profile)) && (! count($profile)))
172 $profile['picdate'] = urlencode($profile['picdate']);
174 if (($profile['network'] != "") AND ($profile['network'] != NETWORK_DFRN)) {
175 require_once('include/contact_selectors.php');
176 if ($profile['url'] != "")
177 $profile['network_name'] = '<a href="'.$profile['url'].'">'.network_to_name($profile['network'])."</a>";
179 $profile['network_name'] = network_to_name($profile['network']);
181 $profile['network_name'] = "";
183 call_hooks('profile_sidebar_enter', $profile);
186 // don't show connect link to yourself
187 $connect = (($profile['uid'] != local_user()) ? t('Connect') : False);
189 // don't show connect link to authenticated visitors either
190 if(remote_user() && count($_SESSION['remote'])) {
191 foreach($_SESSION['remote'] as $visitor) {
192 if($visitor['uid'] == $profile['uid']) {
199 // Is the local user already connected to that user?
200 if ($connect AND local_user()) {
201 if (isset($profile["url"]))
202 $profile_url = normalise_link($profile["url"]);
204 $profile_url = normalise_link($a->get_baseurl()."/profile/".$profile["nickname"]);
206 $r = q("SELECT * FROM `contact` WHERE NOT `pending` AND `uid` = %d AND `nurl` = '%s'",
207 local_user(), $profile_url);
212 if ($connect AND ($profile['network'] != NETWORK_DFRN) AND !isset($profile['remoteconnect']))
215 if (isset($profile['remoteconnect']))
216 $remoteconnect = $profile['remoteconnect'];
218 if( get_my_url() && $profile['unkmail'] && ($profile['uid'] != local_user()) )
219 $wallmessage = t('Message');
221 $wallmessage = false;
223 // show edit profile to yourself
224 if ($profile['uid'] == local_user() && feature_enabled(local_user(),'multi_profiles')) {
225 $profile['edit'] = array($a->get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
226 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
229 $profile['menu'] = array(
230 'chg_photo' => t('Change profile photo'),
231 'cr_new' => t('Create New Profile'),
232 'entries' => array(),
238 $profile['menu']['entries'][] = array(
239 'photo' => $rr['thumb'],
241 'alt' => t('Profile Image'),
242 'profile_name' => $rr['profile-name'],
243 'isdefault' => $rr['is-default'],
244 'visibile_to_everybody' => t('visible to everybody'),
245 'edit_visibility' => t('Edit visibility'),
253 if ($profile['uid'] == local_user() && !feature_enabled(local_user(),'multi_profiles')) {
254 $profile['edit'] = array($a->get_baseurl(). '/profiles/'.$profile['id'], t('Edit profile'),"", t('Edit profile'));
255 $profile['menu'] = array(
256 'chg_photo' => t('Change profile photo'),
258 'entries' => array(),
262 if((x($profile,'address') == 1)
263 || (x($profile,'locality') == 1)
264 || (x($profile,'region') == 1)
265 || (x($profile,'postal-code') == 1)
266 || (x($profile,'country-name') == 1))
267 $location = t('Location:');
269 $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
272 $marital = ((x($profile,'marital') == 1) ? t('Status:') : False);
274 $homepage = ((x($profile,'homepage') == 1) ? t('Homepage:') : False);
276 $about = ((x($profile,'about') == 1) ? t('About:') : False);
278 if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
279 $location = $pdesc = $gender = $marital = $homepage = $about = False;
282 $firstname = ((strpos($profile['name'],' '))
283 ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
284 $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
287 'podloc' => $a->get_baseurl(),
288 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
289 'nickname' => $profile['nickname'],
290 'fullname' => $profile['name'],
291 'firstname' => $firstname,
292 'lastname' => $lastname,
293 'photo300' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg'),
294 'photo100' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg'),
295 'photo50' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/50/' . $profile['uid'] . '.jpg'),
299 $contact_block = contact_block();
301 if(is_array($a->profile) AND !$a->profile['hide-friends']) {
302 $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
303 intval($a->profile['uid']));
305 $updated = date("c", strtotime($r[0]['updated']));
307 $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
308 AND `network` IN ('%s', '%s', '%s', '')",
309 intval($profile['uid']),
311 dbesc(NETWORK_DIASPORA),
312 dbesc(NETWORK_OSTATUS)
315 $contacts = intval($r[0]['total']);
320 foreach($profile as $k => $v) {
321 $k = str_replace('-','_',$k);
325 if($a->theme['template_engine'] === 'internal')
326 $location = template_escape($location);
328 $tpl = get_markup_template('profile_vcard.tpl');
329 $o .= replace_macros($tpl, array(
331 '$connect' => $connect,
332 '$remoteconnect' => $remoteconnect,
333 '$wallmessage' => $wallmessage,
334 '$location' => $location,
335 '$gender' => $gender,
337 '$marital' => $marital,
338 '$homepage' => $homepage,
340 '$network' => t('Network:'),
341 '$contacts' => $contacts,
342 '$updated' => $updated,
343 '$diaspora' => $diaspora,
344 '$contact_block' => $contact_block,
348 $arr = array('profile' => &$profile, 'entry' => &$o);
350 call_hooks('profile_sidebar', $arr);
357 if(! function_exists('get_birthdays')) {
358 function get_birthdays() {
363 if(! local_user() || $a->is_mobile || $a->is_tablet)
366 // $mobile_detect = new Mobile_Detect();
367 // $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
372 $bd_format = t('g A l F d') ; // 8 AM Friday January 18
373 $bd_short = t('F d');
375 $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
376 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
377 WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
378 ORDER BY `start` ASC ",
379 intval(local_user()),
380 dbesc(datetime_convert('UTC','UTC','now + 6 days')),
381 dbesc(datetime_convert('UTC','UTC','now'))
384 if($r && count($r)) {
386 $now = strtotime('now');
391 if(strlen($rr['name']))
393 if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
396 $classtoday = $istoday ? ' birthday-today ' : '';
398 foreach($r as &$rr) {
399 if(! strlen($rr['name']))
404 if(in_array($rr['cid'],$cids))
406 $cids[] = $rr['cid'];
408 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
411 if($rr['network'] === NETWORK_DFRN) {
412 $sparkle = " sparkle";
413 $url = $a->get_baseurl() . '/redir/' . $rr['cid'];
417 $rr['title'] = $rr['name'];
418 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . t('[today]') : '');
419 $rr['startime'] = Null;
420 $rr['today'] = $today;
425 $tpl = get_markup_template("birthdays_reminder.tpl");
426 return replace_macros($tpl, array(
427 '$baseurl' => $a->get_baseurl(),
428 '$classtoday' => $classtoday,
430 '$event_reminders' => t('Birthday Reminders'),
431 '$event_title' => t('Birthdays this week:'),
433 '$lbr' => '{', // raw brackets mess up if/endif macro processing
441 if(! function_exists('get_events')) {
442 function get_events() {
444 require_once('include/bbcode.php');
448 if(! local_user() || $a->is_mobile || $a->is_tablet)
452 // $mobile_detect = new Mobile_Detect();
453 // $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
458 $bd_format = t('g A l F d') ; // 8 AM Friday January 18
459 $bd_short = t('F d');
461 $r = q("SELECT `event`.* FROM `event`
462 WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
463 ORDER BY `start` ASC ",
464 intval(local_user()),
465 dbesc(datetime_convert('UTC','UTC','now + 7 days')),
466 dbesc(datetime_convert('UTC','UTC','now - 1 days'))
469 if($r && count($r)) {
470 $now = strtotime('now');
473 if(strlen($rr['name']))
476 $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
477 if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
480 $classtoday = (($istoday) ? 'event-today' : '');
484 foreach($r as &$rr) {
485 $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
487 if(strlen($title) > 35)
488 $title = substr($title,0,32) . '... ';
490 $description = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
492 $description = t('[No description]');
494 $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
496 if(substr($strt,0,10) < datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
501 $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
503 $rr['title'] = $title;
504 $rr['description'] = $desciption;
505 $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
506 $rr['startime'] = $strt;
507 $rr['today'] = $today;
511 $tpl = get_markup_template("events_reminder.tpl");
512 return replace_macros($tpl, array(
513 '$baseurl' => $a->get_baseurl(),
514 '$classtoday' => $classtoday,
515 '$count' => count($r) - $skip,
516 '$event_reminders' => t('Event Reminders'),
517 '$event_title' => t('Events this week:'),
523 function advanced_profile(&$a) {
527 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
528 '$title' => t('Profile')
531 if($a->profile['name']) {
533 $tpl = get_markup_template('profile_advanced.tpl');
537 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
539 if($a->profile['gender']) $profile['gender'] = array( t('Gender:'), $a->profile['gender'] );
542 if(($a->profile['dob']) && ($a->profile['dob'] != '0000-00-00')) {
544 $year_bd_format = t('j F, Y');
545 $short_bd_format = t('j F');
548 $val = ((intval($a->profile['dob']))
549 ? day_translate(datetime_convert('UTC','UTC',$a->profile['dob'] . ' 00:00 +00:00',$year_bd_format))
550 : day_translate(datetime_convert('UTC','UTC','2001-' . substr($a->profile['dob'],5) . ' 00:00 +00:00',$short_bd_format)));
552 $profile['birthday'] = array( t('Birthday:'), $val);
556 if($age = age($a->profile['dob'],$a->profile['timezone'],'')) $profile['age'] = array( t('Age:'), $age );
559 if($a->profile['marital']) $profile['marital'] = array( t('Status:'), $a->profile['marital']);
562 if($a->profile['with']) $profile['marital']['with'] = $a->profile['with'];
564 if(strlen($a->profile['howlong']) && $a->profile['howlong'] !== '0000-00-00 00:00:00') {
565 $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
568 if($a->profile['sexual']) $profile['sexual'] = array( t('Sexual Preference:'), $a->profile['sexual'] );
570 if($a->profile['homepage']) $profile['homepage'] = array( t('Homepage:'), linkify($a->profile['homepage']) );
572 if($a->profile['hometown']) $profile['hometown'] = array( t('Hometown:'), linkify($a->profile['hometown']) );
574 if($a->profile['pub_keywords']) $profile['pub_keywords'] = array( t('Tags:'), $a->profile['pub_keywords']);
576 if($a->profile['politic']) $profile['politic'] = array( t('Political Views:'), $a->profile['politic']);
578 if($a->profile['religion']) $profile['religion'] = array( t('Religion:'), $a->profile['religion']);
580 if($txt = prepare_text($a->profile['about'])) $profile['about'] = array( t('About:'), $txt );
582 if($txt = prepare_text($a->profile['interest'])) $profile['interest'] = array( t('Hobbies/Interests:'), $txt);
584 if($txt = prepare_text($a->profile['likes'])) $profile['likes'] = array( t('Likes:'), $txt);
586 if($txt = prepare_text($a->profile['dislikes'])) $profile['dislikes'] = array( t('Dislikes:'), $txt);
589 if($txt = prepare_text($a->profile['contact'])) $profile['contact'] = array( t('Contact information and Social Networks:'), $txt);
591 if($txt = prepare_text($a->profile['music'])) $profile['music'] = array( t('Musical interests:'), $txt);
593 if($txt = prepare_text($a->profile['book'])) $profile['book'] = array( t('Books, literature:'), $txt);
595 if($txt = prepare_text($a->profile['tv'])) $profile['tv'] = array( t('Television:'), $txt);
597 if($txt = prepare_text($a->profile['film'])) $profile['film'] = array( t('Film/dance/culture/entertainment:'), $txt);
599 if($txt = prepare_text($a->profile['romance'])) $profile['romance'] = array( t('Love/Romance:'), $txt);
601 if($txt = prepare_text($a->profile['work'])) $profile['work'] = array( t('Work/employment:'), $txt);
603 if($txt = prepare_text($a->profile['education'])) $profile['education'] = array( t('School/education:'), $txt );
605 if ($a->profile['uid'] == local_user())
606 $profile['edit'] = array($a->get_baseurl(). '/profiles/'.$a->profile['id'], t('Edit profile'),"", t('Edit profile'));
608 return replace_macros($tpl, array(
609 '$title' => t('Profile'),
610 '$profile' => $profile
617 if(! function_exists('profile_tabs')){
618 function profile_tabs($a, $is_owner=False, $nickname=Null){
619 //echo "<pre>"; var_dump($a->user); killme();
621 if (is_null($nickname))
622 $nickname = $a->user['nickname'];
625 $tab = notags(trim($_GET['tab']));
627 $url = $a->get_baseurl() . '/profile/' . $nickname;
631 'label'=>t('Status'),
633 'sel' => ((!isset($tab)&&$a->argv[0]=='profile')?'active':''),
634 'title' => t('Status Messages and Posts'),
635 'id' => 'status-tab',
638 'label' => t('Profile'),
639 'url' => $url.'/?tab=profile',
640 'sel' => ((isset($tab) && $tab=='profile')?'active':''),
641 'title' => t('Profile Details'),
642 'id' => 'profile-tab',
645 'label' => t('Photos'),
646 'url' => $a->get_baseurl() . '/photos/' . $nickname,
647 'sel' => ((!isset($tab)&&$a->argv[0]=='photos')?'active':''),
648 'title' => t('Photo Albums'),
652 'label' => t('Videos'),
653 'url' => $a->get_baseurl() . '/videos/' . $nickname,
654 'sel' => ((!isset($tab)&&$a->argv[0]=='videos')?'active':''),
655 'title' => t('Videos'),
662 'label' => t('Events'),
663 'url' => $a->get_baseurl() . '/events',
664 'sel' =>((!isset($tab)&&$a->argv[0]=='events')?'active':''),
665 'title' => t('Events and Calendar'),
666 'id' => 'events-tab',
669 'label' => t('Personal Notes'),
670 'url' => $a->get_baseurl() . '/notes',
671 'sel' =>((!isset($tab)&&$a->argv[0]=='notes')?'active':''),
672 'title' => t('Only You Can See This'),
678 $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
679 call_hooks('profile_tabs', $arr);
681 $tpl = get_markup_template('common_tabs.tpl');
683 return replace_macros($tpl,array('$tabs' => $arr['tabs']));
687 function get_my_url() {
688 if(x($_SESSION,'my_url'))
689 return $_SESSION['my_url'];
693 function zrl_init(&$a) {
694 $tmp_str = get_my_url();
695 if(validate_url($tmp_str)) {
697 // Is it a DDoS attempt?
698 // The check fetches the cached value from gprobe to reduce the load for this system
699 $urlparts = parse_url($tmp_str);
701 $result = Cache::get("gprobe:".$urlparts["host"]);
702 if (!is_null($result)) {
703 $result = unserialize($result);
704 if ($result["network"] == NETWORK_FEED) {
705 logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
710 proc_run('php','include/gprobe.php',bin2hex($tmp_str));
711 $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
712 call_hooks('zrl_init',$arr);
716 function zrl($s,$force = false) {
719 if((! strpos($s,'/profile/')) && (! $force))
721 if($force && substr($s,-1,1) !== '/')
723 $achar = strpos($s,'?') ? '&' : '?';
724 $mine = get_my_url();
725 if($mine and ! link_compare($mine,$s))
726 return $s . $achar . 'zrl=' . urlencode($mine);