Source for file helper.php
Documentation is available at helper.php
 * @package     Joomla.Platform  
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.  
 * @license     GNU General Public License version 2 or later; see LICENSE  
// Always make sure that the password hashing API has been defined.  
// include_once JPATH_ROOT . '/libraries/compat/password/lib/password.php';  
 * Authorisation helper class, provides static methods to perform various tasks relevant  
 * to the Joomla user and authorisation classes  
 * This class has influences and some method logic from the Horde Auth package  
 * @package     Joomla.Platform  
     * Method to add a user to a group.  
     * @param   integer  $userId   The id of the user.  
     * @param   integer  $groupId  The id of the group.  
     * @return  boolean  True on success  
     * @throws  RuntimeException  
        $user = 
new JUser((int) 
$userId);  
        // Add the user to the group if necessary.  
            // Get the title of the group.  
            $query = 
$db->getQuery(true)  
                ->select($db->quoteName('title'))  
                ->from($db->quoteName('#__usergroups'))  
                ->where($db->quoteName('id') . 
' = ' . (int) 
$groupId);  
            $title = 
$db->loadResult();  
            // If the group does not exist, return an exception.  
                throw 
new RuntimeException('Access Usergroup Invalid'); 
            // Add the group data to the user object.  
            $user->groups[$title] = 
$groupId;  
            // Store the user object.  
            // Set the group data for any preloaded user objects.  
            $temp->groups = 
$user->groups;  
            // Set the group data for the user object in the session.  
            if ($temp->id == 
$userId)  
                $temp->groups = 
$user->groups;  
     * Method to get a list of groups a user is in.  
     * @param   integer  $userId  The id of the user.  
     * @return  array    List of groups  
        return isset
($user->groups) ? 
$user->groups : 
array();  
     * Method to remove a user from a group.  
     * @param   integer  $userId   The id of the user.  
     * @param   integer  $groupId  The id of the group.  
     * @return  boolean  True on success  
        // Remove the user from the group if necessary.  
            // Remove the user from the group.  
            unset
($user->groups[$key]); 
            // Store the user object.  
        // Set the group data for any preloaded user objects.  
        $temp->groups = 
$user->groups;  
        // Set the group data for the user object in the session.  
        if ($temp->id == 
$userId)  
            $temp->groups = 
$user->groups;  
     * Method to set the groups for a user.  
     * @param   integer  $userId  The id of the user.  
     * @param   array    $groups  An array of group ids to put the user in.  
     * @return  boolean  True on success  
        // Get the titles for the user groups.  
        $query = 
$db->getQuery(true)  
            ->select($db->quoteName('id') . 
', ' . 
$db->quoteName('title'))  
            ->from($db->quoteName('#__usergroups'))  
            ->where($db->quoteName('id') . 
' = ' . 
implode(' OR ' . 
$db->quoteName('id') . 
' = ', $user->groups));  
        $results = 
$db->loadObjectList();  
        // Set the titles for the user groups.  
        for ($i = 
0, $n = 
count($results); $i < 
$n; $i++
)  
            $user->groups[$results[$i]->id] = 
$results[$i]->id;  
        // Store the user object.  
            // Set the group data for any preloaded user objects.  
            $temp->groups = 
$user->groups;  
            // Set the group data for the user object in the session.  
            if ($temp->id == 
$userId)  
                $temp->groups = 
$user->groups;  
     * Gets the user profile information  
     * @param   integer  $userId  The id of the user.  
        // Get the dispatcher and load the user's plugins.  
        // Trigger the data preparation event.  
        $dispatcher->trigger('onContentPrepareData', array('com_users.profile', &$data));  
     * Method to activate a user  
     * @param   string  $activation  Activation string  
     * @return  boolean  True on success  
        // Let's get the id of the user we want to activate  
        $query = 
$db->getQuery(true)  
            ->select($db->quoteName('id'))  
            ->from($db->quoteName('#__users'))  
            ->where($db->quoteName('activation') . 
' = ' . 
$db->quote($activation))  
            ->where($db->quoteName('block') . 
' = 1')  
            ->where($db->quoteName('lastvisitDate') . 
' = ' . 
$db->quote('0000-00-00 00:00:00'));  
        $id = (int) 
$db->loadResult();  
        // Is it a valid user to activate?  
            $user->set('block', '0');  
            $user->set('activation', '');  
            // Time to take care of business.... store the user.  
     * Returns userid if a user exists  
     * @param   string  $username  The username to search on.  
     * @return  integer  The user id or 0 if not found.  
        // Initialise some variables  
        $query = 
