OPApi.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  4. use MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPRequest;
  5. use MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPReply;
  6. use \DOMDocument;
  7. class OPApi
  8. {
  9. protected $url = null;
  10. protected $error = null;
  11. protected $timeout = null;
  12. protected $debug = null;
  13. static public $encoding = 'UTF-8';
  14. public function __construct ($url = null, $timeout = 1000)
  15. {
  16. $this->url = $url;
  17. $this->timeout = $timeout;
  18. }
  19. public function setDebug ($v)
  20. {
  21. $this->debug = $v;
  22. return $this;
  23. }
  24. public function processRawReply (OPRequest $r) {
  25. if ($this->debug) {
  26. echo $r->getRaw() . "\n";
  27. }
  28. $msg = $r->getRaw();
  29. $str = $this->_send($msg);
  30. if (!$str) {
  31. throw new exceptions\DNSSubmoduleException("Bad reply", 4004);
  32. }
  33. if ($this->debug) {
  34. echo $str . "\n";
  35. }
  36. return $str;
  37. }
  38. public function process (OPRequest $r) {
  39. if ($this->debug) {
  40. echo $r->getRaw() . "\n";
  41. }
  42. $msg = $r->getRaw();
  43. $str = $this->_send($msg);
  44. if (!$str) {
  45. throw new exceptions\DNSSubmoduleException("Bad reply", 4004);
  46. }
  47. if ($this->debug) {
  48. echo $str . "\n";
  49. }
  50. return new OPReply($str);
  51. }
  52. /**
  53. * Check if xml was created successfully with $str
  54. * @param $str string
  55. * @return boolean
  56. */
  57. static function checkCreateXml($str)
  58. {
  59. $dom = new DOMDocument;
  60. $dom->encoding = 'utf-8';
  61. $textNode = $dom->createTextNode($str);
  62. if (!$textNode) {
  63. return false;
  64. }
  65. $element = $dom->createElement('element')
  66. ->appendChild($textNode);
  67. if (!$element) {
  68. return false;
  69. }
  70. @$dom->appendChild($element);
  71. $xml = $dom->saveXML();
  72. return !empty($xml);
  73. }
  74. static function encode ($str)
  75. {
  76. $ret = @htmlentities($str, null, OPApi::$encoding);
  77. // Some tables have data stored in two encodings
  78. if (strlen($str) && !strlen($ret)) {
  79. error_log('ISO charset date = ' . date('d.m.Y H:i:s') . ',STR = ' . $str);
  80. $str = iconv('ISO-8859-1', 'UTF-8', $str);
  81. }
  82. if (!empty($str) && is_object($str)) {
  83. error_log('Exception convertPhpObjToDom date = ' . date('d.m.Y H:i:s') . ', object class = ' . get_class($str));
  84. if (method_exists($str , '__toString')) {
  85. $str = $str->__toString();
  86. } else {
  87. return $str;
  88. }
  89. }
  90. if (!empty($str) && is_string($str) && !self::checkCreateXml($str)) {
  91. error_log('Exception convertPhpObjToDom date = ' . date('d.m.Y H:i:s') . ', STR = ' . $str);
  92. $str = htmlentities($str, null, OPApi::$encoding);
  93. }
  94. return $str;
  95. }
  96. static function decode ($str)
  97. {
  98. return $str;
  99. }
  100. static function createRequest ($xmlStr = null)
  101. {
  102. return new OPRequest ($xmlStr);
  103. }
  104. static function createReply ($xmlStr = null)
  105. {
  106. return new OPReply ($xmlStr);
  107. }
  108. protected function _send ($str)
  109. {
  110. $ch = curl_init();
  111. curl_setopt($ch, CURLOPT_URL, $this->url);
  112. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  113. curl_setopt($ch, CURLOPT_HEADER, 0);
  114. curl_setopt($ch, CURLOPT_POST, 1);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  116. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  117. curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
  118. curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  119. $ret = curl_exec ($ch);
  120. $errno = curl_errno($ch);
  121. $this->error = $error = curl_error($ch);
  122. curl_close ($ch);
  123. if ($errno) {
  124. error_log("CURL error. Code: $errno, Message: $error");
  125. return false;
  126. } else {
  127. return $ret;
  128. }
  129. }
  130. // convert SimpleXML to PhpObj
  131. public static function convertXmlToPhpObj ($node)
  132. {
  133. $ret = array();
  134. if (is_object($node) && $node->hasChildNodes()) {
  135. foreach ($node->childNodes as $child) {
  136. $name = self::decode($child->nodeName);
  137. if ($child->nodeType == XML_TEXT_NODE) {
  138. $ret = self::decode($child->nodeValue);
  139. } else {
  140. if ('array' === $name) {
  141. return self::parseArray($child);
  142. } else {
  143. $ret[$name] = self::convertXmlToPhpObj($child);
  144. }
  145. }
  146. }
  147. }
  148. if(is_string($ret)){
  149. return (0 < strlen($ret)) ? $ret : null;
  150. }
  151. else if(is_array($ret)){
  152. return (!empty($ret)) ? $ret : null;
  153. }
  154. else if(is_null($ret)){
  155. return null;
  156. }
  157. else{
  158. return false;
  159. }
  160. }
  161. // parse array
  162. protected static function parseArray ($node)
  163. {
  164. $ret = array();
  165. foreach ($node->childNodes as $child) {
  166. $name = self::decode($child->nodeName);
  167. if ('item' !== $name) {
  168. throw new exceptions\DNSSubmoduleException('Wrong message format', 4006);
  169. }
  170. $ret[] = self::convertXmlToPhpObj($child);
  171. }
  172. return $ret;
  173. }
  174. /**
  175. * converts php-structure to DOM-object.
  176. *
  177. * @param array $arr php-structure
  178. * @param SimpleXMLElement $node parent node where new element to attach
  179. * @param DOMDocument $dom DOMDocument object
  180. * @return SimpleXMLElement
  181. */
  182. public static function convertPhpObjToDom ($arr, $node, $dom)
  183. {
  184. if (is_array($arr)) {
  185. /**
  186. * If arr has integer keys, this php-array must be converted in
  187. * xml-array representation (<array><item>..</item>..</array>)
  188. */
  189. $arrayParam = array();
  190. foreach ($arr as $k => $v) {
  191. if (is_integer($k)) {
  192. $arrayParam[] = $v;
  193. }
  194. }
  195. if (0 < count($arrayParam)) {
  196. $node->appendChild($arrayDom = $dom->createElement("array"));
  197. foreach ($arrayParam as $key => $val) {
  198. $new = $arrayDom->appendChild($dom->createElement('item'));
  199. self::convertPhpObjToDom($val, $new, $dom);
  200. }
  201. } else {
  202. foreach ($arr as $key => $val) {
  203. $new = $node->appendChild(
  204. $dom->createElement(self::encode($key))
  205. );
  206. self::convertPhpObjToDom($val, $new, $dom);
  207. }
  208. }
  209. } elseif (!is_object($arr)) {
  210. $node->appendChild($dom->createTextNode(self::encode($arr)));
  211. }
  212. }
  213. }