• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3function paramfault ()
4{
5	# xmlrpc-php translates this into a real <fault>
6	$fault["faultCode"] = -32602;
7	$fault["faultString"] = "bad parameter";
8	return $fault;
9}
10
11# We only check the params in sum(), because that is the one that
12# xmlrpc-test tests will fail if given bad args
13
14function sum ($method_name, $params, $app_data)
15{
16	if (xmlrpc_get_type ($params[0]) != "array")
17		return paramfault();
18
19	$sum = 0;
20	foreach ($params[0] as $val) {
21		if (xmlrpc_get_type ($val) != "double")
22			return paramfault();
23
24		$sum = $sum + $val;
25	}
26	return $sum;
27}
28
29function countBools ($method_name, $params, $app_data)
30{
31	$counts["true"] = $counts["false"] = 0;
32	foreach ($params[0] as $val) {
33		if ($val)
34			$counts["true"] = $counts["true"] + 1;
35		else
36			$counts["false"] = $counts["false"] + 1;
37	}
38	return $counts;
39}
40
41function md5sum ($method_name, $params, $app_data)
42{
43	$val = md5 ($params[0]->scalar, true);
44	xmlrpc_set_type ($val, "base64");
45	return $val;
46}
47
48function dateChange ($method_name, $params, $app_data)
49{
50	$date_str = $params[0]->scalar;
51	$date = strptime ($date_str, "%Y%m%dT%H:%M:%S");
52
53	foreach ($params[1] as $name => $val) {
54		if ($name == "date")
55			continue;
56		$date[$name] = $val;
57	}
58
59	$ret = sprintf ("%04d%02d%02dT%02d:%02d:%02d",
60			$date["tm_year"] + 1900, $date["tm_mon"] + 1,
61			$date["tm_mday"], $date["tm_hour"],
62			$date["tm_min"], $date["tm_sec"]);
63	xmlrpc_set_type ($ret, "datetime");
64	return $ret;
65}
66
67function echo_ ($method_name, $params, $app_data)
68{
69	return $params[0];
70}
71
72function ping ($method_name, $params, $app_data)
73{
74	if (is_null ($params) or count ($params) == 0)
75		return "pong";
76	else
77		return paramfault ();
78}
79
80# Work around xmlrpc-php lossage; otherwise the datetime values
81# we return will sometimes get a DST adjustment we do not want.
82putenv ("TZ=");
83
84$xmlrpc_server = xmlrpc_server_create ();
85xmlrpc_server_register_method($xmlrpc_server, "sum", "sum");
86xmlrpc_server_register_method($xmlrpc_server, "countBools", "countBools");
87xmlrpc_server_register_method($xmlrpc_server, "md5sum", "md5sum");
88xmlrpc_server_register_method($xmlrpc_server, "dateChange", "dateChange");
89xmlrpc_server_register_method($xmlrpc_server, "echo", "echo_");
90xmlrpc_server_register_method($xmlrpc_server, "ping", "ping");
91
92$response = xmlrpc_server_call_method ($xmlrpc_server,
93				       implode("\r\n", file('php://input')),
94				       0, array ("output_type" => "xml"));
95echo ($response);
96
97xmlrpc_server_destroy ($xmlrpc_server);
98
99?>
100