unbloat code
[platal.git] / include / platal / xmlrpc-server.inc.php
1 <?php
2
3 /******************************************************************************
4 * *
5 * Original file can be found on http://xmlrpc-epi.sourceforge.net/ *
6 * in the module xmlrpc-epi-php v0.51 file samples/utils/utils.php *
7 * *
8 * The Polytechnique.org TEAM *
9 * *
10 ******************************************************************************/
11
12 /*
13 This file is part of, or distributed with, libXMLRPC - a C library for
14 xml-encoded function calls.
15
16 Author: Dan Libby (dan@libby.com)
17 Epinions.com may be contacted at feedback@epinions-inc.com
18 */
19
20 /*
21 Copyright 2001 Epinions, Inc.
22
23 Subject to the following 3 conditions, Epinions, Inc. permits you, free
24 of charge, to (a) use, copy, distribute, modify, perform and display this
25 software and associated documentation files (the "Software"), and (b)
26 permit others to whom the Software is furnished to do so as well.
27
28 1) The above copyright notice and this permission notice shall be included
29 without modification in all copies or substantial portions of the
30 Software.
31
32 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
33 ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
34 IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 PURPOSE OR NONINFRINGEMENT.
36
37 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
38 SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
39 OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
40 NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
41 DAMAGES.
42
43 */
44
45 /* xmlrpc utilities (xu)
46 * author: Dan Libby (dan@libby.com)
47 */
48
49 /* generic function to call an http server with post method */
50 function xu_query_http_post($request, $host, $uri, $port, $timeout, $user,
51 $pass, $secure=false)
52 {
53 $response_buf = '';
54 if ($host && $uri && $port) {
55 $content_len = strlen($request);
56 $fsockopen = $secure ? 'fsockopen_ssl' : 'fsockopen';
57 $query_fd = $fsockopen($host, $port, $errno, $errstr, 10);
58
59 if ($query_fd) {
60
61 $auth = '';
62 if ($user) {
63 $auth = 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n";
64 }
65
66 $http_request =
67 "POST $uri HTTP/1.0\r\n" .
68 "Host: $host:$port\r\n" .
69 $auth .
70 "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
71 "Content-Type: text/xml\r\n" .
72 "Content-Length: $content_len\r\n" .
73 "Connection: Close\r\n" .
74 "\r\n" .
75 $request;
76
77 fputs($query_fd, $http_request, strlen($http_request));
78
79 $header_parsed = false;
80 while (!feof($query_fd)) {
81 $line = fgets($query_fd, 4096);
82 if (!$header_parsed) {
83 if ($line === "\r\n" || $line === "\n") {
84 $header_parsed = 1;
85 }
86 } else {
87 $response_buf .= $line;
88 }
89 }
90
91 fclose($query_fd);
92 }
93 }
94
95 return $response_buf;
96 }
97
98 function find_and_decode_xml($buf)
99 {
100 if (strlen($buf)) {
101 $xml_begin = substr($buf, strpos($buf, '<?xml'));
102 if (strlen($xml_begin)) {
103 $retval = xmlrpc_decode($xml_begin);
104 }
105 }
106 return $retval;
107 }
108
109
110 /**
111 * @param params a struct containing 3 or more of these key/val pairs:
112 * @param host remote host (required)
113 * @param uri remote uri (required)
114 * @param port remote port (required)
115 * @param method name of method to call
116 * @param args arguments to send (parameters to remote xmlrpc server)
117 * @param timeout timeout in secs. (0 = never)
118 * @param user user name for authentication.
119 * @param pass password for authentication
120 * @param secure secure. wether to use fsockopen_ssl. (requires special php build).
121 */
122 function xu_rpc_http_concise($params) {
123 $host = $uri = $port = $method = $args = null;
124 $timeout = $user = $pass = $secure = null;
125
126 extract($params);
127
128 // default values
129 if (!$port) {
130 $port = 80;
131 }
132 if (!$uri) {
133 $uri = '/';
134 }
135 if ($host && $uri && $port) {
136 $request_xml = xmlrpc_encode_request($method, $args);
137 $response_buf = xu_query_http_post($request_xml, $host, $uri, $port,
138 $timeout, $user, $pass, $secure);
139 $retval = find_and_decode_xml($response_buf);
140 }
141 return $retval;
142 }
143
144 ?>