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 / Cvs.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.1 KB  |  304 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Passwd::Cvs
  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: Cvs.php,v 1.14 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 CVS pserver passwd files.
  31. * <kbd><u>
  32. *   A line of a CVS pserver passwd file consists of 2 to 3 colums:
  33. * </u></kbd>
  34. * <pre>
  35. *   user1:1HCoDDWxK9tbM:sys_user1
  36. *   user2:0O0DYYdzjCVxs
  37. *   user3:MIW9UUoifhqRo:sys_user2
  38. * </pre>
  39. * If the third column is specified, the CVS user named in the first column is 
  40. * mapped to the corresponding system user named in the third column.
  41. * That doesn't really affect us - just for your interest :)
  42. * <kbd><u>Output of listUser()</u></kbd>
  43. * <pre>
  44. *      array
  45. *       + user =>  array
  46. *                   + passwd => crypted_passwd
  47. *                   + system => system_user
  48. *       + user =>  array
  49. *                   + passwd => crypted_passwd
  50. *                   + system => system_user
  51. * </pre>
  52. * @author   Michael Wallner <mike@php.net>
  53. * @package  File_Passwd
  54. * @version  $Revision: 1.14 $
  55. * @access   public
  56. */
  57. class File_Passwd_Cvs extends File_Passwd_Common
  58. {
  59.     /**
  60.     * Constructor
  61.     *
  62.     * @access public
  63.     */
  64.     function File_Passwd_Cvs($file = 'passwd')
  65.     {
  66.         parent::__construct($file);
  67.     }
  68.     
  69.     /**
  70.     * Fast authentication of a certain user
  71.     * 
  72.     * Returns a PEAR_Error if:
  73.     *   o file doesn't exist
  74.     *   o file couldn't be opened in read mode
  75.     *   o file couldn't be locked exclusively
  76.     *   o file couldn't be unlocked (only if auth fails)
  77.     *   o file couldn't be closed (only if auth fails)
  78.     *
  79.     * @static   call this method statically for a reasonable fast authentication
  80.     * @access   public
  81.     * @return   mixed   true if authenticated, false if not or PEAR_Error
  82.     * @param    string  $file   path to passwd file
  83.     * @param    string  $user   user to authenticate
  84.     * @param    string  $pass   plaintext password
  85.     */
  86.     function staticAuth($file, $user, $pass)
  87.     {
  88.         $line = File_Passwd_Common::_auth($file, $user);
  89.         if (!$line || PEAR::isError($line)) {
  90.             return $line;
  91.         }
  92.         @list(,$real)   = explode(':', $line);
  93.         return (File_Passwd_Cvs::generatePassword($pass, $real) === $real);
  94.     }
  95.     
  96.     /**
  97.     * Apply changes and rewrite CVS passwd file
  98.     *
  99.     * Returns a PEAR_Error if:
  100.     *   o directory in which the file should reside couldn't be created
  101.     *   o file couldn't be opened in write mode
  102.     *   o file couldn't be locked exclusively
  103.     *   o file couldn't be unlocked
  104.     *   o file couldn't be closed
  105.     * 
  106.     * @throws PEAR_Error
  107.     * @access public
  108.     * @return mixed true on success or PEAR_Error
  109.     */
  110.     function save()
  111.     {
  112.         $content = '';
  113.         foreach ($this->_users as $user => $v){
  114.             $content .= $user . ':' . $v['passwd'];
  115.             if (isset($v['system']) && !empty($v['system'])) {
  116.                 $content .= ':' . $v['system'];
  117.             }
  118.             $content .= "\n";
  119.         }
  120.         return $this->_save($content);
  121.     }
  122.     
  123.     /** 
  124.     * Parse the CVS passwd file
  125.     *
  126.     * Returns a PEAR_Error if passwd file has invalid format.
  127.     * 
  128.     * @throws PEAR_Error
  129.     * @access public
  130.     * @return mixed true on success or PEAR_Error
  131.     */
  132.     function parse()
  133.     {
  134.         $this->_users = array();
  135.         foreach ($this->_contents as $line) {
  136.             $user = explode(':', $line);
  137.             if (count($user) < 2) {
  138.                 return PEAR::raiseError(
  139.                     FILE_PASSWD_E_INVALID_FORMAT_STR,
  140.                     FILE_PASSWD_E_INVALID_FORMAT
  141.                 );
  142.             }
  143.             @list($user, $pass, $system) = $user;
  144.             $this->_users[$user]['passwd'] = $pass;
  145.             if (!empty($system)) {
  146.                 $this->_users[$user]['system'] = $system;
  147.             }
  148.         }
  149.         $this->_contents = array();
  150.         return true;
  151.     }
  152.  
  153.     /**
  154.     * Add an user
  155.     *
  156.     * The username must start with an alphabetical character and must NOT
  157.     * contain any other characters than alphanumerics, the underline and dash.
  158.     * 
  159.     * Returns a PEAR_Error if:
  160.     *   o user already exists
  161.     *   o user or system_user contains illegal characters
  162.     * 
  163.     * @throws PEAR_Error
  164.     * @access public
  165.     * @return mixed true on success or PEAR_Error
  166.     * @param  string    $user           the name of the user to add
  167.     * @param  string    $pass           the password of the user tot add
  168.     * @param  string    $system_user    the systemuser this user maps to
  169.     */
  170.     function addUser($user, $pass, $system_user = '')
  171.     {
  172.         if ($this->userExists($user)) {
  173.             return PEAR::raiseError(
  174.                 sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
  175.                 FILE_PASSWD_E_EXISTS_ALREADY
  176.             );
  177.         }
  178.         if (!preg_match($this->_pcre, $user)) {
  179.             return PEAR::raiseError(
  180.                 sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
  181.                 FILE_PASSWD_E_INVALID_CHARS
  182.             );
  183.         }
  184.         @setType($system_user, 'string');
  185.         if (!empty($system_user) && !preg_match($this->_pcre, $system_user)) {
  186.             return PEAR::raiseError(
  187.                 sprintf(
  188.                     FILE_PASSWD_E_INVALID_CHARS_STR, 
  189.                     'System user ', 
  190.                     $system_user
  191.                 ),
  192.                 FILE_PASSWD_E_INVALID_CHARS
  193.             );
  194.         }
  195.         $this->_users[$user]['passwd'] = $this->generatePassword($pass);
  196.         $this->_users[$user]['system'] = $system_user;
  197.         return true;
  198.     }
  199.     
  200.     /**
  201.     * Verify the password of a certain user
  202.     *
  203.     * Returns a PEAR_Error if the user doesn't exist.
  204.     * 
  205.     * @throws PEAR_Error
  206.     * @access public
  207.     * @return mixed true if passwords equal, false ifthe don't or PEAR_Error
  208.     * @param  string    $user   user whose password should be verified
  209.     * @param  string    $pass   the plaintext password that should be verified
  210.     */
  211.     function verifyPasswd($user, $pass)
  212.     {
  213.         if (!$this->userExists($user)) {
  214.             return PEAR::raiseError(
  215.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  216.                 FILE_PASSWD_E_EXISTS_NOT
  217.             );
  218.         }
  219.         $real = $this->_users[$user]['passwd'];
  220.         return ($real === $this->generatePassword($pass, $real));
  221.     }
  222.     
  223.     /**
  224.     * Change the password of a certain user
  225.     *
  226.     * Returns a PEAR_Error if user doesn't exist.
  227.     * 
  228.     * @throws PEAR_Error
  229.     * @access public
  230.     * @return mixed true on success or PEAR_Error
  231.     */
  232.     function changePasswd($user, $pass)
  233.     {
  234.         if (!$this->userExists($user)) {
  235.             return PEAR::raiseError(
  236.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  237.                 FILE_PASSWD_E_EXISTS_NOT
  238.             );
  239.         }
  240.         $this->_users[$user]['passwd'] = $this->generatePassword($pass);
  241.         return true;
  242.     }
  243.     
  244.     /**
  245.     * Change the corresponding system user of a certain cvs user
  246.     *
  247.     * Returns a PEAR_Error if:
  248.     *   o user doesn't exist
  249.     *   o system user contains illegal characters
  250.     * 
  251.     * @throws PEAR_Error
  252.     * @access public
  253.     * @return mixed true on success or PEAR_Error
  254.     */
  255.     function changeSysUser($user, $system)
  256.     {
  257.         if (!$this->userExists($user)) {
  258.             return PEAR::raiseError(
  259.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  260.                 FILE_PASSWD_E_EXISTS_NOT
  261.             );
  262.         }
  263.         if (!preg_match($this->_pcre, $system)) {
  264.             return PEAR::raiseError(
  265.                 sprintf(
  266.                     FILE_PASSWD_E_INVALID_CHARS_STR, 
  267.                     'System user ', 
  268.                     $system_user
  269.                 ),
  270.                 FILE_PASSWD_E_INVALID_CHARS
  271.             );
  272.         }
  273.         $this->_users[$user]['system'] = $system;
  274.         return true;
  275.     }
  276.     
  277.     /**
  278.     * Generate crypted password
  279.     *
  280.     * @static
  281.     * @access public
  282.     * @return string    the crypted password
  283.     * @param  string    $pass   new plaintext password
  284.     * @param  string    $salt   new crypted password from which to gain the salt
  285.     */
  286.     function generatePasswd($pass, $salt = null)
  287.     {
  288.         return File_Passwd::crypt_des($pass, $salt);
  289.     }
  290.     
  291.     /**
  292.      * @ignore
  293.      * @deprecated
  294.      */
  295.     function generatePassword($pass, $salt = null)
  296.     {
  297.         return File_Passwd_Cvs::generatePasswd($pass, $salt);
  298.     }
  299. }
  300. ?>