Support Incident Tracker GIT4.x
nusoapmime.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003 $Id: nusoapmime.php,v 1.12 2007/04/17 16:34:03 snichol Exp $
00004 
00005 NuSOAP - Web Services Toolkit for PHP
00006 
00007 Copyright (c) 2002 NuSphere Corporation
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 The NuSOAP project home is:
00024 http://sourceforge.net/projects/nusoap/
00025 
00026 The primary support for NuSOAP is the mailing list:
00027 nusoap-general@lists.sourceforge.net
00028 
00029 If you have any questions or comments, please email:
00030 
00031 Dietrich Ayala
00032 dietrich@ganx4.com
00033 http://dietrich.ganx4.com/nusoap
00034 
00035 NuSphere Corporation
00036 http://www.nusphere.com
00037 
00038 */
00039 
00040 /*require_once('nusoap.php');*/
00041 /* PEAR Mail_MIME library */
00042 require_once('Mail/mimeDecode.php');
00043 require_once('Mail/mimePart.php');
00044 
00054 class nusoap_client_mime extends nusoap_client {
00060     var $requestAttachments = array();
00066     var $responseAttachments;
00071     var $mimeContentType;
00072     
00088     function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
00089         if (! $cid) {
00090             $cid = md5(uniqid(time()));
00091         }
00092 
00093         $info['data'] = $data;
00094         $info['filename'] = $filename;
00095         $info['contenttype'] = $contenttype;
00096         $info['cid'] = $cid;
00097         
00098         $this->requestAttachments[] = $info;
00099 
00100         return $cid;
00101     }
00102 
00108     function clearAttachments() {
00109         $this->requestAttachments = array();
00110     }
00111 
00122     function getAttachments() {
00123         return $this->responseAttachments;
00124     }
00125 
00133     function getHTTPBody($soapmsg) {
00134         if (count($this->requestAttachments) > 0) {
00135             $params['content_type'] = 'multipart/related; type="text/xml"';
00136             $mimeMessage =& new Mail_mimePart('', $params);
00137             unset($params);
00138 
00139             $params['content_type'] = 'text/xml';
00140             $params['encoding']     = '8bit';
00141             $params['charset']      = $this->soap_defencoding;
00142             $mimeMessage->addSubpart($soapmsg, $params);
00143             
00144             foreach ($this->requestAttachments as $att) {
00145                 unset($params);
00146 
00147                 $params['content_type'] = $att['contenttype'];
00148                 $params['encoding']     = 'base64';
00149                 $params['disposition']  = 'attachment';
00150                 $params['dfilename']    = $att['filename'];
00151                 $params['cid']          = $att['cid'];
00152 
00153                 if ($att['data'] == '' && $att['filename'] <> '') {
00154                     if ($fd = fopen($att['filename'], 'rb')) {
00155                         $data = fread($fd, filesize($att['filename']));
00156                         fclose($fd);
00157                     } else {
00158                         $data = '';
00159                     }
00160                     $mimeMessage->addSubpart($data, $params);
00161                 } else {
00162                     $mimeMessage->addSubpart($att['data'], $params);
00163                 }
00164             }
00165 
00166             $output = $mimeMessage->encode();
00167             $mimeHeaders = $output['headers'];
00168     
00169             foreach ($mimeHeaders as $k => $v) {
00170                 $this->debug("MIME header $k: $v");
00171                 if (strtolower($k) == 'content-type') {
00172                     // PHP header() seems to strip leading whitespace starting
00173                     // the second line, so force everything to one line
00174                     $this->mimeContentType = str_replace("\r\n", " ", $v);
00175                 }
00176             }
00177     
00178             return $output['body'];
00179         }
00180 
00181         return parent::getHTTPBody($soapmsg);
00182     }
00183     
00192     function getHTTPContentType() {
00193         if (count($this->requestAttachments) > 0) {
00194             return $this->mimeContentType;
00195         }
00196         return parent::getHTTPContentType();
00197     }
00198     
00208     function getHTTPContentTypeCharset() {
00209         if (count($this->requestAttachments) > 0) {
00210             return false;
00211         }
00212         return parent::getHTTPContentTypeCharset();
00213     }
00214 
00223     function parseResponse($headers, $data) {
00224         $this->debug('Entering parseResponse() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
00225         $this->responseAttachments = array();
00226         if (strstr($headers['content-type'], 'multipart/related')) {
00227             $this->debug('Decode multipart/related');
00228             $input = '';
00229             foreach ($headers as $k => $v) {
00230                 $input .= "$k: $v\r\n";
00231             }
00232             $params['input'] = $input . "\r\n" . $data;
00233             $params['include_bodies'] = true;
00234             $params['decode_bodies'] = true;
00235             $params['decode_headers'] = true;
00236             
00237             $structure = Mail_mimeDecode::decode($params);
00238 
00239             foreach ($structure->parts as $part) {
00240                 if (!isset($part->disposition) && (strstr($part->headers['content-type'], 'text/xml'))) {
00241                     $this->debug('Have root part of type ' . $part->headers['content-type']);
00242                     $root = $part->body;
00243                     $return = parent::parseResponse($part->headers, $part->body);
00244                 } else {
00245                     $this->debug('Have an attachment of type ' . $part->headers['content-type']);
00246                     $info['data'] = $part->body;
00247                     $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : '';
00248                     $info['contenttype'] = $part->headers['content-type'];
00249                     $info['cid'] = $part->headers['content-id'];
00250                     $this->responseAttachments[] = $info;
00251                 }
00252             }
00253         
00254             if (isset($return)) {
00255                 $this->responseData = $root;
00256                 return $return;
00257             }
00258             
00259             $this->setError('No root part found in multipart/related content');
00260             return '';
00261         }
00262         $this->debug('Not multipart/related');
00263         return parent::parseResponse($headers, $data);
00264     }
00265 }
00266 
00267 /*
00268  *  For backwards compatiblity, define soapclientmime unless the PHP SOAP extension is loaded.
00269  */
00270 if (!extension_loaded('soap')) {
00271     class soapclientmime extends nusoap_client_mime {
00272     }
00273 }
00274 
00284 class nusoap_server_mime extends nusoap_server {
00290     var $requestAttachments = array();
00296     var $responseAttachments;
00301     var $mimeContentType;
00302     
00318     function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
00319         if (! $cid) {
00320             $cid = md5(uniqid(time()));
00321         }
00322 
00323         $info['data'] = $data;
00324         $info['filename'] = $filename;
00325         $info['contenttype'] = $contenttype;
00326         $info['cid'] = $cid;
00327         
00328         $this->responseAttachments[] = $info;
00329 
00330         return $cid;
00331     }
00332 
00338     function clearAttachments() {
00339         $this->responseAttachments = array();
00340     }
00341 
00352     function getAttachments() {
00353         return $this->requestAttachments;
00354     }
00355 
00363     function getHTTPBody($soapmsg) {
00364         if (count($this->responseAttachments) > 0) {
00365             $params['content_type'] = 'multipart/related; type="text/xml"';
00366             $mimeMessage =& new Mail_mimePart('', $params);
00367             unset($params);
00368 
00369             $params['content_type'] = 'text/xml';
00370             $params['encoding']     = '8bit';
00371             $params['charset']      = $this->soap_defencoding;
00372             $mimeMessage->addSubpart($soapmsg, $params);
00373             
00374             foreach ($this->responseAttachments as $att) {
00375                 unset($params);
00376 
00377                 $params['content_type'] = $att['contenttype'];
00378                 $params['encoding']     = 'base64';
00379                 $params['disposition']  = 'attachment';
00380                 $params['dfilename']    = $att['filename'];
00381                 $params['cid']          = $att['cid'];
00382 
00383                 if ($att['data'] == '' && $att['filename'] <> '') {
00384                     if ($fd = fopen($att['filename'], 'rb')) {
00385                         $data = fread($fd, filesize($att['filename']));
00386                         fclose($fd);
00387                     } else {
00388                         $data = '';
00389                     }
00390                     $mimeMessage->addSubpart($data, $params);
00391                 } else {
00392                     $mimeMessage->addSubpart($att['data'], $params);
00393                 }
00394             }
00395 
00396             $output = $mimeMessage->encode();
00397             $mimeHeaders = $output['headers'];
00398     
00399             foreach ($mimeHeaders as $k => $v) {
00400                 $this->debug("MIME header $k: $v");
00401                 if (strtolower($k) == 'content-type') {
00402                     // PHP header() seems to strip leading whitespace starting
00403                     // the second line, so force everything to one line
00404                     $this->mimeContentType = str_replace("\r\n", " ", $v);
00405                 }
00406             }
00407     
00408             return $output['body'];
00409         }
00410 
00411         return parent::getHTTPBody($soapmsg);
00412     }
00413     
00422     function getHTTPContentType() {
00423         if (count($this->responseAttachments) > 0) {
00424             return $this->mimeContentType;
00425         }
00426         return parent::getHTTPContentType();
00427     }
00428     
00438     function getHTTPContentTypeCharset() {
00439         if (count($this->responseAttachments) > 0) {
00440             return false;
00441         }
00442         return parent::getHTTPContentTypeCharset();
00443     }
00444 
00453     function parseRequest($headers, $data) {
00454         $this->debug('Entering parseRequest() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
00455         $this->requestAttachments = array();
00456         if (strstr($headers['content-type'], 'multipart/related')) {
00457             $this->debug('Decode multipart/related');
00458             $input = '';
00459             foreach ($headers as $k => $v) {
00460                 $input .= "$k: $v\r\n";
00461             }
00462             $params['input'] = $input . "\r\n" . $data;
00463             $params['include_bodies'] = true;
00464             $params['decode_bodies'] = true;
00465             $params['decode_headers'] = true;
00466             
00467             $structure = Mail_mimeDecode::decode($params);
00468 
00469             foreach ($structure->parts as $part) {
00470                 if (!isset($part->disposition) && (strstr($part->headers['content-type'], 'text/xml'))) {
00471                     $this->debug('Have root part of type ' . $part->headers['content-type']);
00472                     $return = parent::parseRequest($part->headers, $part->body);
00473                 } else {
00474                     $this->debug('Have an attachment of type ' . $part->headers['content-type']);
00475                     $info['data'] = $part->body;
00476                     $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : '';
00477                     $info['contenttype'] = $part->headers['content-type'];
00478                     $info['cid'] = $part->headers['content-id'];
00479                     $this->requestAttachments[] = $info;
00480                 }
00481             }
00482         
00483             if (isset($return)) {
00484                 return $return;
00485             }
00486             
00487             $this->setError('No root part found in multipart/related content');
00488             return;
00489         }
00490         $this->debug('Not multipart/related');
00491         return parent::parseRequest($headers, $data);
00492     }
00493 }
00494 
00495 /*
00496  *  For backwards compatiblity
00497  */
00498 class nusoapservermime extends nusoap_server_mime {
00499 }
00500 
00501 ?>