NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/utils.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
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 /* xmlrpc utilities (xu)
38  * author: Dan Libby (dan@libby.com)
39  */
40
41 // ensure extension is loaded.
42 xu_load_extension();
43
44 // a function to ensure the xmlrpc extension is loaded.
45 // xmlrpc_epi_dir = directory where libxmlrpc.so.0 is located
46 // xmlrpc_php_dir = directory where xmlrpc-epi-php.so is located
47 function xu_load_extension($xmlrpc_php_dir="") {
48    $bSuccess = true;
49    if(!extension_loaded('xmlrpc')) {
50       $bSuccess = true;
51       putenv("LD_LIBRARY_PATH=/usr/lib/php4/apache/xmlrpc/");
52       if ($xmlrpc_php_dir) {
53          $xmlrpc_php_dir .= '/';
54       }
55       if (!extension_loaded("xmlrpc")) {
56          if ( PHP_OS == "WINNT" || PHP_OS == "WIN32" )
57             $bSuccess = dl($xmlrpc_php_dir . "php_xmlrpc.dll");
58          else
59             $bSuccess = dl($xmlrpc_php_dir . "xmlrpc-epi-php.so");
60       }
61    }
62    return $bSuccess;
63 }
64
65 /* generic function to call an http server with post method */
66 function xu_query_http_post($request, $host, $uri, $port, $debug,
67                             $timeout, $user, $pass, $secure=false) {
68    $response_buf = "";
69    if ($host && $uri && $port) {
70       $content_len = strlen($request);
71
72       $fsockopen = $secure ? "fsockopen_ssl" : "fsockopen";
73
74       dbg1("opening socket to host: $host, port: $port, uri: $uri", $debug);
75       $query_fd = $fsockopen($host, $port, $errno, $errstr, 10);
76
77       if ($query_fd) {
78
79          $auth = "";
80          if ($user) {
81             $auth = "Authorization: Basic " .
82                     base64_encode($user . ":" . $pass) . "\r\n";
83          }
84
85          $http_request =
86          "POST $uri HTTP/1.0\r\n" .
87          "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
88          "Host: $host:$port\r\n" .
89          $auth .
90          "Content-Type: text/xml\r\n" .
91          "Content-Length: $content_len\r\n" .
92          "\r\n" .
93          $request;
94
95          dbg1("sending http request:</h3> <xmp>\n$http_request\n</xmp>", $debug);
96
97          fputs($query_fd, $http_request, strlen($http_request));
98
99          dbg1("receiving response...", $debug);
100
101          $header_parsed = false;
102
103          $line = fgets($query_fd, 4096);
104          while ($line) {
105             if (!$header_parsed) {
106                if ($line === "\r\n" || $line === "\n") {
107                   $header_parsed = 1;
108                }
109                dbg2("got header - $line", $debug);
110             }
111             else {
112                $response_buf .= $line;
113             }
114             $line = fgets($query_fd, 4096);
115          }
116
117          fclose($query_fd);
118       }
119       else {
120          dbg1("socket open failed", $debug);
121       }
122    }
123    else {
124       dbg1("missing param(s)", $debug);
125    }
126
127    dbg1("got response:</h3>. <xmp>\n$response_buf\n</xmp>\n", $debug);
128
129    return $response_buf;
130 }
131
132 function xu_fault_code($code, $string) {
133    return array('faultCode' => $code,
134                 'faultString' => $string);
135 }
136
137
138 function find_and_decode_xml($buf, $debug) {
139    $retval = "";
140    if (strlen($buf)) {
141       $xml_begin = substr($buf, strpos($buf, "<?xml"));
142       if (strlen($xml_begin)) {
143
144          $retval = xmlrpc_decode($xml_begin);
145       }
146       else {
147          dbg1("xml start token not found", $debug);
148       }
149    }
150    else {
151       dbg1("no data", $debug);
152    }
153    return $retval;
154 }
155
156  
157 /**
158  * @param params   a struct containing 3 or more of these key/val pairs:
159  * @param host         remote host (required)
160  * @param uri         remote uri     (required)
161  * @param port         remote port (required)
162  * @param method   name of method to call
163  * @param args        arguments to send (parameters to remote xmlrpc server)
164  * @param debug     debug level (0 none, 1, some, 2 more)
165  * @param timeout     timeout in secs.  (0 = never)
166  * @param user         user name for authentication. 
167  * @param pass         password for authentication
168  * @param secure     secure. wether to use fsockopen_ssl. (requires special php build).
169  * @param output     array. xml output options. can be null.  details below:
170  *
171  *     output_type: return data as either php native data types or xml
172  *                  encoded. ifphp is used, then the other values are ignored. default = xml
173  *     verbosity:   determine compactness of generated xml. options are
174  *                  no_white_space, newlines_only, and pretty. default = pretty
175  *     escaping:    determine how/whether to escape certain characters. 1 or
176  *                  more values are allowed. If multiple, they need to be specified as
177  *                  a sub-array. options are: cdata, non-ascii, non-print, and
178  *                  markup. default = non-ascii | non-print | markup
179  *     version:     version of xml vocabulary to use. currently, three are
180  *                  supported: xmlrpc, soap 1.1, and simple. The keyword auto is also
181  *                  recognized to mean respond in whichever version the request came
182  *                  in. default = auto (when applicable), xmlrpc
183  *     encoding:    the encoding that the data is in. Since PHP defaults to
184  *                  iso-8859-1 you will usually want to use that. Change it if you know
185  *                  what you are doing. default=iso-8859-1
186  *
187  *   example usage
188  *
189  *                   $output_options = array('output_type' => 'xml',
190  *                                           'verbosity' => 'pretty',
191  *                                           'escaping' => array('markup', 'non-ascii', 'non-print'),
192  *                                           'version' => 'xmlrpc',
193  *                                           'encoding' => 'utf-8'
194  *                                         );
195  *                   or
196  *
197  *                   $output_options = array('output_type' => 'php');
198  */
199 function xu_rpc_http_concise($params) {
200    $host = $uri = $port = $method = $args = $debug = null;
201    $timeout = $user = $pass = $secure = $debug = null;
202
203     extract($params);
204
205     // default values
206     if(!$port) {
207         $port = 80;
208     }
209     if(!$uri) {
210         $uri = '/';
211     }
212     if(!isset($output)) {
213         $output = array('version' => 'xmlrpc');
214     }
215
216    $response_buf = "";
217    if ($host && $uri && $port) {
218       $request_xml = xmlrpc_encode_request($method, $args, $output);
219
220       $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug,
221                                          $timeout, $user, $pass, $secure);
222
223       $retval = find_and_decode_xml($response_buf, $debug);
224    }
225    return $retval;
226 }
227
228 /* call an xmlrpc method on a remote http server. legacy support. */
229 function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false,
230                      $timeout=0, $user=false, $pass=false, $secure=false) {
231     return xu_rpc_http_concise(
232         array(
233             method  => $method,
234             args    => $args,
235             host    => $host,
236             uri     => $uri,
237             port    => $port,
238             debug   => $debug,
239             timeout => $timeout,
240             user    => $user,
241             pass    => $pass,
242             secure  => $secure
243         ));
244 }
245
246
247 function xu_is_fault($arg) {
248    // xmlrpc extension finally supports this.
249    return is_array($arg) ? xmlrpc_is_fault($arg) : false;
250 }
251
252 /* sets some http headers and prints xml */
253 function xu_server_send_http_response($xml) {
254     header("Content-type: text/xml");
255     header("Content-length: " . strlen($xml) );
256     echo $xml;
257 }
258
259
260 function dbg($msg) {
261    echo "<h3>$msg</h3>"; flush();
262 }
263 function dbg1($msg, $debug_level) {
264    if ($debug_level >= 1) {
265       dbg($msg);
266    }
267 }
268 function dbg2($msg, $debug_level) {
269    if ($debug_level >= 2) {
270       dbg($msg);
271    }
272 }
273
274
275 ?>
276
Note: See TracBrowser for help on using the browser.