nusoapmime.php 14 KB

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