Location: PHPKode > scripts > SRETS (SimpleRETS) > srets-simplerets/SimpleRETS.Base.php
<?php
function RETSSocket($SimpleRETS,$Type,$RETS_Response,$Realm,$Nonce,$Opaque,$Session_ID){
	
	//Basic Authentication	
	if($SimpleRETS['Auth_Type'] == 'Basic'){
		$Response = base64_encode($SimpleRETS['Username'].':'.$SimpleRETS['Password']);
	}
	//Digest Authentication
	if($SimpleRETS['Auth_Type'] == 'Digest'){
		$A1 = $SimpleRETS['Username'].':'.$Realm.':'.$SimpleRETS['Password'];
		$A2 = 'GET:'.$SimpleRETS["$Type"];
		$Digest = md5($A1).':'.$Nonce.':'.md5($A2);
		$Encrypted_Response = md5($Digest);
		$Response  = 'username="'.$SimpleRETS['Username'].'", realm="'.$Realm.'", nonce="'.$Nonce.'", opaque="'.$Opaque.'", response="'.$Encrypted_Response.'", uri="'.$SimpleRETS["$Type"].'"';

	}
	//Digest-QOP Authentication
	if($SimpleRETS['Auth_Type'] == 'Digest-QOP'){
		$A1 = $SimpleRETS['Username'].':'.$Realm.':'.$SimpleRETS['Password'];
		$A2 = 'GET:'.$SimpleRETS["$Type"];
		$Digest = md5($A1).':'.$Nonce.':00000001:'.$SimpleRETS['User_Agent'].':auth:'.md5($A2);
		$Encrypted_Response = md5($Digest);
		$Response  = 'username="'.$SimpleRETS['Username'].'", realm="'.$Realm.'", nonce="'.$Nonce.'", opaque="'.$Opaque.'", qop="auth", nc="00000001", cnonce="'.$SimpleRETS['User_Agent'].'", response="'.$Encrypted_Response.'", uri="'.$SimpleRETS["$Type"].'"';
	}
	

		
	if($Type != 'Login'){
		$Path_Query = $SimpleRETS["$Type"].'?'.$SimpleRETS['Query'];
	}else{
		$Path_Query = $SimpleRETS["$Type"];
	}
		
	$SR_Socket = fsockopen($SimpleRETS['Host'],$SimpleRETS['Port'],$errno,$errstr,120);
	$Headers  = "GET ".$Path_Query." HTTP/1.0\r\n";
	$Headers .= "Authorization: ".$SimpleRETS['Auth_Type']." ".$Response." \r\n";
	$Headers .= "Host: " . $SimpleRETS['Host'] . "\r\n";
	$Headers .= "Accept: */*\r\n";
	$Headers .= "Pragma: no-cache\r\n";
	$Headers .= "RETS-Version: RETS/" . $SimpleRETS['Version'] . "\r\n";
	$Headers .= "User-Agent: " . $SimpleRETS['User_Agent'] . "\r\n";
	
	if($Session_ID != ''){
		$Headers .= "Cookie: RETS-Session-ID=$Session_ID\r\n";
	}
	
	$Headers .= "Connection: Close\r\n\r\n";

	fputs($SR_Socket, $Headers);
		while(!feof($SR_Socket)){
				$RETS_Response .= fgets($SR_Socket,4096);
		}
	fclose($SR_Socket);
	
	//print($RETS_Response);
	return $RETS_Response;
		
}

function SimpleRETS($SimpleRETS,$Type){
	
	//Basic Login - Login in once and then run query
	if($SimpleRETS['Auth_Type'] == 'Basic'){	
	//Login
		$RETS_Response = RETSSocket($SimpleRETS,'Login',$RETS_Response,$Realm,$Nonce,$Opaque,$Session_ID);
	}
	
	//Digest Login - Grab Nonce first then try login then run query	
	if($SimpleRETS['Auth_Type'] == 'Digest' OR $SimpleRETS['Auth_Type'] == 'Digest-QOP'){
		
		$RETS_Response = RETSSocket($SimpleRETS,'Login',$RETS_Response,$Realm,$Nonce,$Opaque,$Session_ID);
		
		//Locate Nonce, Realm, Opaque
		preg_match("/realm.*?\"(.*?)\"/", $RETS_Response, $Realm);
		preg_match("/opaque.*?\"(.*?)\"/", $RETS_Response, $Opaque);
		preg_match("/nonce.*?\"(.*?)\"/", $RETS_Response, $Nonce);
		$Nonce = $Nonce[1];
		$Opaque = $Opaque[1];
		$Realm = $Realm[1];
		preg_match("/RETS-Session-ID=(.*?);/", $RETS_Response, $Session_ID);
		$Session_ID = $Session_ID[1];	
		
		//Login again with Nonce, Realm, Opaque
		$RETS_Response = RETSSocket($SimpleRETS,'Login',$RETS_Response,$Realm,$Nonce,$Opaque,$Session_ID);
		
		//Locate updated Nonce, Realm, Opaque and Session ID
		preg_match("/realm.*?\"(.*?)\"/", $RETS_Response, $Realm);
		preg_match("/opaque.*?\"(.*?)\"/", $RETS_Response, $Opaque);
		preg_match("/nonce.*?\"(.*?)\"/", $RETS_Response, $Nonce);
		$Nonce = $Nonce[1];
		$Opaque = $Opaque[1];
		$Realm = $Realm[1];
		preg_match("/RETS-Session-ID=(.*?);/", $RETS_Response, $Session_ID);
		$Session_ID = $Session_ID[1];
		
	}
	//Search
	$RETS_Response = RETSSocket($SimpleRETS,$Type,$RETS_Response,$Realm,$Nonce,$Opaque,$Session_ID);	
	
	return $RETS_Response;
		
}

?>
Return current item: SRETS (SimpleRETS)