Accessing User Details in Joomla 3.X - joomla

I recently upgraded my website to Joomla 3.
I have built an external application which retrieve Joomla User Information.
Below code worked perfectly for Joomla 2.5:
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', '../' );
//Including the defines.php and framework.php of Joomla 2.5 CMS
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries' .DS. 'joomla'. DS. 'user' .DS. 'authentication.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
//Accessing the Users table in Joomla 2.5 CMS
$mainframe->route();
$user =& JFactory::getUser();
print($user);
?>
After the site upgrade, the above code does not work in Joomla 3.
First I found that DS has been depreciated in Joomla 3.
Second I found that $mainframe has also been depreciated in Joomla 3.
So here is the updated codespec:
<?php
define( '_JEXEC', 1 );
if(!defined('DS'))
{
define('DS',DIRECTORY_SEPARATOR);
}
define('JPATH_BASE', '../' );
//Including the defines.php and framework.php of Joomla 2.5 CMS
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries' .DS. 'joomla'. DS. 'user' .DS. 'authentication.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$app = JFactory::getApplication();
$app->initialise();
//Accessing the Users table in Joomla 2.5 CMS
$app->route();
$user = JFactory::getUser();
print_r($user);
exit;
?>
Unfortunately the above codespec does not work, it gives me a blank page.
Any help will be appreciated.

I've tested this myself using Joomla 3.2 and it works fine. This is all the code you need.
define( '_JEXEC', 1 );
define('JPATH_BASE', '../' );
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$app = JFactory::getApplication('site');
$user = JFactory::getUser();
print_r ($user);
Hope this helps

where you define 'JPATH_BASE' you need to remove the trailing / - or alternatively, removing prepending / from the file locations.

Related

Why is this authorisation code not working since Joomla 3.4.4

I have a script that checks if user is (super)admin and then grants access to that page.
It worked for couple years but now it stopped working. I'm getting a incorrect response text error message. When I comment out the script the thing works fine.
Here is the code:
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
$user = JFactory::getUser();
$userGroups = $user->getAuthorisedGroups();
if(in_array(7,$userGroups) or in_array(8,$userGroups)){
}else{
header( 'Location: /beheer-niet-toegestaan.html' ) ;
}
?>
I'm at a loss. Hope someone can help me.
// EDIT //
Hmm sorry for the incomplete question. After over an hour trying various code samples online i solved it by editing it to:
<?php
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user = JFactory::getUser();
$isAdmin = $user->get('isRoot');
if ($isAdmin == false) {
header( 'Location: /beheer-niet-toegestaan.html' ) ;
}
?>

Migrate users from foomla 1.5 to joomla 2.5

in my joomla 1.5 password hash by md5+salt, they look like this:
6e3bb23b32702ee94ebc18e2a059fe4a:wxIZ6BNkAFpQk4ggeRvoOIrgbWerif0x
but on joomla 2.5 it is:
$P$DTJoqPcMQgUhl6cPrzzBOTFIFHQaNf1
HOW can i change hash algorithm in joomla 2.5?
Try this,
The older algorithm will works fine with 2.5 , only the length is changed
The New code is :
<?php
jimport('joomla.user.helper');
$password = "test";
JUserHelper::hashPassword($password);
?>
Older Version Code:
jimport('joomla.user.helper');
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password_choose, $salt);
$password = $crypt.':'.$salt;
When you are working with external php script load the Joomla library to that file,
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );
require_once( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Hope it make sense..

Possible to joomla model without controller in different files?

I am developing small web application(mobile), based on Joomla component. Is it possible to use methods from specific component in custom files? For example now to use core functions such as database i am using something like:
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__).'../');
define('DS', DIRECTORY_SEPARATOR);
require_once(JPATH_BASE.DS.'includes'.DS.'defines.php');
require_once(JPATH_BASE.DS.'includes'.DS.'framework.php');
$mainframe = &JFactory::getApplication('site');
$mainframe->initialise();
$this->db = &JFactory::getDbo();
Try using the following.
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );
$db = JFactory::getDbo();

