1 <?php
3 class Socket_Client
4 {
5 private $host = "";
6 private $port = "";
7 private $timeout= "";
8 private $errno = "";
9 private $errstr = "";
10 private $b_data_send = FALSE;
11 private $handle = NULL;
12 private $bytes_read = 0;
13 private $error = "";
15 private $encrypt = FALSE;
16 private $crypt_key = "";
18 private $iv;
20 public function __construct($host, $port, $connect = TRUE,$timeout = 3){
21 $this->host = $host;
22 $this->port = $port;
23 $this->timeout = $timeout;
24 if($connect){
25 $this->connect();
26 }
27 }
29 public function SetEncryptionKey($data)
30 {
31 if(!function_exists("mcrypt_get_iv_size")){
32 $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.") ;
33 return(FALSE);
34 }else{
35 $this->encrypt = TRUE;
36 $this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
37 $this->crypt_key= str_pad($data, $this->iv_size, $data);
38 echo "Setting key to $this->crypt_key\n";
39 $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
40 return(TRUE);
41 }
42 }
44 private function encrypt($data)
45 {
46 /* Encrypt data */
47 if($this->encrypt){
48 $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_CBC, $this->iv);
49 }
50 echo "EEEE>>>>>>>>>>>>>>>>>>>>>>>>> ".strlen($data)."\n";
51 return($data);
52 }
54 private function decrypt($data)
55 {
56 echo "DDDD>>>>>>>>>>>>>>>>>>>>>>>>> ".strlen($data)."\n";
57 /* decrypt data */
58 $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, rtrim($data), MCRYPT_MODE_CBC, $this->iv);
59 $data = ltrim($data,"0");
60 return($data);
61 }
63 public function connected()
64 {
65 return($this->handle == TRUE);
66 }
68 public function connect()
69 {
70 $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
71 if(!$this->handle){
72 $this->handle = NULL;
73 $this->error = $this->errstr;
74 }else{
75 $this->b_data_send = TRUE;
76 }
77 }
79 public function get_error()
80 {
81 return($this->error);
82 }
84 public function write($data){
85 return($this->send($data));
86 }
88 public function send($data)
89 {
90 $data= str_repeat("0", 16 - strlen($data)%16).$data;
91 if($this->handle){
92 $data = $this->encrypt($data);
93 $data = trim($data);
94 fputs($this->handle, $data."\n");
95 $this->b_data_send = TRUE;
96 return(TRUE);
97 }else{
98 return(FALSE);
99 }
100 }
102 public function close()
103 {
104 if($this->handle){
105 fclose($this->handle);
106 }
107 }
109 private function _read()
110 {
111 $str = FALSE;
112 if($this->handle){
114 /* Check if there is something to read for us */
115 $read = array("0"=>$this->handle);
116 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
117 $str = "";
119 /* Read data if necessary */
120 while($num && $this->b_data_send){
121 $str.= fread($this->handle, 1024000);
122 $read = array("0"=>$this->handle);
123 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
124 }
125 $this->bytes_read = strlen($str);
126 $this->b_data_send = FALSE;
127 $str = $this->decrypt($str);
128 }
129 return($str);
130 }
132 public function read()
133 {
134 return($this->_read());
135 }
137 public function bytes_read()
138 {
139 return($this->bytes_read);
140 }
141 }
144 ?>