<?php

	class AdminIpModel {

		protected function prepareSaveDatas( $p ) {
			$retval = array();
			$retval["ip_address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["ip_address"] ) );
			$retval["ip_description"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["ip_description"] ) );
			$retval["is_active"]=($p["is_active"]) ? "true" : "false";
			return $retval;
		}

		protected function prepareLoadDatas( $p ) {
			$retval = $p;
			$retval["is_active"]=($p["is_active"]=="t");
			return $retval;
		}

		public function getAdminIpFromId( $aip_id ) {
			$query = sprintf( "select * from admin_ips where admin_ip_id=%d" , $aip_id );
			$row = FNTH_db_get_row( $query );
			return $this->prepareLoadDatas( $row );
		}

		public function addAdminIp( $p ) {
			$ins = $this->prepareSaveDatas( $p );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "admin_ips" , $ins )
				)
			);
		}

		public function updateAdminIp( $aip_id , $p ) {
			$upd = $this->prepareSaveDatas( $p );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_update_query( "admin_ips" , $upd , sprintf( "admin_ip_id=%d" , $aip_id ) )
				)
			);
		}

		public function deleteAdminIp( $aip_id ) {
			$query = sprintf( "delete from admin_ips where admin_ip_id=%d" , $aip_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public static function isAdminIp( $ip ) {
			$query = sprintf( "select count(*) as num from admin_ips where ip_address='%s' and is_active" , FNTH_db_addslashes( $ip ) );
			return (FNTH_db_get_field( $query , "num" )>0);
		}

		public static function isLockedFeature( $feature ) {
			$ip = fo3_get_remote_ipaddress();
			if (AdminIpModel::isAdminIp( $ip )) return false;
			$query = sprintf( "select count(*) as num from locked_features where feature_name='%s'" , FNTH_db_addslashes( $feature ));
			return (FNTH_db_get_field( $query , "num" )>0);
		}

	}


?>
