Location: PHPKode > scripts > DICT Client > dict-client/dictclient.php
<?php
//TODO - OPTION !!!!!!!!!!!!!!!!!!!!!
//!Dictclient - class to work with DICT SERVERS
class dictclient{

	var $server;//!< server name
	var $port=2628; //!< connection port
	var $socket; //!< main socket
	var $msgid; //!< msgid of session
	var $capabilities; //!< capabilities of server

	/*
	 * !Constructor
	 */
	function dictclient($server,$port=2628)
	{
		$this->server= $server;
		$this->port = $port;
	}
/*
 * !Connects to the server. 
 * @return $result - result of socket creation
 */
	function connect()
	{
		$server=gethostbyname($this->server);
		$socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		$result = socket_connect($socket, $server, $this->port);
		$this->socket=$socket;
		$response=socket_read($this->socket, 2048);
		$pattern="/\<[^\<]*\>/"; // <text>
		$matches="";
		preg_match_all($pattern,$response,&$matches);
		$this->msgid=$matches[0][1];
		$capabilities=$matches[0][0];
		$capabilities=str_replace(array("<",">"),array("",""),$capabilities);
		$this->capabilities=explode(".",$capabilities);
		socket_set_option($this->socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1,"usec"=>100));
		return $result;
	}
	/*
	 * ! Closes connection to dict server
	 */
	function quit()
	{
		$command="QUIT\n";
		$response=$this->request($command);
		socket_shutdown($this->socket);
		socket_close($this->socket);
	}
	/*
	 * ! Sends command to server
	 * @param $command - command to send
	 * @return $response - response of server
	 */
	function request($command)
	{
		echo "DEBUG: requested $command"."\n";
		$response='';
		$out='';
		socket_write($this->socket, $command, strlen($command));
		while (socket_recv($this->socket,&$out,1,0))
		{   
			$response.=$out;
		}
		echo "DEBUG: response $response"."\n";
		echo "DEBUG: request finished"."\n";
		return $response;
	}
/*
 * !Shows list of available DATABASES on server
 * @return $databases - array("database_name","database_description")
 */
	function show_db()
	{
		$databases=array();
		$command="SHOW DB\n";
		$response=$this->request($command);
		$response_lines=explode("\r\n",$response);
		if(strpos($response_lines[0],"554 No databases present")===true)
		{
			echo "DEBUG: No databases present\n";
			return false;
		}
		echo "DEBUG : ". $response_lines[0]."\n";
		$pos=strpos($response_lines[0]," ");
		$posn=strpos($response_lines[0]," ",$pos+1);
		$databases_count=substr($response_lines[0],$pos,$posn-$pos+1);
		echo "DEBUG: Found $databases_count databases \n";

		for($i=1;$i<=$databases_count;$i++)
		{
			$pos=strpos($response_lines[$i]," ");
			$database_name=substr($response_lines[$i],0,$pos);
			$database_description=substr($response_lines[$i],$pos+1);
			$database=array();
			$database["name"]=$database_name;
			$database["description"]=$database_description;
			$databases[]=$database;
		}

		return $databases;
	}
	/*
 * !Shows list of available STRATEGIES on server
 * @return $strategies - array("strategy_name","strategy_description")
 */
	function show_strat()
	{
		$strategies=array();
		$command="SHOW STRAT\n";
		$response=$this->request($command);
		$response_lines=explode("\r\n",$response);

		echo "DEBUG : ". $response_lines[0]."\n";
		$pos=strpos($response_lines[0]," ");
		$posn=strpos($response_lines[0]," ",$pos+1);
		$strategies_count=substr($response_lines[0],$pos,$posn-$pos+1);
		echo "DEBUG: Found $strategies_count strategies \n";

		for($i=1;$i<=$strategies_count;$i++)
		{
			$pos=strpos($response_lines[$i]," ");
			$strategy_name=substr($response_lines[$i],0,$pos);
			$strategy_description=substr($response_lines[$i],$pos+1);
			$strategy=array();
			$strategy["name"]=$strategy_name;
			$strategy["description"]=$strategy_description;
			$strategies[]=$strategy;
		}

		return $strategies;

	}
	/*
	 * !Shows info about database
	 * @param $database - database name
	 * @return $info - info about database
	 */
	function show_info($database)
	{
		$command="SHOW INFO $database\n";
		$response=$this->request($command);
		$info=$response;
		//We should delete first line and .\r\n line from response in future
		return $info;
	}
	/*
	 * !Shows status of server
	 * @return $response - server status
	 */
	function status()//useless
	{
		$command="STATUS\n";
		$response=$this->request($command);
		//We should delete first and last line;
		return $response;
	}
	/*
	 * !Shows HELP info of server
	 * @return $response - HELP INFO
	 */
	function help()//useless
	{
		$command="HELP\n";
		$response=$this->request($command);
		//We should delete first and last line;
		return $response;
	}
	
		/*
	 * !Shows info of server
	 * @return $response - server info 
	 */
	function show_server()
	{
		$command="SHOW SERVER\n";
		$response=$this->request($command);
		$server=$response;
		//We should delete first line and .\r\n line from response in future
		return $server;
	}

	/*
	 * !Sends info about client
	 * @param $text - client info string
	 * @return $response - server response
	 */
	function client($text)
	{
		$command="CLIENT $text\n";
		$response=$this->request($command);
		return $response;
	}
