home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / File / Passwd.php < prev    next >
PHP Script  |  2001-07-13  |  6KB  |  209 lines

  1. <?php
  2. /* vim: set ts=4 sw=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Passwd.php,v 1.4 2001/07/13 17:56:50 mj Exp $
  20. //
  21. // Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files
  22.  
  23. require_once 'PEAR.php' ;
  24.  
  25. /**
  26. * Class to manage passwd-style files
  27. *
  28. * @author Rasmus Lerdorf <rasmus@php.net>
  29. */
  30. class File_Passwd {
  31.     
  32.     /**
  33.     * Passwd file
  34.     * @var string
  35.     */
  36.     var $filename ;
  37.     
  38.     /**
  39.     * Hash list of users
  40.     * @var array
  41.     */
  42.     var $users ;
  43.     
  44.     /**
  45.     * hash list of csv-users
  46.     * @var array
  47.     */
  48.     var $cvs ;
  49.     
  50.     /**
  51.     * filehandle for lockfile
  52.     * @var int
  53.     */
  54.     var $fplock ;
  55.     
  56.     /**
  57.     * locking state
  58.     * @var boolean
  59.     */
  60.     var $locked ;
  61.     
  62.     /**
  63.     * name of the lockfile
  64.     * @var string    
  65.     */ 
  66.     var $lockfile = './passwd.lock';
  67.  
  68.     /**
  69.     * Constructor
  70.     * Requires the name of the passwd file. This functions opens the file and read it.
  71.     * Changes to this file will written first in the lock file, so it is still possible
  72.     * to access the passwd file by another programs. The lock parameter controls the locking
  73.     * oft the lockfile, not of the passwd file! ( Swapping $lock and $lockfile would
  74.     * breaks bc to v1.3 and smaller).
  75.     * Don't forget to call close() to save changes!
  76.     * 
  77.     * @param $file        name of the passwd file
  78.     * @param $lock        if 'true' $lockfile will be locked
  79.     * @param $lockfile    name of the temp file, where changes are saved
  80.     *
  81.     * @access public
  82.     * @see close() 
  83.     */
  84.  
  85.     function File_Passwd($file, $lock=0, $lockfile="") {
  86.         $this->filename = $file;
  87.         if( !empty( $lockfile) ) {
  88.             $this->lockfile = $lockfile ;
  89.             }
  90.  
  91.         $this->fplock = fopen($this->lockfile, 'w');
  92.         if($lock) {
  93.             flock($this->fplock, LOCK_EX);
  94.             $this->locked = true;
  95.         }
  96.  
  97.         $fp = fopen($file,'r') ;
  98.         if( !$fp) {
  99.             return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
  100.             }
  101.         while(!feof($fp)) {
  102.             $line = fgets($fp, 128);
  103.             list($user,$pass,$cvsuser) = explode(':',$line);
  104.             if(strlen($user)) {
  105.                 $this->users[$user] = $pass;
  106.                 $this->cvs[$user] = trim($cvsuser);    
  107.             }
  108.         }
  109.         fclose($fp);
  110.     } // end func File_Passwd()
  111.  
  112.     /**
  113.     * Adds a user
  114.     *
  115.     * @param $user new user id
  116.     * @param $pass password for new user
  117.     * @param $cvs  cvs user id (needed for pserver passwd files)
  118.     *
  119.     * @return mixed returns PEAR_Error, if the user already exists
  120.     * @access public
  121.     */
  122.     function addUser($user,$pass,$cvsuser="") {
  123.         if(!isset($this->users[$user]) && $this->locked) {
  124.             $this->users[$user] = crypt($pass);
  125.             $this->cvs[$user] = $cvsuser;
  126.             return true;
  127.         } else {
  128.             return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN) ;
  129.         }
  130.     } // end func addUser()
  131.  
  132.     /**
  133.     * Modifies a user
  134.     *
  135.     * @param $user user id
  136.     * @param $pass new password for user
  137.     * @param $cvs  cvs user id (needed for pserver passwd files)
  138.     *
  139.     * @return mixed returns PEAR_Error, if the user doesn't exists
  140.     * @access public
  141.     */
  142.     
  143.     function modUser($user,$pass,$cvsuser="") {
  144.         if(isset($this->users[$user]) && $this->locked) {
  145.             $this->users[$user] = crypt($pass);
  146.             $this->cvs[$user] = $cvsuser;
  147.             return true;
  148.         } else {
  149.             return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
  150.         }
  151.     } // end func modUser()
  152.  
  153.     /**
  154.     * Deletes a user
  155.     *
  156.     * @param $user user id
  157.     *
  158.     * @return mixed returns PEAR_Error, if the user doesn't exists
  159.     * @access public    
  160.     */
  161.     
  162.     function delUser($user) {
  163.         if(isset($this->users[$user]) && $this->locked) {
  164.             unset($this->users[$user]);
  165.             unset($this->cvs[$user]);
  166.         } else {
  167.             return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ; 
  168.         }    
  169.     } // end func delUser()
  170.  
  171.     /**
  172.     * Verifies a user's password
  173.     *
  174.     * @param $user user id
  175.     * @param $pass password for user
  176.     *
  177.     * @return boolean true if password is ok
  178.     * @access public        
  179.     */
  180.     function verifyPassword($user,$pass) {
  181.         if(isset($this->users[$user])) {
  182.             if($this->users[$user] == crypt($pass,substr($this->users[$user],0,2))) return true;
  183.         }
  184.         return false;
  185.     } // end func verifyPassword()
  186.  
  187.     /**
  188.     * Writes changes to passwd file and unlocks it
  189.     *
  190.     * @access public            
  191.     */
  192.     function close() {
  193.         if($this->locked) {
  194.             foreach($this->users as $user => $pass) {
  195.                 if($this->cvs[$user]) {
  196.                     fputs($this->fplock, "$user:$pass:".$this->cvs[$user]."\n");
  197.                 } else {
  198.                     fputs($this->fplock, "$user:$pass\n");
  199.                 }
  200.             }
  201.             rename($this->lockfile,$this->filename);
  202.             flock($this->fplock, LOCK_UN);
  203.             $this->locked = false;
  204.             fclose($this->fplock);
  205.         }
  206.     } // end func close()
  207. }
  208. ?>
  209.