| 1 | <?php |
| 2 | /****************************************************************************** |
| 3 | * * |
| 4 | * Original file can be found on http://xmlrpc-epi.sourceforge.net/ * |
| 5 | * in the module xmlrpc-epi-php v0.51 file samples/utils/utils.php * |
| 6 | * * |
| 7 | * The Polytechnique.org TEAM * |
| 8 | * * |
| 9 | **************************************************************************{{{*/ |
| 10 | |
| 11 | /* |
| 12 | This file is part of, or distributed with, libXMLRPC - a C library for |
| 13 | xml-encoded function calls. |
| 14 | |
| 15 | Author: Dan Libby (dan@libby.com) |
| 16 | Epinions.com may be contacted at feedback@epinions-inc.com |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | Copyright 2001 Epinions, Inc. |
| 21 | |
| 22 | Subject to the following 3 conditions, Epinions, Inc. permits you, free |
| 23 | of charge, to (a) use, copy, distribute, modify, perform and display this |
| 24 | software and associated documentation files (the "Software"), and (b) |
| 25 | permit others to whom the Software is furnished to do so as well. |
| 26 | |
| 27 | 1) The above copyright notice and this permission notice shall be included |
| 28 | without modification in all copies or substantial portions of the |
| 29 | Software. |
| 30 | |
| 31 | 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF |
| 32 | ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY |
| 33 | IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 34 | PURPOSE OR NONINFRINGEMENT. |
| 35 | |
| 36 | 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 37 | SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT |
| 38 | OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING |
| 39 | NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH |
| 40 | DAMAGES. |
| 41 | |
| 42 | */ |
| 43 | |
| 44 | /* xmlrpc utilities (xu) |
| 45 | * author: Dan Libby (dan@libby.com) |
| 46 | */ |
| 47 | |
| 48 | /* generic function to call an http server with post method */ |
| 49 | function xu_query_http_post($request, $host, $uri, $port, $timeout, $user, |
| 50 | $pass, $secure=false) |
| 51 | { |
| 52 | $response_buf = ''; |
| 53 | if ($host && $uri && $port) { |
| 54 | $content_len = strlen($request); |
| 55 | $fsockopen = $secure ? 'fsockopen_ssl' : 'fsockopen'; |
| 56 | $query_fd = $fsockopen($host, $port, $errno, $errstr, 10); |
| 57 | |
| 58 | if ($query_fd) { |
| 59 | |
| 60 | $auth = ''; |
| 61 | if ($user) { |
| 62 | $auth = 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n"; |
| 63 | } |
| 64 | |
| 65 | $http_request = |
| 66 | "POST $uri HTTP/1.0\r\n" . |
| 67 | "Host: $host:$port\r\n" . |
| 68 | $auth . |
| 69 | "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" . |
| 70 | "Content-Type: text/xml\r\n" . |
| 71 | "Content-Length: $content_len\r\n" . |
| 72 | "Connection: Close\r\n" . |
| 73 | "\r\n" . |
| 74 | $request; |
| 75 | |
| 76 | fputs($query_fd, $http_request, strlen($http_request)); |
| 77 | |
| 78 | $header_parsed = false; |
| 79 | while (!feof($query_fd)) { |
| 80 | $line = fgets($query_fd, 4096); |
| 81 | if (!$header_parsed) { |
| 82 | if ($line === "\r\n" || $line === "\n") { |
| 83 | $header_parsed = 1; |
| 84 | } |
| 85 | } else { |
| 86 | $response_buf .= $line; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fclose($query_fd); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $response_buf; |
| 95 | } |
| 96 | |
| 97 | function find_and_decode_xml($buf) |
| 98 | { |
| 99 | if (strlen($buf)) { |
| 100 | $xml_begin = substr($buf, strpos($buf, '<?xml')); |
| 101 | if (strlen($xml_begin)) { |
| 102 | $retval = xmlrpc_decode($xml_begin); |
| 103 | } |
| 104 | } |
| 105 | return $retval; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * @param params a struct containing 3 or more of these key/val pairs: |
| 111 | * @param host remote host (required) |
| 112 | * @param uri remote uri (required) |
| 113 | * @param port remote port (required) |
| 114 | * @param method name of method to call |
| 115 | * @param args arguments to send (parameters to remote xmlrpc server) |
| 116 | * @param timeout timeout in secs. (0 = never) |
| 117 | * @param user user name for authentication. |
| 118 | * @param pass password for authentication |
| 119 | * @param secure secure. wether to use fsockopen_ssl. (requires special php build). |
| 120 | */ |
| 121 | function xu_rpc_http_concise($params) { |
| 122 | $host = $uri = $port = $method = $args = null; |
| 123 | $timeout = $user = $pass = $secure = null; |
| 124 | |
| 125 | extract($params); |
| 126 | |
| 127 | // default values |
| 128 | if (!$port) { |
| 129 | $port = 80; |
| 130 | } |
| 131 | if (!$uri) { |
| 132 | $uri = '/'; |
| 133 | } |
| 134 | if ($host && $uri && $port) { |
| 135 | $request_xml = xmlrpc_encode_request($method, $args); |
| 136 | $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, |
| 137 | $timeout, $user, $pass, $secure); |
| 138 | $retval = find_and_decode_xml($response_buf); |
| 139 | } |
| 140 | return $retval; |
| 141 | } |
| 142 | |
| 143 | /*}}}*********************************************************************** |
| 144 | * Copyright (C) 2003-2006 Polytechnique.org * |
| 145 | * http://opensource.polytechnique.org/ * |
| 146 | * * |
| 147 | * This program is free software; you can redistribute it and/or modify * |
| 148 | * it under the terms of the GNU General Public License as published by * |
| 149 | * the Free Software Foundation; either version 2 of the License, or * |
| 150 | * (at your option) any later version. * |
| 151 | * * |
| 152 | * This program is distributed in the hope that it will be useful, * |
| 153 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 154 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 155 | * GNU General Public License for more details. * |
| 156 | * * |
| 157 | * You should have received a copy of the GNU General Public License * |
| 158 | * along with this program; if not, write to the Free Software * |
| 159 | * Foundation, Inc., * |
| 160 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 161 | ***************************************************************************/ |
| 162 | |
| 163 | /* taken from : http://fr2.php.net/xml-rpc |
| 164 | * Author mboeren@php.net |
| 165 | * |
| 166 | * Usage: |
| 167 | * $client = new xmlrpc_client("http://localhost:7080"); |
| 168 | * print $client->echo('x')."\n"; |
| 169 | * print $client->add(1, 3)."\n"; |
| 170 | */ |
| 171 | |
| 172 | class XmlrpcClient |
| 173 | { |
| 174 | var $url; |
| 175 | var $urlparts; |
| 176 | |
| 177 | function XmlrpcClient($url) |
| 178 | { |
| 179 | $this->url = $url; |
| 180 | $this->urlparts = parse_url($this->url); |
| 181 | foreach (array('scheme', 'host', 'user', 'pass', 'path', 'query', 'fragment') as $part) { |
| 182 | if (!isset($this->urlparts[$part])) { |
| 183 | $this->urlparts[$part] = null; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | function __call($function, $arguments, &$return) |
| 189 | { |
| 190 | $requestprms['host'] = $this->urlparts['host']; |
| 191 | $requestprms['port'] = $this->urlparts['port']; |
| 192 | $requestprms['uri'] = $this->urlparts['path']; |
| 193 | $requestprms['user'] = $this->urlparts['user']; |
| 194 | $requestprms['pass'] = $this->urlparts['pass']; |
| 195 | $requestprms['method'] = $function; |
| 196 | $requestprms['args'] = $arguments; |
| 197 | $requestprms['timeout'] = 0; |
| 198 | $requestprms['secure'] = 0; |
| 199 | |
| 200 | $result = xu_rpc_http_concise($requestprms); |
| 201 | if (is_array($result) && isset($result['faultCode'])) { |
| 202 | print('Error in xmlrpc call \''.$function.'\''."\n"); |
| 203 | print(' code : '.$result['faultCode']."\n"); |
| 204 | print(' message: '.$result['faultString']."\n"); |
| 205 | return false; |
| 206 | } |
| 207 | $return = $result; |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | |
| 213 | overload('XmlrpcClient'); |
| 214 | |
| 215 | // vim:set et sw=4 sts=4 sws=4: |
| 216 | ?> |