📄 unix.php
字号:
<?php/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: *//** * File::Passwd::Unix * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category FileFormats * @package File_Passwd * @author Michael Wallner <mike@php.net> * @copyright 2003-2005 Michael Wallner * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Unix.php,v 1.17 2005/03/30 18:33:33 mike Exp $ * @link http://pear.php.net/package/File_Passwd *//*** Requires File::Passwd::Common*/require_once 'File/Passwd/Common.php';/*** Manipulate standard Unix passwd files.* * <kbd><u>Usage Example:</u></kbd>* <code>* $passwd = &File_Passwd::factory('Unix');* $passwd->setFile('/my/passwd/file');* $passwd->load();* $passwd->addUser('mike', 'secret');* $passwd->save();* </code>* * * <kbd><u>Output of listUser()</u></kbd>* # using the 'name map':* <pre>* array* + user => array* + pass => crypted_passwd or 'x' if shadowed* + uid => user id* + gid => group id* + gecos => comments* + home => home directory* + shell => standard shell* </pre>* # without 'name map':* <pre>* array* + user => array* + 0 => crypted_passwd* + 1 => ...* + 2 => ...* </pre>* * @author Michael Wallner <mike@php.net>* @package File_Passwd* @version $Revision: 1.17 $* @access public*/class File_Passwd_Unix extends File_Passwd_Common{ /** * A 'name map' wich refer to the extra properties * * @var array * @access private */ var $_map = array('uid', 'gid', 'gecos', 'home', 'shell'); /** * Whether to use the 'name map' or not * * @var boolean * @access private */ var $_usemap = true; /** * Whether the passwords of this passwd file are shadowed in another file * * @var boolean * @access private */ var $_shadowed = false; /** * Encryption mode, either md5 or des * * @var string * @access private */ var $_mode = 'des'; /** * Supported encryption modes * * @var array * @access private */ var $_modes = array('md5' => 'md5', 'des' => 'des'); /** * Constructor * * @access public * @param string $file path to passwd file */ function File_Passwd_Unix($file = 'passwd') { parent::__construct($file); } /** * Fast authentication of a certain user * * Returns a PEAR_Error if: * o file doesn't exist * o file couldn't be opened in read mode * o file couldn't be locked exclusively * o file couldn't be unlocked (only if auth fails) * o file couldn't be closed (only if auth fails) * o invalid encryption mode <var>$mode</var> was provided * * @static call this method statically for a reasonable fast authentication * @access public * @return mixed true if authenticated, false if not or PEAR_Error * @param string $file path to passwd file * @param string $user user to authenticate * @param string $pass plaintext password * @param string $mode encryption mode to use (des or md5) */ function staticAuth($file, $user, $pass, $mode) { $line = File_Passwd_Common::_auth($file, $user); if (!$line || PEAR::isError($line)) { return $line; } list(,$real)= explode(':', $line); $crypted = File_Passwd_Unix::_genPass($pass, $real, $mode); if (PEAR::isError($crypted)) { return $crypted; } return ($crypted === $real); } /** * Apply changes an rewrite passwd file * * Returns a PEAR_Error if: * o directory in which the file should reside couldn't be created * o file couldn't be opened in write mode * o file couldn't be locked exclusively * o file couldn't be unlocked * o file couldn't be closed * * @throws PEAR_Error * @access public * @return mixed true on success or PEAR_Error */ function save() { $content = ''; foreach ($this->_users as $user => $array){ $pass = array_shift($array); $extra = implode(':', $array); $content .= $user . ':' . $pass; if (!empty($extra)) { $content .= ':' . $extra; } $content .= "\n"; } return $this->_save($content); } /** * Parse the Unix password file * * Returns a PEAR_Error if passwd file has invalid format. * * @throws PEAR_Error * @access public * @return mixed true on success or PEAR_Error */ function parse() { $this->_users = array(); foreach ($this->_contents as $line){ $parts = explode(':', $line); if (count($parts) < 2) { return PEAR::raiseError( FILE_PASSWD_E_INVALID_FORMAT_STR, FILE_PASSWD_E_INVALID_FORMAT ); } $user = array_shift($parts); $pass = array_shift($parts); if ($pass == 'x') { $this->_shadowed = true; } $values = array(); if ($this->_usemap) { $values['pass'] = $pass; foreach ($parts as $i => $value){ if (isset($this->_map[$i])) { $values[$this->_map[$i]] = $value; } else { $values[$i+1] = $value; } } } else { $values = array_merge(array($pass), $parts); } $this->_users[$user] = $values; } $this->_contents = array(); return true; } /** * Set the encryption mode * * Supported encryption modes are des and md5. * * Returns a PEAR_Error if supplied encryption mode is not supported. * * @see setMode() * @see listModes() * * @throws PEAR_Error * @access public * @return mixed true on succes or PEAR_Error * @param string $mode encryption mode to use; either md5 or des */ function setMode($mode) { $mode = strToLower($mode); if (!isset($this->_modes[$mode])) { return PEAR::raiseError( sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode), FILE_PASSWD_E_INVALID_ENC_MODE ); } $this->_mode = $mode; return true; } /** * Get supported encryption modes * * <pre> * array * + md5 * + des * </pre> * * @see setMode() * @see getMode() * * @access public * @return array */ function listModes() { return $this->_modes; } /** * Get actual encryption mode * * @see listModes() * @see setMode() * * @access public * @return string */ function getMode() { return $this->_mode; } /** * Whether to use the 'name map' of the extra properties or not * * Default Unix passwd files look like: * <pre> * user:password:user_id:group_id:gecos:home_dir:shell * </pre> * * The default 'name map' for properties except user and password looks like: * o uid * o gid * o gecos * o home * o shell * * If you want to change the naming of the standard map use * File_Passwd_Unix::setMap(array()). * * @see setMap() * @see getMap() * * @access public * @return boolean always true if you set a value (true/false) OR * the actual value if called without param * * @param boolean $bool whether to use the 'name map' or not */ function useMap($bool = null) { if (is_null($bool)) { return $this->_usemap; } $this->_usemap = (bool) $bool; return true; } /** * Set the 'name map' to use with the extra properties of the user * * This map is used for naming the associative array of the extra properties. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -