<?php

if (!defined('XOOPS_ROOT_PATH')) exit();

require_once XOONIPS_CLASS_PATH.'util.php';

/**
 * !! Basic-Auth routine is realize here. !!
 *
 * @author
 *
 */

/**
 * @param
 */
abstract class basic_auth {

  public $realm;

  public function init($realm = 'Realm') {
    $this->realm = $realm;
  }

  /**
   * Create Digest Auth Request
   *
   **/
  private function reqhead() {
    $basic_auth_line = 'WWW-Authenticate:Basic realm="'.$this->realm.'"';
    $arr = array($basic_auth_line);
    res_code(401, $arr);
    exit();
  }
  
  private function bad_request() {
    res_code(400);
    exit;
  }

  /**
   * This is Main function
   *
   */
  public function auth() {
    if (empty($_SERVER['PHP_AUTH_USER'])) {
      $this->reqhead();
    }

    // Server compare passwd.
    if ($this->cmppasswd($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) == false) {
      $this->reqhead();
    }
    return true;
  }

  /**
   * Please implemetn subclaass
   * 
   * @param string $user Username from agent
   * @param string $passwd Password from agent
   * @return boolen true:success,false:fail
   * 
   */
  abstract protected function cmppasswd($user,$passwd);
}