Joomla Custom Module oop php isn't working

I'm trying to get session variables working with Joomla using their session code since php's sessions don't work. I am running into a problem trying to access session variables within the joomla custom code module editor though. When I try to access the session variable using the code:
define( '_JEXEC', 1 );
define('JPATH_BASE', "../");
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$session = JFactory::getSession();
$print $session->get('status');
I get nothing. If I take that same code and paste it into its own php file I can get the status variable fine. When I look at the plain text version of Joomla preview I see this:
get('status'); ?>
Doing some testing I think it's related to the '->' operand. Has anyone encountered this before and know how to fix it? I'm running Direct PHP and can print out strings and other variables inside of the module fwiw.
you can't run PHP (or Javascript!) in Joomla articles without installing a plugin that supports it. I recommend using: http://www.kksou.com/php-gtk2/Joomla/DirectPHP-plugin.php
change $print to print
Try to comment out all the preceding lines and see if one of the "includes" is messing up the page:
/*define( '_JEXEC', 1 );
define('JPATH_BASE', "../");
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
*/
$session = JFactory::getSession();
print $session->get('status');

Joomla 2.5 Get User Data from External Script

I need to get the information of the user that's currently logged in to Joomla from a program outside of Joomla itself. I upgraded from 1.5 to 2.5, and what I had before doesn't work anymore.
<?php
define( '_VALID_MOS', 1 );
include_once( 'globals.php' );
require_once( 'configuration.php' );
require_once( 'includes/joomla.php' );
$option="test";
$mainframe = new mosMainFrame( $database, $option, '.' );
$mainframe->initSession();
$my = $mainframe->getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
After some research, I've come up with this:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$my =& JFactory::getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
$joomla_username = $my->username;
It doesn't produce any errors but seems to work. However, the user object is empty. This script is in the same directory as the Joomla installation. What may be the problem? Thanks!
Sources:
http://www.cmsbloke.com/accessing-joomla-objects-from-an-external-script/
Taken from
http://docs.joomla.org/How_to_access_session_variables_set_by_an_external_script
Solution: Replace session_start(); in your external script with
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Be sure to change JPATH_BASE to suit your directory structure.
Replace the $_SESSION[ 'name' ] = "value"; in your external script with
$session =& JFactory::getSession();
$session->set('name', "value");
Now you can retrieve this session variable using:
$session =& JFactory::getSession();
echo $session->get('name');
It turns out, I had two problems:
My script was on www.example.com, while I was logged in under example.com, so the different domain was throwing off the session data.
I also wasn't initialising the application, which apparently is necessary.
Here are the results:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
$joomla_name = $user->name;
$joomla_email = $user->email;
$joomla_password = $user->password;
$joomla_username = $user->username;
I think people have had problems with this in Joomla 2.5 as things have changed. I haven't found a solution for the way you have requested but wrote this work around (excluding the password field though) that you may want to use.
define a connection to the database:
<?php
$host="localhost";
$username="DB_USERNAME";
$password="DB_PASSWORD";
$db_name="DB_NAME";
mysql_connect($host,$username,$password)or die(mysql_error());
mysql_select_db($db_name)or die(mysql_error());
?>
Echo create a query and echo the results in a table:
<table style="background:#FFF; color:#000;">
<?php
$query = "SELECT username,name,email FROM j25_users ";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<tr><td>',$row["username"],'</td><td>',$row["name"],'</td><td>',$row["email"],'</td></tr>' ;
}
?>
</table>
You will need to change j25_users to the correct prefix of your database tables.
Update: Much better to import the framework to get the user object.
Hope this comes in handy for you. Regards.
If you want to skip using Joomla! functions, you can query the Joomla! database. Join tables js25_session and js25_users with the user id. This way, you can get every currently logged-in user and all guests on the website (and possibly the number of online users).

Resources