1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
<?php
/*
* This code is part of GOsa (http://www.gosa-project.org)
* Copyright (C) 2003-2008 GONICUS GmbH
*
* ID: $$Id: class_socketClient.inc 17830 2010-04-26 08:38:12Z cajus $$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class Socket_Client
{
private $host = "";
private $port = "";
private $timeout= "";
private $errno = "";
private $errstr = "";
private $b_data_send = FALSE;
private $handle = NULL;
private $bytes_read = 0;
private $error = "";
private $is_error = FALSE;
private $b_encrypt = FALSE;
/* Crypto information */
private $ckey= "";
private $iv;
private $openssl_cipher = "aes-256-cbc";
private $iv_len = -1;
public function __construct($host, $port, $connect = TRUE, $timeout = 3){
$this->host= $host;
$this->port= $port;
$this->timeout= $timeout;
$this->reset_error();
$this->iv_len = openssl_cipher_iv_length($openssl_cipher);
/* Connect if needed */
if($connect){
$this->open();
}
}
public function setEncryptionKey($key)
{
if ($this->connected()){
$this->ckey = md5($key);
$this->b_encrypt = TRUE;
}
return($this->b_encrypt);
}
private function encrypt($data)
{
if($this->b_encrypt){
$data = base64_encode(openssl_encrypt($data, $this->openssl_cipher, $this->ckey, OPENSSL_RAW_DATA, $this->iv));
}
return($data);
}
private function decrypt($data)
{
/* decrypt data */
if($this->b_encrypt && strlen($data)){
$data = openssl_decrypt(base64_decode($data), $this->openssl_cipher, $this->ckey, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->iv);
}
return($data);
}
public function connected()
{
return ($this->handle == TRUE);
}
public function open()
{
$this->reset_error();
$this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
if(!$this->handle){
$this->handle = NULL;
$this->set_error(sprintf(_("Socket connection to %s:%s failed: %s"), bold($this->host), bold($this->port), $this->errstr));
}else{
$this->b_data_send = TRUE;
/* Create the IV and determine the keysize length */
$this->iv = substr(md5('GONICUS GmbH'),0, $this->iv_len);
}
}
public function set_error($str)
{
$this->is_error =TRUE;
$this->error=$str;
}
public function reset_error()
{
$this->is_error =FALSE;
$this->error = "";
}
public function is_error()
{
return($this->is_error);
}
public function get_error()
{
return $this->error;
}
public function write($data){
if($this->handle){
$data = $this->encrypt($data);
fputs($this->handle, $data."\n");
$this->b_data_send = TRUE;
}else{
$this->b_data_send = FALSE;
}
return $this->b_data_send;
}
public function read()
{
// Output the request results
$this->reset_error();
$str = "";
$data = "test";
socket_set_timeout($this->handle,$this->timeout);
stream_set_blocking($this->handle,0);
$start = microtime(TRUE);
/* Read while
* nothing was read yet
* the timelimit reached
* there is not data left on the socket.
*/
while(TRUE){
usleep(10000);
$data = fread($this->handle, 1024000);
if($data && strlen($data)>0) {
$str .= $data;
} else {
if(strlen($str) != 0){
/* We got <xml> but </xml> is still missing, keep on reading */
if(preg_match("/<\/xml>/",$this->decrypt($str))){
break;
}
}
}
if((microtime(TRUE) - $start) > $this->timeout ){
$this->set_error(sprintf(_("Socket timeout of %s seconds reached!"), bold($this->timeout)));
break;
}
}
$this->bytes_read = strlen($str);
$this->b_data_send = FALSE;
$str = $this->decrypt($str);
return($str);
}
public function bytes_read()
{
return $this->bytes_read;
}
public function close()
{
if($this->handle){
fclose($this->handle);
}
}
}
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>
|