<?php
/*
copyright: copyright 2004 Sam Yapp. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/
class ace{

	var $config = 0;
	var $defaultsettings = array();

	function ace($conn, $prefix = '', $settings = array()){
		$this->dbprefix = $prefix;
		$this->connection = $conn;
		$this->setsettings($settings);
	}

	function setsettings($settings){
		$this->defaultsettings = array_merge($this->defaultsettings, $settings);
	}

	function init(){
		if( count($this->defaultsettings) > 0 ){
			$this->loadsettings($this->defaultsettings);
		}
	}

	function loadsettings($settings = array()){
		$conf = 0;
		if( !is_array($settings) ) $settings = array($settings);
		if( count($settings) == 0 ){
			$sql = "SELECT * ";
		}else{
			$sql = "SELECT ".join(',',array_values($settings))." ";
		}
		$sql .= "FROM {$this->dbprefix}settings LIMIT 0,1 ";
		$res = $this->query($sql, 'Load Settings');
		if( mysql_num_rows($res) == 1){
			$conf = mysql_fetch_object($res);
			if( $this->config == 0 ){
				$this->config = $conf;
			}else{
				foreach( $conf as $c=>$v ) $this->config->$c = $v;
			}
		}
		return $conf;
	}

	function savesettings($sets = array()){
		if( !is_array($sets) ) $sets = array($sets);
		$ups = array();
		if( count($sets) == 0 ){
			foreach( $this->config as $n=>$v ) $ups[] = "$n='".addslashes($v)."' ";
		}else{
			foreach( $sets as $n ) $ups[] = "$n='".addslashes($this->config->$n)."' ";
		}
		if( count($ups) > 0 ){
			$sql = "UPDATE {pa_dbprefix}settings SET ".join(", ",$ups)." ";
			$this->query($sql, 'Save Settings');
			return true;
		}
		return false;
	}

	function query($sql, $action = 'unspecified'){
		$sql = str_replace('{pa_dbprefix}', $this->dbprefix, $sql);
		$res = @mysql_query($sql, $this->connection);
		if( $res ) return $res;
		$err = 'Database error with function: '.$action;
		$err .='<br />SQL: '.$sql;
		$err .='<br />mySQL Says: '.mysql_error();
		die($err);
	}

	function getstr($name, $from, $default = ''){ return isset($from[$name]) ? trim(get_magic_quotes_gpc()=="1" ? stripslashes($from[$name]) : $from[$name]) : $default;}
	function getint($name, $from, $default = 0){ return isset($from[$name]) ? (int)$from[$name] : $default;}
	function getdouble($name, $from, $default = 0){ return isset($from[$name]) ? (double)$from[$name] : $default;}
	function getids(&$ids){
		$tmp = array();
		if( !is_array($ids) ) $ids = array($ids);
		foreach( $ids as $i ){
			settype($i, 'integer');
			if( $i != 0 ) $tmp[] = $i;
		}
		return $tmp;
	}

	function validateemail($email){  
		if (eregi("(@.*@)(\.\.)|(@\.)|(\.@)|(^\.)", $email) ||  
			!eregi ("^.+\@(\[?)[-_a-zA-Z0-9\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $email)) {  
			return(false);  
		}else{
			list($user, $domain) = explode("@", $email);  
			if ((!eregi("^[_a-zA-Z0-9\.\-]+$", $user)) ||  
				(!eregi("^[_a-zA-Z0-9\.\-]+$", $domain))) {  
				return false;  
			} else {
				return(true);  
			}
		}
	}

	function selectoptions($arr, $cur){
		foreach( $arr as $n=>$v ){
			$sel = $cur == $n ? ' SELECTED ' : '';
			echo '<option value="'.htmlspecialchars($n).'"'.$sel.'>'.htmlspecialchars($v).'</option>'."\n";
		}
	}

	function banips($ips){
		$is = array();
		foreach( $ips as $i ){
			if( preg_match('/^[0-9]([0-9.]*)[0-9]$/i', $i) ) $is[$i] = true;
		}
		$ips = array_keys($is);
		if( count($ips) > 0 ){
			$is = array();
			foreach( $ips as $i ) $is[] = "('".addslashes($i)."') ";
			$sql = "INSERT IGNORE INTO {pa_dbprefix}banned_ips (ip) VALUES ".join(",",$is)." ";
			$this->query($sql, 'Ban Ips');
			return mysql_affected_rows();
		}
		return 0;
	}

	function ipbanned(){
		$ip = isset($_SERVER['X_FORWARDED_FOR']) && $_SERVER['X_FORWARDED_FOR'] != '' ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
		$dotcount = substr_count($ip, '.');
		$ips = array();
		while( $dotcount >= 0 ){
			$ips[] = "'".preg_replace('/[^0-9.]/i', '', $ip)."'";
			$ip = substr($ip, 0, strrpos($ip, '.'));
			$dotcount--;
		}
		if( count($ips) > 0 ){
			$sql = "SELECT COUNT(*) FROM {pa_dbprefix}banned_ips WHERE ip IN (".join(",",$ips).") ";
			$res = $this->query($sql, 'Check IP');
			if( mysql_result($res,0,0) == 0 ) return false;
		}
		return true;
	}

	function unbanips($ips){
		$is = array();
		foreach( $ips as $i ){
			if( preg_match('/^[0-9]([0-9.]*)[0-9]$/i', $i) ) $is[$i] = true;
		}
		$ips = array_keys($is);
		if( count($ips) > 0 ){
			$is = array();
			foreach( $ips as $i ){
				$is[] = "'".addslashes($i)."'";
			}
			$sql = "DELETE FROM {pa_dbprefix}banned_ips WHERE ip IN (".join(",",$is).") ";
			$this->query($sql, 'Unban Ips');
			return mysql_affected_rows();
		}
		return 0;

	}

	function getbannedips(){
		$sql = "SELECT ip FROM {pa_dbprefix}banned_ips ORDER BY ip ";
		$res = $this->query($sql, 'Get Ips');
		$ips = array();
		while( list($ip) = mysql_fetch_row($res) ) $ips[] = $ip;
		return $ips;
	}

}

?>
