home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / File / Passwd / Common.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  9.8 KB  |  380 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: File :: Passwd :: Common                                     |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Common.php,v 1.12 2004/06/07 19:19:47 mike Exp $
  15.  
  16. /**
  17. * Baseclass for File_Passwd_* classes.
  18. * @author   Michael Wallner <mike@php.net>
  19. * @package  File_Passwd
  20. */
  21.  
  22. /**
  23. * Requires System
  24. */
  25. require_once 'System.php';
  26. /**
  27. * Requires File::Passwd
  28. */
  29. require_once 'File/Passwd.php';
  30.  
  31. /**
  32. * Baseclass for File_Passwd_* classes.
  33. * <kbd><u>
  34. *   Provides basic operations:
  35. * </u></kbd>
  36. *   o opening & closing
  37. *   o locking & unlocking
  38. *   o loading & saving
  39. *   o check if user exist
  40. *   o delete a certain user
  41. *   o list users
  42. * @author   Michael Wallner <mike@php.net>
  43. * @package  File_Passwd
  44. * @version  $Revision: 1.12 $
  45. * @access   protected
  46. * @internal extend this class for your File_Passwd_* class
  47. */
  48. class File_Passwd_Common
  49. {
  50.     /**
  51.     * passwd file
  52.     *
  53.     * @var string
  54.     * @access protected
  55.     */
  56.     var $_file = 'passwd';
  57.     
  58.     /**
  59.     * file content
  60.     *
  61.     * @var aray
  62.     * @access protected
  63.     */
  64.     var $_contents = array();
  65.     
  66.     /**
  67.     * users
  68.     *
  69.     * @var array
  70.     * @access protected
  71.     */
  72.     var $_users = array();
  73.     
  74.     /**
  75.     * PCRE for valid chars
  76.     * 
  77.     * @var  string
  78.     * @access   protected
  79.     */
  80.     var $_pcre = '/^[a-z]+[a-z0-9_-]*$/i';
  81.     
  82.     /**
  83.     * Constructor (ZE2)
  84.     *
  85.     * @access protected
  86.     * @param  string    $file   path to passwd file
  87.     */
  88.     function __construct($file = 'passwd')
  89.     {
  90.         $this->setFile($file);
  91.     }
  92.     
  93.     /**
  94.     * Parse the content of the file
  95.     *
  96.     * You must overwrite this method in your File_Passwd_* class.
  97.     * 
  98.     * @abstract
  99.     * @internal
  100.     * @access public
  101.     * @return object    PEAR_Error
  102.     */
  103.     function parse()
  104.     {
  105.         return PEAR::raiseError(
  106.             sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'parse'),
  107.             FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
  108.         );
  109.     }
  110.     
  111.     /**
  112.     * Apply changes and rewrite passwd file
  113.     *
  114.     * You must overwrite this method in your File_Passwd_* class.
  115.     * 
  116.     * @abstract
  117.     * @internal
  118.     * @access public
  119.     * @return object    PEAR_Error
  120.     */
  121.     function save()
  122.     {
  123.         return PEAR::raiseError(
  124.             sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'save'),
  125.             FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
  126.         );
  127.     }
  128.     
  129.     /**
  130.     * Opens a file, locks it exclusively and returns the filehandle
  131.     *
  132.     * Returns a PEAR_Error if:
  133.     *   o directory in which the file should reside couldn't be created
  134.     *   o file couldn't be opened in the desired mode
  135.     *   o file couldn't be locked exclusively
  136.     * 
  137.     * @throws PEAR_Error
  138.     * @access protected
  139.     * @return mixed resource of type file handle or PEAR_Error
  140.     * @param  string    $mode   the mode to open the file with
  141.     */
  142.     function &_open($mode, $file = null)
  143.     {
  144.         $file   = realpath( is_null($file) ? $this->_file : $file );
  145.         $dir    = dirname($file);
  146.         $lock   = strstr($mode, 'r') ? LOCK_SH : LOCK_EX;
  147.         if (!is_dir($dir) && !System::mkDir('-p -m 0755 ' . $dir)) {
  148.             return PEAR::raiseError(
  149.                 sprintf(FILE_PASSWD_E_DIR_NOT_CREATED_STR, $dir),
  150.                 FILE_PASSWD_E_DIR_NOT_CREATED
  151.             );
  152.         }
  153.         if (!is_resource($fh = @fopen($file, $mode))) {
  154.             return PEAR::raiseError(
  155.                 sprintf(FILE_PASSWD_E_FILE_NOT_OPENED_STR, $file),
  156.                 FILE_PASSWD_E_FILE_NOT_OPENED
  157.             );
  158.         }
  159.         if (!@flock($fh, $lock)) {
  160.             fclose($fh);
  161.             return PEAR::raiseError(
  162.                 sprintf(FILE_PASSWD_E_FILE_NOT_LOCKED_STR, $file),
  163.                 FILE_PASSWD_E_FILE_NOT_LOCKED
  164.             );
  165.         }
  166.         return $fh;
  167.     }
  168.     
  169.     /**
  170.     * Closes a prior opened and locked file handle
  171.     *
  172.     * Returns a PEAR_Error if:
  173.     *   o file couldn't be unlocked
  174.     *   o file couldn't be closed
  175.     * 
  176.     * @throws PEAR_Error
  177.     * @access protected
  178.     * @return mixed true on success or PEAR_Error
  179.     * @param  resource  $file_handle    the file handle to operate on
  180.     */
  181.     function _close(&$file_handle)
  182.     {
  183.         if (!@flock($file_handle, LOCK_UN)) {
  184.             return PEAR::raiseError(
  185.                 FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR,
  186.                 FILE_PASSWD_E_FILE_NOT_UNLOCKED
  187.             );
  188.         }
  189.         if (!@fclose($file_handle)) {
  190.             return PEAR::raiseError(
  191.                 FILE_PASSWD_E_FILE_NOT_CLOSED_STR,
  192.                 FILE_PASSWD_E_FILE_NOT_CLOSED
  193.             );
  194.         }
  195.         return true;
  196.     }
  197.     
  198.     /**
  199.     * Loads the file
  200.     *
  201.     * Returns a PEAR_Error if:
  202.     *   o directory in which the file should reside couldn't be created
  203.     *   o file couldn't be opened in read mode
  204.     *   o file couldn't be locked exclusively
  205.     *   o file couldn't be unlocked
  206.     *   o file couldn't be closed
  207.     * 
  208.     * @throws PEAR_Error
  209.     * @access public
  210.     * @return mixed true on success or PEAR_Error
  211.     */
  212.     function load()
  213.     {
  214.         $fh = &$this->_open('r');
  215.         if (PEAR::isError($fh)) {
  216.             return $fh;
  217.         }
  218.         $this->_contents = array();
  219.         while ($line = fgets($fh)) {
  220.             $line = trim(preg_replace('/^(\S*.*)#.*$/', '\\1', $line));
  221.             if (empty($line)) {
  222.                 continue;
  223.             }
  224.             $this->_contents[] = $line;
  225.         }
  226.         $e = $this->_close($fh);
  227.         if (PEAR::isError($e)) {
  228.             return $e;
  229.         }
  230.         return $this->parse();
  231.     }
  232.     
  233.     /**
  234.     * Save the modified content to the passwd file
  235.     *
  236.     * Returns a PEAR_Error if:
  237.     *   o directory in which the file should reside couldn't be created
  238.     *   o file couldn't be opened in write mode
  239.     *   o file couldn't be locked exclusively
  240.     *   o file couldn't be unlocked
  241.     *   o file couldn't be closed
  242.     * 
  243.     * @throws PEAR_Error
  244.     * @access protected
  245.     * @return mixed true on success or PEAR_Error
  246.     */
  247.     function _save($content)
  248.     {
  249.         $fh = &$this->_open('w');
  250.         if (PEAR::isError($fh)) {
  251.             return $fh;
  252.         }
  253.         fputs($fh, $content);
  254.         return $this->_close($fh);
  255.     }
  256.     
  257.     /**
  258.     * Set path to passwd file
  259.     *
  260.     * @access public
  261.     * @return void
  262.     */
  263.     function setFile($file)
  264.     {
  265.         $this->_file = $file;
  266.     }
  267.     
  268.     /**
  269.     * Get path of passwd file
  270.     *
  271.     * @access public
  272.     * @return string
  273.     */
  274.     function getFile()
  275.     {
  276.         return $this->_file;
  277.     }
  278.  
  279.     /**
  280.     * Check if a certain user already exists
  281.     *
  282.     * @access public
  283.     * @return bool
  284.     * @param  string    $user   the name of the user to check if already exists
  285.     */
  286.     function userExists($user)
  287.     {
  288.         return isset($this->_users[$user]);
  289.     }
  290.     
  291.     /**
  292.     * Delete a certain user
  293.     *
  294.     * Returns a PEAR_Error if user doesn't exist.
  295.     * 
  296.     * @throws PEAR_Error
  297.     * @access public
  298.     * @return mixed true on success or PEAR_Error
  299.     * @param  string    
  300.     */
  301.     function delUser($user)
  302.     {
  303.         if (!$this->userExists($user)) {
  304.             return PEAR::raiseError(
  305.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  306.                 FILE_PASSWD_E_EXISTS_NOT
  307.             );
  308.         }
  309.         unset($this->_users[$user]);
  310.         return true;
  311.     }
  312.     
  313.     /**
  314.     * List user
  315.     *
  316.     * Returns a PEAR_Error if <var>$user</var> doesn't exist.
  317.     * 
  318.     * @throws PEAR_Error
  319.     * @access public
  320.     * @return mixed array of a/all user(s) or PEAR_Error
  321.     * @param  string    $user   the user to list or all users if empty
  322.     */
  323.     function listUser($user = '')
  324.     {
  325.         if (empty($user)) {
  326.             return $this->_users;
  327.         }
  328.         if (!$this->userExists($user)) {
  329.             return PEAR::raiseError(
  330.                 sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
  331.                 FILE_PASSWD_E_EXISTS_NOT
  332.             );
  333.         }
  334.         return $this->_users[$user];
  335.     }
  336.  
  337.     /**
  338.     * Base method for File_Passwd::staticAuth()
  339.     * 
  340.     * Returns a PEAR_Error if:
  341.     *   o file doesn't exist
  342.     *   o file couldn't be opened in read mode
  343.     *   o file couldn't be locked exclusively
  344.     *   o file couldn't be unlocked (only if auth fails)
  345.     *   o file couldn't be closed (only if auth fails)
  346.     * 
  347.     * @throws   PEAR_Error
  348.     * @access   protected
  349.     * @return   mixed       line of passwd file containing <var>$id</var>,
  350.     *                       false if <var>$id</var> wasn't found or PEAR_Error
  351.     * @param    string      $file   path to passwd file
  352.     * @param    string      $id     user_id to search for
  353.     */
  354.     function _auth($file, $id)
  355.     {
  356.         $file = realpath($file);
  357.         if (!is_file($file)) {
  358.             return PEAR::raiseError("File '$file' couldn't be found.", 0);
  359.         }
  360.         $fh = &File_Passwd_Common::_open('r', $file);
  361.         if (PEAR::isError($fh)) {
  362.             return $fh;
  363.         }
  364.         while ($line = fgets($fh)) {
  365.             if (strstr($line, $id)) {
  366.                 File_Passwd_Common::_close($fh);
  367.                 return trim($line);
  368.             }
  369.         }
  370.         $e = File_Passwd_Common::_close($fh);
  371.         if (PEAR::isError($e)) {
  372.             return $e;
  373.         }
  374.         return false;
  375.     }
  376. }
  377. ?>