<?php

class loginbox extends module {
	
	var $_loc_vars = array( 'templ'  );
	var $_post_vars = array( 'login', 'passwd', 'logout'  );
	var $templ = 'loginbox.htm';
	var $error = '';
	
	function _on_() {
		global $auth;
		if ( $auth->is_login() ) {
				if ( $this->logout ) {
					$auth->do_logout();
				    _redirect('');
				} else {
					return $this->is_login();
				}
		}
		else {
			if ( $this->login ) {
				$this->checkAuth();
			}
			return $this->r_form();
		}
	}
	
	function r_form() {
		global $R;
		$tpl = $R->getTpl( $this->templ, 'form' );
		if ( $this->error ){
			$tpl = $R->set( 'err_msg', $this->error.'<br>', $tpl );
		}
		return $R->clear( $tpl );
		
	}

	function is_login() {
		global $auth, $R;
		$tpl = $R->getTpl( $this->templ, 'islogin' );
		$tpl = $auth->user_obj->assign( $tpl );
		return $R->clear( $tpl );
	}

	function checkAuth() {
		global $core_db, $auth;
		if (!$this->passwd )  {
			$this->error = 'Не указан пароль!';
			return;
		} else {
		    $db2 = $core_db->q( 'SELECT `id` FROM `_sys_users` WHERE LOWER(`login`) = "'.strtolower($this->login).'" AND `passwd` = MD5("'.$this->passwd.'") AND !`_sys_deleted`' );
		    if ( $db2->r() ) {
				$auth->do_login( $db2->f('id') );
			    _redirect('');
		    } else {
				$this->error = 'Авторизация неверна';
				return;
		    }
		}		
	}	


}

?>