/modules/webservice/classes/general/soap/soapcodec.php

https://github.com/agratushniy/bxApiDocs · PHP · 351 lines · 266 code · 54 blank · 31 comment · 70 complexity · 99158aa89b9c253d00510aff100ebb83 MD5 · raw file

  1. <?php
  2. class CSOAPCodec
  3. {
  4. var $outputVars = array();
  5. var $typensVars = array();
  6. static function CSOAPCodec()
  7. {
  8. }
  9. public function setTypensVars($vars)
  10. {
  11. $this->typensVars = $vars;
  12. }
  13. public function setOutputVars($functionName)
  14. {
  15. if (!isset($this->typensVars[$functionName]["output"]))
  16. {
  17. ShowError("encodeValue() cant find output declaration.");
  18. exit();
  19. }
  20. $this->outputVars = $this->typensVars[$functionName]["output"];
  21. }
  22. public static function _validateSimpleType($dataType, $value)
  23. {
  24. global $xsd_simple_type;
  25. if (!isset($xsd_simple_type[$dataType]))
  26. CSOAPCodec::_errorTypeValidation("[Is not a simple type.{$dataType}]", $value);
  27. if ($dataType != gettype( $value ))
  28. {
  29. // all numbers are same as string
  30. if (is_numeric($value) and (
  31. $dataType == "integer" or
  32. $dataType == "double" or
  33. $dataType == "float"))
  34. return;
  35. //elseif ($dataType == 'base64Binary' && preg_match('/^([A-Za-z0-9]|\+|\/|\-|\=)+$/', $value))
  36. elseif ($dataType == 'base64Binary' || $dataType == 'any')
  37. return;
  38. CSOAPCodec::_errorTypeValidation($dataType, $value);
  39. }
  40. }
  41. public static function _validateClassType($classType, $value)
  42. {
  43. $phpClassType = strtolower($classType);
  44. $phpValue = strtolower(get_class($value));
  45. if ($phpClassType != $phpValue)
  46. {
  47. CSOAPServer::ShowSOAPFault("_errorTypeValidation(): Type validation for func. failed: {$classType} != ".get_class($value));
  48. exit();
  49. }
  50. }
  51. public static function _validateType($dataType, $value)
  52. {
  53. global $xsd_simple_type;
  54. if (isset($xsd_simple_type[$dataType]))
  55. {
  56. /*
  57. if (is_array($value))
  58. {
  59. echo $dataType;
  60. die();
  61. }
  62. else*/if ($dataType != gettype( $value ))
  63. {
  64. // all numbers are same as string
  65. if (is_numeric($value) and (
  66. $dataType == "integer" or
  67. $dataType == "double" or
  68. $dataType == "float"))
  69. return;
  70. //elseif ($dataType == 'base64Binary' && preg_match('/^([A-Za-z0-9]|\+|\/|\-|\=)+$/', $value))
  71. elseif ($dataType == 'base64Binary' || $dataType == 'any')
  72. return;
  73. CSOAPCodec::_errorTypeValidation($dataType, $value);
  74. }
  75. }
  76. else
  77. {
  78. if (!is_object($value) and !is_array($value))
  79. CSOAPCodec::_errorTypeValidation($dataType, $value);
  80. }
  81. }
  82. public static function _errorTypeValidation($dataType, $value)
  83. {
  84. CSOAPServer::ShowSOAPFault("_errorTypeValidation(): Type validation for func. failed: {$dataType} != ".gettype($value));
  85. exit();
  86. }
  87. // Encodes a PHP variable into a SOAP datatype.
  88. public function encodeValue($name, $value, $complexDataTypeName = "")
  89. {
  90. global $xsd_simple_type;
  91. if (!is_array($this->outputVars) or !count($this->outputVars))
  92. {
  93. CSOAPServer::ShowSOAPFault("encodeValue() has no Output Data Type Declaration for validation.");
  94. exit();
  95. }
  96. $dataType = "";
  97. $typeDeclaration = "";
  98. if (isset($this->outputVars[$name]))
  99. $typeDeclaration = $this->outputVars[$name];
  100. else if (isset($this->typensVars[$name]))
  101. $typeDeclaration = $this->typensVars[$name];
  102. else if (isset($this->typensVars[$complexDataTypeName][$name]))
  103. $typeDeclaration = $this->typensVars[$complexDataTypeName][$name];
  104. if (isset($typeDeclaration["varType"])) // if not, name = complex data type
  105. $dataType = $typeDeclaration["varType"];
  106. else
  107. $dataType = $name;
  108. if (isset($xsd_simple_type[$dataType]))
  109. $dataType = $xsd_simple_type[$dataType];
  110. // Type validation
  111. $this->_validateType($dataType, $value);
  112. switch ($dataType)
  113. {
  114. case "string" :
  115. {
  116. $node = new CXMLCreator( $name );
  117. //$node->setAttribute( "type", BX_SOAP_XSD_PREFIX . ":string" );
  118. $node->setData($value);
  119. return $node;
  120. } break;
  121. case "boolean" :
  122. {
  123. $node = new CXMLCreator( $name );
  124. //$node->setAttribute( "type", BX_SOAP_XSD_PREFIX . ":boolean" );
  125. if ( $value === true )
  126. $node->setData( "true" );
  127. else
  128. $node->setData( "false" );
  129. return $node;
  130. } break;
  131. case "integer" :
  132. {
  133. $node = new CXMLCreator( $name );
  134. //$node->setAttribute( "type", BX_SOAP_XSD_PREFIX . ":int" );
  135. $node->setData( intval( $value ) );
  136. return $node;
  137. } break;
  138. case "float":
  139. case "double" :
  140. {
  141. $node = new CXMLCreator( $name );
  142. //$node->setAttribute( "type", BX_SOAP_XSD_PREFIX . ":float" );
  143. $node->setData($value);
  144. return $node;
  145. } break;
  146. // added by Sigurd
  147. case "base64":
  148. case "base64Binary":
  149. $node = new CXMLCreator($name);
  150. //$node->setAttribute("type", BX_SOAP_XSD_PREFIX . ":base64Binary" );
  151. $node->setData(base64_encode($value));
  152. return $node;
  153. break;
  154. case 'any':
  155. $node = new CXMLCreator($name);
  156. if (is_object($value))
  157. {
  158. // $fp = fopen($_SERVER['DOCUMENT_ROOT'].'/ws.log', 'a');
  159. // fwrite($fp, $value->GetTree()."\n");
  160. // fwrite($fp, '===================================='."\r\n");
  161. // fclose($fp);
  162. if (get_class($value) == 'CDataXML')
  163. $node->addChild(CXMLCreator::CreateFromDOM($value->GetTree()));
  164. elseif (get_class($value) == 'CDataXMLDocument')
  165. $node->addChild(CXMLCreator::CreateFromDOM($value));
  166. elseif(get_class($value) == 'CXMLCreator')
  167. $node->addChild($value);
  168. }
  169. else
  170. {
  171. $data = new CDataXML();
  172. if ($data->LoadString($value))
  173. $node->addChild(CXMLCreator::CreateFromDOM($data->GetTree()));
  174. else
  175. $node->setData($value);
  176. }
  177. return $node;
  178. break;
  179. default :
  180. {
  181. $node = new CXMLCreator( $name );
  182. if (isset($typeDeclaration["arrType"]))
  183. {
  184. if (!isset($typeDeclaration["varType"]))
  185. $this->_errorTypeValidation("varType [undef]", $value);
  186. $varType = $typeDeclaration["varType"];
  187. // Decode array
  188. $maxOccurs = 0;
  189. $arrayType = $typeDeclaration["arrType"];
  190. if (isset($typeDeclaration["maxOccursA"]))
  191. $maxOccurs = $typeDeclaration["maxOccursA"];
  192. if (isset($xsd_simple_type[$arrayType]))
  193. {
  194. $i = 0;
  195. $arrayType = $xsd_simple_type[$arrayType];
  196. $arrayTypeEl = $varType."El"; // TODO: non fixed. get El name from wsdl. or decl.
  197. if (!is_array($value))
  198. CSOAPCodec::_errorTypeValidation("Array", $value);
  199. foreach ($value as $valnode)
  200. {
  201. $i++;
  202. $this->_validateType($arrayType, $valnode);
  203. $cndata = new CXMLCreator ( $arrayTypeEl );
  204. $cndata->setData($valnode);
  205. $node->addChild($cndata);
  206. if (intval($maxOccurs)>0 and $i>$maxOccurs)
  207. break;
  208. }
  209. }
  210. else
  211. {
  212. // Complex data type arrays // $arrayType as is.
  213. // TODO: non fixed. get $arrayTypeEl name from wsdl. or decl.
  214. $i = 0;
  215. $arrayTypeEl = $varType."El";
  216. if (!is_array($value))
  217. CSOAPCodec::_errorTypeValidation("Array", $value);
  218. foreach ($value as $valnode)
  219. {
  220. $decoded = null;
  221. $i++;
  222. $this->_validateType($arrayType, $valnode);
  223. $decoded = $this->encodeValue( $arrayType, $valnode );
  224. $cndata = new CXMLCreator ( $arrayTypeEl );
  225. if ($decoded)
  226. {
  227. $this->_validateClassType("CXMLCreator", $decoded);
  228. $decoded->setName($arrayTypeEl);
  229. $node->addChild($decoded);
  230. }
  231. if (intval($maxOccurs)>0 and $i>$maxOccurs)
  232. break;
  233. }
  234. }
  235. }
  236. else
  237. {
  238. // Here we goes with struct, or with class
  239. // First, try to find declaration
  240. $objectDecl = 0;
  241. $returnValue = array();
  242. $params = array();
  243. if (!isset($this->typensVars[$dataType])) break;
  244. $objectDecl = $this->typensVars[$dataType];
  245. if (!$objectDecl)
  246. {
  247. CSOAPServer::ShowSOAPFault("encodeValue() cant find complex type declaration for {$dataType}.");
  248. exit();
  249. }
  250. // Type of serialization: class/assoc array
  251. $objectClass = null;
  252. $serialize = "assoc";
  253. if (isset($objectDecl["serialize"]))
  254. {
  255. $serialize = $objectDecl["serialize"];
  256. unset($objectDecl["serialize"]);
  257. }
  258. // Validate hard complex data types
  259. if ($serialize == "assoc")
  260. $this->_validateType("array", $value);
  261. if ($serialize != "assoc")
  262. $this->_validateClassType($dataType, $value);
  263. foreach($objectDecl as $pname => $param)
  264. {
  265. $decoded = null;
  266. $strict = true;
  267. if (isset($param["strict"])) $strict = ($param["strict"]=="strict")?true:false;
  268. if ($serialize == "assoc")
  269. {
  270. //var_dump($pname); var_dump($value[$pname]); die();
  271. if (isset($value[$pname]))
  272. $decoded = $this->encodeValue( $pname, $value[$pname], $dataType );
  273. }
  274. else
  275. if ($serialize != "assoc")
  276. {
  277. if (isset($value->$pname))
  278. $decoded = $this->encodeValue( $pname, $value->$pname, $dataType );
  279. }
  280. if ($decoded)
  281. $this->_validateClassType("CXMLCreator", $decoded);
  282. if (!$decoded and !$strict)
  283. {
  284. CSOAPServer::ShowSOAPFault("Request has no enought params of strict type to be decoded. ");
  285. exit();
  286. }
  287. $node->addChild($decoded);
  288. }
  289. }
  290. return $node;
  291. } break;
  292. }
  293. return false;
  294. }
  295. }
  296. ?>