From Marco :
[auf_bulletin.git] / lodel-0.9 / scripts / nusoapmime.php
1 <?php
2 /*
3 $Id: nusoapmime.php 3705 2006-09-14 16:06:29Z cenou $
4
5 NuSOAP - Web Services Toolkit for PHP
6
7 Copyright (c) 2002 NuSphere Corporation
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 If you have any questions or comments, please email:
24
25 Dietrich Ayala
26 dietrich@ganx4.com
27 http://dietrich.ganx4.com/nusoap
28
29 NuSphere Corporation
30 http://www.nusphere.com
31
32 */
33
34 /*require_once('nusoap.php');*/
35 /* PEAR Mail_MIME library */
36 require_once('PEAR/Mail/mimeDecode.php');
37 require_once('PEAR/Mail/mimePart.php');
38
39 /**
40 *
41 * soapclientmime client supporting
42 * <a href="http://www.w3.org/TR/SOAP-attachments">MIME attachments</a>.
43 *
44 * @author Scott Nichol <snichol@sourceforge.net>
45 * @author Thanks to Guillaume and Henning Reich for posting great attachment code to the mail list
46 * @version $Id: nusoapmime.php 3705 2006-09-14 16:06:29Z cenou $
47 * @access public
48 */
49 class soapclientmime extends mysoapclient {
50 var $requestAttachments = array();
51 var $responseAttachments;
52 var $mimeContentType;
53
54 /**
55 * adds a MIME attachment to the current request.
56 *
57 * If the $data parameter contains an empty string, this method will read
58 * the contents of the file named by the $filename parameter.
59 *
60 * If the $cid parameter is false, this method will generate the cid.
61 *
62 * @param string $data The data of the attachment
63 * @param string $filename The filename of the attachment (default is empty string)
64 * @param string $contenttype The MIME Content-Type of the attachment (default is application/octet-stream)
65 * @param string $cid The content-id (cid) of the attachment (default is false)
66 * @return string The content-id (cid) of the attachment
67 * @access public
68 */
69 function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
70 if (! $cid) {
71 $cid = md5(uniqid(time()));
72 }
73
74 $info['data'] = $data;
75 $info['filename'] = $filename;
76 $info['contenttype'] = $contenttype;
77 $info['cid'] = $cid;
78
79 $this->requestAttachments[] = $info;
80
81 return $cid;
82 }
83
84 /**
85 * clears the MIME attachments for the current request.
86 *
87 * @access public
88 */
89 function clearAttachments() {
90 $this->requestAttachments = array();
91 }
92
93 /**
94 * gets the MIME attachments from the current response.
95 *
96 * Each array element in the return is an associative array with keys
97 * data, filename, contenttype, cid. These keys correspond to the parameters
98 * for addAttachment.
99 *
100 * @return array The attachments.
101 * @access public
102 */
103 function getAttachments() {
104 return $this->responseAttachments;
105 }
106
107 /**
108 * gets the HTTP body for the current request.
109 *
110 * @param string $soapmsg The SOAP payload
111 * @return string The HTTP body, which includes the SOAP payload
112 * @access protected
113 */
114 function getHTTPBody($soapmsg) {
115 if (count($this->requestAttachments) > 0) {
116 $params['content_type'] = 'multipart/related';
117 $mimeMessage = new Mail_mimePart('', $params);
118 unset($params);
119
120 $params['content_type'] = 'text/xml';
121 $params['encoding'] = '8bit';
122 $params['charset'] = $this->soap_defencoding;
123 $mimeMessage->addSubpart($soapmsg, $params);
124
125 foreach ($this->requestAttachments as $att) {
126 unset($params);
127
128 $params['content_type'] = $att['contenttype'];
129 $params['encoding'] = 'base64';
130 $params['disposition'] = 'attachment';
131 $params['dfilename'] = $att['filename'];
132 $params['cid'] = $att['cid'];
133
134 if ($att['data'] == '' && $att['filename'] <> '') {
135 if ($fd = fopen($att['filename'], 'rb')) {
136 $size=filesize($att['filename']);
137 if ($size) {
138 $data = fread($fd, $size);
139 } else {
140 $data = '';
141 }
142 fclose($fd);
143 } else {
144 $data = '';
145 }
146 $mimeMessage->addSubpart($data, $params);
147 } else {
148 $mimeMessage->addSubpart($att['data'], $params);
149 }
150 }
151
152 $output = $mimeMessage->encode();
153 $mimeHeaders = $output['headers'];
154
155 foreach ($mimeHeaders as $k => $v) {
156 $this->debug("MIME header $k: $v");
157 if (strtolower($k) == 'content-type') {
158 // PHP header() seems to strip leading whitespace starting
159 // the second line, so force everything to one line
160 $this->mimeContentType = str_replace("\r\n", " ", $v);
161 }
162 }
163
164 return $output['body'];
165 }
166
167 return parent::getHTTPBody($soapmsg);
168 }
169
170 /**
171 * gets the HTTP content type for the current request.
172 *
173 * Note: getHTTPBody must be called before this.
174 *
175 * @return string the HTTP content type for the current request.
176 * @access protected
177 */
178 function getHTTPContentType() {
179 if (count($this->requestAttachments) > 0) {
180 return $this->mimeContentType;
181 }
182 return parent::getHTTPContentType();
183 }
184
185 /**
186 * gets the HTTP content type charset for the current request.
187 * returns false for non-text content types.
188 *
189 * Note: getHTTPBody must be called before this.
190 *
191 * @return string the HTTP content type charset for the current request.
192 * @access protected
193 */
194 function getHTTPContentTypeCharset() {
195 if (count($this->requestAttachments) > 0) {
196 return false;
197 }
198 return parent::getHTTPContentTypeCharset();
199 }
200
201 /**
202 * processes SOAP message returned from server
203 *
204 * @param array $headers The HTTP headers
205 * @param string $data unprocessed response data from server
206 * @return mixed value of the message, decoded into a PHP type
207 * @access protected
208 */
209 function parseResponse($headers, $data) {
210 $this->debug('Entering parseResponse() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
211 $this->responseAttachments = array();
212 if (strstr($headers['content-type'], 'multipart/related')) {
213 $this->debug('Decode multipart/related');
214 $input = '';
215 foreach ($headers as $k => $v) {
216 $input .= "$k: $v\r\n";
217 }
218 $params['input'] = $input . "\r\n" . $data;
219 $params['include_bodies'] = true;
220 $params['decode_bodies'] = true;
221 $params['decode_headers'] = true;
222
223 $structure = Mail_mimeDecode::decode($params);
224
225 foreach ($structure->parts as $part) {
226 if (!isset($part->disposition)) {
227 $this->debug('Have root part of type ' . $part->headers['content-type']);
228 $return = parent::parseResponse($part->headers, $part->body);
229 } else {
230 $this->debug('Have an attachment of type ' . $part->headers['content-type']);
231 $info['data'] = $part->body;
232 $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : '';
233 $info['contenttype'] = $part->headers['content-type'];
234 $info['cid'] = $part->headers['content-id'];
235 $this->responseAttachments[] = $info;
236 }
237 }
238
239 if (isset($return)) {
240 return $return;
241 }
242
243 $this->setError('No root part found in multipart/related content');
244 return;
245 }
246 $this->debug('Not multipart/related');
247 return parent::parseResponse($headers, $data);
248 }
249 }
250 ?>