Simple.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /* EPP Client class for PHP, Copyright 2013 CentralNic Ltd
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. require_once dirname(__FILE__).'/Exception.php';
  16. require_once dirname(__FILE__).'/Client.php';
  17. require_once dirname(__FILE__).'/Frame.php';
  18. /**
  19. * @package Net_EPP_Simple
  20. * @author CentralNic <https://www.centralnic.com>
  21. */
  22. class Net_EPP_Simple extends Net_EPP_Client {
  23. private $connected;
  24. private $logged_in;
  25. private $user;
  26. var $debug;
  27. /**
  28. * @var DOMDocument
  29. */
  30. var $greeting;
  31. /**
  32. * @param string $host
  33. * @param string $user
  34. * @param string $pass
  35. * @param boolean $debug
  36. * @param integer $port
  37. * @param integer timeout
  38. * @param boolean $ssl
  39. * @param resource $context
  40. * @throws Net_EPP_Exception
  41. */
  42. function __construct($host=NULL, $user=NULL, $pass=NULL, $debug=false, $port=700, $timeout=1, $ssl=true, $context=NULL) {
  43. $this->connected = false;
  44. $this->logged_in = false;
  45. $this->debug = $debug;
  46. $this->user = $user;
  47. if ($host) $this->connect($host, $port, $timeout, $ssl, $context);
  48. if ($user && $pass) $this->login($user, $pass);
  49. }
  50. /**
  51. * @param string $user
  52. * @param string $pass
  53. * @throws Net_EPP_Exception
  54. */
  55. function login($user, $pass) {
  56. if (!$this->connected) throw new Net_EPP_Exception("Not connected");
  57. $frame = new Net_EPP_Frame_Command_Login;
  58. $frame->clID->appendChild($frame->createTextNode($user));
  59. $frame->pw->appendChild($frame->createTextNode($pass));
  60. $frame->eppVersion->appendChild($frame->createTextNode($this->greeting->getElementsByTagNameNS(Net_EPP_Frame::EPP_URN, 'version')->item(0)->textContent));
  61. $frame->eppLang->appendChild($frame->createTextNode($this->greeting->getElementsByTagNameNS(Net_EPP_Frame::EPP_URN, 'lang')->item(0)->textContent));
  62. $els = $this->greeting->getElementsByTagNameNS(Net_EPP_Frame::EPP_URN, 'objURI');
  63. for ($i = 0 ; $i < $els->length ; $i++) $frame->svcs->appendChild($frame->importNode($els->item($i), true));
  64. $els = $this->greeting->getElementsByTagNameNS(Net_EPP_Frame::EPP_URN, 'svcExtension');
  65. if (1 == $els->length) $frame->svcs->appendChild($frame->importNode($els->item(0), true));
  66. $this->request($frame);
  67. }
  68. function checkDomain($domain) {
  69. }
  70. function checkDomains($domains) {
  71. }
  72. function checkHost($host) {
  73. }
  74. function checkHosts($host) {
  75. }
  76. function checkContact($contact) {
  77. }
  78. function checkContacts($contacts) {
  79. }
  80. function domainInfo($domain, $authInfo=NULL) {
  81. }
  82. function hostInfo($host, $authInfo=NULL) {
  83. }
  84. function contactInfo($contact, $authInfo=NULL) {
  85. }
  86. function domainTransferQuery($domain) {
  87. }
  88. function domainTransferCancel($domain) {
  89. }
  90. function domainTransferRequest($domain, $authInfo, $period, $unit='y') {
  91. }
  92. function domainTransferApprove($domain) {
  93. }
  94. function domainTransferReject($domain) {
  95. }
  96. function contactTransferQuery($contact) {
  97. }
  98. function contactTransferCancel($contact) {
  99. }
  100. function contactTransferRequest($contact, $authInfo) {
  101. }
  102. function contactTransferApprove($contact) {
  103. }
  104. function contactTransferReject($contact) {
  105. }
  106. function createDomain($domain) {
  107. }
  108. function createHost($host) {
  109. }
  110. function createContact($contact) {
  111. }
  112. function updateDomain($domain, $add, $rem, $chg) {
  113. }
  114. function updateContact($contact, $add, $rem, $chg) {
  115. }
  116. function updateHost($host, $add, $rem, $chg) {
  117. }
  118. function deleteDomain($domain) {
  119. }
  120. function deleteHost($host) {
  121. }
  122. function deleteContact($contact) {
  123. }
  124. function renewDomain($domain, $period, $unit='y') {
  125. }
  126. function ping() {
  127. }
  128. private function _check($type, $objects) {
  129. $class = 'Net_EPP_Frame_Command_Check_'.ucfirst($type);
  130. $frame = new $class;
  131. foreach ($objects as $object) $frame->addObject($object);
  132. $frame->clTRID->appendChild($frame->createTextNode($this->clTRID()));
  133. $result = $this->request($frame);
  134. }
  135. /*
  136. * send a frame to the server and get the response
  137. * @param DOMDocument|string $frame
  138. * @return DOMDocument
  139. * @throws Net_EPP_Exception
  140. */
  141. function request($frame) {
  142. $this->sendFrame($frame);
  143. $response = $this->getFrame();
  144. return $response;
  145. }
  146. /*
  147. * send a <logout> to the server
  148. * @throws Net_EPP_Exception
  149. */
  150. function logout() {
  151. $this->debug("logging out");
  152. $frame = new Net_EPP_Frame_Command_Logout;
  153. $frame->clTRID->appendChild($frame->createTextNode($this->clTRID()));
  154. $this->request($frame);
  155. }
  156. /*
  157. * connect to the server
  158. * @param string $host
  159. * @param integer $port
  160. * @param integer timeout
  161. * @param boolean $ssl
  162. * @param resource $context
  163. * @throws Net_EPP_Exception
  164. * @return DOMDocument the <greeting> frame recived from the server
  165. */
  166. function connect($host, $port, $timeout, $ssl, $context) {
  167. $this->debug("attempting to connect to %s:%d", $host, $port);
  168. $dom = parent::connect($host, $port, $timeout, $ssl, $context);
  169. $this->debug("connected OK");
  170. $this->connected = true;
  171. $this->greeting = new Net_EPP_Frame_Greeting;
  172. $this->greeting->loadXML($dom->saveXML());
  173. return $this->greeting;
  174. }
  175. /*
  176. * get a frame from the server
  177. * @return DOMDocument
  178. * @throws Net_EPP_Exception
  179. */
  180. function getFrame() {
  181. $xml = parent::getFrame();
  182. foreach (explode("\n", str_replace('><', ">\n<", trim($xml))) as $line) $this->debug("S: %s", $line);
  183. return DOMDocument::loadXML($xml);
  184. }
  185. /*
  186. * send a frame to the server
  187. * @param DOMDocument|string $xml
  188. * @return integer number of btyes sent
  189. * @throws Net_EPP_Exception
  190. */
  191. function sendFrame($xml) {
  192. if ($xml instanceof DOMDocument) $xml = $xml->saveXML();
  193. foreach (explode("\n", str_replace('><', ">\n<", trim($xml))) as $line) $this->debug("C: %s", $line);
  194. return parent::sendFrame($xml);
  195. }
  196. /*
  197. * write a debug messge to the error log - is silent unless
  198. * debugging is enabled
  199. * @param string $format
  200. * @param one or more variables
  201. * @return boolean
  202. */
  203. protected function debug() {
  204. if (!$this->debug) return true;
  205. $args = func_get_args();
  206. error_log(vsprintf(array_shift($args), $args));
  207. }
  208. /*
  209. * destructor - cleanly disconnect from the server
  210. * @throws Net_EPP_Exception
  211. */
  212. function __destruct() {
  213. if ($this->logged_in) $this->logout();
  214. $this->debug("disconnecting from server");
  215. $this->disconnect();
  216. }
  217. /*
  218. * generate a client transaction ID
  219. * @return string
  220. */
  221. function clTRID() {
  222. $clTRID = base_convert(
  223. hash('sha256', ($this->user ? $this->user.'-' : '').uniqid()),
  224. 16,
  225. 36
  226. );
  227. }
  228. }