Source for file nusoapmime.php

Documentation is available at nusoapmime.php

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

Documentation generated on Tue, 06 Nov 2007 10:40:10 -0500 by phpDocumentor 1.3.0RC3