Code

Updated headers, added Id
[gosa.git] / gosa-core / include / class_socketClient.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class Socket_Client
24 {
25   private $host         = "";
26   private $port         = "";
27   private $timeout= "";
28   private $errno        = "";
29   private $errstr       = "";
30   private $b_data_send = FALSE;
31   private $handle       = NULL;
32   private $bytes_read = 0;
33   private $error = "";
34   private $b_encrypt = FALSE;
36   /* Crypto information */
37   private $td= NULL;
38   private $ckey= "";
39   private $ks;
40   private $iv;
43   public function __construct($host, $port, $connect = TRUE, $timeout = 3){
44     $this->host= $host;
45     $this->port= $port;
46     $this->timeout= $timeout;
48     /* Connect if needed */
49     if($connect){
50       $this->open();
51     }
52   }
55   public function setEncryptionKey($key)
56   {
57     if(!function_exists("mcrypt_get_iv_size")){
58       $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.");
59       $this->ckey = "";
60       $this->b_encrypt = FALSE;
61     }
63     if ($this->connected()){
64       $this->ckey = substr(md5($key), 0, $this->ks);
65       $this->b_encrypt = TRUE;
66     }
68     return($this->b_encrypt);
69   }
72   private function encrypt($data)
73   {
74     if($this->b_encrypt){
75       mcrypt_generic_init($this->td, $this->ckey, $this->iv);
76       $data = base64_encode(mcrypt_generic($this->td, $data));
77     }
78     return($data);
79   }
82   private function decrypt($data)
83   {
84     /* decrypt data */
85     if($this->b_encrypt && strlen($data)){
86       $data = base64_decode($data);
87       mcrypt_generic_init($this->td, $this->ckey, $this->iv);
88       $data = mdecrypt_generic($this->td, $data);
89     }
90     return($data);
91   }
94   public function connected()
95   {
96     return ($this->handle == TRUE);
97   }
100   public function open()
101   {
102     $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
103     if(!$this->handle){
104       $this->handle = NULL;
105       $this->error = $this->errstr;
106     }else{
107       $this->b_data_send = TRUE;
109       /* Open the cipher */
110       $this->td = mcrypt_module_open('rijndael-128', '', 'cbc', '');
112       /* Create the IV and determine the keysize length */
113       $this->iv = substr(md5('GONICUS GmbH'),0, mcrypt_enc_get_iv_size($this->td));
114       $this->ks = mcrypt_enc_get_key_size($this->td);
115     }
116   }
119   public function get_error()
120   {
121     return $this->error;
122   }
125   public function write($data){
126     if($this->handle){
127       $data = $this->encrypt($data);
128       fputs($this->handle, $data."\n");
129       $this->b_data_send = TRUE;
130     }else{
131       $this->b_data_send = FALSE;
132     }
134     return $this->b_data_send;
135   }
138   public function read()
139   {
140     // Output the request results
141     $str = "";
142     $data = "test";
143     socket_set_timeout($this->handle,$this->timeout);                   
144     stream_set_blocking($this->handle,0);
145     $start = microtime(TRUE);
147     /* Read while 
148      * nothing was read yet
149      * the timelimit reached
150      * there is not data left on the socket.
151      */
152     while(TRUE){
153       usleep(10000);
154       $data = fread($this->handle, 1024000);
155       if($data && strlen($data)>0) {
156         $str .= $data;
157       } else {
158         if(strlen($str) != 0){
159           break;
160         }
161       }
162       if((microtime(TRUE) - $start) > $this->timeout ){      
163         break;
164       }
165     }
166     $this->bytes_read = strlen($str);
167     $this->b_data_send = FALSE;
168     $str = $this->decrypt($str);
169     return($str);       
170   }
173   public function bytes_read()
174   {
175     return $this->bytes_read;
176   }
179   public function close()
180   {
181     if($this->handle){
182       fclose($this->handle);
183     }
185     /* Terminate decryption handle and close module */
186     @mcrypt_generic_deinit($this->td);
187   }
189 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
190 ?>