Remote.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <ssb@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Remote.php,v 1.50 2004/06/08 18:03:46 cellog Exp $
  20. require_once 'PEAR.php';
  21. require_once 'PEAR/Config.php';
  22. /**
  23. * This is a class for doing remote operations against the central
  24. * PEAR database.
  25. *
  26. * @nodep XML_RPC_Value
  27. * @nodep XML_RPC_Message
  28. * @nodep XML_RPC_Client
  29. */
  30. class PEAR_Remote extends PEAR
  31. {
  32. // {{{ properties
  33. var $config = null;
  34. var $cache = null;
  35. // }}}
  36. // {{{ PEAR_Remote(config_object)
  37. function PEAR_Remote(&$config)
  38. {
  39. $this->PEAR();
  40. $this->config = &$config;
  41. }
  42. // }}}
  43. // {{{ getCache()
  44. function getCache($args)
  45. {
  46. $id = md5(serialize($args));
  47. $cachedir = $this->config->get('cache_dir');
  48. $filename = $cachedir . DIRECTORY_SEPARATOR . 'xmlrpc_cache_' . $id;
  49. if (!file_exists($filename)) {
  50. return null;
  51. };
  52. $fp = fopen($filename, 'rb');
  53. if (!$fp) {
  54. return null;
  55. }
  56. $content = fread($fp, filesize($filename));
  57. fclose($fp);
  58. $result = array(
  59. 'age' => time() - filemtime($filename),
  60. 'lastChange' => filemtime($filename),
  61. 'content' => unserialize($content),
  62. );
  63. return $result;
  64. }
  65. // }}}
  66. // {{{ saveCache()
  67. function saveCache($args, $data)
  68. {
  69. $id = md5(serialize($args));
  70. $cachedir = $this->config->get('cache_dir');
  71. if (!file_exists($cachedir)) {
  72. System::mkdir(array('-p', $cachedir));
  73. }
  74. $filename = $cachedir.'/xmlrpc_cache_'.$id;
  75. $fp = @fopen($filename, "wb");
  76. if ($fp) {
  77. fwrite($fp, serialize($data));
  78. fclose($fp);
  79. };
  80. }
  81. // }}}
  82. // {{{ call(method, [args...])
  83. function call($method)
  84. {
  85. $_args = $args = func_get_args();
  86. $this->cache = $this->getCache($args);
  87. $cachettl = $this->config->get('cache_ttl');
  88. // If cache is newer than $cachettl seconds, we use the cache!
  89. if ($this->cache !== null && $this->cache['age'] < $cachettl) {
  90. return $this->cache['content'];
  91. };
  92. if (extension_loaded("xmlrpc")) {
  93. $result = call_user_func_array(array(&$this, 'call_epi'), $args);
  94. if (!PEAR::isError($result)) {
  95. $this->saveCache($_args, $result);
  96. };
  97. return $result;
  98. }
  99. if (!@include_once("XML/RPC.php")) {
  100. return $this->raiseError("For this remote PEAR operation you need to install the XML_RPC package");
  101. }
  102. array_shift($args);
  103. $server_host = $this->config->get('master_server');
  104. $username = $this->config->get('username');
  105. $password = $this->config->get('password');
  106. $eargs = array();
  107. foreach($args as $arg) $eargs[] = $this->_encode($arg);
  108. $f = new XML_RPC_Message($method, $eargs);
  109. if ($this->cache !== null) {
  110. $maxAge = '?maxAge='.$this->cache['lastChange'];
  111. } else {
  112. $maxAge = '';
  113. };
  114. $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
  115. if ($proxy = parse_url($this->config->get('http_proxy'))) {
  116. $proxy_host = @$proxy['host'];
  117. $proxy_port = @$proxy['port'];
  118. $proxy_user = @urldecode(@$proxy['user']);
  119. $proxy_pass = @urldecode(@$proxy['pass']);
  120. }
  121. $c = new XML_RPC_Client('/xmlrpc.php'.$maxAge, $server_host, 80, $proxy_host, $proxy_port, $proxy_user, $proxy_pass);
  122. if ($username && $password) {
  123. $c->setCredentials($username, $password);
  124. }
  125. if ($this->config->get('verbose') >= 3) {
  126. $c->setDebug(1);
  127. }
  128. $r = $c->send($f);
  129. if (!$r) {
  130. return $this->raiseError("XML_RPC send failed");
  131. }
  132. $v = $r->value();
  133. if ($e = $r->faultCode()) {
  134. if ($e == $GLOBALS['XML_RPC_err']['http_error'] && strstr($r->faultString(), '304 Not Modified') !== false) {
  135. return $this->cache['content'];
  136. }
  137. return $this->raiseError($r->faultString(), $e);
  138. }
  139. $result = XML_RPC_decode($v);
  140. $this->saveCache($_args, $result);
  141. return $result;
  142. }
  143. // }}}
  144. // {{{ call_epi(method, [args...])
  145. function call_epi($method)
  146. {
  147. do {
  148. if (extension_loaded("xmlrpc")) {
  149. break;
  150. }
  151. if (OS_WINDOWS) {
  152. $ext = 'dll';
  153. } elseif (PHP_OS == 'HP-UX') {
  154. $ext = 'sl';
  155. } elseif (PHP_OS == 'AIX') {
  156. $ext = 'a';
  157. } else {
  158. $ext = 'so';
  159. }
  160. $ext = OS_WINDOWS ? 'dll' : 'so';
  161. @dl("xmlrpc-epi.$ext");
  162. if (extension_loaded("xmlrpc")) {
  163. break;
  164. }
  165. @dl("xmlrpc.$ext");
  166. if (extension_loaded("xmlrpc")) {
  167. break;
  168. }
  169. return $this->raiseError("unable to load xmlrpc extension");
  170. } while (false);
  171. $params = func_get_args();
  172. array_shift($params);
  173. $method = str_replace("_", ".", $method);
  174. $request = xmlrpc_encode_request($method, $params);
  175. $server_host = $this->config->get("master_server");
  176. if (empty($server_host)) {
  177. return $this->raiseError("PEAR_Remote::call: no master_server configured");
  178. }
  179. $server_port = 80;
  180. if ($http_proxy = $this->config->get('http_proxy')) {
  181. $proxy = parse_url($http_proxy);
  182. $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
  183. $proxy_host = @$proxy['host'];
  184. $proxy_port = @$proxy['port'];
  185. $proxy_user = @urldecode(@$proxy['user']);
  186. $proxy_pass = @urldecode(@$proxy['pass']);
  187. $fp = @fsockopen($proxy_host, $proxy_port);
  188. $use_proxy = true;
  189. } else {
  190. $use_proxy = false;
  191. $fp = @fsockopen($server_host, $server_port);
  192. }
  193. if (!$fp && $http_proxy) {
  194. return $this->raiseError("PEAR_Remote::call: fsockopen(`$proxy_host', $proxy_port) failed");
  195. } elseif (!$fp) {
  196. return $this->raiseError("PEAR_Remote::call: fsockopen(`$server_host', $server_port) failed");
  197. }
  198. $len = strlen($request);
  199. $req_headers = "Host: $server_host:$server_port\r\n" .
  200. "Content-type: text/xml\r\n" .
  201. "Content-length: $len\r\n";
  202. $username = $this->config->get('username');
  203. $password = $this->config->get('password');
  204. if ($username && $password) {
  205. $req_headers .= "Cookie: PEAR_USER=$username; PEAR_PW=$password\r\n";
  206. $tmp = base64_encode("$username:$password");
  207. $req_headers .= "Authorization: Basic $tmp\r\n";
  208. }
  209. if ($this->cache !== null) {
  210. $maxAge = '?maxAge='.$this->cache['lastChange'];
  211. } else {
  212. $maxAge = '';
  213. };
  214. if ($use_proxy && $proxy_host != '' && $proxy_user != '') {
  215. $req_headers .= 'Proxy-Authorization: Basic '
  216. .base64_encode($proxy_user.':'.$proxy_pass)
  217. ."\r\n";
  218. }
  219. if ($this->config->get('verbose') > 3) {
  220. print "XMLRPC REQUEST HEADERS:\n";
  221. var_dump($req_headers);
  222. print "XMLRPC REQUEST BODY:\n";
  223. var_dump($request);
  224. }
  225. if ($use_proxy && $proxy_host != '') {
  226. $post_string = "POST http://".$server_host;
  227. if ($proxy_port > '') {
  228. $post_string .= ':'.$server_port;
  229. }
  230. } else {
  231. $post_string = "POST ";
  232. }
  233. fwrite($fp, ($post_string."/xmlrpc.php$maxAge HTTP/1.0\r\n$req_headers\r\n$request"));
  234. $response = '';
  235. $line1 = fgets($fp, 2048);
  236. if (!preg_match('!^HTTP/[0-9\.]+ (\d+) (.*)!', $line1, $matches)) {
  237. return $this->raiseError("PEAR_Remote: invalid HTTP response from XML-RPC server");
  238. }
  239. switch ($matches[1]) {
  240. case "200": // OK
  241. break;
  242. case "304": // Not Modified
  243. return $this->cache['content'];
  244. case "401": // Unauthorized
  245. if ($username && $password) {
  246. return $this->raiseError("PEAR_Remote: authorization failed", 401);
  247. } else {
  248. return $this->raiseError("PEAR_Remote: authorization required, please log in first", 401);
  249. }
  250. default:
  251. return $this->raiseError("PEAR_Remote: unexpected HTTP response", (int)$matches[1], null, null, "$matches[1] $matches[2]");
  252. }
  253. while (trim(fgets($fp, 2048)) != ''); // skip rest of headers
  254. while ($chunk = fread($fp, 10240)) {
  255. $response .= $chunk;
  256. }
  257. fclose($fp);
  258. if ($this->config->get('verbose') > 3) {
  259. print "XMLRPC RESPONSE:\n";
  260. var_dump($response);
  261. }
  262. $ret = xmlrpc_decode($response);
  263. if (is_array($ret) && isset($ret['__PEAR_TYPE__'])) {
  264. if ($ret['__PEAR_TYPE__'] == 'error') {
  265. if (isset($ret['__PEAR_CLASS__'])) {
  266. $class = $ret['__PEAR_CLASS__'];
  267. } else {
  268. $class = "PEAR_Error";
  269. }
  270. if ($ret['code'] === '') $ret['code'] = null;
  271. if ($ret['message'] === '') $ret['message'] = null;
  272. if ($ret['userinfo'] === '') $ret['userinfo'] = null;
  273. if (strtolower($class) == 'db_error') {
  274. $ret = $this->raiseError(PEAR::errorMessage($ret['code']),
  275. $ret['code'], null, null,
  276. $ret['userinfo']);
  277. } else {
  278. $ret = $this->raiseError($ret['message'], $ret['code'],
  279. null, null, $ret['userinfo']);
  280. }
  281. }
  282. } elseif (is_array($ret) && sizeof($ret) == 1 && isset($ret[0])
  283. && is_array($ret[0]) &&
  284. !empty($ret[0]['faultString']) &&
  285. !empty($ret[0]['faultCode'])) {
  286. extract($ret[0]);
  287. $faultString = "XML-RPC Server Fault: " .
  288. str_replace("\n", " ", $faultString);
  289. return $this->raiseError($faultString, $faultCode);
  290. }
  291. return $ret;
  292. }
  293. // }}}
  294. // {{{ _encode
  295. // a slightly extended version of XML_RPC_encode
  296. function _encode($php_val)
  297. {
  298. global $XML_RPC_Boolean, $XML_RPC_Int, $XML_RPC_Double;
  299. global $XML_RPC_String, $XML_RPC_Array, $XML_RPC_Struct;
  300. $type = gettype($php_val);
  301. $xmlrpcval = new XML_RPC_Value;
  302. switch($type) {
  303. case "array":
  304. reset($php_val);
  305. $firstkey = key($php_val);
  306. end($php_val);
  307. $lastkey = key($php_val);
  308. if ($firstkey === 0 && is_int($lastkey) &&
  309. ($lastkey + 1) == count($php_val)) {
  310. $is_continuous = true;
  311. reset($php_val);
  312. $size = count($php_val);
  313. for ($expect = 0; $expect < $size; $expect++, next($php_val)) {
  314. if (key($php_val) !== $expect) {
  315. $is_continuous = false;
  316. break;
  317. }
  318. }
  319. if ($is_continuous) {
  320. reset($php_val);
  321. $arr = array();
  322. while (list($k, $v) = each($php_val)) {
  323. $arr[$k] = $this->_encode($v);
  324. }
  325. $xmlrpcval->addArray($arr);
  326. break;
  327. }
  328. }
  329. // fall though if not numerical and continuous
  330. case "object":
  331. $arr = array();
  332. while (list($k, $v) = each($php_val)) {
  333. $arr[$k] = $this->_encode($v);
  334. }
  335. $xmlrpcval->addStruct($arr);
  336. break;
  337. case "integer":
  338. $xmlrpcval->addScalar($php_val, $XML_RPC_Int);
  339. break;
  340. case "double":
  341. $xmlrpcval->addScalar($php_val, $XML_RPC_Double);
  342. break;
  343. case "string":
  344. case "NULL":
  345. $xmlrpcval->addScalar($php_val, $XML_RPC_String);
  346. break;
  347. case "boolean":
  348. $xmlrpcval->addScalar($php_val, $XML_RPC_Boolean);
  349. break;
  350. case "unknown type":
  351. default:
  352. return null;
  353. }
  354. return $xmlrpcval;
  355. }
  356. // }}}
  357. }
  358. ?>