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 / Common.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.7 KB  |  382 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Passwd::Common
  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: Common.php,v 1.18 2005/03/30 18:33:33 mike Exp $
  21.  * @link       http://pear.php.net/package/File_Passwd
  22.  */
  23.  
  24. /**
  25. * Requires System
  26. */
  27. require_once 'System.php';
  28. /**
  29. * Requires File::Passwd
  30. */
  31. require_once 'File/Passwd.php';
  32.  
  33. /**
  34. * Baseclass for File_Passwd_* classes.
  35. * <kbd><u>
  36. *   Provides basic operations:
  37. * </u></kbd>
  38. *   o opening & closing
  39. *   o locking & unlocking
  40. *   o loading & saving
  41. *   o check if user exist
  42. *   o delete a certain user
  43. *   o list users
  44. * @author   Michael Wallner <mike@php.net>
  45. * @package  File_Passwd
  46. * @version  $Revision: 1.18 $
  47. * @access   protected
  48. * @internal extend this class for your File_Passwd_* class
  49. */
  50. class File_Passwd_Common
  51. {
  52.     /**
  53.     * passwd file
  54.     *
  55.     * @var string
  56.     * @access protected
  57.     */
  58.     var $_file = 'passwd';
  59.     
  60.     /**
  61.     * file content
  62.     *
  63.     * @var aray
  64.     * @access protected
  65.     */
  66.     var $_contents = array();
  67.     
  68.     /**
  69.     * users
  70.     *
  71.     * @var array
  72.     * @access protected
  73.     */
  74.     var $_users = array();
  75.     
  76.     /**
  77.     * PCRE for valid chars
  78.     * 
  79.     * @var  string
  80.     * @access   protected
  81.     */
  82.     var $_pcre = '/^[a-z]+[a-z0-9_-]*$/i';
  83.     
  84.     /**
  85.     * Constructor (ZE2)
  86.     *
  87.     * @access protected
  88.     * @param  string    $file   path to passwd file
  89.     */
  90.     function __construct($file = 'passwd')
  91.     {
  92.         $this->setFile($file);
  93.     }
  94.     
  95.     /**
  96.     * Parse the content of the file
  97.     *
  98.     * You must overwrite this method in your File_Passwd_* class.
  99.     * 
  100.     * @abstract
  101.     * @internal
  102.     * @access public
  103.     * @return object    PEAR_Error
  104.     */
  105.     function parse()
  106.     {
  107.         return PEAR::raiseError(
  108.             sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'parse'),
  109.             FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
  110.         );
  111.     }
  112.     
  113.     /**
  114.     * Apply changes and rewrite passwd file
  115.     *
  116.     * You must overwrite this method in your File_Passwd_* class.
  117.     * 
  118.     * @abstract
  119.     * @internal
  120.     * @access public
  121.     * @return object    PEAR_Error
  122.     */
  123.     function save()
  124.     {
  125.         return PEAR::raiseError(
  126.             sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'save'),
  127.             FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
  128.         );
  129.     }
  130.     
  131.     /**
  132.     * Opens a file, locks it exclusively and returns the filehandle
  133.     *
  134.     * Returns a PEAR_Error if:
  135.     *   o directory in which the file should reside couldn't be created
  136.     *   o file couldn't be opened in the desired mode
  137.     *   o file couldn't be locked exclusively
  138.     * 
  139.     * @throws PEAR_Error
  140.     * @access protected
  141.     * @return mixed resource of type file handle or PEAR_Error
  142.     * @param  string    $mode   the mode to open the file with
  143.     */
  144.     function &_open($mode, $file = null)
  145.     {
  146.         isset($file) or $file = $this->_file;
  147.         $dir  = dirname($file);
  148.         $lock = strstr($mode, 'r') ? LOCK_SH : LOCK_EX;
  149.         if (!is_dir($dir) && !System::mkDir('-p -m 0755 ' . $dir)) {
  150.             return PEAR::raiseError(
  151.                 sprintf(FILE_PASSWD_E_DIR_NOT_CREATED_STR, $dir),
  152.                 FILE_PASSWD_E_DIR_NOT_CREATED
  153.             );
  154.         }
  155.         if (!is_resource($fh = @fopen($file, $mode))) {
  156.             return PEAR::raiseError(
  157.                 sprintf(FILE_PASSWD_E_FILE_NOT_OPENED_STR, $file),
  158.                 FILE_PASSWD_E_FILE_NOT_OPENED
  159.             );
  160.         }
  161.         if (!@flock($fh, $lock)) {
  162.             fclose($fh);
  163.             return PEAR::raiseError(
  164.                 sprintf(FILE_PASSWD_E_FILE_NOT_LOCKED_STR, $file),
  165.                 FILE_PASSWD_E_FILE_NOT_LOCKED
  166.             );
  167.         }
  168.         return $fh;
  169.     }
  170.     
  171.     /**
  172.     * Closes a prior opened and locked file handle
  173.     *
  174.     * Returns a PEAR_Error if:
  175.     *   o file couldn't be unlocked
  176.     *   o file couldn't be closed
  177.     * 
  178.     * @throws PEAR_Error
  179.     * @access protected
  180.     * @return mixed true on success or PEAR_Error
  181.     * @param  resource  $file_handle    the file handle to operate on
  182.     */
  183.     function _close(&$file_handle)
  184.     {
  185.         if (!@flock($file_handle, LOCK_UN)) {
  186.             return PEAR::raiseError(
  187.                 FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR,
  188.                 FILE_PASSWD_E_FILE_NOT_UNLOCKED
  189.             );
  190.         }
  191.         if (!@fclose($file_handle)) {
  192.             return PEAR::raiseError(
  193.                 FILE_PASSWD_E_FILE_NOT_CLOSED_STR,
  194.                 FILE_PASSWD_E_FILE_NOT_CLOSED
  195.             );
  196.         }
  197.         return true;
  198.     }
  199.     
  200.     /**
  201.     * Loads the file
  202.     *
  203.     * Returns a PEAR_Error if:
  204.     *   o directory in which the file should reside couldn't be created
  205.     *   o file couldn't be opened in read mode
  206.     *   o file couldn't be locked exclusively
  207.     *   o file couldn't be unlocked
  208.     *   o file couldn't be closed
  209.     * 
  210.     * @throws PEAR_Error
  211.     * @access public
  212.     * @return mixed true on success or PEAR_Error
  213.     */
  214.     function load()
  215.     {
  216.         $fh = &$this->_open('r');
  217.         if (PEAR::isError($fh)) {
  218.             return $fh;
  219.         }
  220.         $this->_contents = array();
  221.         while ($line = fgets($fh)) {
  222.             if (!preg_match('/^\s*#/', $line) && $line = trim($line)) {
  223.                 $this->_contents[] = $line;
  224.             }
  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.     * @param    string      $sep    field separator
  354.     */
  355.     function _auth($file, $id, $sep = ':')
  356.     {
  357.         $file = realpath($file);
  358.         if (!is_file($file)) {
  359.             return PEAR::raiseError("File '$file' couldn't be found.", 0);
  360.         }
  361.         $fh = &File_Passwd_Common::_open('r', $file);
  362.         if (PEAR::isError($fh)) {
  363.             return $fh;
  364.         }
  365.         $cmp = $id . $sep;
  366.         $len = strlen($cmp);
  367.         while ($line = fgets($fh)) {
  368.             if (!strncmp($line, $cmp, $len)) {
  369.                 File_Passwd_Common::_close($fh);
  370.                 return trim($line);
  371.             }
  372.         }
  373.         $e = File_Passwd_Common::_close($fh);
  374.         if (PEAR::isError($e)) {
  375.             return $e;
  376.         }
  377.         return false;
  378.     }
  379. }
  380. ?>