LiveUser
[ class tree: LiveUser ] [ index: LiveUser ] [ all elements ]

Source for file Common.php

Documentation is available at Common.php

  1. <?php
  2. // LiveUser: A framework for authentication and authorization in PHP applications
  3. // Copyright (C) 2002-2003 Markus Wolff
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. /**
  20.  * Base class for authentication backends.
  21.  *
  22.  * @author   Lukas Smith <smith@backendmedia.com>
  23.  * @version  $Id: Common.php,v 1.5 2004/12/18 22:03:10 lsmith Exp $
  24.  * @package  LiveUser
  25.  * @category authentication
  26.  */
  27. {
  28.     /**
  29.      * Set posible encryption modes.
  30.      *
  31.      * @access private
  32.      * @var    array 
  33.      */
  34.     var $encryptionModes = array('MD5'   => 'MD5',
  35.                                  'RC4'   => 'RC4',
  36.                                  'PLAIN' => 'PLAIN',
  37.                                  'SHA1'  => 'SHA1');
  38.  
  39.     /**
  40.      * Defines the algorithm used for encrypting/decrypting
  41.      * passwords. Default: "MD5".
  42.      *
  43.      * @access private
  44.      * @var    string 
  45.      */
  46.     var $passwordEncryptionMode 'MD5';
  47.  
  48.  
  49.     /**
  50.      * The name associated with this auth container. The name is used
  51.      * when adding users from this container to the reference table
  52.      * in the permission container. This way it is possible to see
  53.      * from which auth container the user data is coming from.
  54.      *
  55.      * @var    string 
  56.      * @access public
  57.      */
  58.     var $name = null;
  59.  
  60.     /**
  61.      * Class constructor. Feel free to override in backend subclasses.
  62.      *
  63.      * @access protected
  64.      */
  65.     function LiveUser_Admin_Auth_Common(&$connectOptions$name = null)
  66.     {
  67.         $this->_stack &PEAR_ErrorStack::singleton('LiveUser_Admin');
  68.  
  69.         if (is_array($connectOptions)) {
  70.             foreach ($connectOptions as $key => $value{
  71.                 if (isset($this->$key)) {
  72.                     $this->$key $value;
  73.                 }
  74.             }
  75.         }
  76.         if (!is_null($name)) {
  77.             $this->name = $name;
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Decrypts a password so that it can be compared with the user
  83.      * input. Uses the algorithm defined in the passwordEncryptionMode
  84.      * property.
  85.      *
  86.      * @access public
  87.      * @param  string password as an encrypted string
  88.      * @return string The decrypted password
  89.      */
  90.     function decryptPW($encryptedPW)
  91.     {
  92.         $decryptedPW 'Encryption type not supported.';
  93.  
  94.         switch (strtoupper($this->passwordEncryptionMode)) {
  95.             case 'PLAIN':
  96.                 $decryptedPW $encryptedPW;
  97.                 break;
  98.             case 'MD5':
  99.                 // MD5 can't be decoded, so return the string unmodified
  100.                 $decryptedPW $encryptedPW;
  101.                 break;
  102.             case 'RC4':
  103.                 $rc4 =LiveUser::CryptRC4Factory($this->_options['cookie']['secret']);
  104.                 if ($rc4 === false{
  105.                     return false;
  106.                 }
  107.                 $this->rc4 =$rc4;
  108.                 $decryptedPW $encryptedPW;
  109.                 $this->rc4->decrypt($decryptedPW);
  110.                 break;
  111.             case 'SHA1':
  112.                 // SHA1 can't be decoded, so return the string unmodified
  113.                 $decryptedPW $encryptedPW;
  114.                 break;
  115.         }
  116.  
  117.         return $decryptedPW;
  118.     }
  119.  
  120.     /**
  121.      * Encrypts a password for storage in a backend container.
  122.      * Uses the algorithm defined in the passwordEncryptionMode
  123.      * property.
  124.      *
  125.      * @access public
  126.      * @param string  password as plain text
  127.      * @return string The encrypted password
  128.      */
  129.     function encryptPW($plainPW)
  130.     {
  131.         $encryptedPW 'Encryption type not supported.';
  132.  
  133.         switch (strtoupper($this->passwordEncryptionMode)) {
  134.             case 'PLAIN':
  135.                 $encryptedPW $plainPW;
  136.                 break;
  137.             case 'MD5':
  138.                 $encryptedPW md5($plainPW);
  139.                 break;
  140.             case 'RC4':
  141.                 if (!is_object($this->rc4)) {
  142.                     $rc4 =LiveUser::CryptRC4Factory($this->_options['cookie']['secret']);
  143.                     if ($rc4 === false{
  144.                         return false;;
  145.                     }
  146.                     $this->rc4 =$rc4;
  147.                 }
  148.                 $encryptedPW $plainPW;
  149.                 $this->rc4->crypt($encryptedPW);
  150.                 break;
  151.             case 'SHA1':
  152.                 if (!function_exists('sha1')) {
  153.                     return LiveUser_Admin::raiseError(LIVEUSER_ADMIN_ERROR_NOT_SUPPORTEDnullnull,
  154.                         'SHA1 function doesn\'t exist. Upgrade your PHP version.');
  155.                 }
  156.                 $encryptedPW sha1($plainPW);
  157.                 break;
  158.         }
  159.  
  160.         return $encryptedPW;
  161.     }
  162.  
  163.     /**
  164.      * Function returns the inquired value if it exists in the class.
  165.      *
  166.      * @access public
  167.      * @param  string   Name of the property to be returned.
  168.      * @return mixed    null, a value or an array.
  169.      */
  170.     function getProperty($what)
  171.     {
  172.         $that = null;
  173.         if (isset($this->$what)) {
  174.             $that $this->$what;
  175.         }
  176.         return $that;
  177.     }
  178.  
  179.     /**
  180.      * Adds a new user to Auth.
  181.      *
  182.      * @access  public
  183.      * @param   string  Handle (username).
  184.      * @param   string  Password.
  185.      * @param   array   Array of optional fields values to be added array('alias' => ''value')
  186.      * @param   array   Array of custom fields values to be added array('alias' => ''value')
  187.      * @param   mixed   If specificed no new ID will be automatically generated instead
  188.      * @return  mixed   Users auth ID on success, DB error if not, false if not initialized
  189.      */
  190.     function addUser($handle$password ''$optionalFields = array(),
  191.                               $customFields = array()$authId = null)
  192.     {
  193.         return LiveUser_Admin::raiseError(LIVEUSER_ADMIN_ERROR_NOT_SUPPORTEDnullnull,
  194.             'addUser(): Method not supported by this container');
  195.     }
  196.  
  197.     /**
  198.      * Removes an existing user from Auth.
  199.      *
  200.      * @access  public
  201.      * @param   string   Auth user ID of the user that should be removed.
  202.      * @return  mixed    True on success, error object if not.
  203.      */
  204.     function removeUser($authId)
  205.     {
  206.         return LiveUser_Admin::raiseError(LIVEUSER_ADMIN_ERROR_NOT_SUPPORTEDnullnull,
  207.             'removeUser(): Method not supported by this container');
  208.     }
  209.  
  210.     /**
  211.      * Changes user data in auth table.
  212.      *
  213.      * @access  public
  214.      * @param   string   Auth user ID.
  215.      * @param   string   Handle (username) (optional).
  216.      * @param   string   Password (optional).
  217.      * @param   array   Array of optional fields values to be added array('alias' => ''value')
  218.      * @param   array    Array of custom fields values to be updated
  219.      * @return  mixed    True on success, DB error if not.
  220.      */
  221.     function updateUser($authId$handle ''$password '',
  222.                                    $optionalFields = array()$customFields = array())
  223.     {
  224.         return LiveUser_Admin::raiseError(LIVEUSER_ADMIN_ERROR_NOT_SUPPORTEDnullnull,
  225.             'updateUser(): Method not supported by this container');
  226.     }
  227.  
  228.     /**
  229.      * Gets all users with handle, passwd, authId,
  230.      * lastlogin, is_active and individual rights.
  231.      *
  232.      * The array will look like this:
  233.      * <code>
  234.      * $userData[0]['auth_user_id']       = 'wujha433gawefawfwfiuj2ou9823r98h';
  235.      *             ['handle']       = 'myLogin';
  236.      *             ['passwd']     = 'd346gs2gwaeiuhaeiuuweijfjuwaefhj';
  237.      *             ['lastlogin']    = 1254801292; (Unix timestamp)
  238.      *             ['is_active']     = 1; (1 = yes, 0 = no)
  239.      * </code>
  240.      *
  241.      * @access  public
  242.      * @param   array  filters to apply to fetched data
  243.      * @param   string  if not null 'ORDER BY $order' will be appended to the query
  244.      * @param   boolean will return an associative array with the auth_user_id
  245.      *                   as the key by using DB::getAssoc() instead of DB::getAll()
  246.      * @return  mixed  Array with user data or DB error.
  247.      */
  248.     function getUsers($filters = array()$order = null$rekey = false)
  249.     {
  250.         return LiveUser_Admin::raiseError(LIVEUSER_ADMIN_ERROR_NOT_SUPPORTEDnullnull,
  251.             'getUsers(): Method not supported by this container');
  252.     }
  253. }

Documentation generated on Mon, 11 Mar 2019 14:00:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.