Code

dee887e3ae820f5b1e319c1d5e9c69e3367ef4d2
[gosa.git] / gosa-core / include / class_socketClient.inc
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         }
110         
111         private function _is_timeout($start,$stop = 0)
112         {
113                 if($stop == 0){
114                         $stop = microtime();
115                 }
116                 $a = split("\ ",$start);
117                 $b = split("\ ",$stop);
118                 $secs = $b[1] - $a[1];
119                 $msecs= $b[0] - $a[0];
120                 $ret = (float) ($secs+ $msecs);
121                 return($ret => $this->timeout);
122         }
125         public function read()
126         {
127                 // Output the request results
128                 $str = "";
129                 $data = "test";
130                 socket_set_timeout($this->handle,$this->timeout);                       
131                 stream_set_blocking($this->handle,0);
132                 $start = microtime();
134                 while(strlen($str) == 0 || !$this->_is_timeout($start)) {
135                         usleep(10000);
136                         $data = fread($this->handle, 1024000);
137                         if($data && strlen($data)>0) {
138                                 $str .= $data;
139                         } else {
140                                 break;
141                         }
142                 }
143                 if($this->_is_timeout($start)){
144                         trigger_error(sprintf("Exceeded timeout %f while reading from socket",$this->timeout));
145                 }
146                 $this->bytes_read = strlen($str);
147                 $this->b_data_send = FALSE;
148                 $str = $this->decrypt($str);
149                 return($str);   
150         }
153         public function bytes_read()
154         {
155                 return $this->bytes_read;
156         }
159         public function close()
160         {
161                 if($this->handle){
162                         fclose($this->handle);
163                 }
165                 /* Terminate decryption handle and close module */
166                 @mcrypt_generic_deinit($this->td);
167         }
169 ?>