UnixPasswordFile.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:3k
- <?php
- class UnixPasswordFile extends BaseObject {
- var $passwd_file;
- var $shadow_file;
- var $uses_shadowed_passwords;
- var $fp;
- var $file_open;
- var $error_message;
- Function UnixPasswordFile() {
- $this->BaseObject( 'UnixPasswordFile' );
- $this->passwd_file = '/etc/passwd';
- $this->shadow_file = '/etc/shadow';
- if ( file_exists( $this->shadow_file ) ) {
- $this->uses_shadowed_passwords = 1;
- }
- $this->fp = '';
- $this->file_open = 0;
- $this->error_message = '';
- }
- Function OpenFile() {
- /* Open that sucker up */
- $this->file_open =
- ( $this->fp = fopen( $this->passwd_file, 'r' ) );
- return $this->file_open;
- }
- Function CloseFile() {
- $this->file_open = 0;
- fclose( $this->fp );
- return 1;
- }
- Function ResetToBof() {
- if ( $this->file_open ) {
- fseek( $this->fp, 0 );
- }
- }
- Function GetPasswordEntry( $user_obj ) {
- if ( $this->file_open == 1 ) {
- $this->ResetToBof();
- }
- $tmp_obj = new UnixUser();
- while( $tmp_obj = $this->NextPasswordEntry() ) {
- if ( $tmp_obj->name == $user_obj->name ) {
- return Array( true, $tmp_obj );
- }
- if ( $tmp_obj->uid == $user_obj->name ) {
- return Array( true, $tmp_obj );
- }
- }
- return Array( false, 'No match found' );
- }
- Function NextPasswordEntry() {
- if ( $this->file_open == 0 ) {
- if ( ! $this->OpenFile() ) {
- $this->error_message =
- 'Failed to open ' . $this->passwd_file . ' for reading!';
- return false;
- }
- }
- /* Okay we have a open file read a line and parse it */
- $line_buffer = '';
- $read_result = 0;
- if ( !( $line_buffer = fgets( $this->fp, 4096) ) ) {
- $this->error_message = 'EOF';
- return false;
- }
- /* Okay we have a good line buffer let's parse it */
- $tmp_obj = new UnixUser();
- list (
- $tmp_obj->name,
- $tmp_obj->passwd,
- $tmp_obj->uid,
- $tmp_obj->gid,
- $tmp_obj->gcos,
- $tmp_obj->dir,
- $tmp_obj->shell
- ) = explode( ':', $line_buffer );
- return $tmp_obj;
- }
- Function All() {
- if ( $this->file_open == 1 ) {
- $this->ResetToBof();
- }
- $tmp_arr = Array();
- $tmp_cnt = 0;
- while( $tmp_unix_user = $this->NextPasswordEntry() ) {
- $tmp_arr[ $tmp_cnt ] = $tmp_unix_user;
- $tmp_cnt++;
- }
- return Array( true, $tmp_cnt, $tmp_arr );
- }
- }
- ?>