]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/validate.php
add the xmlrpc testsuite (wiki and interop) from xmlrpc-epi
[SourceForge/phpwiki.git] / tests / xmlrpc / validate.php
1 <?php
2
3 /*
4   This file is part of, or distributed with, libXMLRPC - a C library for 
5   xml-encoded function calls.
6
7   Author: Dan Libby (dan@libby.com)
8   Epinions.com may be contacted at feedback@epinions-inc.com
9 */
10
11 /*  
12   Copyright 2001 Epinions, Inc. 
13
14   Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
15   of charge, to (a) use, copy, distribute, modify, perform and display this 
16   software and associated documentation files (the "Software"), and (b) 
17   permit others to whom the Software is furnished to do so as well.  
18
19   1) The above copyright notice and this permission notice shall be included 
20   without modification in all copies or substantial portions of the 
21   Software.  
22
23   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
24   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
25   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
26   PURPOSE OR NONINFRINGEMENT.  
27
28   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
29   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
30   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
31   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
32   DAMAGES.    
33
34 */
35
36
37 include("xmlrpc_utils.php");
38
39 // ensure extension is loaded.
40 xu_load_extension();
41
42 /*   
43  * This handler takes a single parameter, an array of structs, each of which 
44  * contains at least three elements named moe, larry and curly, all <i4>s.  
45  * Your handler must add all the struct elements named curly and return the 
46  * result.  
47  */
48 function validator1_arrayOfStructsTest($method_name, $params, $app_data) {
49    $iCurly = 0;
50
51    foreach(array_pop($params) as $iter) {
52       $iCurly += $iter["curly"];
53    }
54                               
55    return $iCurly;
56 }
57
58 /*
59  * This handler takes a single parameter, a string, that contains any number 
60  * of predefined entities, namely <, >, &, ' and ".  
61  *
62  * Your handler must return a struct that contains five fields, all numbers: 
63  * ctLeftAngleBrackets, ctRightAngleBrackets, ctAmpersands, ctApostrophes, 
64  * ctQuotes.  
65  *
66  * To validate, the numbers must be correct.
67  */
68 function validator1_countTheEntities ($method_name, $params, $app_data) {
69    $xStruct = array();
70
71    //returns struct
72    $counts = count_chars(array_pop($params), 0);
73
74    $xStruct["ctLeftAngleBrackets"] = $counts[ord('<')];
75    $xStruct["ctRightAngleBrackets"] = $counts[ord('>')];
76    $xStruct["ctAmpersands"] = $counts[ord('&')];
77    $xStruct["ctApostrophes"] = $counts[ord("'")];
78    $xStruct["ctQuotes"] = $counts[ord('"')];
79
80    return $xStruct;
81 }
82
83 /*
84  * This handler takes a single parameter, a struct, containing at least three 
85  * elements named moe, larry and curly, all <i4>s.  Your handler must add the 
86  * three numbers and return the result.  
87  */
88 function validator1_easyStructTest ($method_name, $params, $app_data) {
89    $iSum = 0;
90
91    $xStruct = array_pop($params);
92    if($xStruct) {
93       $iSum += $xStruct[curly];
94       $iSum += $xStruct[moe];
95       $iSum += $xStruct[larry];
96    }
97
98    return $iSum;
99 }
100
101 /*
102  * This handler takes a single parameter, a struct.  Your handler must return 
103  * the struct.  
104  */
105 function validator1_echoStructTest($method_name, $params, $app_data) {
106    return $params[0];
107 }
108
109
110 /*
111  * This handler takes six parameters, and returns an array containing all the 
112  * parameters.  
113  */
114 function validator1_manyTypesTest ($method_name, $params, $app_data) {
115    $xArray = array();
116    
117    foreach($params as $iter) {
118      array_push($xArray, $iter);
119    }
120
121
122    return $xArray;
123 }
124
125 /*
126  * This handler takes a single parameter, which is an array containing 
127  * between 100 and 200 elements.  Each of the items is a string, your handler 
128  * must return a string containing the concatenated text of the first and 
129  * last elements.  
130  */
131 function validator1_moderateSizeArrayCheck ($method_name, $params, $app_data) {
132    $xArray = array_pop($params);
133    if($xArray) {
134       $buf = $xArray[0] . $xArray[count($xArray) - 1];
135    }
136
137    return $buf;
138 }
139
140 /*
141  * This handler takes a single parameter, a struct, that models a daily 
142  * calendar.  At the top level, there is one struct for each year.  Each year 
143  * is broken down into months, and months into days.  Most of the days are 
144  * empty in the struct you receive, but the entry for April 1, 2000 contains 
145  * a least three elements named moe, larry and curly, all <i4>s.  Your 
146  * handler must add the three numbers and return the result.  
147  * 
148  * Ken MacLeod: "This description isn't clear, I expected '2000.April.1' when 
149  * in fact it's '2000.04.01'.  Adding a note saying that month and day are 
150  * two-digits with leading 0s, and January is 01 would help." Done.  
151  */
152 function validator1_nestedStructTest ($method_name, $params, $app_data) {
153
154    $iSum = 0;
155
156    $xStruct = array_pop($params);
157    $xYear   = $xStruct['2000'];
158    $xMonth  = $xYear['04'];
159    $xDay    = $xMonth['01'];
160
161    $iSum += $xDay["larry"];
162    $iSum += $xDay["curly"];
163    $iSum += $xDay["moe"];
164
165    return $iSum;
166 }
167
168 /*
169  * This handler takes one parameter, and returns a struct containing three 
170  * elements, times10, times100 and times1000, the result of multiplying the 
171  * number by 10, 100 and 1000.  
172  */
173 function validator1_simpleStructReturnTest ($method_name, $params, $app_data) {
174    $xStruct = array();
175
176    $iIncoming = array_pop($params);
177
178    $xStruct["times10"] = $iIncoming * 10;
179    $xStruct["times100"] = $iIncoming * 100;
180    $xStruct["times1000"] = $iIncoming * 1000;
181
182    return $xStruct;
183 }
184
185
186    /* create a new server object */
187    $server = xmlrpc_server_create();
188
189    xmlrpc_server_register_method($server, "validator1.arrayOfStructsTest", "validator1_arrayOfStructsTest");
190    xmlrpc_server_register_method($server, "validator1.countTheEntities", "validator1_countTheEntities");
191    xmlrpc_server_register_method($server, "validator1.easyStructTest", "validator1_easyStructTest");
192    xmlrpc_server_register_method($server, "validator1.echoStructTest", "validator1_echoStructTest");
193    xmlrpc_server_register_method($server, "validator1.manyTypesTest", "validator1_manyTypesTest");
194    xmlrpc_server_register_method($server, "validator1.moderateSizeArrayCheck", "validator1_moderateSizeArrayCheck");
195    xmlrpc_server_register_method($server, "validator1.nestedStructTest", "validator1_nestedStructTest");
196    xmlrpc_server_register_method($server, "validator1.simpleStructReturnTest", "validator1_simpleStructReturnTest");
197
198         // name differently for soap.
199    xmlrpc_server_register_method($server, "arrayOfStructsTest", "validator1_arrayOfStructsTest");
200    xmlrpc_server_register_method($server, "countTheEntities", "validator1_countTheEntities");
201    xmlrpc_server_register_method($server, "easyStructTest", "validator1_easyStructTest");
202    xmlrpc_server_register_method($server, "echoStructTest", "validator1_echoStructTest");
203    xmlrpc_server_register_method($server, "manyTypesTest", "validator1_manyTypesTest");
204    xmlrpc_server_register_method($server, "moderateSizeArrayCheck", "validator1_moderateSizeArrayCheck");
205    xmlrpc_server_register_method($server, "nestedStructTest", "validator1_nestedStructTest");
206    xmlrpc_server_register_method($server, "simpleStructReturnTest", "validator1_simpleStructReturnTest");
207
208
209    /* Now, let's get the client's request from the post data.... */
210    $request_xml = $HTTP_RAW_POST_DATA;
211    if(!$request_xml) {
212       $request_xml = $HTTP_GET_VARS[xml];
213    }
214    if(!$request_xml) {
215       echo "<h1>No XML input found!</h1>";
216    }
217    else {
218       /* setup some (optional) output options */
219       $display = array();
220       if($HTTP_POST_VARS[verbosity]) {
221          $display[verbosity] = $HTTP_POST_VARS[verbosity];
222       }
223       if($HTTP_POST_VARS[escaping]) {
224          $display[escaping] = $HTTP_POST_VARS[escaping];
225       }
226       else {
227          $display[escaping] = array("non-ascii", "markup");
228       }
229       if($HTTP_POST_VARS[version]) {
230          $display[version] = $HTTP_POST_VARS[version];
231       }
232       if($HTTP_POST_VARS[encoding]) {
233          $display[encoding] = $HTTP_POST_VARS[encoding];
234       }
235       if($HTTP_POST_VARS[output_type]) {
236          $display[output_type] = $HTTP_POST_VARS[output_type];
237       }
238
239       /* handle the request */
240       $response = xmlrpc_server_call_method($server, $request_xml, $response, $display);
241  
242       if($HTTP_POST_VARS[disp] === "html") {
243          if($HTTP_POST_VARS[output_type] === "php") {
244             echo "<xmp>\n";
245             var_dump($response);
246             echo "\n</xmp>\n";
247          }
248          else {
249             echo "<xmp>\n$response\n</xmp>\n";
250          }
251       }
252       else {
253          echo "$response";
254       }
255    }
256
257 ?>