. /** * @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. */ // 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) { $response_buf = ""; if ($host && $uri && $port) { $content_len = strlen($request); $fsockopen = $secure ? "fsockopen_ssl" : "fsockopen"; $query_fd = $fsockopen($host, $port, $errno, $errstr, 10); if ($query_fd) { $auth = ""; if ($user) { $auth = "Authorization: Basic " . base64_encode($user . ":" . $pass) . "\r\n"; } $http_request = "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: $content_len\r\n" . "\r\n" . $request; fputs($query_fd, $http_request, strlen($http_request)); $header_parsed = false; $line = fgets($query_fd, 4096); while ($line) { if (!$header_parsed) { if ($line === "\r\n" || $line === "\n") { $header_parsed = 1; } } else { $response_buf .= $line; } $line = fgets($query_fd, 4096); } fclose($query_fd); } else { debugging('Socket open faile', DEBUG_DEVELOPER); } } else { debugging('Missing param(s)', DEBUG_DEVELOPER); } return $response_buf; } function xu_fault_code($code, $string) { return array('faultCode' => $code, 'faultString' => $string); } function find_and_decode_xml($buf, $debug) { if (strlen($buf)) { $xml_begin = substr($buf, strpos($buf, " '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; extract($params); // Default values. if(!$port) { $port = 80; } if(!$uri) { $uri = '/'; } if(!isset($output)) { $output = array('version' => 'xmlrpc'); } $response_buf = ""; if ($host && $uri && $port) { $request_xml = xmlrpc_encode_request($method, $args, $output); $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug, $timeout, $user, $pass, $secure); $retval = find_and_decode_xml($response_buf, $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; }