Code

Fixed misplaced "Z"
[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 = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv); 
48                 }
49                 return(base64_encode($data));
50         }
52         private function decrypt($data)
53         {
54                 $data = base64_decode($data);   
55                 /* decrypt data */
56                 if($this->encrypt){
57                         $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv);
58                         $data = rtrim($data,"\x00");
59                 }
60                 return($data);
61         }
63         public function connected()
64         {
65                 return($this->handle == TRUE);
66         }
68         public function connect()
69         {
70                 $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
71                 if(!$this->handle){
72                         $this->handle = NULL;
73                         $this->error = $this->errstr;
74                 }else{
75                         $this->b_data_send = TRUE;
76                 }
77         }
79         public function get_error()
80         {
81                 return($this->error);   
82         }
84         public function write($data){
85                 return($this->send($data));
86         }
88         public function send($data)
89         {
90                 if($this->handle){
91                         $data = $this->encrypt($data);
92                         $data = trim($data);
93                         fputs($this->handle,$data."\n");
94                         $this->b_data_send = TRUE;
95                         return(TRUE);
96                 }else{
97                         return(FALSE);
98                 }
99         }
101         public function close()
102         {
103                 if($this->handle){
104                         fclose($this->handle);
105                 }
106         }
107         
108         private function _read()
109         {
110                 $str = FALSE;
111                 if($this->handle){
113                         /* Check if there is something to read for us */
114                         $read = array("0"=>$this->handle);      
115                         $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
116                         $str = "";              
117                 
118                         /* Read data if necessary */
119                         while($num && $this->b_data_send){
120                                 $str.= fread($this->handle, 1024000);
121                                 $read = array("0"=>$this->handle);      
122                                 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
123                         }
124                         $this->bytes_read = strlen($str);
125                         $this->b_data_send = FALSE;
126                         $str = $this->decrypt($str);
127                 }
128                 return($str);
129         }
131         public function read()
132         {
133                 return($this->_read());
134         }
136         public function bytes_read()
137         {
138                 return($this->bytes_read);
139         }
143 ?>