Proof of concept:
[platal.git] / include / platal / xmlrpc-server.inc.php
CommitLineData
0337d704 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 */
c36b9cb0 50function xu_query_http_post($request, $host, $uri, $port, $timeout, $user,
51 $pass, $secure=false)
52{
53 $response_buf = '';
0337d704 54 if ($host && $uri && $port) {
55 $content_len = strlen($request);
c36b9cb0 56 $fsockopen = $secure ? 'fsockopen_ssl' : 'fsockopen';
57 $query_fd = $fsockopen($host, $port, $errno, $errstr, 10);
0337d704 58
59 if ($query_fd) {
60
c36b9cb0 61 $auth = '';
0337d704 62 if ($user) {
c36b9cb0 63 $auth = 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n";
0337d704 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
0337d704 77 fputs($query_fd, $http_request, strlen($http_request));
78
0337d704 79 $header_parsed = false;
0337d704 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 }
c36b9cb0 86 } else {
0337d704 87 $response_buf .= $line;
88 }
89 }
90
91 fclose($query_fd);
92 }
0337d704 93 }
94
0337d704 95 return $response_buf;
96}
97
c36b9cb0 98function 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;
0337d704 107}
108
109
110/**
c36b9cb0 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)
0337d704 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).
0337d704 121 */
122function xu_rpc_http_concise($params) {
c36b9cb0 123 $host = $uri = $port = $method = $args = null;
124 $timeout = $user = $pass = $secure = null;
0337d704 125
c36b9cb0 126 extract($params);
0337d704 127
c36b9cb0 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);
0337d704 140 }
141 return $retval;
142}
143
0337d704 144?>