[2] | 1 | <?php |
---|
[2364] | 2 | |
---|
| 3 | /*************************************************************************** |
---|
| 4 | * Expresso Livre * |
---|
| 5 | * http://www.expressolivre.org * |
---|
| 6 | * -------------------------------------------- * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 8 | * under the terms of the GNU General Public License as published by the * |
---|
| 9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 10 | * option) any later version. * |
---|
| 11 | \**************************************************************************/ |
---|
[2] | 12 | // Copyright (c) 1999,2000,2001 Edd Dumbill. |
---|
| 13 | // All rights reserved. |
---|
| 14 | // |
---|
| 15 | // Redistribution and use in source and binary forms, with or without |
---|
| 16 | // modification, are permitted provided that the following conditions |
---|
| 17 | // are met: |
---|
| 18 | // |
---|
| 19 | // * Redistributions of source code must retain the above copyright |
---|
| 20 | // notice, this list of conditions and the following disclaimer. |
---|
| 21 | // |
---|
| 22 | // * Redistributions in binary form must reproduce the above |
---|
| 23 | // copyright notice, this list of conditions and the following |
---|
| 24 | // disclaimer in the documentation and/or other materials provided |
---|
| 25 | // with the distribution. |
---|
| 26 | // |
---|
| 27 | // * Neither the name of the "XML-RPC for PHP" nor the names of its |
---|
| 28 | // contributors may be used to endorse or promote products derived |
---|
| 29 | // from this software without specific prior written permission. |
---|
| 30 | // |
---|
| 31 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 32 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 33 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
| 34 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
| 35 | // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
| 36 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
| 37 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
| 38 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 39 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
| 40 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 41 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
| 42 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 43 | |
---|
| 44 | class xmlrpc_client |
---|
| 45 | { |
---|
| 46 | var $path; |
---|
| 47 | var $server; |
---|
| 48 | var $port; |
---|
| 49 | var $errno; |
---|
| 50 | var $errstring; |
---|
| 51 | var $debug = 0; |
---|
| 52 | var $username = ''; |
---|
| 53 | var $password = ''; |
---|
| 54 | var $cert = ''; |
---|
| 55 | var $certpass = ''; |
---|
| 56 | |
---|
| 57 | function xmlrpc_client($path='', $server='', $port=0) |
---|
| 58 | { |
---|
| 59 | $this->port = $port; |
---|
| 60 | $this->server = $server; |
---|
| 61 | $this->path = $path; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | function setDebug($in) |
---|
| 65 | { |
---|
| 66 | if ($in) |
---|
| 67 | { |
---|
| 68 | $this->debug = 1; |
---|
| 69 | } |
---|
| 70 | else |
---|
| 71 | { |
---|
| 72 | $this->debug = 0; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | function setCredentials($u, $p) |
---|
| 77 | { |
---|
| 78 | $this->username = $u; |
---|
| 79 | $this->password = $p; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | function setCertificate($cert, $certpass) |
---|
| 83 | { |
---|
| 84 | $this->cert = $cert; |
---|
| 85 | $this->certpass = $certpass; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | function send($msg, $timeout=0, $method='http') |
---|
| 89 | { |
---|
| 90 | /* where msg is an xmlrpcmsg */ |
---|
| 91 | $msg->debug = $this->debug; |
---|
| 92 | |
---|
| 93 | if ($method == 'https') |
---|
| 94 | { |
---|
| 95 | return $this->sendPayloadHTTPS( |
---|
| 96 | $msg, |
---|
| 97 | $this->server, |
---|
| 98 | $this->port, |
---|
| 99 | $timeout, |
---|
| 100 | $this->username, |
---|
| 101 | $this->password, |
---|
| 102 | $this->cert, |
---|
| 103 | $this->certpass |
---|
| 104 | ); |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | { |
---|
| 108 | return $this->sendPayloadHTTP10( |
---|
| 109 | $msg, |
---|
| 110 | $this->server, |
---|
| 111 | $this->port, |
---|
| 112 | $timeout, |
---|
| 113 | $this->username, |
---|
| 114 | $this->password |
---|
| 115 | ); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | function sendPayloadHTTP10($msg, $server, $port, $timeout=0,$username='', $password='') |
---|
| 120 | { |
---|
| 121 | if($port == 0) |
---|
| 122 | { |
---|
| 123 | $port = 80; |
---|
| 124 | } |
---|
| 125 | if($timeout>0) |
---|
| 126 | { |
---|
| 127 | $fp = fsockopen($server, $port, &$this->errno, &$this->errstr, $timeout); |
---|
| 128 | } |
---|
| 129 | else |
---|
| 130 | { |
---|
| 131 | $fp = fsockopen($server, $port, &$this->errno, &$this->errstr); |
---|
| 132 | } |
---|
| 133 | if (!$fp) |
---|
| 134 | { |
---|
| 135 | $r = CreateObject( |
---|
| 136 | 'phpgwapi.xmlrpcresp', |
---|
| 137 | '', |
---|
| 138 | $GLOBALS['xmlrpcerr']['http_error'], |
---|
| 139 | $GLOBALS['xmlrpcstr']['http_error'] |
---|
| 140 | ); |
---|
| 141 | return $r; |
---|
| 142 | } |
---|
| 143 | // Only create the payload if it was not created previously |
---|
| 144 | if(empty($msg->payload)) |
---|
| 145 | { |
---|
| 146 | $msg->createPayload(); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | // thanks to Grant Rauscher <grant7@firstworld.net> |
---|
| 150 | // for this |
---|
| 151 | $credentials = ''; |
---|
| 152 | if ($username && $password) |
---|
| 153 | { |
---|
| 154 | $credentials = 'Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n"; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | $op = 'POST ' . $this->path . " HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\n" |
---|
| 158 | . 'Host: '. $this->server . "\r\n" |
---|
| 159 | . 'X-PHPGW-Server: ' . $this->server . ' ' . "\r\n" |
---|
| 160 | . 'X-PHPGW-Version: ' . $GLOBALS['phpgw_info']['server']['versions']['phpgwapi'] . "\r\n" |
---|
| 161 | . $credentials |
---|
| 162 | . "Content-Type: text/xml\r\nContent-Length: " |
---|
| 163 | . strlen($msg->payload) . "\r\n\r\n" |
---|
| 164 | . $msg->payload; |
---|
| 165 | |
---|
| 166 | if (!fputs($fp, $op, strlen($op))) |
---|
| 167 | { |
---|
| 168 | $this->errstr = 'Write error'; |
---|
| 169 | return CreateObject( |
---|
| 170 | 'phpgwapi.xmlrpcresp', |
---|
| 171 | '', |
---|
| 172 | $GLOBALS['xmlrpcerr']['http_error'], |
---|
| 173 | $GLOBALS['xmlrpcstr']['http_error'] |
---|
| 174 | ); |
---|
| 175 | } |
---|
| 176 | $resp = $msg->parseResponseFile($fp); |
---|
| 177 | fclose($fp); |
---|
| 178 | return $resp; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /* contributed by Justin Miller <justin@voxel.net> - requires curl to be built into PHP */ |
---|
| 182 | function sendPayloadHTTPS($msg, $server, $port, $timeout=0,$username='', $password='', $cert='',$certpass='') |
---|
| 183 | { |
---|
| 184 | if (!function_exists('curl_init')) |
---|
| 185 | { |
---|
| 186 | return CreateObject( |
---|
| 187 | 'phpgwapi.xmlrpcresp', |
---|
| 188 | '', |
---|
| 189 | $GLOBALS['xmlrpcerr']['no_ssl'], |
---|
| 190 | $GLOBALS['xmlrpcstr']['no_ssl'] |
---|
| 191 | ); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | if ($port == 0) |
---|
| 195 | { |
---|
| 196 | $port = 443; |
---|
| 197 | } |
---|
| 198 | /* Only create the payload if it was not created previously */ |
---|
| 199 | if(empty($msg->payload)) |
---|
| 200 | { |
---|
| 201 | $msg->createPayload(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | $curl = curl_init('https://' . $server . ':' . $port . $this->path); |
---|
| 205 | |
---|
| 206 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
---|
| 207 | // results into variable |
---|
| 208 | if ($this->debug) |
---|
| 209 | { |
---|
| 210 | curl_setopt($curl, CURLOPT_VERBOSE, 1); |
---|
| 211 | } |
---|
| 212 | curl_setopt($curl, CURLOPT_USERAGENT, 'PHP XMLRPC 1.0'); |
---|
| 213 | // required for XMLRPC |
---|
| 214 | curl_setopt($curl, CURLOPT_POST, 1); |
---|
| 215 | // post the data |
---|
| 216 | curl_setopt($curl, CURLOPT_POSTFIELDS, $msg->payload); |
---|
| 217 | // the data |
---|
| 218 | curl_setopt($curl, CURLOPT_HEADER, 1); |
---|
| 219 | // return the header too |
---|
| 220 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
---|
| 221 | 'X-PHPGW-Server: ' . $this->server, |
---|
| 222 | 'X-PHPGW-Version: ' . $GLOBALS['phpgw_info']['server']['versions']['phpgwapi'], |
---|
| 223 | 'Content-Type: text/xml' |
---|
| 224 | )); |
---|
| 225 | if ($timeout) |
---|
| 226 | { |
---|
| 227 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1); |
---|
| 228 | } |
---|
| 229 | if ($username && $password) |
---|
| 230 | { |
---|
| 231 | curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); |
---|
| 232 | } |
---|
| 233 | if ($cert) |
---|
| 234 | { |
---|
| 235 | curl_setopt($curl, CURLOPT_SSLCERT, $cert); |
---|
| 236 | } |
---|
| 237 | if ($certpass) |
---|
| 238 | { |
---|
| 239 | curl_setopt($curl, CURLOPT_SSLCERTPASSWD,$certpass); |
---|
| 240 | } |
---|
| 241 | // set cert password |
---|
| 242 | |
---|
| 243 | $result = curl_exec($curl); |
---|
| 244 | |
---|
| 245 | if (!$result) |
---|
| 246 | { |
---|
| 247 | $this->errstr = 'Write error'; |
---|
| 248 | $resp = CreateObject( |
---|
| 249 | 'phpgwapi.xmlrpcresp', |
---|
| 250 | '', |
---|
| 251 | $GLOBALS['xmlrpcerr']['curl_fail'], |
---|
| 252 | $GLOBALS['xmlrpcstr']['curl_fail'] . ': ' . curl_error($curl) |
---|
| 253 | ); |
---|
| 254 | } |
---|
| 255 | else |
---|
| 256 | { |
---|
| 257 | $resp = $msg->parseResponse($result); |
---|
| 258 | } |
---|
| 259 | curl_close($curl); |
---|
| 260 | |
---|
| 261 | return $resp; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | ?> |
---|