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 / Custom.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  17.8 KB  |  593 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Passwd::Custom
  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: Custom.php,v 1.10 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 custom formatted passwd files
  31. *
  32. * Usage Example:
  33. * <code>
  34. * $cust = &File_Passwd::factory('Custom');
  35. * $cust->setDelim('|');
  36. * $cust->load();
  37. * $cust->setEncFunc(array('File_Passwd', 'crypt_apr_md5'));
  38. * $cust->addUser('mike', 'pass');
  39. * $cust->save();
  40. * </code>
  41. * @author   Michael Wallner <mike@php.net>
  42. * @version  $Revision: 1.10 $
  43. * @access   public
  44. */
  45. class File_Passwd_Custom extends File_Passwd_Common
  46. {
  47.     /**
  48.     * Delimiter
  49.     *
  50.     * @access   private
  51.     * @var      string
  52.     */
  53.     var $_delim = ':';
  54.     
  55.     /**
  56.     * Encryption function
  57.     *
  58.     * @access   private
  59.     * @var      string
  60.     */
  61.     var $_enc = array('File_Passwd', 'crypt_md5');
  62.     
  63.     /**
  64.     * 'name map'
  65.     *
  66.     * @access   private
  67.     * @var      array
  68.     */
  69.     var $_map = array();
  70.     
  71.     /**
  72.     * Whether to use the 'name map' or not
  73.     *
  74.     * @var      boolean
  75.     * @access   private
  76.     */
  77.     var $_usemap = false;
  78.  
  79.     /**
  80.     * Constructor
  81.     *
  82.     * @access   protected
  83.     * @return   object
  84.     */
  85.     function File_Passwd_Custom($file = 'passwd')
  86.     {
  87.         $this->__construct($file);
  88.     }
  89.  
  90.     /**
  91.     * Fast authentication of a certain user
  92.     * 
  93.     * Returns a PEAR_Error if:
  94.     *   o file doesn't exist
  95.     *   o file couldn't be opened in read mode
  96.     *   o file couldn't be locked exclusively
  97.     *   o file couldn't be unlocked (only if auth fails)
  98.     *   o file couldn't be closed (only if auth fails)
  99.     *   o invalid encryption function <var>$opts[0]</var>,
  100.     *     or no delimiter character <var>$opts[1]</var> was provided
  101.     *
  102.     * @throws   PEAR_Error  FILE_PASSWD_E_UNDEFINED |
  103.     *                       FILE_PASSWD_E_FILE_NOT_OPENED |
  104.     *                       FILE_PASSWD_E_FILE_NOT_LOCKED |
  105.     *                       FILE_PASSWD_E_FILE_NOT_UNLOCKED |
  106.     *                       FILE_PASSWD_E_FILE_NOT_CLOSED |
  107.     *                       FILE_PASSWD_E_INVALID_ENC_MODE
  108.     * @static   call this method statically for a reasonable fast authentication
  109.     * @access   public
  110.     * @return   mixed   Returns &true; if authenticated, &false; if not or 
  111.     *                   <classname>PEAR_Error</classname> on failure.
  112.     * @param    string  $file   path to passwd file
  113.     * @param    string  $user   user to authenticate
  114.     * @param    string  $pass   plaintext password
  115.     * @param    array   $otps   encryption function and delimiter charachter
  116.     *                           (in this order)
  117.     */
  118.     function staticAuth($file, $user, $pass, $opts)
  119.     {
  120.         setType($opts, 'array');
  121.         if (count($opts) != 2 || empty($opts[1])) {
  122.             return PEAR::raiseError('Insufficient options.', 0);
  123.         }
  124.         
  125.         $line = File_Passwd_Common::_auth($file, $user, $opts[1]);
  126.         
  127.         if (!$line || PEAR::isError($line)) {
  128.             return $line;
  129.         }
  130.         
  131.         list(,$real)= explode($opts[1], $line);
  132.         $crypted    = File_Passwd_Custom::_genPass($pass, $real, $opts[0]);
  133.         
  134.         if (PEAR::isError($crypted)) {
  135.             return $crypted;
  136.         }
  137.         
  138.         return ($crypted === $real);
  139.     }
  140.  
  141.     /**
  142.     * Set delimiter
  143.     * 
  144.     * You can set a custom char to delimit the columns of a data set.
  145.     * Defaults to a colon (':'). Be aware that this char mustn't be
  146.     * in the values of your data sets.
  147.     *
  148.     * @access   public
  149.     * @return   void
  150.     * @param    string  $delim  custom delimiting character
  151.     */
  152.     function setDelim($delim = ':')
  153.     {
  154.         @setType($delim, 'string');
  155.         if (empty($delim)) {
  156.             $this->_delim = ':';
  157.         } else {
  158.             $this->_delim = $delim{0};
  159.         }
  160.     }
  161.     
  162.     /**
  163.     * Get custom delimiter
  164.     *
  165.     * @access   public
  166.     * @return   string
  167.     */
  168.     function getDelim()
  169.     {
  170.         return $this->_delim;
  171.     }
  172.     
  173.     /**
  174.     * Set encryption function
  175.     *
  176.     * You can set a custom encryption function to use.
  177.     * The supplied function will be called by php's call_user_function(), 
  178.     * so you can supply an array with a method of a class/object, too 
  179.     * (i.e. array('File_Passwd', 'crypt_apr_md5').
  180.     * 
  181.     * 
  182.     * @throws   PEAR_Error          FILE_PASSWD_E_INVALID_ENC_MODE
  183.     * @access   public
  184.     * @return   mixed   Returns &true; on success or 
  185.     *                   <classname>PEAR_Error</classname> on failure.
  186.     * @param    mixed   $function    callable encryption function
  187.     */
  188.     function setEncFunc($function = array('File_Passwd', 'crypt_md5'))
  189.     {
  190.         if (!is_callable($function)) {
  191.             if (is_array($function)) {
  192.                 $function = implode('::', $function);
  193.             }
  194.             return PEAR::raiseError(
  195.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $function),
  196.                 FILE_PASSWD_E_INVALID_ENC_MODE
  197.             );
  198.         }
  199.         
  200.         $this->_enc = $function;
  201.         return true;
  202.     }
  203.     
  204.     /**
  205.     * Get current custom encryption method
  206.     *
  207.     * Possible return values (examples):
  208.     *   o 'md5'
  209.     *   o 'File_Passwd::crypt_md5'
  210.     * 
  211.     * @access   public
  212.     * @return   string
  213.     */
  214.     function getEncFunc()
  215.     {
  216.         if (is_array($this->_enc)) {
  217.             return implode('::', $this->_enc);
  218.         }
  219.         return $this->_enc;
  220.     }
  221.     
  222.     /**
  223.     * Whether to use the 'name map' of the extra properties or not
  224.     * 
  225.     * @see      File_Passwd_Custom::useMap()
  226.     * @see      setMap()
  227.     * @see      getMap()
  228.     * 
  229.     * @access   public
  230.     * @return   boolean always true if you set a value (true/false) OR
  231.     *                   the actual value if called without param
  232.     * 
  233.     * @param    boolean $bool   whether to use the 'name map' or not
  234.     */
  235.     function useMap($bool = null)
  236.     {
  237.         if (is_null($bool)) {
  238.             return $this->_usemap;
  239.         }
  240.         $this->_usemap = (bool) $bool;
  241.         return true;
  242.     }
  243.     
  244.     /**
  245.     * Set the 'name map' to use with the extra properties of the user
  246.     * 
  247.     * This map is used for naming the associative array of the extra properties.
  248.     *
  249.     * Returns a PEAR_Error if <var>$map</var> was not of type array.
  250.     * 
  251.     * @see      getMap()
  252.     * @see      useMap()
  253.     * 
  254.     * @throws   PEAR_Error  FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
  255.     * @access   public
  256.     * @return   mixed       true on success or PEAR_Error
  257.     */
  258.     function setMap($map = array())
  259.     {
  260.         if (!is_array($map)) {
  261.             return PEAR::raiseError(
  262.                 sprintf(FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR, '$map'),
  263.                 FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
  264.             );
  265.         }
  266.         $this->_map = $map;
  267.         return true;
  268.     }
  269.     
  270.     /**
  271.     * Get the 'name map' which is used for the extra properties of the user
  272.     *
  273.     * @see      setMap()
  274.     * @see      useMap()
  275.     * 
  276.     * @access   public
  277.     * @return   array
  278.     */
  279.     function getMap()
  280.     {
  281.         return $this->_map;
  282.     }
  283.  
  284.     /**
  285.     * Apply changes an rewrite passwd file
  286.     *
  287.     * Returns a PEAR_Error if:
  288.     *   o directory in which the file should reside couldn't be created
  289.     *   o file couldn't be opened in write mode
  290.     *   o file couldn't be locked exclusively
  291.     *   o file couldn't be unlocked
  292.     *   o file couldn't be closed
  293.     * 
  294.     * @throws   PEAR_Error  FILE_PASSWD_E_FILE_NOT_OPENED |
  295.     *                       FILE_PASSWD_E_FILE_NOT_LOCKED |
  296.     *                       FILE_PASSWD_E_FILE_NOT_UNLOCKED |
  297.     *                       FILE_PASSWD_E_FILE_NOT_CLOSED
  298.     * @access   public
  299.     * @return   mixed   Returns &true; on success or 
  300.     *                   <classname>PEAR_Error</classname> on failure.
  301.     */
  302.     function save()
  303.     {
  304.         $content = '';
  305.         foreach ($this->_users as $user => $array){
  306.             $pass   = array_shift($array);
  307.             $extra  = implode($this->_delim, $array);
  308.             $content .= $user . $this->_delim . $pass;
  309.             if (!empty($extra)) {
  310.                 $content .= $this->_delim . $extra;
  311.             }
  312.             $content .= "\n";
  313.         }
  314.         return $this->_save($content);
  315.     }
  316.  
  317.     /**
  318.     * Parse the Custom password file
  319.     *
  320.     * Returns a PEAR_Error if passwd file has invalid format.
  321.     * 
  322.     * @throws   PEAR_Error  FILE_PASSWD_E_INVALID_FORMAT
  323.     * @access   public
  324.     * @return   mixed   Returns &true; on success or
  325.     *                   <classname>PEAR_Error</classname> on failure.
  326.     */
  327.     function parse()
  328.     {
  329.         $this->_users = array();
  330.         foreach ($this->_contents as $line){
  331.             $parts = explode($this->_delim, $line);
  332.             if (count($parts) < 2) {
  333.                 return PEAR::raiseError(
  334.                     FILE_PASSWD_E_INVALID_FORMAT_STR,
  335.                     FILE_PASSWD_E_INVALID_FORMAT
  336.                 );
  337.             }
  338.             $user = array_shift($parts);
  339.             $pass = array_shift($parts);
  340.             $values = array();
  341.             if ($this->_usemap) {
  342.                 $values['pass'] = $pass;
  343.                 foreach ($parts as $i => $value){
  344.                     if (isset($this->_map[$i])) {
  345.                         $values[$this->_map[$i]] = $value;
  346.                     } else {
  347.                         $values[$i+1] = $value;
  348.                     }
  349.                 }
  350.             } else {
  351.                 $values = array_merge(array($pass), $parts);
  352.             }
  353.             $this->_users[$user] = $values;
  354.             
  355.         }
  356.         $this->_contents = array();
  357.         return true;
  358.     }
  359.  
  360.     /**
  361.     * Add an user
  362.     *
  363.     * The username must start with an alphabetical character and must NOT
  364.     * contain any other characters than alphanumerics, the underline and dash.
  365.     * 
  366.     * If you use the 'name map' you should also use these naming in
  367.     * the supplied extra array, because your values would get mixed up
  368.     * if they are in the wrong order, which is always true if you
  369.     * DON'T use the 'name map'!
  370.     * 
  371.     * So be warned and USE the 'name map'!
  372.     * 
  373.     * Returns a PEAR_Error if:
  374.     *   o user already exists
  375.     *   o user contains illegal characters
  376.     *   o encryption mode is not supported
  377.     *   o any element of the <var>$extra</var> array contains the delimiter char
  378.     * 
  379.     * @throws   PEAR_Error  FILE_PASSWD_E_EXISTS_ALREADY |
  380.     *                       FILE_PASSWD_E_INVALID_ENC_MODE |
  381.     *                       FILE_PASSWD_E_INVALID_CHARS
  382.     * @access   public
  383.     * @return   mixed   Returns &true; on success or 
  384.     *                   <classname>PEAR_Error</classname> on failure.
  385.     * @param    string  $user   the name of the user to add
  386.     * @param    string  $pass   the password of the user to add
  387.     * @param    array   $extra  extra properties of user to add
  388.     */
  389.     function addUser($user, $pass, $extra = array())
  390.     {
  391.         if ($this->userExists($user)) {
  392.             return PEAR::raiseError(
  393.                 sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
  394.                 FILE_PASSWD_E_EXISTS_ALREADY
  395.             );
  396.         }
  397.         if (!preg_match($this->_pcre, $user) || strstr($user, $this->_delim)) {
  398.             return PEAR::raiseError(
  399.                 sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  400.                 FILE_PASSWD_E_INVALID_CHARS
  401.             );
  402.         }
  403.         if (!is_array($extra)) {
  404.             setType($extra, 'array');
  405.         }
  406.         foreach ($extra as $e){
  407.             if (strstr($e, $this->_delim)) {
  408.                 return PEAR::raiseError(
  409.                     sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
  410.                     FILE_PASSWD_E_INVALID_CHARS
  411.                 );
  412.             }
  413.         }
  414.         
  415.         $pass = $this->_genPass($pass);
  416.         if (PEAR::isError($pass)) {
  417.             return $pass;
  418.         }
  419.         
  420.         /**
  421.         * If you don't use the 'name map' the user array will be numeric.
  422.         */
  423.         if (!$this->_usemap) {
  424.             array_unshift($extra, $pass);
  425.             $this->_users[$user] = $extra;
  426.         } else {
  427.             $map = $this->_map;
  428.             array_unshift($map, 'pass');
  429.             $extra['pass'] = $pass;
  430.             foreach ($map as $key){
  431.                 $this->_users[$user][$key] = @$extra[$key];
  432.             }
  433.         }
  434.         
  435.         return true;
  436.     }
  437.  
  438.     /**
  439.     * Modify properties of a certain user
  440.     *
  441.     * # DON'T MODIFY THE PASSWORD WITH THIS METHOD!
  442.     * 
  443.     * You should use this method only if the 'name map' is used, too.
  444.     * 
  445.     * Returns a PEAR_Error if:
  446.     *   o user doesn't exist
  447.     *   o any property contains the custom delimiter character
  448.     * 
  449.     * @see      changePasswd()
  450.     * 
  451.     * @throws   PEAR_Error  FILE_PASSWD_E_EXISTS_NOT | 
  452.     *                       FILE_PASSWD_E_INVALID_CHARS
  453.     * @access   public
  454.     * @return   mixed       true on success or PEAR_Error
  455.     * @param    string      $user           the user to modify
  456.     * @param    array       $properties     an associative array of 
  457.     *                                       properties to modify
  458.     */
  459.     function modUser($user, $properties = array())
  460.     {
  461.         if (!$this->userExists($user)) {
  462.             return PEAR::raiseError(
  463.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  464.                 FILE_PASSWD_E_EXISTS_NOT
  465.             );
  466.         }
  467.         
  468.         if (!is_array($properties)) {
  469.             setType($properties, 'array');
  470.         }
  471.         
  472.         foreach ($properties as $key => $value){
  473.             if (strstr($value, $this->_delim)) {
  474.                 return PEAR::raiseError(
  475.                     sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  476.                     FILE_PASSWD_E_INVALID_CHARS
  477.                 );
  478.             }
  479.             $this->_users[$user][$key] = $value;
  480.         }
  481.         
  482.         return true;
  483.     }
  484.  
  485.     /**
  486.     * Change the password of a certain user
  487.     *
  488.     * Returns a PEAR_Error if:
  489.     *   o user doesn't exists
  490.     *   o encryption mode is not supported
  491.     * 
  492.     * @throws   PEAR_Error  FILE_PASSWD_E_EXISTS_NOT |
  493.     *                       FILE_PASSWD_E_INVALID_ENC_MODE
  494.     * @access   public
  495.     * @return   mixed   Returns &true; on success or 
  496.     *                   <classname>PEAR_Error</classname> on failure.
  497.     * @param    string  $user   the user whose password should be changed
  498.     * @param    string  $pass   the new plaintext password
  499.     */
  500.     function changePasswd($user, $pass)
  501.     {
  502.         if (!$this->userExists($user)) {
  503.             return PEAR::raiseError(
  504.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  505.                 FILE_PASSWD_E_EXISTS_NOT
  506.             );
  507.         }
  508.         
  509.         $pass = $this->_genPass($pass);
  510.         if (PEAR::isError($pass)) {
  511.             return $pass;
  512.         }
  513.         
  514.         if ($this->_usemap) {
  515.             $this->_users[$user]['pass'] = $pass;
  516.         } else {
  517.             $this->_users[$user][0] = $pass;
  518.         }
  519.         
  520.         return true;
  521.     }
  522.  
  523.     /**
  524.     * Verify the password of a certain user
  525.     * 
  526.     * Returns a PEAR_Error if:
  527.     *   o user doesn't exist
  528.     *   o encryption mode is not supported
  529.     *
  530.     * @throws   PEAR_Error  FILE_PASSWD_E_EXISTS_NOT |
  531.     *                       FILE_PASSWD_E_INVALID_ENC_MODE
  532.     * @access   public
  533.     * @return   mixed   Returns &true; if passwors equal, &false; if they don't 
  534.     *                   or <classname>PEAR_Error</classname> on fialure.
  535.     * @param    string  $user   the user whose password should be verified
  536.     * @param    string  $pass   the password to verify
  537.     */
  538.     function verifyPasswd($user, $pass)
  539.     {
  540.         if (!$this->userExists($user)) {
  541.             return PEAR::raiseError(
  542.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  543.                 FILE_PASSWD_E_EXISTS_NOT
  544.             );
  545.         }
  546.         $real = 
  547.             $this->_usemap ? 
  548.             $this->_users[$user]['pass'] : 
  549.             $this->_users[$user][0]
  550.         ;
  551.         return ($real === $this->_genPass($pass, $real));
  552.     }
  553.     
  554.     /**
  555.     * Generate crypted password from the plaintext password
  556.     *
  557.     * Returns a PEAR_Error if actual encryption mode is not supported.
  558.     * 
  559.     * @throws   PEAR_Error  FILE_PASSWD_E_INVALID_ENC_MODE
  560.     * @access   private
  561.     * @return   mixed   Returns the crypted password or 
  562.     *                   <classname>PEAR_Error</classname>
  563.     * @param    string  $pass   the plaintext password
  564.     * @param    string  $salt   the crypted password from which to gain the salt
  565.     * @param    string  $func   the encryption function to use
  566.     */
  567.     function _genPass($pass, $salt = null, $func = null)
  568.     {
  569.         if (is_null($func)) {
  570.             $func = $this->_enc;
  571.         }
  572.         
  573.         if (!is_callable($func)) {
  574.             if (is_array($func)) {
  575.                 $func = implode('::', $func);
  576.             }
  577.             return PEAR::raiseError(
  578.                 sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $func),
  579.                 FILE_PASSWD_E_INVALID_ENC_MODE
  580.             );
  581.         }
  582.         
  583.         $return = @call_user_func($func, $pass, $salt);
  584.         
  585.         if (is_null($return) || $return === false) {
  586.             $return = @call_user_func($func, $pass);
  587.         }
  588.         
  589.         return $return;
  590.     }
  591. }
  592. ?>