ec8ff5a7a299e2c3626e6200df06a9e7b86bafe4
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 echo "Key: $this->ckey\n";
45 mcrypt_generic_init($this->td, $this->ckey, $this->iv);
46 return TRUE;
47 }
49 return FALSE;
50 }
53 private function encrypt($data)
54 {
55 return mcrypt_generic($this->td, $data);
56 }
59 private function decrypt($data)
60 {
61 /* decrypt data */
62 return mdecrypt_generic($this->td, $data);
63 }
66 public function connected()
67 {
68 return ($this->handle == TRUE);
69 }
72 public function open()
73 {
74 $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
75 if(!$this->handle){
76 $this->handle = NULL;
77 $this->error = $this->errstr;
78 }else{
79 $this->b_data_send = TRUE;
81 /* Open the cipher */
82 $this->td = mcrypt_module_open('rijndael-256', '', 'cbc', '');
84 /* Create the IV and determine the keysize length */
85 $this->iv = substr(md5('GONICUS GmbH'),0, mcrypt_enc_get_iv_size($this->td));
86 $this->ks = mcrypt_enc_get_key_size($this->td);
87 }
88 }
91 public function get_error()
92 {
93 return $this->error;
94 }
97 public function write($data){
98 if($this->handle){
99 $data = $this->encrypt($data);
100 fputs($this->handle, $data."\n");
101 $this->b_data_send = TRUE;
102 }else{
103 $this->b_data_send = FALSE;
104 }
106 return $this->b_data_send;
107 }
110 public function read()
111 {
112 $str = FALSE;
113 if($this->handle){
115 /* Check if there is something to read for us */
116 $read = array("0"=>$this->handle);
117 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
118 $str = "";
120 /* Read data if necessary */
121 while($num && $this->b_data_send){
122 $str.= fread($this->handle, 1024000);
123 $read = array("0"=>$this->handle);
124 $num = @stream_select($read,$write=NULL,$accept=NULL,$this->timeout);
125 }
126 $this->bytes_read = strlen($str);
127 $this->b_data_send = FALSE;
128 $str = $this->decrypt($str);
129 }
130 return $str;
131 }
134 public function bytes_read()
135 {
136 return $this->bytes_read;
137 }
140 public function close()
141 {
142 if($this->handle){
143 fclose($this->handle);
144 }
146 /* Terminate decryption handle and close module */
147 mcrypt_generic_deinit($this->td);
148 }
150 }
153 ?>