| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @author Ma Bingyao(andot@ujn.edu.cn)
|
| 4 |
* @copyright CoolCode.CN
|
| 5 |
* @package PHPRPC_CLIENT
|
| 6 |
* @version 2.1
|
| 7 |
* @last_update 2006-08-09
|
| 8 |
* @link http://www.coolcode.cn/?p=144
|
| 9 |
*
|
| 10 |
* Example usage:
|
| 11 |
*
|
| 12 |
* client.php
|
| 13 |
* <?php
|
| 14 |
* include('phprpc.php');
|
| 15 |
* $rpc_client = new phprpc_client();
|
| 16 |
* $rpc_client->use_service('http://test.coolcode.cn/phprpc/server.php', true);
|
| 17 |
* $rpc_client->encrypt = 2;
|
| 18 |
* echo $rpc_client->add(1, 2);
|
| 19 |
* echo "<br />";
|
| 20 |
* echo $rpc_client->Sub(1, 2); // the function name is case-insensitive
|
| 21 |
* echo "<br />";
|
| 22 |
* // error handle
|
| 23 |
* echo "<pre>";
|
| 24 |
* $result = $rpc_client->mul(1, 2); // no mul function
|
| 25 |
* if (get_class($result) == "phprpc_error") {
|
| 26 |
* print_r($result);
|
| 27 |
* }
|
| 28 |
* $result = $rpc_client->add(1); // wrong arguments
|
| 29 |
* if (get_class($result) == "phprpc_error") {
|
| 30 |
* print_r($result);
|
| 31 |
* }
|
| 32 |
* $rpc_client->use_service('wrong url'); // wrong url
|
| 33 |
* $result = $rpc_client->add(1, 2);
|
| 34 |
* if (get_class($result) == "phprpc_error") {
|
| 35 |
* print_r($result);
|
| 36 |
* }
|
| 37 |
* echo "</pre>";
|
| 38 |
* ?>
|
| 39 |
*/
|
| 40 |
|
| 41 |
class phprpc_error {
|
| 42 |
var $errno;
|
| 43 |
var $errstr;
|
| 44 |
function phprpc_error($errno, $errstr) {
|
| 45 |
$this->errno = $errno;
|
| 46 |
$this->errstr = $errstr;
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
class __phprpc_client {
|
| 51 |
var $scheme;
|
| 52 |
var $host;
|
| 53 |
var $port;
|
| 54 |
var $path;
|
| 55 |
var $user;
|
| 56 |
var $pass;
|
| 57 |
var $timeout;
|
| 58 |
var $output;
|
| 59 |
var $warning;
|
| 60 |
var $proxy;
|
| 61 |
var $__encrypt;
|
| 62 |
var $encrypt;
|
| 63 |
var $cookie;
|
| 64 |
|
| 65 |
function __phprpc_client($url = '', $user = '', $pass = '', $timeout = 10) {
|
| 66 |
if ($url != '') {
|
| 67 |
$this->use_service($url);
|
| 68 |
}
|
| 69 |
$this->user = $user;
|
| 70 |
$this->pass = $pass;
|
| 71 |
$this->timeout = $timeout;
|
| 72 |
$this->encrypt = 0;
|
| 73 |
$this->cookie = '';
|
| 74 |
$this->proxy = null;
|
| 75 |
}
|
| 76 |
function set_proxy($host, $port, $user = null, $pass = null) {
|
| 77 |
$this->proxy = array();
|
| 78 |
$this->proxy['host'] = $host;
|
| 79 |
$this->proxy['port'] = $port;
|
| 80 |
$this->proxy['user'] = $user;
|
| 81 |
$this->proxy['pass'] = $pass;
|
| 82 |
}
|
| 83 |
function use_service($url, $encrypt = false) {
|
| 84 |
$urlparts = parse_url($url);
|
| 85 |
$this->__encrypt = $encrypt;
|
| 86 |
if (!isset($urlparts['host'])) {
|
| 87 |
if (isset($_SERVER["HTTP_HOST"])) {
|
| 88 |
$urlparts['host'] = $_SERVER["HTTP_HOST"];
|
| 89 |
}
|
| 90 |
else if (isset($_SERVER["SERVER_NAME"])) {
|
| 91 |
$urlparts['host'] = $_SERVER["SERVER_NAME"];
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
$urlparts['host'] = "localhost";
|
| 95 |
}
|
| 96 |
if (!isset($urlparts['scheme'])) {
|
| 97 |
if (!isset($_SERVER["HTTPS"]) ||
|
| 98 |
$_SERVER["HTTPS"] == "off" ||
|
| 99 |
$_SERVER["HTTPS"] == "") {
|
| 100 |
$urlparts['scheme'] = "";
|
| 101 |
}
|
| 102 |
else {
|
| 103 |
$urlparts['scheme'] = "https";
|
| 104 |
}
|
| 105 |
}
|
| 106 |
if (!isset($urlparts['port'])) {
|
| 107 |
$urlparts['port'] = $_SERVER["SERVER_PORT"];
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
if (isset($urlparts['scheme']) && ($urlparts['scheme'] == "https")) {
|
| 112 |
$urlparts['scheme'] = "ssl";
|
| 113 |
}
|
| 114 |
else {
|
| 115 |
$urlparts['scheme'] = "";
|
| 116 |
}
|
| 117 |
|
| 118 |
if (!isset($urlparts['port'])) {
|
| 119 |
if ($urlparts['scheme'] == "ssl") {
|
| 120 |
$urlparts['port'] = 443;
|
| 121 |
}
|
| 122 |
else {
|
| 123 |
$urlparts['port'] = 80;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
if (!isset($urlparts['path'])) {
|
| 128 |
$urlparts['path'] = "/";
|
| 129 |
}
|
| 130 |
else if (($urlparts['path']{0} != '/') && ($_SERVER["PHP_SELF"]{0} == '/')) {
|
| 131 |
$urlparts['path'] = substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], '/') + 1) . $urlparts['path'];
|
| 132 |
}
|
| 133 |
|
| 134 |
$this->scheme = $urlparts['scheme'];
|
| 135 |
$this->host = $urlparts['host'];
|
| 136 |
$this->port = $urlparts['port'];
|
| 137 |
$this->path = $urlparts['path'];
|
| 138 |
if ($this->__encrypt) {
|
| 139 |
return $this->__switch_key();
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
function __post($request) {
|
| 144 |
if ($this->proxy == null) {
|
| 145 |
$host = ($this->scheme) ? $this->scheme . "://" . $this->host : $this->host;
|
| 146 |
$handle = @fsockopen($host, $this->port, $errno, $errstr, $this->timeout);
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
$handle = @fsockopen($this->proxy['host'], $this->proxy['port'], $errno, $errstr, $this->timeout);
|
| 150 |
}
|
| 151 |
if ($handle) {
|
| 152 |
$proxy = '';
|
| 153 |
if ($this->proxy) {
|
| 154 |
$proxy = "Proxy-Connection: Keep-Alive\r\n";
|
| 155 |
if ($this->proxy['user']) {
|
| 156 |
$proxy .= "Proxy-Authorization: Basic " . base64_encode($this->proxy['user'] . ":" . $this->proxy['pass']) . "\r\n";
|
| 157 |
}
|
| 158 |
}
|
| 159 |
$auth = '';
|
| 160 |
if ($this->user) {
|
| 161 |
$auth = "Authorization: Basic " . base64_encode($this->user . ":" . $this->pass) . "\r\n";
|
| 162 |
}
|
| 163 |
$cookie = '';
|
| 164 |
if ($this->cookie) {
|
| 165 |
$cookie = "Cookie: " . $this->cookie . "\r\n";
|
| 166 |
}
|
| 167 |
$content_len = strlen($request);
|
| 168 |
$url = (($this->scheme) ? "https://" : "http://") . $this->host . ":" . $this->port . $this->path;
|
| 169 |
$http_request =
|
| 170 |
"POST $url HTTP/1.0\r\n" .
|
| 171 |
"User-Agent: PHPRPC Client/2.1\r\n" .
|
| 172 |
"Host: $this->host:$this->port\r\n" .
|
| 173 |
$proxy .
|
| 174 |
$auth .
|
| 175 |
$cookie .
|
| 176 |
"Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" .
|
| 177 |
"Content-Length: $content_len\r\n" .
|
| 178 |
"\r\n" .
|
| 179 |
$request;
|
| 180 |
fputs($handle, $http_request, strlen($http_request));
|
| 181 |
$buf = '';
|
| 182 |
while (!feof($handle)) {
|
| 183 |
$buf .= fgets($handle, 1024);
|
| 184 |
}
|
| 185 |
fclose($handle);
|
| 186 |
$buf = explode("\r\n\r\n", $buf);
|
| 187 |
return $buf;
|
| 188 |
}
|
| 189 |
else {
|
| 190 |
$result = new phprpc_error($errno, $errstr);
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
| 194 |
function __switch_key() {
|
| 195 |
$errno = 0;
|
| 196 |
$errstr = '';
|
| 197 |
$result = true;
|
| 198 |
$request = "phprpc_encrypt=true";
|
| 199 |
$response = $this->__post($request);
|
| 200 |
if (is_array($response)) {
|
| 201 |
$header = $response[0];
|
| 202 |
$body = $response[1];
|
| 203 |
if (strpos($header, 'X-Powered-By: PHPRPC Server') !== FALSE) {
|
| 204 |
if (preg_match('/\r\nSet\-Cookie\:(.*?)(\r\n|$)/', $header, $match)) {
|
| 205 |
$this->cookie = array();
|
| 206 |
$cookie = explode(";", $match[1]);
|
| 207 |
for ($i = 0; $i < count($cookie); $i++) {
|
| 208 |
$cookie[$i] = trim($cookie[$i]);
|
| 209 |
if ((substr($cookie[$i], 0, 5) != 'path=') and
|
| 210 |
(substr($cookie[$i], 0, 7) != 'domain=')) {
|
| 211 |
$this->cookie[] = $cookie[$i];
|
| 212 |
}
|
| 213 |
}
|
| 214 |
$this->cookie = join('; ', $this->cookie);
|
| 215 |
}
|
| 216 |
$body = explode("\r\n", trim($body));
|
| 217 |
if (substr($body[0], 0, 14) == "phprpc_encrypt") {
|
| 218 |
require_once('bcmath.php');
|
| 219 |
if (!extension_loaded('xxtea')) {
|
| 220 |
require_once('xxtea.php');
|
| 221 |
}
|
| 222 |
$this->__encrypt = unserialize(base64_decode(substr($body[0], 16, strlen($body[0]) - 18)));
|
| 223 |
if (extension_loaded('big_int')) {
|
| 224 |
$this->__encrypt['x'] = bi_to_str(bi_set_bit(bi_rand(127), 126));
|
| 225 |
$key = bcdec2str(bi_to_str(bi_powmod(bi_from_str($this->__encrypt['y']),
|
| 226 |
bi_from_str($this->__encrypt['x']),
|
| 227 |
bi_from_str($this->__encrypt['p']))));
|
| 228 |
$this->__encrypt['k'] = str_repeat("\0", 16 - strlen($key)) . $key;
|
| 229 |
$encrypt = bi_to_str(bi_powmod(bi_from_str($this->__encrypt['g']),
|
| 230 |
bi_from_str($this->__encrypt['x']),
|
| 231 |
bi_from_str($this->__encrypt['p'])));
|
| 232 |
}
|
| 233 |
else {
|
| 234 |
$this->__encrypt['x'] = bcrand(127, 1);
|
| 235 |
$key = bcdec2str(bcpowmod($this->__encrypt['y'],
|
| 236 |
$this->__encrypt['x'],
|
| 237 |
$this->__encrypt['p']));
|
| 238 |
$this->__encrypt['k'] = str_repeat("\0", 16 - strlen($key)) . $key;
|
| 239 |
$encrypt = bcpowmod($this->__encrypt['g'],
|
| 240 |
$this->__encrypt['x'],
|
| 241 |
$this->__encrypt['p']);
|
| 242 |
}
|
| 243 |
$request = "phprpc_encrypt=$encrypt";
|
| 244 |
$this->__post($request);
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
$this->__encrypt = false;
|
| 248 |
}
|
| 249 |
}
|
| 250 |
else {
|
| 251 |
$this->__encrypt = false;
|
| 252 |
$result = new phprpc_error(E_ERROR, "Wrong PHPRPC Server");
|
| 253 |
}
|
| 254 |
}
|
| 255 |
else {
|
| 256 |
$this->__encrypt = false;
|
| 257 |
$result = $response;
|
| 258 |
}
|
| 259 |
return $result;
|
| 260 |
}
|
| 261 |
function call($function, &$arguments, $ref = true) {
|
| 262 |
$request = "phprpc_func=$function";
|
| 263 |
if (count($arguments) > 0) {
|
| 264 |
$args = serialize($arguments);
|
| 265 |
if (($this->__encrypt !== false) and ($this->encrypt > 0)) {
|
| 266 |
$args = xxtea_encrypt($args, $this->__encrypt['k']);
|
| 267 |
}
|
| 268 |
$request .= "&phprpc_args=" . base64_encode($args);
|
| 269 |
}
|
| 270 |
$request .= "&phprpc_encrypt={$this->encrypt}";
|
| 271 |
if (!$ref) {
|
| 272 |
$request .= "&phprpc_ref=false";
|
| 273 |
}
|
| 274 |
$request = str_replace('+', '%2B', $request);
|
| 275 |
$response = $this->__post($request);
|
| 276 |
if (is_array($response)) {
|
| 277 |
$header = $response[0];
|
| 278 |
if (strpos($header, 'X-Powered-By: PHPRPC Server') !== false) {
|
| 279 |
$body = explode("\r\n", trim($response[1]));
|
| 280 |
$this->warning = null;
|
| 281 |
if (substr($body[0], 0, 12) == "phprpc_errno") {
|
| 282 |
$errno = (int)substr($body[0], 14, strlen($body[0]) - 16);
|
| 283 |
$errstr = base64_decode(substr($body[1], 15, strlen($body[1]) - 17));
|
| 284 |
$result = new phprpc_error($errno, $errstr);
|
| 285 |
$this->output = base64_decode(substr($body[2], 15, strlen($body[2]) - 17));
|
| 286 |
}
|
| 287 |
else if (substr($body[0], 0, 13) == "phprpc_result") {
|
| 288 |
$result = base64_decode(substr($body[0], 15, strlen($body[0]) - 17));
|
| 289 |
$has_args = (substr($body[1], 0, 11) == "phprpc_args");
|
| 290 |
if ($has_args) {
|
| 291 |
$arguments = base64_decode(substr($body[1], 13, strlen($body[1]) - 15));
|
| 292 |
$errno = $body[2];
|
| 293 |
$errstr = $body[3];
|
| 294 |
$output = $body[4];
|
| 295 |
}
|
| 296 |
else {
|
| 297 |
$errno = $body[1];
|
| 298 |
$errstr = $body[2];
|
| 299 |
$output = $body[3];
|
| 300 |
}
|
| 301 |
if (($this->__encrypt !== false) and ($this->encrypt > 0)) {
|
| 302 |
if ($this->encrypt > 1) {
|
| 303 |
$result = xxtea_decrypt($result, $this->__encrypt['k']);
|
| 304 |
}
|
| 305 |
if ($has_args) {
|
| 306 |
$arguments = xxtea_decrypt($arguments, $this->__encrypt['k']);
|
| 307 |
}
|
| 308 |
}
|
| 309 |
$result = unserialize($result);
|
| 310 |
if ($has_args) {
|
| 311 |
$arguments = unserialize($arguments);
|
| 312 |
}
|
| 313 |
$errno = (int)substr($errno, 14, strlen($errno) - 16);
|
| 314 |
$errstr = base64_decode(substr($errstr, 15, strlen($errstr) - 17));
|
| 315 |
if ($errno != 0) {
|
| 316 |
$this->warning = new phprpc_error($errno, $errstr);
|
| 317 |
}
|
| 318 |
$this->output = base64_decode(substr($output, 15, strlen($output) - 17));
|
| 319 |
}
|
| 320 |
else {
|
| 321 |
$result = new phprpc_error(E_ERROR, "Wrong PHPRPC Server");
|
| 322 |
}
|
| 323 |
}
|
| 324 |
else {
|
| 325 |
$result = new phprpc_error(E_ERROR, "Wrong PHPRPC Server");
|
| 326 |
}
|
| 327 |
}
|
| 328 |
else {
|
| 329 |
$result = $response;
|
| 330 |
}
|
| 331 |
return $result;
|
| 332 |
}
|
| 333 |
}
|
| 334 |
|
| 335 |
if (function_exists("overload") && version_compare(phpversion(), "5", "<")) {
|
| 336 |
eval('
|
| 337 |
class phprpc_client extends __phprpc_client {
|
| 338 |
function __call($function, $arguments, &$return) {
|
| 339 |
$return = $this->call($function, $arguments, false);
|
| 340 |
return true;
|
| 341 |
}
|
| 342 |
}
|
| 343 |
overload("phprpc_client");
|
| 344 |
');
|
| 345 |
}
|
| 346 |
else {
|
| 347 |
class phprpc_client extends __phprpc_client {
|
| 348 |
function __call($function, $arguments) {
|
| 349 |
return $this->call($function, $arguments, false);
|
| 350 |
}
|
| 351 |
}
|
| 352 |
}
|
| 353 |
?>
|