• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

webservice/soap/lib/nusoapmime.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 $Id: nusoapmime.php 14918 2007-10-07 17:02:40Z rkuester $
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 If you have any questions or comments, please email:
00024 
00025 Dietrich Ayala
00026 dietrich@ganx4.com
00027 http://dietrich.ganx4.com/nusoap
00028 
00029 NuSphere Corporation
00030 http://www.nusphere.com
00031 
00032 */
00033 
00034 /*require_once('nusoap.php');*/
00035 /* PEAR Mail_MIME library */
00036 require_once('Mail/mimeDecode.php');
00037 require_once('Mail/mimePart.php');
00038 
00048 class soap_clientmime extends soap_client {
00054         var $requestAttachments = array();
00060         var $responseAttachments;
00065         var $mimeContentType;
00066         
00082         function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
00083                 if (! $cid) {
00084                         $cid = md5(uniqid(time()));
00085                 }
00086 
00087                 $info['data'] = $data;
00088                 $info['filename'] = $filename;
00089                 $info['contenttype'] = $contenttype;
00090                 $info['cid'] = $cid;
00091                 
00092                 $this->requestAttachments[] = $info;
00093 
00094                 return $cid;
00095         }
00096 
00102         function clearAttachments() {
00103                 $this->requestAttachments = array();
00104         }
00105 
00116         function getAttachments() {
00117                 return $this->responseAttachments;
00118         }
00119 
00127         function getHTTPBody($soapmsg) {
00128                 if (count($this->requestAttachments) > 0) {
00129                         $params['content_type'] = 'multipart/related; type=text/xml';
00130                         $mimeMessage =& new Mail_mimePart('', $params);
00131                         unset($params);
00132 
00133                         $params['content_type'] = 'text/xml';
00134                         $params['encoding']     = '8bit';
00135                         $params['charset']      = $this->soap_defencoding;
00136                         $mimeMessage->addSubpart($soapmsg, $params);
00137                         
00138                         foreach ($this->requestAttachments as $att) {
00139                                 unset($params);
00140 
00141                                 $params['content_type'] = $att['contenttype'];
00142                                 $params['encoding']     = 'base64';
00143                                 $params['disposition']  = 'attachment';
00144                                 $params['dfilename']    = $att['filename'];
00145                                 $params['cid']          = $att['cid'];
00146 
00147                                 if ($att['data'] == '' && $att['filename'] <> '') {
00148                                         if ($fd = fopen($att['filename'], 'rb')) {
00149                                                 $data = fread($fd, filesize($att['filename']));
00150                                                 fclose($fd);
00151                                         } else {
00152                                                 $data = '';
00153                                         }
00154                                         $mimeMessage->addSubpart($data, $params);
00155                                 } else {
00156                                         $mimeMessage->addSubpart($att['data'], $params);
00157                                 }
00158                         }
00159 
00160                         $output = $mimeMessage->encode();
00161                         $mimeHeaders = $output['headers'];
00162         
00163                         foreach ($mimeHeaders as $k => $v) {
00164                                 $this->debug("MIME header $k: $v");
00165                                 if (strtolower($k) == 'content-type') {
00166                                         // PHP header() seems to strip leading whitespace starting
00167                                         // the second line, so force everything to one line
00168                                         $this->mimeContentType = str_replace("\r\n", " ", $v);
00169                                 }
00170                         }
00171         
00172                         return $output['body'];
00173                 }
00174 
00175                 return parent::getHTTPBody($soapmsg);
00176         }
00177         
00186         function getHTTPContentType() {
00187                 if (count($this->requestAttachments) > 0) {
00188                         return $this->mimeContentType;
00189                 }
00190                 return parent::getHTTPContentType();
00191         }
00192         
00202         function getHTTPContentTypeCharset() {
00203                 if (count($this->requestAttachments) > 0) {
00204                         return false;
00205                 }
00206                 return parent::getHTTPContentTypeCharset();
00207         }
00208 
00217     function parseResponse($headers, $data) {
00218                 $this->debug('Entering parseResponse() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
00219                 $this->responseAttachments = array();
00220                 if (strstr($headers['content-type'], 'multipart/related')) {
00221                         $this->debug('Decode multipart/related');
00222                         $input = '';
00223                         foreach ($headers as $k => $v) {
00224                                 $input .= "$k: $v\r\n";
00225                         }
00226                         $params['input'] = $input . "\r\n" . $data;
00227                         $params['include_bodies'] = true;
00228                         $params['decode_bodies'] = true;
00229                         $params['decode_headers'] = true;
00230                         
00231                         $structure = Mail_mimeDecode::decode($params);
00232 
00233                         foreach ($structure->parts as $part) {
00234                                 if (!isset($part->disposition)) {
00235                                         $this->debug('Have root part of type ' . $part->headers['content-type']);
00236                                         $return = parent::parseResponse($part->headers, $part->body);
00237                                 } else {
00238                                         $this->debug('Have an attachment of type ' . $part->headers['content-type']);
00239                                         $info['data'] = $part->body;
00240                                         $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : '';
00241                                         $info['contenttype'] = $part->headers['content-type'];
00242                                         $info['cid'] = $part->headers['content-id'];
00243                                         $this->responseAttachments[] = $info;
00244                                 }
00245                         }
00246                 
00247                         if (isset($return)) {
00248                                 return $return;
00249                         }
00250                         
00251                         $this->setError('No root part found in multipart/related content');
00252                         return;
00253                 }
00254                 $this->debug('Not multipart/related');
00255                 return parent::parseResponse($headers, $data);
00256         }
00257 }
00258 
00268 class nusoapservermime extends soap_server {
00274         var $requestAttachments = array();
00280         var $responseAttachments;
00285         var $mimeContentType;
00286         
00302         function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
00303                 if (! $cid) {
00304                         $cid = md5(uniqid(time()));
00305                 }
00306 
00307                 $info['data'] = $data;
00308                 $info['filename'] = $filename;
00309                 $info['contenttype'] = $contenttype;
00310                 $info['cid'] = $cid;
00311                 
00312                 $this->responseAttachments[] = $info;
00313 
00314                 return $cid;
00315         }
00316 
00322         function clearAttachments() {
00323                 $this->responseAttachments = array();
00324         }
00325 
00336         function getAttachments() {
00337                 return $this->requestAttachments;
00338         }
00339 
00347         function getHTTPBody($soapmsg) {
00348                 if (count($this->responseAttachments) > 0) {
00349                         $params['content_type'] = 'multipart/related; type=text/xml';
00350                         $mimeMessage =& new Mail_mimePart('', $params);
00351                         unset($params);
00352 
00353                         $params['content_type'] = 'text/xml';
00354                         $params['encoding']     = '8bit';
00355                         $params['charset']      = $this->soap_defencoding;
00356                         $mimeMessage->addSubpart($soapmsg, $params);
00357                         
00358                         foreach ($this->responseAttachments as $att) {
00359                                 unset($params);
00360 
00361                                 $params['content_type'] = $att['contenttype'];
00362                                 $params['encoding']     = 'base64';
00363                                 $params['disposition']  = 'attachment';
00364                                 $params['dfilename']    = $att['filename'];
00365                                 $params['cid']          = $att['cid'];
00366 
00367                                 if ($att['data'] == '' && $att['filename'] <> '') {
00368                                         if ($fd = fopen($att['filename'], 'rb')) {
00369                                                 $data = fread($fd, filesize($att['filename']));
00370                                                 fclose($fd);
00371                                         } else {
00372                                                 $data = '';
00373                                         }
00374                                         $mimeMessage->addSubpart($data, $params);
00375                                 } else {
00376                                         $mimeMessage->addSubpart($att['data'], $params);
00377                                 }
00378                         }
00379 
00380                         $output = $mimeMessage->encode();
00381                         $mimeHeaders = $output['headers'];
00382         
00383                         foreach ($mimeHeaders as $k => $v) {
00384                                 $this->debug("MIME header $k: $v");
00385                                 if (strtolower($k) == 'content-type') {
00386                                         // PHP header() seems to strip leading whitespace starting
00387                                         // the second line, so force everything to one line
00388                                         $this->mimeContentType = str_replace("\r\n", " ", $v);
00389                                 }
00390                         }
00391         
00392                         return $output['body'];
00393                 }
00394 
00395                 return parent::getHTTPBody($soapmsg);
00396         }
00397         
00406         function getHTTPContentType() {
00407                 if (count($this->responseAttachments) > 0) {
00408                         return $this->mimeContentType;
00409                 }
00410                 return parent::getHTTPContentType();
00411         }
00412         
00422         function getHTTPContentTypeCharset() {
00423                 if (count($this->responseAttachments) > 0) {
00424                         return false;
00425                 }
00426                 return parent::getHTTPContentTypeCharset();
00427         }
00428 
00437     function parseRequest($headers, $data) {
00438                 $this->debug('Entering parseRequest() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
00439                 $this->requestAttachments = array();
00440                 if (strstr($headers['content-type'], 'multipart/related')) {
00441                         $this->debug('Decode multipart/related');
00442                         $input = '';
00443                         foreach ($headers as $k => $v) {
00444                                 $input .= "$k: $v\r\n";
00445                         }
00446                         $params['input'] = $input . "\r\n" . $data;
00447                         $params['include_bodies'] = true;
00448                         $params['decode_bodies'] = true;
00449                         $params['decode_headers'] = true;
00450                         
00451                         $structure = Mail_mimeDecode::decode($params);
00452 
00453                         foreach ($structure->parts as $part) {
00454                                 if (!isset($part->disposition)) {
00455                                         $this->debug('Have root part of type ' . $part->headers['content-type']);
00456                                         $return = parent::parseRequest($part->headers, $part->body);
00457                                 } else {
00458                                         $this->debug('Have an attachment of type ' . $part->headers['content-type']);
00459                                         $info['data'] = $part->body;
00460                                         $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : '';
00461                                         $info['contenttype'] = $part->headers['content-type'];
00462                                         $info['cid'] = $part->headers['content-id'];
00463                                         $this->requestAttachments[] = $info;
00464                                 }
00465                         }
00466                 
00467                         if (isset($return)) {
00468                                 return $return;
00469                         }
00470                         
00471                         $this->setError('No root part found in multipart/related content');
00472                         return;
00473                 }
00474                 $this->debug('Not multipart/related');
00475                 return parent::parseRequest($headers, $data);
00476         }
00477 }
00478 ?>

Generated on Fri Dec 13 2013 17:57:03 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1