source: trunk/phpgwapi/inc/class.xmlrpc_client.inc.php @ 2

Revision 2, 6.3 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

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