"MDL-14932, move the course section"
[moodle-linuxchix.git] / lib / soaplib.php
blobb5eaf962c11814cb5cc6f4521ff54af0df4cac4f
1 <?php
3 // Web services wrapper library script
5 if (check_php_version('5') && class_exists('SoapClient')) {
6 // Use the native PHP5 support
7 require_once($CFG->libdir . '/soap/phpsoap.php');
9 else{
10 // Use nuSOAP instead
11 require_once($CFG->libdir . '/soap/nusoap.php');
13 function make_soap_fault($faultcode, $faultstring, $faultactor='', $detail='', $faultname='', $headerfault='') {
14 return new soap_fault($faultcode, $faultactor, $faultstring, $detail);
17 function is_soap_fault($obj) {
18 if (!is_object($obj))
19 return false;
20 return (strcasecmp(get_class($obj), 'soap_fault') === 0);
23 if (class_exists('soap_client')) {
24 function soap_connect($wsdl, $trace=false) {
25 return new soap_client($wsdl, 'wsdl');
28 else {
29 function soap_connect($wsdl, $trace=false) {
30 return new soapclient($wsdl, 'wsdl');
34 function soap_call($connection, $call, $params) {
35 $result = $connection->call($call, $params);
36 if ($connection->fault) {
37 return @make_soap_fault($result['faultcode'], $result['faultstring'], '', $result['detail']);
39 if ($connection->error_str) {
40 return @make_soap_fault('server', $connection->error_str, '', $connection->response);
42 /* Fix objects being returned as associative arrays (to fit with PHP5
43 SOAP support */
44 return fix_object($result);
47 function soap_serve($wsdl, $functions) {
48 global $HTTP_RAW_POST_DATA;
50 $s = new soap_server($wsdl);
51 $s->service(isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '');
54 function get_last_soap_messages($connection) {
55 return array('request'=>$connection->request, 'response'=>$connection->response);
58 /* Fix objects being returned as associative arrays (to fit with PHP5
59 SOAP support */
60 function fix_object($value) {
61 if (is_array($value)) {
62 $value = array_map('fix_object', $value);
63 $keys = array_keys($value);
64 /* check for arrays of length 1 (they get given the key "item"
65 rather than 0 by nusoap) */
66 if (1 === count($value) && 'item' === $keys[0]) {
67 $value = array_values($value);
69 else {
70 /* cast to object if it is an associative array with at least
71 one string key */
72 foreach ($keys as $key) {
73 if (is_string($key)) {
74 $value = (object) $value;
75 break;
80 return $value;
83 // Fix simple type encoding - not needed for nuSOAP
84 function soap_encode($value, $name, $type, $namespace, $encode=0) {
85 return $value;
88 // Fix complex type encoding - not needed for nuSOAP
89 function soap_encode_object($value, $name, $type, $namespace) {
90 return $value;
93 // Fix array encoding - not needed for nuSOAP
94 function soap_encode_array($value, $name, $type, $namespace) {
95 return $value;
99 // In both cases...
100 function handle_soap_wsdl_request($wsdlfile, $address=false) {
101 header('Content-type: application/wsdl+xml');
102 $wsdl = file_get_contents($wsdlfile);
103 if (false !== $address) {
104 if (true === $address) {
105 $address = (($_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
107 $wsdl = str_replace('###SERVER_ADDRESS###', $address, $wsdl);
109 echo $wsdl;
110 exit;