Like this code?
Show the author your appreciation.
Usage Example
<?php
require_once "userinfo.class.php" ;
$user = new UserInfo ( "apache" );
echo "Username: " . $user -> username . "\n" ;
echo "User ID: " . $user -> uid . "\n" ;
echo "Group ID: " . $user -> gid . "\n" ;
echo "Comment: " . $user -> comment . "\n" ;
echo "Home: " . $user -> home . "\n" ;
echo "Shell: " . $user -> shell . "\n" ;
?>
userinfo.class.php
<?php
/*
* Simple script to retrieve the user information from an
* /etc/passwd file for unix-like OSes
*
* Copyright (C) 2007 Victor Roman Archidona
* http://blog.daijo.org
*/
class UserInfo
{
private $_user_info = null ;
public function __construct ( $user )
{
$entry = $this -> getUserEntry ( $user );
$this -> processUserEntry ( $entry );
}
public function getUserInfo ()
{
return $this -> _user_info ;
}
public function __get ( $var )
{
if ( array_key_exists ( $var , $this -> _user_info ))
return $this -> _user_info [ $var ];
return null ;
}
private function processUserEntry ( $entry )
{
list( $this -> _user_info [ "username" ],
$this -> _user_info [ "unused" ],
$this -> _user_info [ "uid" ],
$this -> _user_info [ "gid" ],
$this -> _user_info [ "comment" ],
$this -> _user_info [ "home" ],
$this -> _user_info [ "shell" ]) = explode ( ":" , $entry );
$this -> _user_info [ "shell" ] = trim ( $this -> _user_info [ "shell" ]);
}
private function getUserEntry ( $user )
{
$passwd_file = "/etc/passwd" ;
if (! is_file ( $passwd_file ))
throw new Exception ( "File $passwd_file does not exists" );
if (! is_readable ( $passwd_file ))
throw new Exception ( "File $passwd_file cannot be read" );
$passwd_content = file ( $passwd_file );
foreach ( $passwd_content as $entry ) {
$params = explode ( ":" , $entry );
/* We can find by username or by uid: */
$search = is_numeric ( $user ) ? $params [ 2 ] : $params [ 0 ];
if ( $search == $user )
return $entry ;
}
return null ;
}
}
?>
Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text Categories : PHP , PHP Classes , Search , Filesystem PHP Transfer data from text file to Mysql Table Categories : PHP , PHP Classes , Filesystem , Databases , MySQL Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP , Filesystem , PHP Classes PHP4 DirectoryIterator Class Categories : PHP , PHP Classes , Filesystem , Directories Compare two texts and display a block of text with the differences between them. Categories : PHP , PHP Classes , Filesystem , Strings , Arrays Easy upload class Categories : PHP Classes , Filesystem , HTTP , PHP Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP , PHP , PHP Classes , Filesystem 3 lines of Code to extract Tar, Zip, Gzip etc.. Categories : PHP , Filesystem , PHP Classes , Compression Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support Categories : PHP , FTP , Filesystem , PHP Classes , Compression filesplit : Split big text files in multiple small ones Categories : PHP , Log Files , Filesystem , PHP Classes A File Browser Class.To Read Drives,Directories and Files .Files writing is also possible Categories : PHP , PHP Classes , Filesystem file class , uploade file , download file already uploaded on another website Categories : PHP , PHP Classes , Filesystem , Web Services An efficient iterative and buffered text file reader Categories : PHP , Classes and Objects , Filesystem , PHP Classes , Log Files Class to Create protected URLs Categories : PHP , PHP Classes , URLs Class to convert any document, that can be read by MS Word, to another format supported by Word. Categories : PHP Classes , PHP , Windows 2000 , Microsoft Word , WinNT