/*
 * !Returns list of matched words using strategy in database
 * @param $database - database name to search in
 * @param $strategy - strategy to use
 * @param $word - word to look for
 */
	function match($database,$strategy,$word)
	{	
		$matches=array();
		$command = "MATCH $database $strategy $word\n";
		$response = $this->request($command);
		$response_lines = explode ("\r\n",$response);
		if(strpos($response_lines[0],"551")==0)
			{
				echo "DEBUG: No matches found";
				//return false;
			}
		$pos=strpos($response_lines[0]," ");
		$posn=strpos($response_lines[0]," ",$pos+1);
		$matches_count=substr($response_lines[0],$pos+1,$posn-$pos-1);
		echo "DEBUG: <$matches_count> matches found";
		for($i=0;$i<$matches_count;$i++)
		{
			$match=explode(" ",$response_lines[$i],2);
			$matches[]=$match;
		}
		return $matches;
	}
/*
 * !Returns definitions of word in database
 * @param $database - database name
 * @param $word - word to look for
 * @return - array ("database name","definition_info")
 */
	function define($database,$word)
	{
		$definitions=array();
		$command="DEFINE ".$database." ".$word ."\n";
		$result=$this->request($command);
		$pos=strpos($result,"\r\n");
		$response=substr($result,1,$pos);
		if(strpos($result,"150 ")===false)
		{
			echo "DEBUG: No definitions found\n";
			return false;
		}

		$temp=explode(" ",$response);
		$matches_count=$temp[1];
		echo "DEBUG: Found $matches_count DEFINITIONS\n";
		$temp=substr($result,$pos+2);
		echo "DEBUG: $temp \n";
		$matches=explode("\n.\r\n",$temp);
		for($i=0;$i<$matches_count;$i++)
		{
			$match=$matches[$i];
			$pos=strpos($match,"\r\n");
			$posd=$pos;
			$descr=substr($match,4,$pos-4-1);
			$pos=strpos($descr," ");
			$word=substr($descr,1,$pos);$word='"'.$word;
			$posn=strpos($descr," ",$pos+1);
			$database=substr($descr,$pos+1,$posn-$pos-1);
			$database_name=substr($descr,$posn+1);
			$definition=array();
			$definition["word"]=$word;
			$definition["database"]=$database;
			$definition["database_name"]=$database_name;
			$def=substr($match,$posd+2);
			$definition["definition"]=$def;
			$definitions[]=$definition;
		}
		return $definitions;
	}



};
?>
Return current item: DICT Client