Get username and email from Logged User

Dan, I really need your help.
I would like to integrate the CMS with the Intercom (https://app.intercom.io) service management system.
From what I saw is simple, just take the account ID and include the header.php

The problem is I need to get the login and user email through PHP. Would you help me?

In the future this could be a module, would pretty well.
There are some tools, evergave, intercom and others.
I think this could be a simple module.
Enable the module and the module ask app_id the Intercon.

<script> 
window.intercomSettings = { 
app_id: "APP_ID_CHANGE_ME", 
name: "<?php echo $current-user->name ?>", // Full name 
email: "<?php echo $current-user-mail->email ?>", // Email address 
created_at: 1438911379 // Signup date as a Unix timestamp 
}; 
</script>
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/zuntlo9h';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>

I tried using the code below, but it worked.

<script> 
	window.intercomSettings = { 
	app_id: "MY_APP_CODE", 
	name: "<?php echo Kit::GetParam('username', _SESSION, _USERNAME)->name ?>", // Full name 
	email: "<?php echo Kit::GetParam('username', _SESSION, _USERNAME)->email ?>", // Email address 
	created_at: 1438911379 // Signup date as a Unix timestamp 
	}; 
</script>
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/zuntlo9h';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>

ERROR:

name: "

Notice: Trying to get property of non-object in D:\Sistemas\desenv.suatv.com.br\theme\mobile\html\header.php on line 60
", // Full name
email: “
Notice: Trying to get property of non-object in D:\Sistemas\desenv.suatv.com.br\theme\mobile\html\header.php on line 61
”, // Email address

I think I understand… you want to include that script code on every page by putting it in a theme header?

You can get the user name easily, but unfortunately the email address is not exposed so you will need to make some core changes to get that.

In lib/app/thememanager.class.php add a new method like so:

public static function getUser() {
    return Theme::GetInstance()->user;
}

Then you can use:

Theme::getUser()->username;
Theme::getUser()->getEmailFromID(Theme::getUser()->userid);

In 1.8 it would be slightly different - in authed.twig for your theme you would have access to:

{{ currentUser.userName }}
{{ currentUser.email }}

Hello Dan,

I recived this error:

Fatal error: Call to undefined method Theme::getUser() in D:\Sistemas\desenv.suatv.com.br\theme\mobile\html\header.php on line 58

You need to add that method to the theme manager. Dan’s given you the code to add for that in his post.

Hello Alex,
I inserted the code as requested by DAN, the displayed error occurred with the function in thememanager.class.php file.

In lib/app/thememanager.class.php add a new method like so:

public static function getUser() {
return Theme::GetInstance()->user;
}

OK but it’s saying it can’t find that exact method when it comes to use it, so that would suggest perhaps it’s not quite in the right place.

It needs to be within the Theme class but not within another method. So say if you were to put it between line 357 and 358:
https://github.com/xibosignage/xibo-cms/blob/develop/lib/app/thememanager.class.php#L357

It should then work.

Hello Alex, I’m sorry if I’m comentendo some basic error. Unfortunately I am unable to identify where I am going wrong. Would you help me? The images below are sufficient?

So that’s a different error message from the one you posted earlier. I’m not sure how to fix that I’m afraid.

Thank you Alex for his attempt and attention.

Dan basically the script need to get the logged in user and the email of this person.
Unfortunately I do not know how to do, thank its guidelines.

This option returns the user logged in?

$username = Kit::GetParam(‘username’, _SESSION, _USERNAME);

Dan, it worked, I could return the user logged in with the code below.

$username = Kit::GetParam(‘username’, _SESSION, _USERNAME);

Could you help me to return the user’s email?

Dan and Alex, Thank you for your attention and guidance.
I managed to get the information using the existing functions in the system without the need for customization, thus ensuring compatibility with your code.

1 Like