0ed42a5fa3fda35febbf76d423f71a1e342b5f34
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 /* Crypto information */
16 private $td= NULL;
17 private $ckey= "";
18 private $ks;
19 private $iv;
22 public function __construct($host, $port, $connect = TRUE, $timeout = 3){
23 $this->host= $host;
24 $this->port= $port;
25 $this->timeout= $timeout;
27 /* Connect if needed */
28 if($connect){
29 $this->open();
30 }
31 }
34 public function setEncryptionKey($key)
35 {
36 if(!function_exists("mcrypt_get_iv_size")){
37 $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.");
38 $this->ckey = "";
39 return FALSE ;
40 }
42 if ($this->connected()){
43 $this->ckey = substr(md5($key), 0, $this->ks);
44 return TRUE;
45 }
47 return FALSE;
48 }
51 private function encrypt($data)
52 {
53 mcrypt_generic_init($this->td, $this->ckey, $this->iv);
54 return base64_encode(mcrypt_generic($this->td, $data));
55 }
58 private function decrypt($data)
59 {
60 /* decrypt data */
61 $data = base64_decode($data);
62 mcrypt_generic_init($this->td, $this->ckey, $this->iv);
63 return mdecrypt_generic($this->td, $data);
64 }
67 public function connected()
68 {
69 return ($this->handle == TRUE);
70 }
73 public function open()
74 {
75 $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
76 if(!$this->handle){
77 $this->handle = NULL;
78 $this->error = $this->errstr;
79 }else{
80 $this->b_data_send = TRUE;
82 /* Open the cipher */
83 $this->td = mcrypt_module_open('rijndael-128', '', 'cbc', '');
85 /* Create the IV and determine the keysize length */
86 $this->iv = substr(md5('GONICUS GmbH'),0, mcrypt_enc_get_iv_size($this->td));
87 $this->ks = mcrypt_enc_get_key_size($this->td);
88 }
89 }
92 public function get_error()
93 {
94 return $this->error;
95 }
98 public function write($data){
99 if($this->handle){
100 $data = $this->encrypt($data);
101 fputs($this->handle, $data."\n");
102 $this->b_data_send = TRUE;
103 }else{
104 $this->b_data_send = FALSE;
105 }
107 return $this->b_data_send;
108 }
111 public function read()
112 {
113 $str = FALSE;
114 if($this->handle){
116 /* Check if there is something to read for us */
117 $read = array("0"=>$this->handle);
118 $write = array();
119 $accept = array();
120 $num = @stream_select($read,$write,$accept,floor($this->timeout), ceil($this->timeout*1000000));
121 $str = "";
123 /* Read data if necessary */
124 while($num && $this->b_data_send){
125 $str.= fread($this->handle, 1024000);
126 $read = array("0"=>$this->handle);
127 $num = @stream_select($read,$write,$accept,floor($this->timeout), ceil($this->timeout*1000000));
128 }
129 $this->bytes_read = strlen($str);
130 $this->b_data_send = FALSE;
131 $str = $this->decrypt($str);
132 }
133 return $str;
134 }
137 public function bytes_read()
138 {
139 return $this->bytes_read;
140 }
143 public function close()
144 {
145 if($this->handle){
146 fclose($this->handle);
147 }
149 /* Terminate decryption handle and close module */
150 mcrypt_generic_deinit($this->td);
151 }
152 }
153 ?>