$db->getQuery(true)  
            ->select($db->quoteName('id'))  
            ->from($db->quoteName('#__users'))  
            ->where($db->quoteName('username') . 
' = ' . 
$db->quote($username));  
        $db->setQuery($query, 0, 1);  
        return $db->loadResult();  
     * Formats a password using the current encryption.  
     * @param   string   $plaintext     The plaintext password to encrypt.  
     * @param   string   $salt          The salt to use to encrypt the password. []  
     *                                   If not present, a new salt will be  
     * @param   string   $encryption    The kind of password encryption to use.  
     * @param   boolean  $show_encrypt  Some password systems prepend the kind of  
     *                                   encryption to the crypted password ({SHA},  
     *                                   etc). Defaults to false.  
     * @return  string  The encrypted password.  
     * @note    In Joomla! CMS 3.2 the default encrytion has been changed to bcrypt. When PHP 5.5 is the minimum  
     *           supported version it will be changed to the PHP PASSWORD_DEFAULT constant.  
    public static function getCryptedPassword($plaintext, $salt = 
'', $encryption = 
'bcrypt', $show_encrypt = 
false)  
        if ($app->getClientId() != 
2)  
        // The Joomla user plugin allows you to use weaker passwords if necessary.  
        if (!empty($joomlaPluginEnabled))  
            $userPluginParams = 
new JRegistry($userPlugin->params);  
        // Not all controllers check the length, although they should to avoid DOS attacks.  
        // The maximum password length for bcrypt is 55:  
            $app->enqueueMessage(JText::_('JLIB_USER_ERROR_PASSWORD_TOO_LONG'), 'error');  
            $salt = 
self::getSalt($encryption, $salt, $plaintext);  
                return ($show_encrypt) ? 
'{SHA}' . 
$encrypted : 
$encrypted;  
                return ($show_encrypt ? 
'{crypt}' : 
'') . 
crypt($plaintext, $salt);  
                return ($show_encrypt) ? 
'{MD5}' . 
$encrypted : 
$encrypted;  
                return ($show_encrypt) ? 
'{SSHA}' . 
$encrypted : 
$encrypted;  
                return ($show_encrypt) ? 
'{SMD5}' . 
$encrypted : 
$encrypted;  
                $context = 
$plaintext . 
'$apr1$' . 
$salt;  
                $binary = 
self::_bin(md5($plaintext . 
$salt . 
$plaintext));  
                for ($i = 
$length; $i > 
0; $i -= 
16)  
                    $context .= 
substr($binary, 0, ($i > 
16 ? 
16 : 
$i));  
                for ($i = 
$length; $i > 
0; $i >>= 
1)  
                    $context .= 
($i & 1) ? 
chr(0) : 
$plaintext[0];  
                $binary = 
self::_bin(md5($context));  
                for ($i = 
0; $i < 
1000; $i++
)  
                    $new = 
($i & 1) ? 
$plaintext : 
substr($binary, 0, 16);  
                    $new .= 
($i & 1) ? 
substr($binary, 0, 16) : 
$plaintext;  
                    $binary = 
self::_bin(md5($new));  
                for ($i = 
0; $i < 
5; $i++
)  
                    $p[] = 
self::_toAPRMD5((ord($binary[$i]) << 
16) | 
(ord($binary[$k]) << 
8) | 
(ord($binary[$j])), 5);  
                return '$apr1$' . 
$salt . 
'$' . 
implode('', $p) . 
self::_toAPRMD5(ord($binary[11]), 3);  
                $encrypted = 
($salt) ? 
md5($plaintext . 
$salt) : 
md5($plaintext);  
                return ($show_encrypt) ? 
'{MD5}' . 
$encrypted : 
$encrypted;  
                $encrypted = 
($salt) ? 
hash('sha256', $plaintext . 
$salt) . 
':' . 
$salt : 
hash('sha256', $plaintext);  
                return ($show_encrypt) ? 
'{SHA256}' . 
$encrypted : 
'{SHA256}' . 
$encrypted;  
            // 'bcrypt' is the default case starting in CMS 3.2.  
                if ($useStrongEncryption === 
true)  
                        // Something went wrong fall back to sha256.  
                            return static::getCryptedPassword($plaintext, '', 'sha256', false);  
                    return ($show_encrypt) ? 
