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 = "";
14 private $b_encrypt = FALSE;
16 /* Crypto information */
17 private $td= NULL;
18 private $ckey= "";
19 private $ks;
20 private $iv;
23 public function __construct($host, $port, $connect = TRUE, $timeout = 3){
24 $this->host= $host;
25 $this->port= $port;
26 $this->timeout= $timeout;
28 /* Connect if needed */
29 if($connect){
30 $this->open();
31 }
32 }
35 public function setEncryptionKey($key)
36 {
37 if(!function_exists("mcrypt_get_iv_size")){
38 $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.");
39 $this->ckey = "";
40 $this->b_encrypt = FALSE;
41 }
43 if ($this->connected()){
44 $this->ckey = substr(md5($key), 0, $this->ks);
45 $this->b_encrypt = TRUE;
46 }
48 return($this->b_encrypt);
49 }
52 private function encrypt($data)
53 {
54 if($this->b_encrypt){
55 mcrypt_generic_init($this->td, $this->ckey, $this->iv);
56 $data = base64_encode(mcrypt_generic($this->td, $data));
57 }
58 return($data);
59 }
62 private function decrypt($data)
63 {
64 /* decrypt data */
65 if($this->b_encrypt){
66 $data = base64_decode($data);
67 mcrypt_generic_init($this->td, $this->ckey, $this->iv);
68 $data = mdecrypt_generic($this->td, $data);
69 }
70 return($data);
71 }
74 public function connected()
75 {
76 return ($this->handle == TRUE);
77 }
80 public function open()
81 {
82 $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
83 if(!$this->handle){
84 $this->handle = NULL;
85 $this->error = $this->errstr;
86 }else{
87 $this->b_data_send = TRUE;
89 /* Open the cipher */
90 $this->td = mcrypt_module_open('rijndael-128', '', 'cbc', '');
92 /* Create the IV and determine the keysize length */
93 $this->iv = substr(md5('GONICUS GmbH'),0, mcrypt_enc_get_iv_size($this->td));
94 $this->ks = mcrypt_enc_get_key_size($this->td);
95 }
96 }
99 public function get_error()
100 {
101 return $this->error;
102 }
105 public function write($data){
106 if($this->handle){
107 $data = $this->encrypt($data);
108 fputs($this->handle, $data."\n");
109 $this->b_data_send = TRUE;
110 }else{
111 $this->b_data_send = FALSE;
112 }
114 return $this->b_data_send;
115 }
118 public function read()
119 {
120 // Output the request results
121 $str = "";
122 $data = "test";
123 socket_set_timeout($this->handle,$this->timeout);
124 stream_set_blocking($this->handle,0);
125 $start = microtime(TRUE);
127 /* Read while
128 * nothing was read yet
129 * the timelimit reached
130 * there is not data left on the socket.
131 */
132 while(TRUE){
133 usleep(10000);
134 $data = fread($this->handle, 1024000);
135 if($data && strlen($data)>0) {
136 $str .= $data;
137 } else {
138 if(strlen($str) != 0){
139 break;
140 }
141 }
142 if((microtime(TRUE) - $start) > $this->timeout ){
143 trigger_error(sprintf("Exceeded timeout %f while reading from socket",$this->timeout));
144 break;
145 }
146 }
147 $this->bytes_read = strlen($str);
148 $this->b_data_send = FALSE;
149 $str = $this->decrypt($str);
150 return($str);
151 }
154 public function bytes_read()
155 {
156 return $this->bytes_read;
157 }
160 public function close()
161 {
162 if($this->handle){
163 fclose($this->handle);
164 }
166 /* Terminate decryption handle and close module */
167 @mcrypt_generic_deinit($this->td);
168 }
169 }
170 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
171 ?>