Source for file nusoapmime.php

Documentation is available at nusoapmime.php

  1. <?php
  2. /*
  3. $Id: nusoapmime.php,v 1.7 2005/07/27 19:24:42 snichol Exp $
  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('Mail/mimeDecode.php');
  37. require_once('Mail/mimePart.php');
  38.  
  39. /**
  40. * soapclientmime client supporting MIME attachments defined at
  41. * http://www.w3.org/TR/SOAP-attachments. It depends on the PEAR Mail_MIME library.
  42. *
  43. * @author Scott Nichol <snichol@sourceforge.net>
  44. * @author Thanks to Guillaume and Henning Reich for posting great attachment code to the mail list
  45. * @version $Id: nusoapmime.php,v 1.7 2005/07/27 19:24:42 snichol Exp $
  46. * @access public
  47. */
  48. class soapclientmime extends soapclient {
  49. /**
  50. * @var array Each array element in the return is an associative array with keys
  51. * data, filename, contenttype, cid
  52. * @access private
  53. */
  54. var $requestAttachments = array();
  55. /**
  56. * @var array Each array element in the return is an associative array with keys
  57. * data, filename, contenttype, cid
  58. * @access private
  59. */
  60. var $responseAttachments;
  61. /**
  62. * @var string
  63. * @access private
  64. */
  65. var $mimeContentType;
  66. /**
  67. * adds a MIME attachment to the current request.
  68. *
  69. * If the $data parameter contains an empty string, this method will read
  70. * the contents of the file named by the $filename parameter.
  71. *
  72. * If the $cid parameter is false, this method will generate the cid.
  73. *
  74. * @param string $data The data of the attachment
  75. * @param string $filename The filename of the attachment (default is empty string)
  76. * @param string $contenttype The MIME Content-Type of the attachment (default is application/octet-stream)
  77. * @param string $cid The content-id (cid) of the attachment (default is false)
  78. * @return string The content-id (cid) of the attachment
  79. * @access public
  80. */
  81. function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
  82. if (! $cid) {
  83. $cid = md5(uniqid(time()));
  84. }
  85.  
  86. $info['data'] = $data;
  87. $info['filename'] = $filename;
  88. $info['contenttype'] = $contenttype;
  89. $info['cid'] = $cid;
  90. $this->requestAttachments[] = $info;
  91.  
  92. return $cid;
  93. }
  94.  
  95. /**
  96. * clears the MIME attachments for the current request.
  97. *
  98. * @access public
  99. */
  100. function clearAttachments() {
  101. $this->requestAttachments = array();
  102. }
  103.  
  104. /**
  105. * gets the MIME attachments from the current response.
  106. *
  107. * Each array element in the return is an associative array with keys
  108. * data, filename, contenttype, cid. These keys correspond to the parameters
  109. * for addAttachment.
  110. *
  111. * @return array The attachments.
  112. * @access public
  113. */
  114. function getAttachments() {
  115. return $this->responseAttachments;
  116. }
  117.  
  118. /**
  119. * gets the HTTP body for the current request.
  120. *
  121. * @param string $soapmsg The SOAP payload
  122. * @return string The HTTP body, which includes the SOAP payload
  123. * @access private
  124. */
  125. function getHTTPBody($soapmsg) {
  126. if (count($this->requestAttachments) > 0) {
  127. $params['content_type'] = 'multipart/related; type=text/xml';
  128. $mimeMessage =& new Mail_mimePart('', $params);
  129. unset($params);
  130.  
  131. $params['content_type'] = 'text/xml';
  132. $params['encoding'] = '8bit';
  133. $params['charset'] = $this->soap_defencoding;
  134. $mimeMessage->addSubpart($soapmsg, $params);
  135. foreach ($this->requestAttachments as $att) {
  136. unset($params);
  137.  
  138. $params['content_type'] = $att['contenttype'];
  139. $params['encoding'] = 'base64';
  140. $params['disposition'] = 'attachment';
  141. $params['dfilename'] = $att['filename'];
  142. $params['cid'] = $att['cid'];
  143.  
  144. if ($att['data'] == '' && $att['filename'] <> '') {
  145. if ($fd = fopen($att['filename'], 'rb')) {
  146. $data = fread($fd, filesize($att['filename']));
  147. fclose($fd);
  148. } else {
  149. $data = '';
  150. }
  151. $mimeMessage->addSubpart($data, $params);
  152. } else {
  153. $mimeMessage->addSubpart($att['data'], $params);
  154. }
  155. }
  156.  
  157. $output = $mimeMessage->encode();
  158. $mimeHeaders = $output['headers'];
  159. foreach ($mimeHeaders as $k => $v) {
  160. $this->debug("MIME header $k: $v");
  161. if (strtolower($k) == 'content-type') {
  162. // PHP header() seems to strip leading whitespace starting
  163. // the second line, so force everything to one line
  164. $this->mimeContentType = str_replace("\r\n", " ", $v);
  165. }
  166. }
  167. return $output['body'];
  168. }
  169.  
  170. return parent::getHTTPBody($soapmsg);
  171. }
  172. /**
  173. * gets the HTTP content type for the current request.
  174. *
  175. * Note: getHTTPBody must be called before this.
  176. *
  177. * @return string the HTTP content type for the current request.
  178. * @access private
  179. */
  180. function getHTTPContentType() {
  181. if (count($this->requestAttachments) > 0) {
  182. return $this->mimeContentType;
  183. }
  184. return parent::getHTTPContentType();
  185. }
  186. /**
  187. * gets the HTTP content type charset for the current request.
  188. * returns false for non-text content types.
  189. *
  190. * Note: getHTTPBody must be called before this.
  191. *
  192. * @return string the HTTP content type charset for the current request.
  193. * @access private
  194. */
  195. function getHTTPContentTypeCharset() {
  196. if (count($this->requestAttachments) > 0) {
  197. return false;
  198. }
  199. return parent::getHTTPContentTypeCharset();
  200. }
  201.  
  202. /**
  203. * processes SOAP message returned from server
  204. *
  205. * @param array $headers The HTTP headers
  206. * @param string $data unprocessed response data from server
  207. * @return mixed value of the message, decoded into a PHP type
  208. * @access private
  209. */
  210. function parseResponse($headers, $data) {
  211. $this->debug('Entering parseResponse() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
  212. $this->responseAttachments = array();
  213. if (strstr($headers['content-type'], 'multipart/related')) {
  214. $this->debug('Decode multipart/related');
  215. $input = '';
  216. foreach ($headers as $k => $v) {
  217. $input .= "$k: $v\r\n";
  218. }
  219. $params['input'] = $input . "\r\n" . $data;
  220. $params['include_bodies'] = true;
  221. $params['decode_bodies'] = true;
  222. $params['decode_headers'] = true;
  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. if (isset($return)) {
  239. return $return;
  240. }
  241. $this->setError('No root part found in multipart/related content');
  242. return;
  243. }
  244. $this->debug('Not multipart/related');
  245. return parent::parseResponse($headers, $data);
  246. }
  247. }
  248.  
  249. /**
  250. * nusoapservermime server supporting MIME attachments defined at
  251. * http://www.w3.org/TR/SOAP-attachments. It depends on the PEAR Mail_MIME library.
  252. *
  253. * @author Scott Nichol <snichol@sourceforge.net>
  254. * @author Thanks to Guillaume and Henning Reich for posting great attachment code to the mail list
  255. * @version $Id: nusoapmime.php,v 1.7 2005/07/27 19:24:42 snichol Exp $
  256. * @access public
  257. */
  258. class nusoapservermime extends soap_server {
  259. /**
  260. * @var array Each array element in the return is an associative array with keys
  261. * data, filename, contenttype, cid
  262. * @access private
  263. */
  264. var $requestAttachments = array();
  265. /**
  266. * @var array Each array element in the return is an associative array with keys
  267. * data, filename, contenttype, cid
  268. * @access private
  269. */
  270. var $responseAttachments;
  271. /**
  272. * @var string
  273. * @access private
  274. */
  275. var $mimeContentType;
  276. /**
  277. * adds a MIME attachment to the current response.
  278. *
  279. * If the $data parameter contains an empty string, this method will read
  280. * the contents of the file named by the $filename parameter.
  281. *
  282. * If the $cid parameter is false, this method will generate the cid.
  283. *
  284. * @param string $data The data of the attachment
  285. * @param string $filename The filename of the attachment (default is empty string)
  286. * @param string $contenttype The MIME Content-Type of the attachment (default is application/octet-stream)
  287. * @param string $cid The content-id (cid) of the attachment (default is false)
  288. * @return string The content-id (cid) of the attachment
  289. * @access public
  290. */
  291. function addAttachment($data, $filename = '', $contenttype = 'application/octet-stream', $cid = false) {
  292. if (! $cid) {
  293. $cid = md5(uniqid(time()));
  294. }
  295.  
  296. $info['data'] = $data;
  297. $info['filename'] = $filename;
  298. $info['contenttype'] = $contenttype;
  299. $info['cid'] = $cid;
  300. $this->responseAttachments[] = $info;
  301.  
  302. return $cid;
  303. }
  304.  
  305. /**
  306. * clears the MIME attachments for the current response.
  307. *
  308. * @access public
  309. */
  310. function clearAttachments() {
  311. $this->responseAttachments = array();
  312. }
  313.  
  314. /**
  315. * gets the MIME attachments from the current request.
  316. *
  317. * Each array element in the return is an associative array with keys
  318. * data, filename, contenttype, cid. These keys correspond to the parameters
  319. * for addAttachment.
  320. *
  321. * @return array The attachments.
  322. * @access public
  323. */
  324. function getAttachments() {
  325. return $this->requestAttachments;
  326. }
  327.  
  328. /**
  329. * gets the HTTP body for the current response.
  330. *
  331. * @param string $soapmsg The SOAP payload
  332. * @return string The HTTP body, which includes the SOAP payload
  333. * @access private
  334. */
  335. function getHTTPBody($soapmsg) {
  336. if (count($this->responseAttachments) > 0) {
  337. $params['content_type'] = 'multipart/related; type=text/xml';
  338. $mimeMessage =& new Mail_mimePart('', $params);
  339. unset($params);
  340.  
  341. $params['content_type'] = 'text/xml';
  342. $params['encoding'] = '8bit';
  343. $params['charset'] = $this->soap_defencoding;
  344. $mimeMessage->addSubpart($soapmsg, $params);
  345. foreach ($this->responseAttachments as $att) {
  346. unset($params);
  347.  
  348. $params['content_type'] = $att['contenttype'];
  349. $params['encoding'] = 'base64';
  350. $params['disposition'] = 'attachment';
  351. $params['dfilename'] = $att['filename'];
  352. $params['cid'] = $att['cid'];
  353.  
  354. if ($att['data'] == '' && $att['filename'] <> '') {
  355. if ($fd = fopen($att['filename'], 'rb')) {
  356. $data = fread($fd, filesize($att['filename']));
  357. fclose($fd);
  358. } else {
  359. $data = '';
  360. }
  361. $mimeMessage->addSubpart($data, $params);
  362. } else {
  363. $mimeMessage->addSubpart($att['data'], $params);
  364. }
  365. }
  366.  
  367. $output = $mimeMessage->encode();
  368. $mimeHeaders = $output['headers'];
  369. foreach ($mimeHeaders as $k => $v) {
  370. $this->debug("MIME header $k: $v");
  371. if (strtolower($k) == 'content-type') {
  372. // PHP header() seems to strip leading whitespace starting
  373. // the second line, so force everything to one line
  374. $this->mimeContentType = str_replace("\r\n", " ", $v);
  375. }
  376. }
  377. return $output['body'];
  378. }
  379.  
  380. return parent::getHTTPBody($soapmsg);
  381. }
  382. /**
  383. * gets the HTTP content type for the current response.
  384. *
  385. * Note: getHTTPBody must be called before this.
  386. *
  387. * @return string the HTTP content type for the current response.
  388. * @access private
  389. */
  390. function getHTTPContentType() {
  391. if (count($this->responseAttachments) > 0) {
  392. return $this->mimeContentType;
  393. }
  394. return parent::getHTTPContentType();
  395. }
  396. /**
  397. * gets the HTTP content type charset for the current response.
  398. * returns false for non-text content types.
  399. *
  400. * Note: getHTTPBody must be called before this.
  401. *
  402. * @return string the HTTP content type charset for the current response.
  403. * @access private
  404. */
  405. function getHTTPContentTypeCharset() {
  406. if (count($this->responseAttachments) > 0) {
  407. return false;
  408. }
  409. return parent::getHTTPContentTypeCharset();
  410. }
  411.  
  412. /**
  413. * processes SOAP message received from client
  414. *
  415. * @param array $headers The HTTP headers
  416. * @param string $data unprocessed request data from client
  417. * @return mixed value of the message, decoded into a PHP type
  418. * @access private
  419. */
  420. function parseRequest($headers, $data) {
  421. $this->debug('Entering parseRequest() for payload of length ' . strlen($data) . ' and type of ' . $headers['content-type']);
  422. $this->requestAttachments = array();
  423. if (strstr($headers['content-type'], 'multipart/related')) {
  424. $this->debug('Decode multipart/related');
  425. $input = '';
  426. foreach ($headers as $k => $v) {
  427. $input .= "$k: $v\r\n";
  428. }
  429. $params['input'] = $input . "\r\n" . $data;
  430. $params['include_bodies'] = true;
  431. $params['decode_bodies'] = true;
  432. $params['decode_headers'] = true;
  433. $structure = Mail_mimeDecode::decode($params);
  434.  
  435. foreach ($structure->parts as $part) {
  436. if (!isset($part->disposition)) {
  437. $this->debug('Have root part of type ' . $part->headers['content-type']);
  438. $return = parent::parseRequest($part->headers, $part->body);
  439. } else {
  440. $this->debug('Have an attachment of type ' . $part->headers['content-type']);
  441. $info['data'] = $part->body;
  442. $info['filename'] = isset($part->d_parameters['filename']) ? $part->d_parameters['filename'] : '';
  443. $info['contenttype'] = $part->headers['content-type'];
  444. $info['cid'] = $part->headers['content-id'];
  445. $this->requestAttachments[] = $info;
  446. }
  447. }
  448. if (isset($return)) {
  449. return $return;
  450. }
  451. $this->setError('No root part found in multipart/related content');
  452. return;
  453. }
  454. $this->debug('Not multipart/related');
  455. return parent::parseRequest($headers, $data);
  456. }
  457. }
  458. ?>

Documentation generated on Wed, 3 Aug 2005 21:29:43 -0400 by phpDocumentor 1.3.0RC3