3 $Id: nusoapmime.php 3705 2006-09-14 16:06:29Z cenou $
5 NuSOAP - Web Services Toolkit for PHP
7 Copyright (c) 2002 NuSphere Corporation
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.
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.
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
23 If you have any questions or comments, please email:
27 http://dietrich.ganx4.com/nusoap
30 http://www.nusphere.com
34 /*require_once('nusoap.php');*/
35 /* PEAR Mail_MIME library */
36 require_once('PEAR/Mail/mimeDecode.php');
37 require_once('PEAR/Mail/mimePart.php');
41 * soapclientmime client supporting
42 * <a href="http://www.w3.org/TR/SOAP-attachments">MIME attachments</a>.
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 $
49 class soapclientmime
extends mysoapclient
{
50 var $requestAttachments = array();
51 var $responseAttachments;
55 * adds a MIME attachment to the current request.
57 * If the $data parameter contains an empty string, this method will read
58 * the contents of the file named by the $filename parameter.
60 * If the $cid parameter is false, this method will generate the cid.
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
69 function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false
) {
71 $cid = md5(uniqid(time()));
74 $info['data'] = $data;
75 $info['filename'] = $filename;
76 $info['contenttype'] = $contenttype;
79 $this->requestAttachments
[] = $info;
85 * clears the MIME attachments for the current request.
89 function clearAttachments() {
90 $this->requestAttachments
= array();
94 * gets the MIME attachments from the current response.
96 * Each array element in the return is an associative array with keys
97 * data, filename, contenttype, cid. These keys correspond to the parameters
100 * @return array The attachments.
103 function getAttachments() {
104 return $this->responseAttachments
;
108 * gets the HTTP body for the current request.
110 * @param string $soapmsg The SOAP payload
111 * @return string The HTTP body, which includes the SOAP payload
114 function getHTTPBody($soapmsg) {
115 if (count($this->requestAttachments
) > 0) {
116 $params['content_type'] = 'multipart/related';
117 $mimeMessage = new Mail_mimePart('', $params);
120 $params['content_type'] = 'text/xml';
121 $params['encoding'] = '8bit';
122 $params['charset'] = $this->soap_defencoding
;
123 $mimeMessage->addSubpart($soapmsg, $params);
125 foreach ($this->requestAttachments
as $att) {
128 $params['content_type'] = $att['contenttype'];
129 $params['encoding'] = 'base64';
130 $params['disposition'] = 'attachment';
131 $params['dfilename'] = $att['filename'];
132 $params['cid'] = $att['cid'];
134 if ($att['data'] == '' && $att['filename'] <> '') {
135 if ($fd = fopen($att['filename'], 'rb')) {
136 $size=filesize($att['filename']);
138 $data = fread($fd, $size);
146 $mimeMessage->addSubpart($data, $params);
148 $mimeMessage->addSubpart($att['data'], $params);
152 $output = $mimeMessage->encode();
153 $mimeHeaders = $output['headers'];
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);
164 return $output['body'];
167 return parent
::getHTTPBody($soapmsg);
171 * gets the HTTP content type for the current request.
173 * Note: getHTTPBody must be called before this.
175 * @return string the HTTP content type for the current request.
178 function getHTTPContentType() {
179 if (count($this->requestAttachments
) > 0) {
180 return $this->mimeContentType
;
182 return parent
::getHTTPContentType();
186 * gets the HTTP content type charset for the current request.
187 * returns false for non-text content types.
189 * Note: getHTTPBody must be called before this.
191 * @return string the HTTP content type charset for the current request.
194 function getHTTPContentTypeCharset() {
195 if (count($this->requestAttachments
) > 0) {
198 return parent
::getHTTPContentTypeCharset();
202 * processes SOAP message returned from server
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
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');
215 foreach ($headers as $k => $v) {
216 $input .= "$k: $v\r\n";
218 $params['input'] = $input . "\r\n" . $data;
219 $params['include_bodies'] = true
;
220 $params['decode_bodies'] = true
;
221 $params['decode_headers'] = true
;
223 $structure = Mail_mimeDecode
::decode($params);
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
);
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;
239 if (isset($return)) {
243 $this->setError('No root part found in multipart/related content');
246 $this->debug('Not multipart/related');
247 return parent
::parseResponse($headers, $data);