<?
  class poppassd {

  var $fp;
  var $err_str;

  function connect() {
    $fp = fsockopen("localhost", 106, &$err_no, &$err_str);
    if (!$fp) {
      $this->err_str = $err_str;
    } else {
      $this->fp = $fp;
      if ($this->get_prompt()) {
        return true;
      }
    }
    return false;
  }

  function disconnect() {
    if ($this->fp) {
      fputs($this->fp, "quit\n");
      fclose($this->fp);
    }
  }

  function get_prompt() {
    $prompt = fgets($this->fp, 4096);
    if (substr($prompt, 0, 3) == "200") {
      return true;
    } else {
      $this->err_str = $prompt;
      return false;
    }
  }

  function change_password($user_name, $old_password, $new_password) {
    $return_value = false;
    if ($this->connect()) {
      if ($this->send_user_name($user_name)) {
        if ($this->send_old_password($old_password)) {
          if ($this->send_new_password($new_password)) {
            $return_value = true;
          }
        }
      }
    }
    $this->disconnect();
    return $return_value;
  }

  function send_user_name($user_name) {
    fputs($this->fp, "user $user_name\n");
    return $this->get_prompt();
  }

  function send_old_password($password) {
    fputs($this->fp, "pass $password\n");
    return $this->get_prompt();
  }

  function send_new_password($password) {
    fputs($this->fp, "newpass $password\n");
    return $this->get_prompt();
  }

}
?>

