OPRequest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider;
  3. use MGModule\DNSManager2\mgLibs\custom\dns\submodules\OpenProvider\OPApi;
  4. use \DOMDocument;
  5. class OPRequest
  6. {
  7. protected $cmd = null;
  8. protected $args = null;
  9. protected $username = null;
  10. protected $password = null;
  11. protected $hash = null;
  12. protected $token = null;
  13. protected $ip = null;
  14. protected $language = null;
  15. protected $raw = null;
  16. protected $dom = null;
  17. protected $misc = null;
  18. protected $filters = [];
  19. public function __construct ($str = null)
  20. {
  21. if ($str) {
  22. $this->setContent($str);
  23. }
  24. }
  25. public function addFilter($filter)
  26. {
  27. $this->filters[] = $filter;
  28. }
  29. public function setContent($str)
  30. {
  31. $this->raw = $str;
  32. }
  33. protected function initDom()
  34. {
  35. if ($this->raw) {
  36. $this->dom = new DOMDocument;
  37. $this->dom->loadXML($this->raw, LIBXML_NOBLANKS);
  38. }
  39. }
  40. public function getDom()
  41. {
  42. if (!$this->dom) {
  43. $this->initDom();
  44. }
  45. return $this->dom;
  46. }
  47. protected function setDom($dom)
  48. {
  49. $this->dom = $dom;
  50. }
  51. public function parseContent()
  52. {
  53. $this->initDom();
  54. if (!$this->dom) {
  55. return;
  56. }
  57. foreach ($this->filters as $f) {
  58. $f->filter($this);
  59. }
  60. $this->_retrieveDataFromDom($this->dom);
  61. }
  62. protected function _retrieveDataFromDom ($dom)
  63. {
  64. $arr = OPApi::convertXmlToPhpObj($dom->documentElement);
  65. list($dummy, $credentials) = each($arr);
  66. list($this->cmd, $this->args) = each($arr);
  67. $this->username = $credentials['username'];
  68. $this->password = $credentials['password'];
  69. if (isset($credentials['hash'])) {
  70. $this->hash = $credentials['hash'];
  71. }
  72. if (isset($credentials['misc'])) {
  73. $this->misc = $credentials['misc'];
  74. }
  75. $this->token = isset($credentials['token']) ? $credentials['token'] : null;
  76. $this->ip = isset($credentials['ip']) ? $credentials['ip'] : null;
  77. if (isset($credentials['language'])) {
  78. $this->language = $credentials['language'];
  79. }
  80. }
  81. public function setCommand ($v)
  82. {
  83. $this->cmd = $v;
  84. return $this;
  85. }
  86. public function getCommand ()
  87. {
  88. return $this->cmd;
  89. }
  90. public function setLanguage ($v)
  91. {
  92. $this->language = $v;
  93. return $this;
  94. }
  95. public function getLanguage ()
  96. {
  97. return $this->language;
  98. }
  99. public function setArgs ($v)
  100. {
  101. $this->args = $v;
  102. return $this;
  103. }
  104. public function getArgs ()
  105. {
  106. return $this->args;
  107. }
  108. public function setMisc ($v)
  109. {
  110. $this->misc = $v;
  111. return $this;
  112. }
  113. public function getMisc ()
  114. {
  115. return $this->misc;
  116. }
  117. public function setAuth ($args)
  118. {
  119. $this->username = isset($args["username"]) ? $args["username"] : null;
  120. $this->password = isset($args["password"]) ? $args["password"] : null;
  121. $this->hash = isset($args["hash"]) ? $args["hash"] : null;
  122. $this->token = isset($args["token"]) ? $args["token"] : null;
  123. $this->ip = isset($args["ip"]) ? $args["ip"] : null;
  124. $this->misc = isset($args["misc"]) ? $args["misc"] : null;
  125. return $this;
  126. }
  127. public function getAuth ()
  128. {
  129. return array(
  130. "username" => $this->username,
  131. "password" => $this->password,
  132. "hash" => $this->hash,
  133. "token" => $this->token,
  134. "ip" => $this->ip,
  135. "misc" => $this->misc,
  136. );
  137. }
  138. public function getRaw ()
  139. {
  140. if (!$this->raw) {
  141. $this->raw .= $this->_getRequest();
  142. }
  143. return $this->raw;
  144. }
  145. public function _getRequest ()
  146. {
  147. $dom = new DOMDocument('1.0', OPApi::$encoding);
  148. $credentialsElement = $dom->createElement('credentials');
  149. $usernameElement = $dom->createElement('username');
  150. $usernameElement->appendChild(
  151. $dom->createTextNode(OPApi::encode($this->username))
  152. );
  153. $credentialsElement->appendChild($usernameElement);
  154. $passwordElement = $dom->createElement('password');
  155. $passwordElement->appendChild(
  156. $dom->createTextNode(OPApi::encode($this->password))
  157. );
  158. $credentialsElement->appendChild($passwordElement);
  159. $hashElement = $dom->createElement('hash');
  160. $hashElement->appendChild(
  161. $dom->createTextNode(OPApi::encode($this->hash))
  162. );
  163. $credentialsElement->appendChild($hashElement);
  164. if (isset($this->language)) {
  165. $languageElement = $dom->createElement('language');
  166. $languageElement->appendChild($dom->createTextNode($this->language));
  167. $credentialsElement->appendChild($languageElement);
  168. }
  169. if (isset($this->token)) {
  170. $tokenElement = $dom->createElement('token');
  171. $tokenElement->appendChild($dom->createTextNode($this->token));
  172. $credentialsElement->appendChild($tokenElement);
  173. }
  174. if (isset($this->ip)) {
  175. $ipElement = $dom->createElement('ip');
  176. $ipElement->appendChild($dom->createTextNode($this->ip));
  177. $credentialsElement->appendChild($ipElement);
  178. }
  179. if (isset($this->misc)) {
  180. $miscElement = $dom->createElement('misc');
  181. $credentialsElement->appendChild($miscElement);
  182. OPApi::convertPhpObjToDom($this->misc, $miscElement, $dom);
  183. }
  184. $rootElement = $dom->createElement('openXML');
  185. $rootElement->appendChild($credentialsElement);
  186. $rootNode = $dom->appendChild($rootElement);
  187. $cmdNode = $rootNode->appendChild(
  188. $dom->createElement($this->getCommand())
  189. );
  190. OPApi::convertPhpObjToDom($this->args, $cmdNode, $dom);
  191. return $dom->saveXML();
  192. }
  193. }