OVH.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\submodules\OVH;
  6. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  7. class OVH extends dns\SubmoduleAbstract implements interfaces\SubmoduleImportInterface
  8. {
  9. public $configFields = array(
  10. 'application_key' =>array (
  11. 'friendlyName' => 'Application Key',
  12. 'validators' => array(
  13. 'required' => 'required'
  14. )
  15. ),
  16. 'application_secret' =>array (
  17. 'friendlyName' => 'Application Secret',
  18. 'validators' => array(
  19. 'required' => 'required'
  20. )
  21. ),
  22. 'consumer_key' =>array (
  23. 'friendlyName' => 'Consumer Key',
  24. 'validators' => array(
  25. 'required' => 'required'
  26. )
  27. ),
  28. 'api_endpoint' => array(
  29. 'friendlyName' => 'Account Is',
  30. 'type' => 'select',
  31. 'options' => array( 'ovh-eu' => 'OVH-EU',
  32. 'ovh-ca' => 'OVH-CA',
  33. 'kimsufi-eu' => 'KIMSUFI-EU',
  34. 'kimsufi-ca' => 'KIMSUFI-CA',
  35. 'soyoustart-eu' => 'SOYOUSTART-EU',
  36. 'soyoustart-ca' => 'SOYOUSTART-CA',
  37. 'runabove-ca' => 'RUNABOVE-CA')
  38. ),
  39. 'minimized' => array(
  40. 'friendlyName' => 'Minimized',
  41. 'type' => 'yesno',
  42. 'help' => "Create only mandatory records"
  43. )
  44. );
  45. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV','SPF','CAA');
  46. protected $disabledPopulateNs = true;
  47. /**
  48. * @var OVH\Ovh;
  49. */
  50. private $api;
  51. public function loadApiInstance()
  52. {
  53. if(!$this->api)
  54. {
  55. $request = new OVH\Api(
  56. $this->config['application_key'],
  57. $this->config['application_secret'],
  58. $this->config['api_endpoint'],
  59. $this->config['consumer_key']);
  60. $this->api = new OVH\Ovh($request);
  61. return $this->api;
  62. }
  63. return $this->api;
  64. }
  65. public function getZones()
  66. {
  67. $out = array();
  68. $this->loadApiInstance();
  69. try
  70. {
  71. $zones = $this->api->listZones();
  72. }
  73. catch (\Exception $ex)
  74. {
  75. }
  76. foreach($zones as $zone)
  77. {
  78. $out[$zone] = '';
  79. }
  80. return $out;
  81. }
  82. public function activateZone()
  83. {
  84. $this->loadApiInstance();
  85. try
  86. {
  87. $result = $this->api->createZone(array(
  88. 'minimized' => $this->hasMinimizedOption(),
  89. 'zoneName' => $this->domain,
  90. ));
  91. $orderId = $result['orderId'];
  92. $paymentMethod = 'fidelityAccount';
  93. $this->api->payOrder($orderId, array(
  94. 'paymentMean' => $paymentMethod
  95. ));
  96. }
  97. catch (\Exception $ex)
  98. {
  99. $this->parseApiResponse($ex->getMessage());
  100. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  101. }
  102. return true;
  103. }
  104. public function addRecord(dns\record\Record $record)
  105. {
  106. $record->name = $this->toApiRecordName($record);
  107. $this->loadApiInstance();
  108. try
  109. {
  110. $this->api->addRecord($this->domain,array(
  111. 'fieldType' => $record->type,
  112. 'subDomain' => $record->name,
  113. 'target' => $record->rdata->toString(),
  114. 'ttl' => $record->ttl
  115. ));
  116. $this->api->refreshZone($this->domain);
  117. }
  118. catch (\Exception $ex)
  119. {
  120. throw new exceptions\DNSSubmoduleException($ex->getMessage());
  121. }
  122. return true;
  123. }
  124. public function deleteRecord(dns\record\Record $record)
  125. {
  126. $record->name = $this->toApiRecordName($record);
  127. $this->loadApiInstance();
  128. try
  129. {
  130. $this->api->deleteRecord($this->domain, $record->line);
  131. $this->api->refreshZone($this->domain);
  132. }
  133. catch (\Exception $ex)
  134. {
  135. throw new exceptions\DNSSubmoduleException($ex->getMessage());
  136. }
  137. return true;
  138. }
  139. public function editRecord(dns\record\Record $record)
  140. {
  141. $record->name = $this->toApiRecordName($record);
  142. $this->loadApiInstance();
  143. try
  144. {
  145. switch ($record->type)
  146. {
  147. case 'SOA':
  148. $this->editSOArecord($record);
  149. break;
  150. default :
  151. $this->api->updateRecord($this->domain, $record->line, array(
  152. 'target' => $record->rdata->toString(),
  153. 'ttl' => (int)$record->ttl,
  154. 'subDomain' => $record->name
  155. ));
  156. break;
  157. }
  158. }
  159. catch (\Exception $ex)
  160. {
  161. throw new exceptions\DNSSubmoduleException($ex->getMessage(),dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
  162. }
  163. return true;
  164. }
  165. public function getRecords($recordType = false)
  166. {
  167. $this->loadApiInstance();
  168. try
  169. {
  170. $this->checkZone();
  171. $recordsId = $this->api->getRecords($this->domain);
  172. $records = array();
  173. foreach($recordsId as $recordId)
  174. {
  175. $records[] = $this->api->getRecordProperties($this->domain, $recordId);
  176. }
  177. $out = [];
  178. foreach($records as $r)
  179. {
  180. $record = new dns\record\Record();
  181. $record->line = $r['id'];
  182. $record->name = $r['subDomain'];
  183. $record->type = $r['fieldType'];
  184. $record->ttl = $r['ttl'];
  185. $record->createRDATAObject();
  186. $record->rdata->fromString($r['target']);
  187. $out[] = $record;
  188. }
  189. if($this->hasRecordSupport('SOA'))
  190. {
  191. $soa = $this->api->getSOA($this->domain);
  192. $soaRecord = array();
  193. $soaRecord['mname'] = $soa['server'];
  194. $soaRecord['rname'] = $soa['email'];
  195. $soaRecord['minimum'] = $soa['ttl'];
  196. $soaRecord['retry'] = $soa['nxDomainTtl'];
  197. $soaRecord['serial'] = $soa['serial'];
  198. $soaRecord['expire'] = $soa['expire'];
  199. $soaRecord['refresh'] = $soa['refresh'];
  200. $record = new dns\record\Record();
  201. $record->name = $this->domain;
  202. $record->type = 'SOA';
  203. $record->ttl = $soaRecord['ttl'];
  204. $record->createRDATAObject();
  205. $record->rdata->setDataFromArray($soaRecord);
  206. $out[] = $record;
  207. }
  208. }
  209. catch (\Exception $ex)
  210. {
  211. throw new exceptions\DNSSubmoduleException($ex->getMessage(),dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  212. }
  213. return $out;
  214. }
  215. public function terminateZone()
  216. {
  217. $this->loadApiInstance();
  218. try
  219. {
  220. $this->api->terminateZone($this->domain);
  221. }
  222. catch (\Exception $ex)
  223. {
  224. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  225. }
  226. return true;
  227. }
  228. public function testConnection()
  229. {
  230. $this->loadApiInstance();
  231. try
  232. {
  233. $this->api->listZones();
  234. }
  235. catch (\Exception $ex)
  236. {
  237. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  238. }
  239. return true;
  240. }
  241. public function zoneExists()
  242. {
  243. $this->loadApiInstance();
  244. try
  245. {
  246. $this->checkZone();
  247. $zones = $this->api->listZones();
  248. }
  249. catch (\Exception $ex)
  250. {
  251. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  252. }
  253. foreach($zones as $zone)
  254. {
  255. if($zone == $this->domain)
  256. {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. public function hasMinimizedOption()
  263. {
  264. if($this->config['minimized'] == 'on')
  265. {
  266. return true;
  267. }
  268. return false;
  269. }
  270. private function editSOArecord(dns\record\Record $record)
  271. {
  272. return $this->api->updateSOA($this->domain,array(
  273. 'email' => $record->rdata->rname,
  274. 'nxDomainTtl' => $record->rdata->retry,
  275. 'refresh' => $record->rdata->refresh,
  276. 'ttl' => $record->rdata->minimum,
  277. 'serial' => $record->rdata->serial,
  278. 'server' => $record->rdata->mname,
  279. 'expire' => $record->rdata->expire
  280. ));
  281. }
  282. private function editDSrecord(dns\record\Record $record)
  283. {
  284. return $this->api->updateDSRecord($this->domain, array(
  285. 'keys' => array(
  286. 'algorithm' => $record->rdata->algorithm,
  287. 'flags' => $record->rdata->digesttype,
  288. 'publicKey' => $record->rdata->digest,
  289. 'tag' => $record->rdata->keytag
  290. )
  291. ));
  292. }
  293. private function checkZone()
  294. {
  295. try
  296. {
  297. $zone = \MGModule\DNSManager2\models\custom\zone\Repository::factory()->setFilter('name', $this->domain)->get()[0];
  298. $check = $this->api->getZoneInformation(array(
  299. 'minimized' => 'false',
  300. 'zoneName' => $this->domain
  301. ));
  302. }
  303. catch (\Exception $ex)
  304. {
  305. try
  306. {
  307. $check2 = $this->api->getZoneInformation2($this->domain);
  308. }
  309. catch (\Exception $ex)
  310. {
  311. if((int)$zone->status == 1)
  312. {
  313. throw new exceptions\DNSSubmoduleException('The zone probably is while processing. Please refresh the page in a few minutes.');
  314. }
  315. }
  316. return;
  317. }
  318. if($check['orderId'] == NULL && (int)$zone->status == 1)
  319. {
  320. throw new exceptions\DNSSubmoduleException('The zone probably is while processing. Please refresh the page in a few minutes.');
  321. }
  322. }
  323. private function parseApiResponse($response)
  324. {
  325. if(strpos($response, 'ObjectAlreadyExists') !== false)
  326. {
  327. throw new exceptions\DNSSubmoduleException('Zone server returns : The selected zone already exists.');
  328. }
  329. }
  330. /**
  331. * Returns valid api record name
  332. * @param dns\record\Record $record
  333. * @return string|string[]
  334. */
  335. private function toApiRecordName( dns\record\Record $record )
  336. {
  337. return str_replace('@', '', $record->nameToRelative($this->domain));
  338. }
  339. }