summaryrefslogtreecommitdiff
path: root/tests/xmlrpc-server.php
blob: 2e3dd381f169931981c18a08941f34361e0ba3a4 (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
<?php

function paramfault ()
{
	# xmlrpc-epi-php translates this into a real <fault>
	$fault["faultCode"] = -32602;
	$fault["faultString"] = "bad parameter";
	return $fault;
}

# We only check the params in sum(), because that's the one that
# xmlrpc-test tests will fail if given bad args

function sum ($method_name, $params, $app_data)
{
	if (xmlrpc_get_type ($params[0]) != "array")
		return paramfault();

	$sum = 0;
	foreach ($params[0] as $val) {
		if (xmlrpc_get_type ($val) != "int")
			return paramfault();
			
		$sum = $sum + $val;
	}
	return $sum;
}

function countBools ($method_name, $params, $app_data)
{
	$counts["true"] = $counts["false"] = 0;
	foreach ($params[0] as $val) {
		if ($val)
			$counts["true"] = $counts["true"] + 1;
		else
			$counts["false"] = $counts["false"] + 1;
	}
	return $counts;
}

function md5sum ($method_name, $params, $app_data)
{
	$val = md5 ($params[0]->scalar, true);
	xmlrpc_set_type ($val, "base64");
	return $val;
}

function dateChange ($method_name, $params, $app_data)
{
	$date_str = $params[0]->scalar;
	$date = strptime ($date_str, "%Y%m%dT%H:%M:%S");

	foreach ($params[1] as $name => $val) {
		if ($name == "date")
			continue;
		$date[$name] = $val;
	}

	$ret = sprintf ("%04d%02d%02dT%02d:%02d:%02d",
			$date["tm_year"] + 1900, $date["tm_mon"] + 1,
			$date["tm_mday"], $date["tm_hour"],
			$date["tm_min"], $date["tm_sec"]);
	xmlrpc_set_type ($ret, "datetime");
	return $ret;
}

function echo_ ($method_name, $params, $app_data)
{
	return $params[0];
}

# Work around xmlrpc-epi-php lossage; otherwise the datetime values
# we return will sometimes get a DST adjustment we don't want.
putenv ("TZ=");

$xmlrpc_server = xmlrpc_server_create ();
xmlrpc_server_register_method($xmlrpc_server, "sum", "sum");
xmlrpc_server_register_method($xmlrpc_server, "countBools", "countBools");
xmlrpc_server_register_method($xmlrpc_server, "md5sum", "md5sum");
xmlrpc_server_register_method($xmlrpc_server, "dateChange", "dateChange");
xmlrpc_server_register_method($xmlrpc_server, "echo", "echo_");

$response = xmlrpc_server_call_method ($xmlrpc_server,
				       implode("\r\n", file('php://input')),
				       0, array ("output_type" => "xml"));
echo ($response);

xmlrpc_server_destroy ($xmlrpc_server);

?>