PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/php-pear-SOAP-0.13.0/SOAP-0.13.0/SOAP/Value.php

#
PHP | 286 lines | 92 code | 28 blank | 166 comment | 6 complexity | da18ba7258ba6b6f02ccc775c498f06e MD5 | raw file
  1. <?php
  2. /**
  3. * This file contains the code for converting values between SOAP and PHP.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 2.02 of the PHP license,
  8. * that is bundled with this package in the file LICENSE, and is available at
  9. * through the world-wide-web at http://www.php.net/license/2_02.txt. If you
  10. * did not receive a copy of the PHP license and are unable to obtain it
  11. * through the world-wide-web, please send a note to license@php.net so we can
  12. * mail you a copy immediately.
  13. *
  14. * @category Web Services
  15. * @package SOAP
  16. * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
  17. * @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more
  18. * @author Chuck Hagenbuch <chuck@horde.org> Maintenance
  19. * @author Jan Schneider <jan@horde.org> Maintenance
  20. * @copyright 2003-2007 The PHP Group
  21. * @license http://www.php.net/license/2_02.txt PHP License 2.02
  22. * @link http://pear.php.net/package/SOAP
  23. */
  24. require_once 'SOAP/Base.php';
  25. /**
  26. * SOAP::Value
  27. *
  28. * This class converts values between PHP and SOAP.
  29. *
  30. * Originally based on SOAPx4 by Dietrich Ayala
  31. * http://dietrich.ganx4.com/soapx4
  32. *
  33. * @access public
  34. * @package SOAP
  35. * @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  36. * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
  37. */
  38. class SOAP_Value
  39. {
  40. /**
  41. * The actual value.
  42. *
  43. * @var mixed
  44. */
  45. var $value = null;
  46. /**
  47. * QName instance representing the value name.
  48. *
  49. * @var QName
  50. */
  51. var $nqn;
  52. /**
  53. * The value name, without namespace information.
  54. *
  55. * @var string
  56. */
  57. var $name = '';
  58. /**
  59. * The namespace of the value name.
  60. *
  61. * @var string
  62. */
  63. var $namespace = '';
  64. /**
  65. * QName instance representing the value type.
  66. *
  67. * @var QName
  68. */
  69. var $tqn;
  70. /**
  71. * The value type, without namespace information.
  72. *
  73. * @var string
  74. */
  75. var $type = '';
  76. /**
  77. * The namespace of the value type.
  78. *
  79. * @var string
  80. */
  81. var $type_namespace = '';
  82. /**
  83. * The type of the array elements, if this value is an array.
  84. *
  85. * @var string
  86. */
  87. var $arrayType = '';
  88. /**
  89. * A hash of additional attributes.
  90. *
  91. * @see SOAP_Value()
  92. * @var array
  93. */
  94. var $attributes = array();
  95. /**
  96. * List of encoding and serialization options.
  97. *
  98. * @see SOAP_Value()
  99. * @var array
  100. */
  101. var $options = array();
  102. /**
  103. * Constructor.
  104. *
  105. * @param string $name Name of the SOAP value {namespace}name.
  106. * @param mixed $type SOAP value {namespace}type. Determined
  107. * automatically if not set.
  108. * @param mixed $value Value to set.
  109. * @param array $attributes A has of additional XML attributes to be
  110. * added to the serialized value.
  111. * @param array $options A list of encoding and serialization options:
  112. * - 'attachment': array with information about
  113. * the attachment
  114. * - 'soap_encoding': defines encoding for SOAP
  115. * message part of a MIME encoded SOAP request
  116. * (default: base64)
  117. * - 'keep_arrays_flat': use the tag name
  118. * multiple times for each element when
  119. * passing in an array in literal mode
  120. * - 'no_type_prefix': supress adding of the
  121. * namespace prefix
  122. */
  123. function SOAP_Value($name = '', $type = false, $value = null,
  124. $attributes = array(), $options = array())
  125. {
  126. $this->nqn = new QName($name);
  127. $this->name = $this->nqn->name;
  128. $this->namespace = $this->nqn->namespace;
  129. $this->tqn = new QName($type);
  130. $this->type = $this->tqn->name;
  131. $this->type_namespace = $this->tqn->namespace;
  132. $this->value = $value;
  133. $this->attributes = $attributes;
  134. $this->options = $options;
  135. }
  136. /**
  137. * Serializes this value.
  138. *
  139. * @param SOAP_Base $serializer A SOAP_Base instance or subclass to
  140. * serialize with.
  141. *
  142. * @return string XML representation of $this.
  143. */
  144. function serialize(&$serializer)
  145. {
  146. return $serializer->_serializeValue($this->value,
  147. $this->nqn,
  148. $this->tqn,
  149. $this->options,
  150. $this->attributes,
  151. $this->arrayType);
  152. }
  153. }
  154. /**
  155. * This class converts values between PHP and SOAP. It is a simple wrapper
  156. * around SOAP_Value, adding support for SOAP actor and mustunderstand
  157. * parameters.
  158. *
  159. * Originally based on SOAPx4 by Dietrich Ayala
  160. * http://dietrich.ganx4.com/soapx4
  161. *
  162. * @access public
  163. * @package SOAP
  164. * @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  165. * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
  166. */
  167. class SOAP_Header extends SOAP_Value
  168. {
  169. /**
  170. * Constructor
  171. *
  172. * @param string $name Name of the SOAP value {namespace}name.
  173. * @param mixed $type SOAP value {namespace}type. Determined
  174. * automatically if not set.
  175. * @param mixed $value Value to set
  176. * @param integer $mustunderstand Zero or one.
  177. * @param mixed $attributes Attributes.
  178. */
  179. function SOAP_Header($name = '', $type, $value, $mustunderstand = 0,
  180. $attributes = array())
  181. {
  182. if (!is_array($attributes)) {
  183. $actor = $attributes;
  184. $attributes = array();
  185. }
  186. parent::SOAP_Value($name, $type, $value, $attributes);
  187. if (isset($actor)) {
  188. $this->attributes[SOAP_BASE::SOAPENVPrefix().':actor'] = $actor;
  189. } elseif (!isset($this->attributes[SOAP_BASE::SOAPENVPrefix().':actor'])) {
  190. $this->attributes[SOAP_BASE::SOAPENVPrefix().':actor'] = 'http://schemas.xmlsoap.org/soap/actor/next';
  191. }
  192. $this->attributes[SOAP_BASE::SOAPENVPrefix().':mustUnderstand'] = (int)$mustunderstand;
  193. }
  194. }
  195. /**
  196. * This class handles MIME attachements per W3C's Note on Soap Attachements at
  197. * http://www.w3.org/TR/SOAP-attachments
  198. *
  199. * @access public
  200. * @package SOAP
  201. * @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  202. */
  203. class SOAP_Attachment extends SOAP_Value
  204. {
  205. /**
  206. * Constructor.
  207. *
  208. * @param string $name Name of the SOAP value <value_name>
  209. * @param string $type The attachment's MIME type.
  210. * @param string $filename The attachment's file name. Ignored if $file
  211. * is provide.
  212. * @param string $file The attachment data.
  213. * @param array $attributes Attributes.
  214. */
  215. function SOAP_Attachment($name = '', $type = 'application/octet-stream',
  216. $filename, $file = null, $attributes = null)
  217. {
  218. parent::SOAP_Value($name, null, null);
  219. $filedata = $file === null ? $this->_file2str($filename) : $file;
  220. $filename = basename($filename);
  221. if (PEAR::isError($filedata)) {
  222. $this->options['attachment'] = $filedata;
  223. return;
  224. }
  225. $cid = md5(uniqid(time()));
  226. $this->attributes = $attributes;
  227. $this->attributes['href'] = 'cid:' . $cid;
  228. $this->options['attachment'] = array('body' => $filedata,
  229. 'disposition' => $filename,
  230. 'content_type' => $type,
  231. 'encoding' => 'base64',
  232. 'cid' => $cid);
  233. }
  234. /**
  235. * Returns the contents of the given file name as string.
  236. *
  237. * @access private
  238. *
  239. * @param string $file_name The file location.
  240. *
  241. * @return string The file data or a PEAR_Error.
  242. */
  243. function _file2str($file_name)
  244. {
  245. if (!is_readable($file_name)) {
  246. return PEAR::raiseError('File is not readable: ' . $file_name);
  247. }
  248. if (function_exists('file_get_contents')) {
  249. return file_get_contents($file_name);
  250. }
  251. if (!$fd = fopen($file_name, 'rb')) {
  252. return PEAR::raiseError('Could not open ' . $file_name);
  253. }
  254. $cont = fread($fd, filesize($file_name));
  255. fclose($fd);
  256. return $cont;
  257. }
  258. }