Code

Updated Ammount of read bytes .
[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                 $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                         $start = microtime();           
121                         $num = @stream_select($read,$write,$accept,floor($this->timeout), ceil($this->timeout*100000));
122                         $str = "";              
123                         socket_set_timeout($this->handle,$this->timeout);                       
125                         /* Read data if necessary */
126                         while($num && get_MicroTimeDiff($start,microtime()) < $this->timeout){
127                                 $str.= fread($this->handle, 1024000);
128                                 $read = array("0"=>$this->handle);      
129                                 $num = @stream_select($read,$write,$accept,floor($this->timeout), ceil($this->timeout*100000));
130                         }
131                         $this->bytes_read = strlen($str);
132                         $this->b_data_send = FALSE;
133                         $str = $this->decrypt($str);
134                 }
135                 return $str;
136         }
139         public function bytes_read()
140         {
141                 return $this->bytes_read;
142         }
145         public function close()
146         {
147                 if($this->handle){
148                         fclose($this->handle);
149                 }
151                 /* Terminate decryption handle and close module */
152                 mcrypt_generic_deinit($this->td);
153         }
155 ?>