<?php
	/**
	 * 
	 */
	class SQLprepare {
		private $db;
		function __construct($db) {
			$this->db = $db;
		}
		function prepareSQL($sql){
			$sql = preg_replace('/\s{2,}/', ' ', $sql);
			$equal = strpos($sql, "=");
			$lastPos = 0;
			$positions = array();
			while ($lastPos = strpos($sql, "=", $lastPos)) {
			    $positions[] = $lastPos;
			    $lastPos = $lastPos + strlen($needle);
			}
			foreach ($positions as $value) {
				//if($sql[$value-1] != ' ' && $sql[$value +1] != ' ')
					echo $sql[$value];
			}
			//$sql = strip_tags($sql);
			$v = explode(" ", $sql);
			//TO-DO REMOVE qutes ADD real_escape_string return ok sql
			switch(strtoupper($v[0])){
				case 'INSERT':
					return $sql;
					break;
				case 'UPDATE':
					$this->PrepareUpdateSql($v);
					break;
				case 'SELECT':
					return $sql;
					if(in_array("WHERE", $v)){
						$start = array_search("WHERE", $v);
						while($start < sizeof($v)-1){
							$v[$start+3] = '"'.mysql_real_escape_string($this->StripQuotes($v[$start+3])).'"';
							$start+=3;
						}						
					}
					break;
			}
		}
		private function PrepareUpdateSql($array){
			echo "<pre>";
			print_r($array);
			echo "</pre>";
			if(in_array("SET", $array)){
				$start = array_search("SET", $array);
				$end = false;
				while($start < sizeof($array)){
					$start+=3;
					$split = 0;
					$where = false;
					$date = false;
					if($array[$start-1] == "=" && substr($array[$start],-1) == ','){
						$word = $array[$start];
						if(substr($word, 0,3) == "STR"){
							$word.= " ".$array[$start+1];
							unset($array[$start+1]);
							$array = array_values($array);
							$date = true;
						}
					}
					else{
						if($array[$start+1] == "WHERE") {$end = true; $word = "";}
						if($array[$start+1] != "WHERE" && !$end){
							$i = -1;
							$k = array();
							do{
								$i++;
								if($array[$start+$i] != "WHERE"){
									array_push($k, $array[$start+$i]);
								}else{
									$where = true;
								}
								if($i > 10)
									break;
							}while($array[$start+$i+2] != '=');
							$word = implode(" ", $k);
							//echo $start." => ".$word."<br>";
							$split = $i;
							
							for($j = 0; $j<$split;$j++)
								unset($array[$start+$j]);
							$array = array_values($array);
							if($where){
								array_splice($array, $start+1, 0, "WHERE");
							}
						}
					}
					if($array[$start] != '='){
						if(!$date)
							$array[$start] = '"'.mysql_real_escape_string(rtrim($this->StripQuotes($word), ',')).'",';
						else
							$array[$start] = $word;	
					}
					if($where){
						$start++;
					}
					
				}
			}
			if(preg_replace('/\s{2,}/', ' ', $array[end(array_keys($array))]) == '"",') {unset($array[end(array_keys($array))]); array_values($array);}
			
			if(in_array("WHERE", $array)){
				$start = array_search("WHERE", $array);
				//echo $start;
				if(substr($array[$start-1],-1) == ','){
					$array[$start-1] = substr($array[$start-1], 0, strlen($array[$start-1])-1);
				}
				while($start < sizeof($array)){
					$start+=3;
					if($array[$start-1] == "="){
						$word = $array[$start];
						
						$array[$start] = '"'.mysql_real_escape_string(rtrim($this->StripQuotes($word), ',')).'"';
					}
				}					
			}
			echo "<pre>";
			print_r($array);
			echo "</pre>";
			return implode(" ", $array);
		}
		private function StripQuotes($string){
			$string = preg_replace("/'/", "", $string, 2);
			$string = preg_replace('/"/', "", $string, 2);
			//$string = str_replace('""', '', $string);
			//$string = str_replace("'", "", $string);
		//	$string = str_replace("|", '\'', $string);
			$string = rtrim($string, ",");
			$string = rtrim($string, "\n");
			$string = rtrim($string, "\t");
			$string = rtrim($string, " ");
			return $string;
		}
	}
	
?>