<?php
/*
 * Copyright 1999 - 2003 by Gero Kohnert
 *
 * CVS Info:  $Id: auth_db.pinc,v 1.16 2005/05/03 13:18:49 saraj Exp $
 * $Author: saraj $
 */

require_once 'auth.pinc';
/**
 * Authentification via database
 *
 * @modulegroup BASE
 * @module auth_db
 * @package BASE
 */
class auth_db extends auth{
  /**
   * get the password
   */
  function getpw() {
    if ( isset($_POST['user_password']) ) {
      return trim($_POST['user_password']);
    } elseif ( isset($_GET['user_password']) ) {
      return trim($_GET['user_password']);
    }
    return;
  }
  /**
   * get the username
   */
  function getuname() {
    if ( isset($_POST['user_name']) ) {
      return trim($_POST['user_name']);
    } elseif ( isset($_GET['user_name']) ) {
      return trim($_GET['user_name']);
    }
    return;
  }
  /**
   * Check for permmision
   * using a DB query
   */
  function check() {
    global $current_user,$lang;

    $q = "SELECT a.* ,p.*,p.id as u_id FROM ". $this->dbconn->prefix ."addresses a, ". $this->dbconn->prefix ."people p ";
    $q .= " WHERE p.login = ". $this->dbconn->String($this->getuname()) ;
    $q .= " AND p.pw = ". $this->dbconn->Password($this->getpw()) ." AND p.adr_id = a.id";
    $result = $this->dbconn->Exec($q);

    $r = array();
    if ( ! $result ) {
      ReadLang($lang);
      $r[0] = "No Result for ". $this->getuname() ." / ". $this->getpw();
      $r[1] = $lang['Err0026'];
      return $r;
    }

    /* OK there is a user and a Password so lets check */
    if ( 1 != $result->numrows()) {
      ReadLang($lang);
      $r[0] = "not unique ". $result->numrows() ." Result for ". $this->getuname() ." / ". $this->getpw() ." (". $_SERVER['REMOTE_ADDR'] .")";
      $r[1] = $lang['Err0027'];
      $result->free();
      return $r;
    }

    $current_user= new tutos_user($this->dbconn);
    $current_user->read_result($result,0);
    $result->free();
  }
}
?>
