<?php
	require_once( $GLOBALS["site_root"] . "/includes/models/IpRestrict.inc.php" );

	class MyUserModel {

		public function isUserLoggedin() {
			return isset($_SESSION["user_id"]);
		}
		public function getLoggedinUserId() {
			return (isset($_SESSION["user_id"])) ? $_SESSION["user_id"] : false;
		}

		public function setLoggedinUserAutoLogin() {
			$user_id = $this->getLoggedInUserId();
			if (!$user_id) return;
			require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
			$this->um = new UserModel();
			$user = $this->um->getUserFromUserId( $user_id );
			if (!$user) return;
			setcookie( "autologin" , $user["hash"] , fo3_time() + 31*24*3600 , "/" );
		}
		
		public function clearUserAutoLogin() {
			setcookie( "autologin" , "" , fo3_time() - 86400 , "/" );
		}

		public function getUserIdFromEmail( $email ) {
			$query = sprintf( "select user_id from users where email='%s' and not is_deleted",
				FNTH_db_addslashes($email) );
			return FNTH_db_get_field( $query , "user_id" );
		}

		public function getUserIdFromEmailAndPassword( $email , $password ) {
			switch ($GLOBALS["model_version"]) {
			case 3.0:
			default:
				$query = sprintf( "select user_id as id from users
				where
					email='%s' and pwd='%s' and not is_deleted",
					FNTH_db_addslashes( $email ),
					FNTH_db_addslashes( $password ) );
				break;
			}
			return FNTH_db_get_field( $query , "id" );
		}

		public function falseLoginTry() {
			$ipm = new IpRestrictModel();
			$ipm->addUpdateRestriction( fo3_get_remote_ipaddress() );
			return;
			// session based
			if (!isset( $_SESSION["login_tries"] )) $_SESSION["login_tries"]=0;
			$_SESSION["login_tries"]++;
		}

		public function isFalseLoginsExceeded() {
			$ipm = new IpRestrictModel();
			$retval = $ipm->hasHardRestriction( fo3_get_remote_ipaddress() );
			return $retval;

			// session based
			if (!isset( $_SESSION["login_tries"] )) return false;
			return ($_SESSION["login_tries"]>=10);
		}

		public function clearFalseLoginTries() {
			$ipm = new IpRestrictModel();
			$ipm->clearRestriction( fo3_get_remote_ipaddress() );
			return;
			// session based
			unset( $_SESSION["login_tries"] );
		}

		public function loginUser( $user_id ) {
			$_SESSION["user_id"]=$user_id;
		}

		public function logoutUser( ) {
			unset( $_SESSION["user_id"] );
		}

		public function getUserMember( $user_id ) {
			switch ($GLOBALS["model_version"]) {
			case 3.0:
			default:
				$query = sprintf( "select member_id from members where user_id=%d" , $user_id );
				$row = FNTH_db_get_row( $query );
				if (!$row) return false;
				return $row["member_id"];
			}
		}

		public function addUserTemplate( $template_name , $user_ids ) {
			if (!$this->isUserLoggedin()) return false;
			$ins = array();
			$ins["owner_user_id"]=sprintf( "%d" , $this->getLoggedinUserId() );
			$ins["user_template_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $template_name ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "user_templates" , $ins ) ) );
			$template_id = FNTH_db_get_field( "select currval('user_templates_user_template_id_seq') as num" , "num" );
			$this->setUserTemplateUsers( $template_id , $user_ids );
			return $template_id;
		}

		public function getUserTemplates() {
			if (!$this->isUserLoggedin()) return false;
			return FNTH_db_get_associative(
				sprintf( "select user_template_id,user_template_title from user_templates where owner_user_id=%d",
					$this->getLoggedinUserId() ),
				"user_template_id" , "user_template_title" );
		}

		public function getUserTemplate( $template_id ) {
			FNTH_db_set_debugging( true );
			if (!$this->isUserLoggedin()) return false;
			$query = sprintf( "select * from user_templates where owner_user_id=%d and user_template_id=%d",
				$this->getLoggedinUserId(),
				$template_id );
			return FNTH_db_get_row( $query );
		}

		public function getUserTemplateUsers( $template_id ) {
			FNTH_db_set_debugging( true );
			$query = sprintf( "select user_id from user_template_users where user_template_id=%d" , $template_id );
			return FNTH_db_get_column( $query , "user_id" );
		}

		public function setUserTemplateUsers( $template_id , $user_ids = array() ) {
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from user_template_users where user_template_id=%d" , $template_id ) ) );
			$ins = array();
			$ins["user_template_id"]=sprintf( "%d" , $template_id );
			foreach ($user_ids as $user_id) {
				$ins["user_id"]=sprintf( "%d" , $user_id );
				FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_insert_query( "user_template_users" , $ins ) ) );
			}
		}

		public function deleteUserTemplate( $template_id ) {
			if (!$this->isUserLoggedin()) return false;
			$query = sprintf( "delete from user_template_users where user_template_id in (select user_template_id from user_templates where owner_user_id=%d and user_template_id=%d)",
				$this->getLoggedinUserId(),
				$template_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			$query = sprintf( "delete from user_templates where owner_user_id=%d and user_template_id=%d",
				$this->getLoggedinUserId(),
				$template_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function hashLogin( $a = array() ) {
			if (!isset( $a["hash"] )) return;
			$um = new UserModel();
			$user_id = $um->getUserIdFromHash( $a["hash"] );
			if ($user_id) {
				$this->loginUser( $user_id );
				$url = $_SERVER["REQUEST_URI"];
				$pos = strpos( $url , "?hash=" );
				if ($pos===false)
					$pos = strpos( $url , "&hash=" );
				if ($pos!==false) {
					$newurl = substr( $url , 0 , $pos );
					$pos=strpos( $url , "&" , $pos+1 );
					if ($pos!==false)
						$newurl.=substr( $url , $pos );
					FNTH_redirect( $newurl );
				}

			}
			
		}

	}

?>