'{BCRYPT}' . 
$encrypted : 
$encrypted;  
                    // BCrypt isn't available but we want strong passwords, fall back to sha256.  
                    return static::getCryptedPassword($plaintext, '', 'sha256', false);  
     * Returns a salt for the appropriate kind of password encryption.  
     * Optionally takes a seed and a plaintext password, to extract the seed  
     * of an existing password, or for encryption types that use the plaintext  
     * in the generation of the salt.  
     * @param   string  $encryption  The kind of password encryption to use.  
     * @param   string  $seed        The seed to get the salt from (probably a  
     *                                previously generated password). Defaults to  
     * @param   string  $plaintext   The plaintext password that we're generating  
     *                                a salt for. Defaults to none.  
     * @return  string  The generated or extracted salt.  
     * @note    Default $encryption will be changed to 'bcrypt' in CMS 3.2 and will at  
     *           the type used by the PHP PASSWORD_DEFAULT constant until 5.5 is the minimum  
     *           version required. At that point the default will be PASSWORD_DEFAULT.  
    public static function getSalt($encryption = 
'md5-hex', $seed = 
'', $plaintext = 
'')  
                        return static::genRandomPassword(16);  
            case 'aprmd5': 
/* 64 characters that are valid for APRMD5 passwords. */  
                $APRMD5 = 
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';  
                    for ($i = 
0; $i < 
8; $i++
)  
                        $salt .= 
$APRMD5{rand(0, 63)};  
            // BCrypt is aliased because a BCrypt has may be requested when it is not present, and so it falls back to  
            // the default behavior of generating a salt.  
     * Generate a random password  
     * @param   integer  $length  Length of the password to generate  
     * @return  string  Random Password  
        $salt = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
         * Start with a cryptographic strength random string, then convert it to  
         * a string with the numeric base of the salt.  
         * Shift the base conversion on each character so the character  
         * distribution is even, and randomize the start shift so it's not  
        $shift = 
ord($random[0]);  
        for ($i = 
1; $i <= 
$length; ++
$i)  
            $makepass .= 
$salt[($shift + 
ord($random[$i])) % 
$base];  
            $shift += 
ord($random[$i]);  
     * Converts to allowed 64 characters for APRMD5 passwords.  
     * @param   string   $value  The value to convert.  
     * @param   integer  $count  The number of characters to convert.  
     * @return  string  $value converted to the 64 MD5 characters.  
    protected static function _toAPRMD5($value, $count)  
        /* 64 characters that are valid for APRMD5 passwords. */  
        $APRMD5 = 
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';  
            $aprmd5 .= 
$APRMD5[$value & 0x3f];  
     * Converts hexadecimal string to binary data.  
     * @param   string  $hex  Hex data.  
     * @return  string  Binary data.  
    private static function _bin($hex)  
        for ($i = 
0; $i < 
$length; $i += 
2)  
     * Method to remove a cookie record from the database and the browser  
     * @param   string  $userId      User ID for this user  
     * @param   string  $cookieName  Series id (cookie name decoded)  
     * @return  boolean  True on success  
     * @see     JInput::setCookie for more details  
        $query = 
$db->getQuery(true);  
        // Invalidate cookie in the database  
            ->update($db->quoteName('#__user_keys'))  
            ->set($db->quoteName('invalid') . 
' = 1')  
            ->where($db->quotename('user_id') . 
' = ' . 
$db->quote($userId));  
        $db->setQuery($query)->execute();  
        // Destroy the cookie in the browser.  
        $app->input->cookie->set($cookieName, false, time() - 
42000, $app->get('cookie_path'), $app->get('cookie_domain'), false, true);  
     * Clear all expired tokens for all users.  
     * @return  mixed  Database query result  
        $query = 
$db->getQuery(true)  
        ->where($db->quoteName('time') . 
' < ' . 
$db->quote($now));  
        return $db->setQuery($query)->execute();  
     * Method to get the remember me cookie data  
     * @return  mixed  An array of information from an authentication cookie or false if there is no cookie  
        // Create the cookie name  
        $cookieName = 
static::getShortHashedUserAgent();  
        // Fetch the cookie value  
        $cookieValue = 
$app->input->cookie->get($cookieName);  
        if (!empty($cookieValue))  
     * Method to get a hashed user agent string that does not include browser version.  
     * Used when frequent version changes cause problems.  
     * @return  string  A hashed user agent string with version replaced by 'abcd'  
        $uaString = 
$ua->userAgent;  
        $browserVersion = 
$ua->browserVersion;  
        $uaShort = 
str_replace($browserVersion, 'abcd', $uaString);  
 
 
	
		Documentation generated on Tue, 19 Nov 2013 15:04:36 +0100 by phpDocumentor 1.4.3