Code

Updated gosaServerDeamon
[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         }
111         public function read()
112         {
113                 // Output the request results
114                 $str = FALSE;
115                 $data = "test";
116                 socket_set_timeout($this->handle,$this->timeout);                       
117                 stream_set_blocking($this->handle,1);
119                 /* Read while last character is Newline, or we exceeded the timelimit 
120          */
121                 $start = microtime();
122                 while(!preg_match("/\\\n$/",$str) && get_MicroTimeDiff($start,microtime()) < $this->timeout) {
123                         usleep(10000);
124                         $data = fread($this->handle, 1024000);
125                         $str .= $data;
126                 }
127                 if(get_MicroTimeDiff($start,microtime()) >= $this->timeout){
128                         trigger_error(sprintf("Exceeded timeout %f while reading from socket. Time spend for reading was %f.",$this->timeout,get_MicroTimeDiff($start,microtime())));
129                 }
130                 $this->bytes_read = strlen($str);
131                 $this->b_data_send = FALSE;
132                 $str = $this->decrypt($str);
133                 return($str);   
136 #       $str = FALSE;
137 #       if($this->handle){
139 #               /* Check if there is something to read for us */
140 #               $read = array("0"=>$this->handle);
141 #               $write = array();
142 #               $accept = array();      
143 #               $start = microtime();           
144 #               $num = @stream_select($read,$write,$accept,floor($this->timeout), ceil($this->timeout*100000));
145 #               $str = "";              
146 #               socket_set_timeout($this->handle,$this->timeout);                       
148 #               /* Read data if necessary */
149 #               while($num && get_MicroTimeDiff($start,microtime()) < $this->timeout){
150 #                       $str.= fread($this->handle, 1024000);
151 #                       $read = array("0"=>$this->handle);      
152 #                       $num = stream_select($read,$write,$accept,0,200000);
153 #               }
154 #               $this->bytes_read = strlen($str);
155 #               $this->b_data_send = FALSE;
156 #               $str = $this->decrypt($str);
157 #       }
158 #       return $str;
159         }
162         public function bytes_read()
163         {
164                 return $this->bytes_read;
165         }
168         public function close()
169         {
170                 if($this->handle){
171                         fclose($this->handle);
172                 }
174                 /* Terminate decryption handle and close module */
175                 @mcrypt_generic_deinit($this->td);
176         }
178 ?>