aboutsummaryrefslogtreecommitdiff
path: root/xmlrpc-utils.php
blob: a324a7449926ef6c1cfabf2fb4698d35afe8d267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * @package    qtype_algebra
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

// This file was part of, or distributed with, libXMLRPC - a C library for
// xml-encoded function calls.
// Author: Dan Libby (dan@libby.com)
// Epinions.com may be contacted at feedback@epinions-inc.com
// It was adapted to Moodle standards and coding style

// Copyright 2001 Epinions, Inc.

// Subject to the following 3 conditions, Epinions, Inc.  permits you, free
// of charge, to (a) use, copy, distribute, modify, perform and display this
// software and associated documentation files (the "Software"), and (b)
// permit others to whom the Software is furnished to do so as well.

// 1) The above copyright notice and this permission notice shall be included
// without modification in all copies or substantial portions of the
// Software.

// 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
// ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE OR NONINFRINGEMENT.

// 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
// SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
// OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
// NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH
// DAMAGES.

defined('MOODLE_INTERNAL') || die();

// Ensure extension is loaded.
if (!extension_loaded('xmlrpc')) {
    debugging('The php xml-rpc extension is not loaded, SAGE evaluation will fail.', DEBUG_DEVELOPER);
}

// Generic function to call an http server with post method.
function xu_query_http_post($request, $host, $uri, $port, $debug,
                            $timeout, $user, $pass, $secure = false) {
    $responsebuf = "";
    if ($host && $uri && $port) {
        $contentlen = strlen($request);

        $fsockopen = $secure ? "fsockopen_ssl" : "fsockopen";

        $queryfd = $fsockopen($host, $port, $errno, $errstr, 10);

        if ($queryfd) {
            $auth = "";
            if ($user) {
                $auth = "Authorization: Basic " .
                    base64_encode($user . ":" . $pass) . "\r\n";
            }

            $myhttprequest = "POST $uri HTTP/1.0\r\n" .
            "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
            "Host: $host:$port\r\n" .
            $auth .
            "Content-Type: text/xml\r\n" .
            "Content-Length: $contentlen\r\n" .
            "\r\n" .
            $request;

            fputs($queryfd, $myhttprequest, strlen($myhttprequest));

            $headerparsed = false;

            $line = fgets($queryfd, 4096);
            while ($line) {
                if (!$headerparsed) {
                    if ($line === "\r\n" || $line === "\n") {
                        $headerparsed = 1;
                    }
                } else {
                    $responsebuf .= $line;
                }
                $line = fgets($queryfd, 4096);
            }

            fclose($queryfd);
        } else {
            debugging('Socket open faile', DEBUG_DEVELOPER);
        }
    } else {
        debugging('Missing param(s)', DEBUG_DEVELOPER);
    }

    return $responsebuf;
}

function xu_fault_code($code, $string) {
    return array('faultCode' => $code,
            'faultString' => $string);
}


function find_and_decode_xml($buf, $debug) {
    if (strlen($buf)) {
        $xmlbegin = substr($buf, strpos($buf, "<?xml"));
        if (strlen($xmlbegin)) {
            $retval = xmlrpc_decode($xmlbegin);
        } else {
            debugging('xml start token not found', DEBUG_DEVELOPER);
        }
    } else {
        debugging('no data', DEBUG_DEVELOPER);
    }
    return $retval;
}


/**
 * @param params   a struct containing 3 or more of these key/val pairs:
 * @param host         remote host (required)
 * @param uri         remote uri     (required)
 * @param port         remote port (required)
 * @param method   name of method to call
 * @param args        arguments to send (parameters to remote xmlrpc server)
 * @param debug     debug level (0 none, 1, some, 2 more)
 * @param timeout     timeout in secs.  (0 = never)
 * @param user         user name for authentication.
 * @param pass         password for authentication
 * @param secure     secure. wether to use fsockopen_ssl. (requires special php build).
 * @param output     array. xml output options. can be null.  details below:
 *
 *     output_type: return data as either php native data types or xml
 *                  encoded. ifphp is used, then the other values are ignored. default = xml
 *     verbosity:   determine compactness of generated xml. options are
 *                  no_white_space, newlines_only, and pretty. default = pretty
 *     escaping:    determine how/whether to escape certain characters. 1 or
 *                  more values are allowed. If multiple, they need to be specified as
 *                  a sub-array. options are: cdata, non-ascii, non-print, and
 *                  markup. default = non-ascii | non-print | markup
 *     version:     version of xml vocabulary to use. currently, three are
 *                  supported: xmlrpc, soap 1.1, and simple. The keyword auto is also
 *                  recognized to mean respond in whichever version the request came
 *                  in. default = auto (when applicable), xmlrpc
 *     encoding:    the encoding that the data is in. Since PHP defaults to
 *                  iso-8859-1 you will usually want to use that. Change it if you know
 *                  what you are doing. default=iso-8859-1
 *
 *   example usage
 *
 *                   $output_options = array('output_type' => 'xml',
 *                                           'verbosity' => 'pretty',
 *                                           'escaping' => array('markup', 'non-ascii', 'non-print'),
 *                                           'version' => 'xmlrpc',
 *                                           'encoding' => 'utf-8'
 *                                         );
 *                   or
 *
 *                   $output_options = array('output_type' => 'php');
 */
function xu_rpc_http_concise($params) {
    $host = $uri = $port = $method = $args = $debug = null;
    $timeout = $user = $pass = $secure = $debug = null;

    foreach ($params as $key => $value) {
        $$key = $value;
    }

    // Default values.
    if (!$port) {
        $port = 80;
    }
    if (!$uri) {
        $uri = '/';
    }
    if (!isset($output)) {
        $output = array('version' => 'xmlrpc');
    }

    $responsebuf = "";
    if ($host && $uri && $port) {
        $requestxml = xmlrpc_encode_request($method, $args, $output);
        $responsebuf = xu_query_http_post($requestxml, $host, $uri, $port, $debug,
                                           $timeout, $user, $pass, $secure);

        $retval = find_and_decode_xml($responsebuf, $debug);
    }
    return $retval;
}

// Call an xmlrpc method on a remote http server. legacy support.
function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false,
                     $timeout=0, $user=false, $pass=false, $secure=false) {
    return xu_rpc_http_concise(
        array(
            method  => $method,
            args    => $args,
            host    => $host,
            uri     => $uri,
            port    => $port,
            debug   => $debug,
            timeout => $timeout,
            user    => $user,
            pass    => $pass,
            secure  => $secure
        ));
}



function xu_is_fault($arg) {
    // The xmlrpc extension finally supports this.
    return is_array($arg) ? xmlrpc_is_fault($arg) : false;
}

// Sets some http headers and prints xml.
function xu_server_send_http_response($xml) {
    header("Content-type: text/xml");
    header("Content-length: " . strlen($xml) );
    echo $xml;
}