nusoap_client_mime

Nusoap_client_mime client supporting MIME attachments defined at http://www.w3.org/TR/SOAP-attachments.

Defined (1)

The class is defined in the following location(s).

/lib/nusoapmime.php  
  1. class nusoap_client_mime extends nusoap_client { 
  2. /** 
  3. * @var array Each array element in the return is an associative array with keys 
  4. * data, filename, contenttype, cid 
  5. * @access private 
  6. */ 
  7. var $requestAttachments = array(); 
  8. /** 
  9. * @var array Each array element in the return is an associative array with keys 
  10. * data, filename, contenttype, cid 
  11. * @access private 
  12. */ 
  13. var $responseAttachments; 
  14. /** 
  15. * @var string 
  16. * @access private 
  17. */ 
  18. var $mimeContentType; 
  19.  
  20. /** 
  21. * adds a MIME attachment to the current request. 
  22. * If the $data parameter contains an empty string, this method will read 
  23. * the contents of the file named by the $filename parameter. 
  24. * If the $cid parameter is false, this method will generate the cid. 
  25. * @param string $data The data of the attachment 
  26. * @param string $filename The filename of the attachment (default is empty string) 
  27. * @param string $contenttype The MIME Content-Type of the attachment (default is application/octet-stream) 
  28. * @param string $cid The content-id (cid) of the attachment (default is false) 
  29. * @return string The content-id (cid) of the attachment 
  30. * @access public 
  31. */ 
  32. function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) { 
  33. if (! $cid) { 
  34. $cid = md5(uniqid(time())); 
  35.  
  36. $info['data'] = $data; 
  37. $info['filename'] = $filename; 
  38. $info['contenttype'] = $contenttype; 
  39. $info['cid'] = $cid; 
  40.  
  41. $this->requestAttachments[] = $info; 
  42.  
  43. return $cid; 
  44.  
  45. /** 
  46. * clears the MIME attachments for the current request. 
  47. * @access public 
  48. */ 
  49. function clearAttachments() { 
  50. $this->requestAttachments = array(); 
  51.  
  52. /** 
  53. * gets the MIME attachments from the current response. 
  54. * Each array element in the return is an associative array with keys 
  55. * data, filename, contenttype, cid. These keys correspond to the parameters 
  56. * for addAttachment. 
  57. * @return array The attachments. 
  58. * @access public 
  59. */ 
  60. function getAttachments() { 
  61. return $this->responseAttachments; 
  62.  
  63. /** 
  64. * gets the HTTP body for the current request. 
  65. * @param string $soapmsg The SOAP payload 
  66. * @return string The HTTP body, which includes the SOAP payload 
  67. * @access private 
  68. */ 
  69. function getHTTPBody($soapmsg) { 
  70. if (count($this->requestAttachments) > 0) { 
  71. $params['content_type'] = 'multipart/related; type="text/xml"'; 
  72. $mimeMessage = new Mail_mimePart('', $params); 
  73. unset($params); 
  74.  
  75. $params['content_type'] = 'text/xml'; 
  76. $params['encoding'] = '8bit'; 
  77. $params['charset'] = $this->soap_defencoding; 
  78. $mimeMessage->addSubpart($soapmsg, $params); 
  79.  
  80. foreach ($this->requestAttachments as $att) { 
  81. unset($params); 
  82.  
  83. $params['content_type'] = $att['contenttype']; 
  84. $params['encoding'] = 'base64'; 
  85. $params['disposition'] = 'attachment'; 
  86. $params['dfilename'] = $att['filename']; 
  87. $params['cid'] = $att['cid']; 
  88.  
  89. if ($att['data'] == '' && $att['filename'] <> '') { 
  90. if ($fd = fopen($att['filename'], 'rb')) { 
  91. $data = fread($fd, filesize($att['filename'])); 
  92. fclose($fd); 
  93. } else { 
  94. $data = ''; 
  95. $mimeMessage->addSubpart($data, $params); 
  96. } else { 
  97. $mimeMessage->addSubpart($att['data'], $params); 
  98.  
  99. $output = $mimeMessage->encode(); 
  100. $mimeHeaders = $output['headers']; 
  101.  
  102. foreach ($mimeHeaders as $k => $v) { 
  103. $this->debug("MIME header $k: $v"); 
  104. if (strtolower($k) == 'content-type') { 
  105. // PHP header() seems to strip leading whitespace starting 
  106. // the second line, so force everything to one line 
  107. $this->mimeContentType = str_replace("\r\n", " ", $v); 
  108.  
  109. return $output['body']; 
  110.  
  111. return parent::getHTTPBody($soapmsg); 
  112.  
  113. /** 
  114. * gets the HTTP content type for the current request. 
  115. * Note: getHTTPBody must be called before this. 
  116. * @return string the HTTP content type for the current request. 
  117. * @access private 
  118. */ 
  119. function getHTTPContentType() { 
  120. if (count($this->requestAttachments) > 0) { 
  121. return $this->mimeContentType; 
  122. return parent::getHTTPContentType(); 
  123.  
  124. /** 
  125. * gets the HTTP content type charset for the current request. 
  126. * returns false for non-text content types. 
  127. * Note: getHTTPBody must be called before this. 
  128. * @return string the HTTP content type charset for the current request. 
  129. * @access private 
  130. */ 
  131. function getHTTPContentTypeCharset() { 
  132. if (count($this->requestAttachments) > 0) { 
  133. return false; 
  134. return parent::getHTTPContentTypeCharset(); 
  135.  
  136. /** 
  137. * processes SOAP message returned from server 
  138. * @param array $headers The HTTP headers 
  139. * @param string $data unprocessed response data from server 
  140. * @return mixed value of the message, decoded into a PHP type 
  141. * @access private 
  142. */ 
  143. function parseResponse($headers, $data) { 
  144. $this->debug('Entering parseResponse() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']); 
  145. $this->responseAttachments = array(); 
  146. if (strstr($headers['content-type'], 'multipart/related')) { 
  147. $this->debug('Decode multipart/related'); 
  148. $input = ''; 
  149. foreach ($headers as $k => $v) { 
  150. $input .= "$k: $v\r\n"; 
  151. $params['input'] = $input . "\r\n" . $data; 
  152. $params['include_bodies'] = true; 
  153. $params['decode_bodies'] = true; 
  154. $params['decode_headers'] = true; 
  155.  
  156. $structure = Mail_mimeDecode::decode($params); 
  157.  
  158. foreach ($structure->parts as $part) { 
  159. if (!isset($part->disposition) && (strstr($part->headers['content-type'], 'text/xml'))) { 
  160. $this->debug('Have root part of type ' . $part->headers['content-type']); 
  161. $root = $part->body; 
  162. $return = parent::parseResponse($part->headers, $part->body); 
  163. } else { 
  164. $this->debug('Have an attachment of type ' . $part->headers['content-type']); 
  165. $info['data'] = $part->body; 
  166. $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : ''; 
  167. $info['contenttype'] = $part->headers['content-type']; 
  168. $info['cid'] = $part->headers['content-id']; 
  169. $this->responseAttachments[] = $info; 
  170.  
  171. if (isset($return)) { 
  172. $this->responseData = $root; 
  173. return $return; 
  174.  
  175. $this->setError('No root part found in multipart/related content'); 
  176. return ''; 
  177. $this->debug('Not multipart/related'); 
  178. return parent::parseResponse($headers, $data);