<?php
class cLogin extends cBase {
	function cLogin($sha1_user_name='', $sha1_user_pass='', $tabinit=0) {
		parent::cBase($tabinit);
		if ($sha1_user_name!='') define('USER_NAME', $sha1_user_name);
		if ($sha1_user_pass!='') define('USER_PASS', $sha1_user_pass);
	}
	
	function show() {
		$t[] = new cH2('Administration', 'cTitle');
		$t[] = new cSpan('Fill the required fields to access to the administration:', '', 'bold');
		$dadmin = new cDiv('divAdmin');
		$dadmin->addchilds($t);
		$frm = new cAjaxForm('formSendLogin', 'divSendLogin', 'ajax_admin_login');
		$frm->set_tds_width('110', '160', '274');
		$frm->addtextbox('user', 'User:');
		$frm->addpass('pass','Password:');
		$frm->addbutton('enviar', 'Enviar');
		$dadmin->addchild($frm);
		
		$this->pl( $dadmin->render() );
	}
	
	function ajax_admin_login($form) {
		$objResponse = new xajaxResponse();
		slash_arr($form);
		
		if (USER_NAME==sha1($form['user'])) {          	// Entonces hemos encontrado nombre
			if (USER_PASS==sha1($form['pass'])) {		// Password correcta
				session_start();
				$_SESSION['session_name']=USER_NAME;
				$_SESSION['session_pass']=USER_PASS;
				$_SESSION['session_code']=USER_CODE;
				$seccion=1;
			}
		} else { $login_mal=1; }
		
		if ($seccion==1) {
			$objResponse->redirect(ADMIN_PATH.'&c=1');
		} else {
			if ($login_mal==1) {
				$objResponse->assign('formSendLogin_user_state','innerHTML',"Login Incorrecto");
				$objResponse->assign('formSendLogin_pass_state','innerHTML',"");
			} else {
				$objResponse->assign('formSendLogin_pass_state','innerHTML',"Password Incorrecto");
				$objResponse->assign('formSendLogin_user_state','innerHTML',"");
			}
		}
		
		return $objResponse;
	}
}
?>