Code

Added base64_encoding to encrypted transmissions.
[gosa.git] / 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         private $encrypt        = FALSE;
16         private $crypt_key      = "";
18         private $iv;
20         public function __construct($host, $port, $connect = TRUE,$timeout = 3){
21                 $this->host     = $host;
22                 $this->port     = $port;
23                 $this->timeout  = $timeout;
24                 if($connect){
25                         $this->connect();
26                 }
27         }
29         public function SetEncryptionKey($data)
30         {
31                 if(!function_exists("mcrypt_get_iv_size")){
32                         $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.") ;      
33                         return(FALSE);
34                 }else{
35                         $this->encrypt  = TRUE;
36                         $this->crypt_key= $data; 
37                         $iv_size  = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
38                         $this->iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
39                         return(TRUE);
40                 }
41         }
43         private function encrypt($data)
44         {
45                 /* Encrypt data */
46                 if($this->encrypt){
47                         $data = base64_encode(mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv)); 
48                 }
49                 return($data);
50         }
52         private function decrypt($data)
53         {
54                 /* decrypt data */
55                 if($this->encrypt){
56                         $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, base64_decode($data), MCRYPT_MODE_ECB, $this->iv);
57                 }
58                 return($data);
59         }
61         public function connected()
62         {
63                 return($this->handle == TRUE);
64         }
66         public function connect()
67         {
68                 $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
69                 if(!$this->handle){
70                         $this->handle = NULL;
71                         $this->error = $this->errstr;
72                 }else{
73                         $this->b_data_send = TRUE;
74                 }
75         }
77         public function get_error()
78         {
79                 return($this->error);   
80         }
82         public function write($data){
83                 return($this->send($data));
84         }
86         public function send($data)
87         {
88                 if($this->handle){
89                         if($this->encrypt){
90                                 $data = $this->encrypt($data);
91                         }
93                         $data = trim($data);
94                         fputs($this->handle,$data."\n");
95                         $this->b_data_send = TRUE;
96                         return(TRUE);
97                 }else{
98                         return(FALSE);
99                 }
100         }
102         public function close()
103         {
104                 if($this->handle){
105                         fclose($this->handle);
106                 }
107         }
108         
109         private function _read()
110         {
111                 $str = FALSE;
112                 if($this->handle){
114                         /* Check if there is something to read for us */
115                         $read = array("0"=>$this->handle);      
116                         $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
117                         $str = "";              
118                 
119                         /* Read data if necessary */
120                         while($num && $this->b_data_send){
121                                 $str.= fread($this->handle, 1024000);
122                                 $read = array("0"=>$this->handle);      
123                                 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
124                         }
125                         $this->bytes_read = strlen($str);
126                         $this->b_data_send = FALSE;
127                         if($this->encrypt){
128                                 $str = $this->decrypt($str);
129                         }
130                 }
131                 return($str);
132         }
134         public function read()
135         {
136                 return($this->_read());
137         }
139         public function bytes_read()
140         {
141                 return($this->bytes_read);
142         }
146 ?>