/***************************
	*
	*	@author  xiaohouzi79	xiaohouzi79(atgmaildotcom)
	*	This class comes without warranty of any kind including merhcantability or fitness for any purpose whatsoever
	*	furthermore this may be an altered version of the original
	*
	****************************/


	class sqlsrv {
		
		private $host;
		
		private $database;
		
		private $connection;
		
		private $errors = array();
		
		public function __construct($user, $password, $host, $database) {
			$this->connect($user, $password, $host, $database);
		}
		
		public function connect($user, $password, $host = '', $database = '') {
			if ($host) {
				$this->host = $host;
			} else {
				$host = $this->host;
			}
			
			if ($database) {
				$this->database = $database;
			} else {
				$database = $this->database;
			}
			
			if (!$user || !$password || !$host || !$database) {
				$this->errors[] = "Please provide valid connection details";
				return false;
			}
			
			$connectioninfo = array('UID' => $user, 'PWD' => $password, 'Database' => $database);
			
			$connection = sqlsrv_connect($host, $connectioninfo);
			
			if (!$connection) {
				$this->errors[] = sqlsrv_errors();
				return false;
			}
			
			$this->connection = $connection;
			return true;
		}
		
		public function fetchrow($strSQL) {
			return $this->fetchrows($strSQL, '', '', true);
		}
		
		public function fetchassoc($strSQL, $assoc, $multi = false) {
			return $this->fetchrows($strSQL, $assoc, $multi);
		}
		
		public function fetchrows($strSQL, $assoc = '', $multi = false, $fetchrow = false) {
			$stmt = $this->query($strSQL);
			if (!$stmt) return false;
			
			$meta = sqlsrv_field_metadata($stmt);
			
			$rows = array();
			
			// Return single row
			if ($fetchrow) {
				sqlsrv_fetch($stmt);
				$row = array();
				foreach ($meta as $key => $values) {
					$field = sqlsrv_get_field($stmt, $key, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
					$row[$values['Name']] = $field;
				}
				sqlsrv_free_stmt($stmt);
				return $row;
			}
			
			// Return multiple rows
			while (sqlsrv_fetch($stmt)) {
				$row = array();
				foreach ($meta as $key => $values) {
					$field = sqlsrv_get_field($stmt, $key, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
					$row[$values['Name']] = $field;
				}
				
				if ($assoc) {
					if ($multi) {
						$rows[$row[$assoc]][] = $row;
					} else {
						$rows[$row[$assoc]] = $row;
					}
				} else {
					$rows[] = $row;
				}					
			}			
			
			sqlsrv_free_stmt($stmt);
			return $rows;
		}
		
		public function fetchutf8($strSQL, $fields, $assoc = '', $multi = false) {
			$stmt = $this->query($strSQL);
			if (!$stmt) return false;
			
			$meta = sqlsrv_field_metadata($stmt);
			
			$rows = array();
			
			// Return multiple rows
			while (sqlsrv_fetch($stmt)) {
				$row = array();
				foreach ($meta as $key => $values) {
					if (in_array($values['Name'], $fields)) {
						$field = $this->convmulti(sqlsrv_get_field($stmt, $key, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)));
					} else {				
						$field = sqlsrv_get_field($stmt, $key, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
					}
					$row[$values['Name']] = $field;
				}
				
				if ($assoc) {
					if ($multi) {
						$rows[$row[$assoc]][] = $row;
					} else {
						$rows[$row[$assoc]] = $row;
					}
				} else {
					$rows[] = $row;
				}					
			}
			
			sqlsrv_free_stmt($stmt);
			
			return $rows;
		}
		
		public function query($strSQL) {
			$cleanSQL = $this->cleanquery($strSQL);
		
			if (!$this->connection) {
				$this->errors[] = "No database connection established";
				return false;
			}
			
			$stmt = sqlsrv_query($this->connection, $strSQL);
			
			if ($stmt === false) {
				$this->errors[] = sqlsrv_errors();
				return false;
			}
			
			return $stmt;
		}
		
		private function er($data) {
			echo "
";
			print_r($data);
			echo "
"; } private function convmulti($data) { return iconv("utf-16le", "utf-8", $data); } private function cleanquery($strSQL) { $cleanedstr = htmlspecialchars($strSQL, ENT_NOQUOTES); $celanedstr = str_replace("'", "''", $cleanedstr); return $cleanedstr; } public function geterrors() { return er($this->errors); } public function close() { sqlsrv_close($this->connection); } }