PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/gulliver/thirdparty/pear/nusoapmime.php

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