Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/user/helper.php

Documentation is available at helper.php

  1. <?php
  2. /**
  3. * @version        $Id: authenticate.php 6257 2007-01-11 22:03:46Z friesengeist $
  4. * @package        Joomla.Framework
  5. * @subpackage    User
  6. * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7. * @license        GNU/GPL, see LICENSE.php
  8. *  Joomla! is free software. This version may have been modified pursuant
  9. *  to the GNU General Public License, and as distributed it includes or
  10. *  is derivative of works licensed under the GNU General Public License or
  11. *  other free or open source software licenses.
  12. *  See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. /**
  16.  * Authorization helper class, provides static methods to perform various tasks relevant
  17.  * to the Joomla user and authorization classes
  18.  *
  19.  * This class has influences and some method logic from the Horde Auth package
  20.  *
  21.  * @static
  22.  * @author         Louis Landry <[email protected]>
  23.  * @package     Joomla.Framework
  24.  * @subpackage    User
  25.  * @since        1.5
  26.  */
  27. {
  28.     /**
  29.      * Method to activate a user
  30.      *
  31.      * @param    string    $activation    Activation string
  32.      * @return     boolean             True on success
  33.      * @since    1.5
  34.      */
  35.     function activateUser($activation)
  36.     {
  37.         //Initialize some variables
  38.         $db = & JFactory::getDBO();
  39.  
  40.         // Lets get the id of the user we want to activate
  41.         $query = 'SELECT id'
  42.         . ' FROM #__users'
  43.         . ' WHERE activation = "'.$activation.'"'
  44.         . ' AND block = 1'
  45.         ;
  46.         $db->setQuery( $query );
  47.         $id = intval( $db->loadResult() );
  48.  
  49.         // Is it a valid user to activate?
  50.         if ($id)
  51.         {
  52.             $user =& JUser::getInstance( (int) $id );
  53.  
  54.             $user->set('block', '0');
  55.             $user->set('activation', '');
  56.  
  57.             // Time to take care of business.... store the user.
  58.             if (!$user->save())
  59.             {
  60.                 JError::raiseWarning( "SOME_ERROR_CODE", $user->getError() );
  61.                 return false;
  62.             }
  63.         }
  64.         else
  65.         {
  66.             JError::raiseWarning( "SOME_ERROR_CODE", JText::_('Unable to find a user with given activation string.') );
  67.             return false;
  68.         }
  69.  
  70.         return true;
  71.     }
  72.  
  73.     /**
  74.      * Returns userid if a user exists
  75.      *
  76.      * @param string The username to search on
  77.      * @return int The user id or 0 if not found
  78.      */
  79.     function getUserId($username)
  80.     {
  81.         // Initialize some variables
  82.         $db = & JFactory::getDBO();
  83.  
  84.         $query = 'SELECT id FROM #__users WHERE username = ' . $db->Quote( $username );
  85.         $db->setQuery($query, 0, 1);
  86.         return $db->loadResult();
  87.     }
  88.  
  89.     /**
  90.      * Formats a password using the current encryption.
  91.      *
  92.      * @access    public
  93.      * @param    string    $plaintext    The plaintext password to encrypt.
  94.      * @param    string    $salt        The salt to use to encrypt the password. []
  95.      *                                 If not present, a new salt will be
  96.      *                                 generated.
  97.      * @param    string    $encryption    The kind of pasword encryption to use.
  98.      *                                 Defaults to md5-hex.
  99.      * @param    boolean    $show_encrypt  Some password systems prepend the kind of
  100.      *                                 encryption to the crypted password ({SHA},
  101.      *                                 etc). Defaults to false.
  102.      *
  103.      * @return string  The encrypted password.
  104.      */
  105.     function getCryptedPassword($plaintext, $salt = '', $encryption = 'md5-hex', $show_encrypt = false)
  106.     {
  107.         // Get the salt to use.
  108.         $salt = JUserHelper::getSalt($encryption, $salt, $plaintext);
  109.  
  110.         // Encrypt the password.
  111.         switch ($encryption)
  112.         {
  113.             case 'plain' :
  114.                 return $plaintext;
  115.  
  116.             case 'sha' :
  117.                 $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext));
  118.                 return ($show_encrypt) ? '{SHA}'.$encrypted : $encrypted;
  119.  
  120.             case 'crypt' :
  121.             case 'crypt-des' :
  122.             case 'crypt-md5' :
  123.             case 'crypt-blowfish' :
  124.                 return ($show_encrypt ? '{crypt}' : '').crypt($plaintext, $salt);
  125.  
  126.             case 'md5-base64' :
  127.                 $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext));
  128.                 return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
  129.  
  130.             case 'ssha' :
  131.                 $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext.$salt).$salt);
  132.                 return ($show_encrypt) ? '{SSHA}'.$encrypted : $encrypted;
  133.  
  134.             case 'smd5' :
  135.                 $encrypted = base64_encode(mhash(MHASH_MD5, $plaintext.$salt).$salt);
  136.                 return ($show_encrypt) ? '{SMD5}'.$encrypted : $encrypted;
  137.  
  138.             case 'aprmd5' :
  139.                 $length = strlen($plaintext);
  140.                 $context = $plaintext.'$apr1$'.$salt;
  141.                 $binary = JUserHelper::_bin(md5($plaintext.$salt.$plaintext));
  142.  
  143.                 for ($i = $length; $i > 0; $i -= 16) {
  144.                     $context .= substr($binary, 0, ($i > 16 ? 16 : $i));
  145.                 }
  146.                 for ($i = $length; $i > 0; $i >>= 1) {
  147.                     $context .= ($i & 1) ? chr(0) : $plaintext[0];
  148.                 }
  149.  
  150.                 $binary = JUserHelper::_bin(md5($context));
  151.  
  152.                 for ($i = 0; $i < 1000; $i ++) {
  153.                     $new = ($i & 1) ? $plaintext : substr($binary, 0, 16);
  154.                     if ($i % 3) {
  155.                         $new .= $salt;
  156.                     }
  157.                     if ($i % 7) {
  158.                         $new .= $plaintext;
  159.                     }
  160.                     $new .= ($i & 1) ? substr($binary, 0, 16) : $plaintext;
  161.                     $binary = JUserHelper::_bin(md5($new));
  162.                 }
  163.  
  164.                 $p = array ();
  165.                 for ($i = 0; $i < 5; $i ++) {
  166.                     $k = $i +6;
  167.                     $j = $i +12;
  168.                     if ($j == 16) {
  169.                         $j = 5;
  170.                     }
  171.                     $p[] = JUserHelper::_toAPRMD5((ord($binary[$i]) << 16) | (ord($binary[$k]) << 8) | (ord($binary[$j])), 5);
  172.                 }
  173.  
  174.                 return '$apr1$'.$salt.'$'.implode('', $p).JUserHelper::_toAPRMD5(ord($binary[11]), 3);
  175.  
  176.             case 'md5-hex' :
  177.             default :
  178.                 return ($show_encrypt) ? '{MD5}'.md5($plaintext) : md5($plaintext);
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Returns a salt for the appropriate kind of password encryption.
  184.      * Optionally takes a seed and a plaintext password, to extract the seed
  185.      * of an existing password, or for encryption types that use the plaintext
  186.      * in the generation of the salt.
  187.      *
  188.      * @access public
  189.      * @param string $encryption  The kind of pasword encryption to use.
  190.      *                             Defaults to md5-hex.
  191.      * @param string $seed        The seed to get the salt from (probably a
  192.      *                             previously generated password). Defaults to
  193.      *                             generating a new seed.
  194.      * @param string $plaintext   The plaintext password that we're generating
  195.      *                             a salt for. Defaults to none.
  196.      *
  197.      * @return string  The generated or extracted salt.
  198.      */
  199.     function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
  200.     {
  201.         // Encrypt the password.
  202.         switch ($encryption)
  203.         {
  204.             case 'crypt' :
  205.             case 'crypt-des' :
  206.                 if ($seed) {
  207.                     return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
  208.                 } else {
  209.                     return substr(md5(mt_rand()), 0, 2);
  210.                 }
  211.  
  212.             case 'crypt-md5' :
  213.                 if ($seed) {
  214.                     return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
  215.                 } else {
  216.                     return '$1$'.substr(md5(mt_rand()), 0, 8).'$';
  217.                 }
  218.  
  219.             case 'crypt-blowfish' :
  220.                 if ($seed) {
  221.                     return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
  222.                 } else {
  223.                     return '$2$'.substr(md5(mt_rand()), 0, 12).'$';
  224.                 }
  225.  
  226.             case 'ssha' :
  227.                 if ($seed) {
  228.                     return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
  229.                 } else {
  230.                     return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  231.                 }
  232.  
  233.             case 'smd5' :
  234.                 if ($seed) {
  235.                     return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
  236.                 } else {
  237.                     return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  238.                 }
  239.  
  240.             case 'aprmd5' :
  241.                 /* 64 characters that are valid for APRMD5 passwords. */
  242.                 $APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  243.  
  244.                 if ($seed) {
  245.                     return substr(preg_replace('/^\$apr1\$(.{8}).*/', '\\1', $seed), 0, 8);
  246.                 } else {
  247.                     $salt = '';
  248.                     for ($i = 0; $i < 8; $i ++) {
  249.                         $salt .= $APRMD5 {
  250.                             rand(0, 63)
  251.                             };
  252.                     }
  253.                     return $salt;
  254.                 }
  255.  
  256.             default :
  257.                 return '';
  258.         }
  259.     }
  260.  
  261.     /**
  262.      * Generate a random password
  263.      *
  264.      * @static
  265.      * @param    int        $length    Length of the password to generate
  266.      * @return    string            Random Password
  267.      * @since    1.5
  268.      */
  269.     function genRandomPassword($length = 8)
  270.     {
  271.         $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  272.         $len = strlen($salt);
  273.         $makepass = '';
  274.         mt_srand(10000000 * (double) microtime());
  275.  
  276.         for ($i = 0; $i < $length; $i ++) {
  277.             $makepass .= $salt[mt_rand(0, $len -1)];
  278.         }
  279.  
  280.         return $makepass;
  281.     }
  282.  
  283.     /**
  284.      * Converts to allowed 64 characters for APRMD5 passwords.
  285.      *
  286.      * @access private
  287.      * @param string  $value 
  288.      * @param integer $count 
  289.      * @return string  $value converted to the 64 MD5 characters.
  290.      * @since 1.5
  291.      */
  292.     function _toAPRMD5($value, $count)
  293.     {
  294.         /* 64 characters that are valid for APRMD5 passwords. */
  295.         $APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  296.  
  297.         $aprmd5 = '';
  298.         $count = abs($count);
  299.         while (-- $count) {
  300.             $aprmd5 .= $APRMD5[$value & 0x3f];
  301.             $value >>= 6;
  302.         }
  303.         return $aprmd5;
  304.     }
  305.  
  306.     /**
  307.      * Converts hexadecimal string to binary data.
  308.      *
  309.      * @access private
  310.      * @param string $hex  Hex data.
  311.      * @return string  Binary data.
  312.      * @since 1.5
  313.      */
  314.     function _bin($hex)
  315.     {
  316.         $bin = '';
  317.         $length = strlen($hex);
  318.         for ($i = 0; $i < $length; $i += 2) {
  319.             $tmp = sscanf(substr($hex, $i, 2), '%x');
  320.             $bin .= chr(array_shift($tmp));
  321.         }
  322.         return $bin;
  323.     }
  324. }
  325.  
  326. ?>

Documentation generated on Mon, 05 Mar 2007 21:05:44 +0000 by phpDocumentor 1.3.1