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 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
119 $str = "";
121 /* Read data if necessary */
122 while($num && $this->b_data_send){
123 $str.= fread($this->handle, 1024000);
124 $read = array("0"=>$this->handle);
125 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
126 }
127 $this->bytes_read = strlen($str);
128 $this->b_data_send = FALSE;
129 $str = $this->decrypt($str);
130 }
131 return $str;
132 }
135 public function bytes_read()
136 {
137 return $this->bytes_read;
138 }
141 public function close()
142 {
143 if($this->handle){
144 fclose($this->handle);
145 }
147 /* Terminate decryption handle and close module */
148 mcrypt_generic_deinit($this->td);
149 }
151 }
154 ?>