home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / File / Passwd / Authbasic.php next >
Encoding:
PHP Script  |  2008-07-02  |  10.7 KB  |  396 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Passwd::Authbasic
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   FileFormats
  16.  * @package    File_Passwd
  17.  * @author     Michael Wallner <mike@php.net>
  18.  * @copyright  2003-2005 Michael Wallner
  19.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20.  * @version    CVS: $Id: Authbasic.php,v 1.17 2005/03/30 18:33:33 mike Exp $
  21.  * @link       http://pear.php.net/package/File_Passwd
  22.  */
  23.  
  24. /**
  25. * Requires File::Passwd::Common
  26. */
  27. require_once 'File/Passwd/Common.php';
  28.  
  29. /**
  30. * Manipulate AuthUserFiles as used for HTTP Basic Authentication.
  31. *
  32. * <kbd><u>
  33. *   Usage Example:
  34. * </u></kbd>
  35. * <code>
  36. *   $htp = &File_Passwd::factory('AuthBasic');
  37. *   $htp->setMode('sha');
  38. *   $htp->setFile('/www/mike/auth/.htpasswd');
  39. *   $htp->load();
  40. *   $htp->addUser('mike', 'secret');
  41. *   $htp->save();
  42. * </code>
  43. * <kbd><u>
  44. *   Output of listUser()
  45. * </u></kbd>
  46. * <pre>
  47. *      array
  48. *       + user => crypted_passwd
  49. *       + user => crypted_passwd
  50. * </pre>
  51. * @author   Michael Wallner <mike@php.net>
  52. * @package  File_Passwd
  53. * @version  $Revision: 1.17 $
  54. * @access   public
  55. */
  56. class File_Passwd_Authbasic extends File_Passwd_Common
  57. {
  58.     /** 
  59.     * Path to AuthUserFile
  60.     *
  61.     * @var string
  62.     * @access private
  63.     */
  64.     var $_file = '.htpasswd';
  65.  
  66.     /** 
  67.     * Actual encryption mode
  68.     *
  69.     * @var string
  70.     * @access private
  71.     */
  72.     var $_mode = 'sha';
  73.  
  74.     /** 
  75.     * Supported encryption modes
  76.     *
  77.     * @var array
  78.     * @access private
  79.     */
  80.     var $_modes = array('md5' => 'm', 'des' => 'd', 'sha' => 's');
  81.  
  82.     /** 
  83.     * Constructor
  84.     * 
  85.     * @access public
  86.     * @param  string $file   path to AuthUserFile
  87.     */
  88.     function File_Passwd_Authbasic($file = '.htpasswd')
  89.     {
  90.         File_Passwd_Authbasic::__construct($file);
  91.     }
  92.  
  93.     /**
  94.     * Constructor (ZE2)
  95.     * 
  96.     * Rewritten because DES encryption is not 
  97.     * supportet by the Win32 httpd.
  98.     * 
  99.     * @access protected
  100.     * @param  string $file   path to AuthUserFile
  101.     */
  102.     function __construct($file = '.htpasswd')
  103.     {
  104.         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  105.             unset($this->_modes['des']);
  106.         }
  107.         $this->setFile($file);
  108.     }
  109.  
  110.     /**
  111.     * Fast authentication of a certain user
  112.     * 
  113.     * Returns a PEAR_Error if:
  114.     *   o file doesn't exist
  115.     *   o file couldn't be opened in read mode
  116.     *   o file couldn't be locked exclusively
  117.     *   o file couldn't be unlocked (only if auth fails)
  118.     *   o file couldn't be closed (only if auth fails)
  119.     *
  120.     * @static   call this method statically for a reasonable fast authentication
  121.     * 
  122.     * @throws   PEAR_Error
  123.     * @access   public
  124.     * @return   mixed   true if authenticated, false if not or PEAR_Error
  125.     * @param    string  $file   path to passwd file
  126.     * @param    string  $user   user to authenticate
  127.     * @param    string  $pass   plaintext password
  128.     * @param    string  $mode   des, sha or md5
  129.     */
  130.     function staticAuth($file, $user, $pass, $mode)
  131.     {
  132.         $line = File_Passwd_Common::_auth($file, $user);
  133.         if (!$line || PEAR::isError($line)) {
  134.             return $line;
  135.         }
  136.         list(,$real)    = explode(':', $line);
  137.         $crypted        = File_Passwd_Authbasic::_genPass($pass, $real, $mode);
  138.         if (PEAR::isError($crypted)) {
  139.             return $crypted;
  140.         }
  141.         return ($real === $crypted);
  142.     }
  143.     
  144.     /** 
  145.     * Apply changes and rewrite AuthUserFile
  146.     *
  147.     * Returns a PEAR_Error if:
  148.     *   o directory in which the file should reside couldn't be created
  149.     *   o file couldn't be opened in write mode
  150.     *   o file couldn't be locked exclusively
  151.     *   o file couldn't be unlocked
  152.     *   o file couldn't be closed
  153.     * 
  154.     * @throws PEAR_Error
  155.     * @access public
  156.     * @return mixed true on success or PEAR_Error
  157.     */
  158.     function save()
  159.     {
  160.         $content = '';
  161.         foreach ($this->_users as $user => $pass) {
  162.             $content .= $user . ':' . $pass . "\n";
  163.         }
  164.         return $this->_save($content);
  165.     }
  166.  
  167.     /** 
  168.     * Add an user
  169.     *
  170.     * The username must start with an alphabetical character and must NOT
  171.     * contain any other characters than alphanumerics, the underline and dash.
  172.     * 
  173.     * Returns a PEAR_Error if:
  174.     *   o user already exists
  175.     *   o user contains illegal characters
  176.     * 
  177.     * @throws PEAR_Error
  178.     * @access public
  179.     * @return mixed true on success or PEAR_Error
  180.     * @param string $user
  181.     * @param string $pass
  182.     */
  183.     function addUser($user, $pass)
  184.     {
  185.         if ($this->userExists($user)) {
  186.             return PEAR::raiseError(
  187.                 sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
  188.                 FILE_PASSWD_E_EXISTS_ALREADY
  189.             );
  190.         }
  191.         if (!preg_match($this->_pcre, $user)) {
  192.             return PEAR::raiseError(
  193.                 sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  194.                 FILE_PASSWD_E_INVALID_CHARS
  195.             );
  196.         }
  197.         $this->_users[$user] = $this->_genPass($pass);
  198.         return true;
  199.     }
  200.  
  201.     /** 
  202.     * Change the password of a certain user
  203.     *
  204.     * Returns a PEAR_Error if user doesn't exist.
  205.     * 
  206.     * @throws PEAR_Error
  207.     * @access public
  208.     * @return mixed true on success or a PEAR_Error
  209.     * @param string $user   the user whose password should be changed
  210.     * @param string $pass   the new plaintext password
  211.     */
  212.     function changePasswd($user, $pass)
  213.     {
  214.         if (!$this->userExists($user)) {
  215.             return PEAR::raiseError(
  216.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  217.                 FILE_PASSWD_E_EXISTS_NOT
  218.             );
  219.         }
  220.         $this->_users[$user] = $this->_genPass($pass);
  221.         return true;
  222.     }
  223.  
  224.     /** 
  225.     * Verify password
  226.     *
  227.     * Returns a PEAR_Error if:
  228.     *   o user doesn't exist
  229.     *   o an invalid encryption mode was supplied
  230.     * 
  231.     * @throws PEAR_Error
  232.     * @access public
  233.     * @return mixed true if passwords equal, false if they don't, or PEAR_Error
  234.     * @param string $user   the user whose password should be verified
  235.     * @param string $pass   the plaintext password to verify
  236.     */
  237.     function verifyPasswd($user, $pass)
  238.     {
  239.         if (!$this->userExists($user)) {
  240.             return PEAR::raiseError(
  241.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  242.                 FILE_PASSWD_E_EXISTS_NOT
  243.             );
  244.         }
  245.         $real = $this->_users[$user];
  246.         return ($real === $this->_genPass($pass, $real));
  247.     }
  248.  
  249.     /** 
  250.     * Get actual encryption mode
  251.     *
  252.     * @access public
  253.     * @return string
  254.     */
  255.     function getMode()
  256.     {
  257.         return $this->_mode;
  258.     }
  259.  
  260.     /** 
  261.     * Get supported encryption modes
  262.     *
  263.     * <pre>
  264.     *   array
  265.     *    + md5
  266.     *    + sha
  267.     *    + des
  268.     * </pre>
  269.     * 
  270.     * ATTN: DES encryption not available on Win32!
  271.     * 
  272.     * @access public
  273.     * @return array
  274.     */
  275.     function listModes()
  276.     {
  277.         return array_keys($this->_modes);
  278.     }
  279.  
  280.     /** 
  281.     * Set the encryption mode
  282.     *
  283.     * You can choose one of md5, sha or des.
  284.     * 
  285.     * ATTN: DES encryption not available on Win32!
  286.     * 
  287.     * Returns a PEAR_Error if a specific encryption mode is not supported.
  288.     * 
  289.     * @throws PEAR_Error
  290.     * @access public
  291.     * @return mixed true on succes or PEAR_Error
  292.     * @param string $mode
  293.     */
  294.     function setMode($mode)
  295.     {
  296.         $mode = strToLower($mode);
  297.         if (!isset($this->_modes[$mode])) {
  298.             return PEAR::raiseError(
  299.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $this->_mode),
  300.                 FILE_PASSWD_E_INVALID_ENC_MODE
  301.             );
  302.         }
  303.         $this->_mode = $mode;
  304.         return true;
  305.     }
  306.  
  307.     /**
  308.     * Generate password with htpasswd executable
  309.     * 
  310.     * @access   private
  311.     * @return   string  the crypted password
  312.     * @param    string  $pass   the plaintext password
  313.     * @param    string  $salt   the salt to use
  314.     * @param    string  $mode   encyption mode, usually determined from
  315.     *                           <var>$this->_mode</var>
  316.     */
  317.     function _genPass($pass, $salt = null, $mode = null)
  318.     {
  319.         $mode = is_null($mode) ? strToLower($this->_mode) : strToLower($mode);
  320.  
  321.         if ($mode == 'md5') {
  322.             return File_Passwd::crypt_apr_md5($pass, $salt);
  323.         } elseif ($mode == 'des') {
  324.             return File_Passwd::crypt_des($pass, $salt);
  325.         } elseif ($mode == 'sha') {
  326.             return File_Passwd::crypt_sha($pass, $salt);
  327.         }
  328.         
  329.         return PEAR::raiseError(
  330.             sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
  331.             FILE_PASSWD_E_INVALID_ENC_MODE                
  332.         );
  333.     }
  334.     
  335.     /** 
  336.     * Parse the AuthUserFile
  337.     * 
  338.     * Returns a PEAR_Error if AuthUserFile has invalid format.
  339.     *
  340.     * @throws PEAR_Error
  341.     * @access public
  342.     * @return mixed true on success or PEAR_error
  343.     */
  344.     function parse()
  345.     {
  346.         $this->_users = array();
  347.         foreach ($this->_contents as $line) {
  348.             $user = explode(':', $line);
  349.             if (count($user) != 2) {
  350.                 return PEAR::raiseError(
  351.                     FILE_PASSWD_E_INVALID_FORMAT_STR,
  352.                     FILE_PASSWD_E_INVALID_FORMAT
  353.                 );
  354.             }
  355.             $this->_users[$user[0]] = trim($user[1]);
  356.         }
  357.         $this->_contents = array();
  358.         return true;
  359.     }
  360.     
  361.     /**
  362.     * Generate Password
  363.     * 
  364.     * Returns PEAR_Error FILE_PASSD_E_INVALID_ENC_MODE if the supplied
  365.     * encryption mode is not supported.
  366.     *
  367.     * @static
  368.     * @access   public
  369.     * @return   mixed   The crypted password on success or PEAR_Error on failure.
  370.     * @param    string  $pass The plaintext password.
  371.     * @param    string  $mode The encryption mode to use (des|md5|sha).
  372.     * @param    string  $salt The salt to use.
  373.     */
  374.     function generatePasswd($pass, $mode = FILE_PASSWD_DES, $salt = null)
  375.     {
  376.         if (!in_array(strToLower($mode), array('des', 'md5', 'sha'))) {
  377.             return PEAR::raiseError(
  378.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
  379.                 FILE_PASSWD_E_INVALID_ENC_MODE                
  380.             );
  381.         }
  382.         return File_Passwd_Authbasic::_genPass($pass, $salt, $mode);
  383.     }
  384.     
  385.     /**
  386.      * @ignore
  387.      * @deprecated
  388.      */
  389.     function generatePassword($pass, $mode = FILE_PASSWD_DES, $salt = null)
  390.     {
  391.         return File_Passwd_Authbasic::generatePasswd($pass, $mode, $salt);
  392.     }
  393. }
  394. ?>