1 | <?php |
---|
2 | /* |
---|
3 | |
---|
4 | SOAPx4 |
---|
5 | by Dietrich Ayala (C) 2001 dietrich@ganx4.com |
---|
6 | |
---|
7 | This project began based on code from the 2 projects below, |
---|
8 | and still contains some original code. The licenses of both must be respected. |
---|
9 | |
---|
10 | XML-RPC for PHP |
---|
11 | originally by Edd Dumbill (C) 1999-2000 |
---|
12 | |
---|
13 | SOAP for PHP |
---|
14 | by Victor Zou (C) 2000-2001 <victor@gigaideas.com.cn> |
---|
15 | |
---|
16 | */ |
---|
17 | |
---|
18 | /* changelog: |
---|
19 | 2001-07-04 |
---|
20 | - abstract type system to support either 1999 or 2001 schema (arg, typing still needs much |
---|
21 | solidification.) |
---|
22 | - implemented proxy support, based on sample code from miles lott <milos@speakeasy.net> |
---|
23 | - much general cleanup of code & cleaned out what was left of original xml-rpc/gigaideas code |
---|
24 | - implemented a transport argument into send() that allows you to specify different transports |
---|
25 | (assuming you have implemented the function, and added it to the conditional statement in send() |
---|
26 | - abstracted the determination of charset in Content-type header |
---|
27 | 2001-07-5 |
---|
28 | - fixed more weird type/namespace issues |
---|
29 | */ |
---|
30 | |
---|
31 | // $path can be a complete endpoint url, with the other parameters left blank: |
---|
32 | // $soap_client = new soap_client("http://path/to/soap/server"); |
---|
33 | |
---|
34 | |
---|
35 | class soap_client |
---|
36 | { |
---|
37 | function soap_client($path,$server=False,$port=False) |
---|
38 | { |
---|
39 | $this->port = 80; |
---|
40 | $this->path = $path; |
---|
41 | $this->server = $server; |
---|
42 | $this->errno; |
---|
43 | $this->errstring; |
---|
44 | $this->debug_flag = True; |
---|
45 | $this->debug_str = ''; |
---|
46 | $this->username = ''; |
---|
47 | $this->password = ''; |
---|
48 | $this->action = ''; |
---|
49 | $this->incoming_payload = ''; |
---|
50 | $this->outgoing_payload = ''; |
---|
51 | $this->response = ''; |
---|
52 | $this->action = ''; |
---|
53 | |
---|
54 | // endpoint mangling |
---|
55 | if(ereg("^http://",$path)) |
---|
56 | { |
---|
57 | $path = str_replace('http://','',$path); |
---|
58 | $this->path = strstr($path,'/'); |
---|
59 | $this->debug("path = $this->path"); |
---|
60 | if(ereg(':',$path)) |
---|
61 | { |
---|
62 | $this->server = substr($path,0,strpos($path,':')); |
---|
63 | $this->port = substr(strstr($path,':'),1); |
---|
64 | $this->port = substr($this->port,0,strpos($this->port,'/')); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | $this->server = substr($path,0,strpos($path,'/')); |
---|
69 | } |
---|
70 | } |
---|
71 | if($port) |
---|
72 | { |
---|
73 | $this->port = $port; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | function setCredentials($u, $p) |
---|
78 | { |
---|
79 | $this->username = $u; |
---|
80 | $this->password = $p; |
---|
81 | } |
---|
82 | |
---|
83 | function send($msg, $action, $timeout=0, $ssl=False) |
---|
84 | { |
---|
85 | // where msg is an soapmsg |
---|
86 | $msg->debug_flag = $this->debug_flag; |
---|
87 | $this->action = $action; |
---|
88 | if($ssl) |
---|
89 | { |
---|
90 | return $this->ssl_sendPayloadHTTP10( |
---|
91 | $msg, |
---|
92 | $this->server, |
---|
93 | $this->port, |
---|
94 | $timeout, |
---|
95 | $this->username, |
---|
96 | $this->password |
---|
97 | ); |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | return $this->sendPayloadHTTP10( |
---|
102 | $msg, |
---|
103 | $this->server, |
---|
104 | $this->port, |
---|
105 | $timeout, |
---|
106 | $this->username, |
---|
107 | $this->password |
---|
108 | ); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | function sendPayloadHTTP10($msg, $server, $port, $timeout=0, $username='', $password='') |
---|
113 | { |
---|
114 | if($timeout > 0) |
---|
115 | { |
---|
116 | $fp = fsockopen($server, $port,&$this->errno, &$this->errstr, $timeout); |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | $fp = fsockopen($server, $port,&$this->errno, &$this->errstr); |
---|
121 | } |
---|
122 | if (!$fp) |
---|
123 | { |
---|
124 | $this->debug("Couldn't open socket connection to server!"); |
---|
125 | $this->debug("Server: $this->server"); |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | // thanks to Grant Rauscher <grant7@firstworld.net> for this |
---|
130 | $credentials = ''; |
---|
131 | if ($username != '') |
---|
132 | { |
---|
133 | $credentials = "Authorization: Basic " . base64_encode($username . ":" . $password) . "\r\n"; |
---|
134 | } |
---|
135 | |
---|
136 | $soap_data = $msg->serialize(); |
---|
137 | $this->outgoing_payload = 'POST ' |
---|
138 | . $this->path |
---|
139 | . " HTTP/1.0\r\n" |
---|
140 | . 'User-Agent: phpGroupware/' . $cliversion . '(PHP) ' . "\r\n" |
---|
141 | . 'X-PHPGW-Server: ' . $this->server . "\r\n" |
---|
142 | . 'X-PHPGW-Version: ' . $GLOBALS['phpgw_info']['server']['versions']['phpgwapi'] . "\r\n" |
---|
143 | . 'Host: '.$this->server . "\r\n" |
---|
144 | . $credentials |
---|
145 | . "Content-Type: text/xml\r\nContent-Length: " . strlen($soap_data) . "\r\n" |
---|
146 | . 'SOAPAction: "' . $this->action . '"' . "\r\n\r\n" |
---|
147 | . $soap_data; |
---|
148 | // send |
---|
149 | if(!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload))) |
---|
150 | { |
---|
151 | $this->debug('Write error'); |
---|
152 | } |
---|
153 | |
---|
154 | // get reponse |
---|
155 | while($data = fread($fp, 32768)) |
---|
156 | { |
---|
157 | $incoming_payload .= $data; |
---|
158 | } |
---|
159 | |
---|
160 | fclose($fp); |
---|
161 | $this->incoming_payload = $incoming_payload; |
---|
162 | // $response is a soapmsg object |
---|
163 | $this->response = $msg->parseResponse($incoming_payload); |
---|
164 | $this->debug($msg->debug_str); |
---|
165 | return $this->response; |
---|
166 | } |
---|
167 | |
---|
168 | function ssl_sendPayloadHTTP10($msg, $server, $port, $timeout=0,$username='', $password='') |
---|
169 | { |
---|
170 | if(!function_exists(curl_init)) |
---|
171 | { |
---|
172 | $this->errstr = 'No curl functions available - use of ssl is invalid'; |
---|
173 | return False; |
---|
174 | } |
---|
175 | /* curl Method borrowed from: |
---|
176 | http://sourceforge.net/tracker/index.php?func=detail&aid=427359&group_id=23199&atid=377731 |
---|
177 | */ |
---|
178 | |
---|
179 | // thanks to Grant Rauscher <grant7@firstworld.net> |
---|
180 | // for this |
---|
181 | $credentials = ''; |
---|
182 | if ($username!='') |
---|
183 | { |
---|
184 | $credentials = "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"; |
---|
185 | } |
---|
186 | |
---|
187 | $soap_data = $msg->serialize(); |
---|
188 | $this->outgoing_payload = 'POST ' |
---|
189 | . $this->path |
---|
190 | . " HTTP/1.0\r\n" |
---|
191 | . 'User-Agent: phpGroupware/' . $cliversion . '(PHP) ' . "\r\n" |
---|
192 | . 'X-PHPGW-Server: ' . $this->server . "\r\n" |
---|
193 | . 'X-PHPGW-Version: ' . $GLOBALS['phpgw_info']['server']['versions']['phpgwapi'] . "\r\n" |
---|
194 | . 'Host: ' . $this->server . "\r\n" |
---|
195 | . $credentials |
---|
196 | . "Content-Type: text/xml\r\nContent-Length: " . strlen($soap_data) . "\r\n" |
---|
197 | . 'SOAPAction: "' . $this->action . '"' . "\r\n\r\n" |
---|
198 | . $soap_data; |
---|
199 | |
---|
200 | // send |
---|
201 | $ch = curl_init(); |
---|
202 | curl_setopt($ch, CURLOPT_URL,$this->server); |
---|
203 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
204 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->outgoing_payload); |
---|
205 | curl_setopt($ch, CURLOPT_HEADER, 0); |
---|
206 | $incoming_payload = curl_exec($ch); |
---|
207 | curl_close($ch); |
---|
208 | |
---|
209 | $this->incoming_payload = $incoming_payload; |
---|
210 | // $response is a soapmsg object |
---|
211 | $this->response = $msg->parseResponse($incoming_payload); |
---|
212 | $this->debug($msg->debug_str); |
---|
213 | return $this->response; |
---|
214 | } |
---|
215 | |
---|
216 | function debug($string) |
---|
217 | { |
---|
218 | if($this->debug_flag) |
---|
219 | { |
---|
220 | $this->debug_str .= "$string\n"; |
---|
221 | } |
---|
222 | } |
---|
223 | } // end class soap_client |
---|
224 | ?> |
---|