nusoapmime.php 15 KB

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