UnixPasswordFile.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:3k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. class UnixPasswordFile extends BaseObject {
  3.    var $passwd_file;
  4.    var $shadow_file;
  5.    var $uses_shadowed_passwords;
  6.    var $fp;
  7.    var $file_open;
  8.    var $error_message;
  9.    Function UnixPasswordFile() {
  10.       $this->BaseObject( 'UnixPasswordFile' );
  11.       $this->passwd_file = '/etc/passwd';
  12.       $this->shadow_file = '/etc/shadow';
  13.       if ( file_exists( $this->shadow_file ) ) {
  14.          $this->uses_shadowed_passwords = 1;
  15.       }
  16.       $this->fp = '';
  17.       $this->file_open  = 0;
  18.       $this->error_message = '';
  19.    }
  20.    Function OpenFile() {
  21.       /* Open that sucker up */
  22.       $this->file_open =
  23.        ( $this->fp = fopen( $this->passwd_file, 'r' ) );
  24.       return $this->file_open;
  25.    }
  26.    Function CloseFile() {
  27.       $this->file_open = 0;
  28.       fclose( $this->fp );
  29.       return 1;
  30.    }
  31.    Function ResetToBof() {
  32.       if ( $this->file_open ) {
  33.          fseek( $this->fp, 0 );
  34.       }
  35.    }
  36.    Function GetPasswordEntry( $user_obj ) {
  37.       if ( $this->file_open == 1 ) {
  38.          $this->ResetToBof();
  39.       }
  40.       $tmp_obj = new UnixUser();
  41.       while( $tmp_obj = $this->NextPasswordEntry() ) {
  42.          if ( $tmp_obj->name == $user_obj->name ) {
  43.             return Array( true, $tmp_obj );
  44.          }
  45.          if ( $tmp_obj->uid == $user_obj->name ) {
  46.             return Array( true, $tmp_obj );
  47.          }
  48.       }
  49.       return Array( false, 'No match found' );
  50.    }
  51.    Function NextPasswordEntry() {
  52.       if ( $this->file_open == 0 ) {
  53.          if ( ! $this->OpenFile() ) {
  54.             $this->error_message = 
  55.             'Failed to open ' .  $this->passwd_file . ' for reading!';
  56.             return false;
  57.          }
  58.       }
  59.       /* Okay we have a open file read a line and parse it */
  60.       $line_buffer = '';
  61.       $read_result = 0;
  62.       if ( !( $line_buffer = fgets( $this->fp, 4096) ) ) {
  63.          $this->error_message = 'EOF';
  64.          return false;
  65.       }
  66.       /* Okay we have a good line buffer let's parse it */
  67.       $tmp_obj = new UnixUser();
  68.       list (
  69.          $tmp_obj->name,
  70.          $tmp_obj->passwd,
  71.          $tmp_obj->uid,
  72.          $tmp_obj->gid,
  73.          $tmp_obj->gcos,
  74.          $tmp_obj->dir,
  75.          $tmp_obj->shell
  76.       ) = explode( ':', $line_buffer );
  77.       return $tmp_obj;
  78.    }
  79.    Function All() {
  80.       if ( $this->file_open == 1 ) {
  81.          $this->ResetToBof();
  82.       }
  83.       $tmp_arr = Array();
  84.       $tmp_cnt = 0;
  85.       while( $tmp_unix_user = $this->NextPasswordEntry() ) {
  86.          $tmp_arr[ $tmp_cnt ] = $tmp_unix_user;
  87.          $tmp_cnt++;
  88.       }
  89.       return Array( true, $tmp_cnt, $tmp_arr );
  90.    }
  91. }
  92. ?>