Print

Export Joomla Users script

What?
Somebody said I could pay to get an export list of my Joomla users. I said Joomla is a FREE OpenSource system, why?

Why?
I want to move a client's list of Joomla users to a new system which allows the passwords to be in MD5. All I want is a quick bit of code to get all the users out of a Joomla CMS.

How?
I could just type a query on the database and get the same result, but this script was more fun. Simply create a text file with the following code and upload it to the root of your Joomla website. Access the page from a browser and you should get all your users listed... For security reasons, delete the file after you have used it.

IT IS A SERIOUSLY BAD IDEA TO LEAVE THIS FILE ON YOUR WEBSITE FOR ANYONE TO ACCESS!!! DELETE IT AFTER USE.

<?php

        // init Joomla Framework
        define( '_JEXEC', 1 );
        define( 'JPATH_BASE', realpath(dirname(__FILE__) )); // print this out or observe errors to see which directory you should be in (this is two subfolders in)
        define( 'DS', '/' );

        require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
        require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
        require_once ( JPATH_CONFIGURATION   .DS.'configuration.php' );
        require_once ( JPATH_LIBRARIES .DS.'joomla'.DS.'database'.DS.'database.php' );
        require_once ( JPATH_LIBRARIES .DS.'joomla'.DS.'access'.DS.'access.php' );
        require_once ( JPATH_LIBRARIES .DS.'import.php' );

        // DB Connection
        $Config = new JConfig();
        $db_driver      = $Config->dbtype;   // Database driver name
        $db_host        = $Config->host;     // Database host name
        $db_user        = $Config->user;     // User for database authentication
        $db_pass        = $Config->password; // Password for database authentication
        $db_name        = $Config->db;       // Database name
        $db_prefix      = $Config->dbprefix; // Database prefix (may be empty)

        // Database prefix (if empty then remove prefixing double underscore)
        $db_prefix      = (trim($db_prefix)=='') ? '':$db_prefix;

        // Connect to the database
        $db_link = mysqli_connect($db_host,$db_user,$db_pass,$db_name) or die('Error connecting to the database' . mysqli_error($db_link));

        // Create a query
        $list_this_many = 100;
        $userlist_query = 'SELECT `id` FROM `'.$db_prefix.'users` ORDER BY `registerDate` LIMIT 0, '.$list_this_many;

        $output = '<html>
        <head>
                <title>Joes Joomla Users script</title>
                <style>
                        body{font-family:Verdana;font-size:75%;}
                        table{width:100%;}
                        th{text-align:left;padding:2px;font-size:75%;}
                        td{white-space:nowrap;padding:2px;font-size:75%;}
                        div{font-weight:700;}
                        .even{background-color:#eee;}
                </style>
        </head><body>';
        $output .= '<h1>Users in `'.$db_prefix.'users`</h1><table>';
        $output .= '<tr><th>Name</th><th>Username</th><th>Password</th><th>Salt</th><th>E-mail</th><th>Created</th><th>Usergroups</th><th>Blocked</th><th>Reset Count</th></tr>';

        $user_count = 0;

        // Execute the query
        if ($userlist_result = mysqli_query($db_link, $userlist_query)){

                // Return results
                while($user_row = mysqli_fetch_array($userlist_result)) {

                        // single this user out
                        $this_user_id = $user_row['id'] * 1;

                        // change background for alternating rows
                        $bg_class=($bg_class=='')?' class="even"':'';

                        // use Joomla library to get user details
                        $selected_user = JFactory::getUser($this_user_id);

                        // split the joomla password to recover the MD5
                        $password_salt = explode(':', $selected_user->password);
                        $pass = $password_salt[0];
                        $salt = $password_salt[1];

                        // add the record to our output
                        $output .=
                                '<tr'.$bg_class.'><td>'.
                                $selected_user->name.'</td><td>'.
                                $selected_user->username.'</td><td>'.
                                $pass.'</td><td>'.
                                $salt.'</td><td>'.
                                $selected_user->email.'</td><td>'.
                                $selected_user->registerDate.'</td><td>'.
                                '['.implode('][', $selected_user->groups).']</td><td>'.
                                $selected_user->block.'</td><td>'.
                                $selected_user->resetCount.'</td></tr>';

                        // for auditing purposes (displays at bottom of page)
                        $user_count++;
                }
        }

        // close off output
        $output .= '</table><br />';
        $output .= '<div>Total User(s): '.$user_count.'</div>';
        $output .= '</body></html>';

        // print out output
        echo $output;

        // free up results
        mysqli_free_result($userlist_result);

?>