Location: PHPKode > scripts > RemoteSQL > remotesql/class.sql_link.php
<?php
//SQL_link class
//Pablo Gazmuri [hide@address.com]
//See README for details on how to use this class
//and sql-link.php for example usage


//special thanks to Mukul Sabharwal [hide@address.com] for
//a PHP implementation of RC4 encryption
include_once("class.rc4crypt.php");

class SQL_link{

	//encryption key
	var $key;
	
	//allowed IP addresses
	var $allowed;
	
	//constructor - sets allowed to array object
	function SQL_link(){
	$allowed = array();
	}
	
	//this function adds an ip address to the allowed list
	function allowIP($ip){
	$this->allowed[] = $ip;
	}
	
	
	//this function sets the encryption key
	function setKey($k){
	$this->key = $k;
	}

	//this function processes the incoming query
	function processQuery(){
		global $query, $rquery, $REMOTE_ADDR;
		
		
		//if allowed ip's are set, make sure the remote address is one of them
		if (count($this->allowed)){
		if(!in_array($REMOTE_ADDR, $this->allowed)) { die(); }
		}
		
		//instantiate rc4 class
		$rc4 = new rc4crypt();
		
		if($query){
			//decrypt query
			$query=$rc4->endecrypt($this->key, $query, "de");
			
			//execute query
			mysql_query($query);
			if (strlen(mysql_error())==0){
				$response.= "Query Executed!";
			}else{
				$response.= mysql_error();
			}
			
			//output encrypted result
			echo $rc4->endecrypt($this->key, $response, "en");
			
		}else if($rquery){
		
			//decrypt query
			$rquery=$rc4->endecrypt($this->key, $rquery, "de");
			
			//execute query
			$r=mysql_query($rquery);
			
			//build response string form results
			if (strlen(mysql_error())==0){
				//first line is # of rows returned
				$response.= mysql_numrows($r).chr(10);
				
				while($row = mysql_fetch_array($r)){
				if(!$first){
					//second line is number of columns/fields per row
					$response.= (count($row)/2).chr(10);
					$first = true;
				}
					for($i=0;$i<(count($row)/2);$i++){
						$response.= str_replace("|", "\\|", str_replace("\\", "\\\\", $row[$i]))."|";
					}
				}
			}else{
				$response.=  "#ERR#".mysql_error();
			}
			//output encrypted response string
			echo $rc4->endecrypt($this->key, $response, "en");
		}
	}
}
Return current item: RemoteSQL