<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/DGreenBox.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/UserLog.inc.php" );

	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Logout.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MyUser.inc.php" );

	class LoginBox extends DGreenBox {
		protected $bLoggedIn = false;

		public function __construct( $id = "LoginBox" , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->mum = new MyUserModel();
			$this->mmm = new MyMemberModel();
			$this->um = new UserModel();
			$this->setParam( "errormessage" , "" );
		}

		public function tryAutoLogin() {
			if ($this->mum->isUserLoggedIn()) return; // mar be van jelentkezve
			if (!isset( $_COOKIE["autologin"] )) return; // nincs cookieja ugy hogy nem
			if (!strlen(trim($_COOKIE["autologin"]))) return;	// van cookie, de ervenytelen
			// megnezzuk, hatha
			$user_id = $this->um->getUserIdFromHash( $_COOKIE["autologin"] );
			if (!$user_id) return;	// hackgyanus
			$this->mmm->loginUser( $user_id );	// beleptet
			$this->mmm->setLoggedinUserAutoLogin();			// cookie frissit
			FNTH_refresh();			// refresh ha beleptettuk
		}

		public function handleEvents() {
			if (isset( $this->aP["inajax"] ) &&
				isset( $this->aP["ajaxevent"] ) &&
				$this->aP["ajaxevent"]=="refresh_loginbox") {
				$this->render();
				$json = array(
					"htmlsource" => $this->renderContent() );
				FNTH_echo_json_response( $json , 0 , "fo3_exit" );
			}

			$this->tryAutoLogin();
			if (!isset( $this->aP["loginname"] )) {
				parent::handleEvents();
				return;
			}
			if (!isset( $this->aP["passwd"] )) {
				parent::handleEvents();
				return;
			}

			$user_id = $this->mmm->getUserIdFromUsernameAndPassword( $this->aP["loginname"] , $this->aP["passwd"] );

			if (!$user_id) $user_id = $this->mmm->getUserIdFromEmailAndPassword( $this->aP["loginname"] , $this->aP["passwd"] );
			if ($user_id) {
				if ($this->um->isUserAcknowledged($user_id)) {
					$mm = new MemberModel();
					$member = $mm->getMemberFromUserId( $user_id );
					if (is_array( $member ) && $GLOBALS["language"]!="hu") {
						$um = new UserModel();
						$user = $um->getUserFromUserId( $user_id );
						$u = array();
						$u["user_id"]=$user["user_id"];
						$u["hash"]=$user["hash"];
						$url = FNTH_render_url( "http://".GlobalsModel::getSiteDomain( "hu" ) . "/crosslogin/" , $u );
						FNTH_redirect( $url );
					}
					$this->mmm->loginUser( $user_id );
					$ulm = new UserLogModel();
					$ulm->logLogin();
					if (isset( $this->aP["autologin"] )) $this->mmm->setLoggedinUserAutoLogin(); // set autologin
					FNTH_refresh();
					fo_exit();
				} else {
					$text = __("Acknowledge_mail_before_login") ."<br/><br/>";
					FrontendPage::warningMessage( "login_error" , $text );
				}
			} else {
				$text = __("Invalid_username_or_password") . "<br/><br/>";
				$text.=sprintf( "<a href=\"%s\">%s &raquo;</a>" , MyUserURL::renderRegistrationURL() , __("Registration") );
				$text.=sprintf( " | <a href=\"%s\">%s &raquo;</a>" , MyUserURL::renderForgottenPasswordURL() , __("Forgotten_password") );
				FrontendPage::warningMessage( "login_error" , $text );
			}
			parent::handleEvents();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "loginbox_Email" );
			fo3_add_js_langdef( "loginbox_Password" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();
			FNTHPage::addUniquePageFullInit( sprintf( "InitLoginBox('%s');\n" , $this->getId() ) );

			$user = new MyMemberModel();
			$this->bLoggedIn = $user->isUserLoggedIn();
			$this->setParam( "is_logged_in" , $this->bLoggedIn );
			if ($this->bLoggedIn) {
				$this->setParam( "user_identificator" , $user->getLoggedInUserIdentificator() );
				$this->setParam( "user_type" , $user->getLoggedInUserType( true ) );
				$this->setParam( "url_logout" , LogoutURL::renderURL() );
			} else {
				$this->setParam( "url_registration" , MyUserURL::renderRegistrationURL() );
				$this->setParam( "url_forgotten_password" , MyUserURL::renderForgottenPasswordURL() );
			}	
			return parent::render();
		}
	}
?>
