MyDNSHostAPI.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. <?php
  2. /**
  3. * Class to interact with mydnshost api.
  4. */
  5. class MyDNSHostAPI {
  6. /** Base URL. */
  7. private $baseurl = 'https://api.mydnshost.co.uk/';
  8. /** API Version. */
  9. private $version = '1.0';
  10. /** Auth Data. */
  11. private $auth = FALSE;
  12. /** Are we impersonating someone? */
  13. private $impersonate = FALSE;
  14. /** Are we impersonating an email address or an ID? */
  15. private $impersonateType = FALSE;
  16. /** Are we accessing domain functions with admin override? */
  17. private $domainAdmin = FALSE;
  18. /** Debug mode value. */
  19. private $debug = FALSE;
  20. /** Last API Response */
  21. private $lastResponse = NULL;
  22. /** Our device ID. */
  23. private $deviceID = NULL;
  24. /** Our device Name. */
  25. private $deviceName = NULL;
  26. /**
  27. * Create a new MyDNSHostAPI
  28. *
  29. * @param $baseurl Base URL to connect to.
  30. */
  31. public function __construct($baseurl) {
  32. $this->baseurl = $baseurl;
  33. }
  34. /**
  35. * Enable/disable debug mode
  36. *
  37. * @param $value New debug value
  38. * @return $this for chaining.
  39. */
  40. public function setDebug($value) {
  41. $this->debug = $value;
  42. return $this;
  43. }
  44. /**
  45. * Are we in debug mode?
  46. *
  47. * @return True/False if in debugging mode.
  48. */
  49. public function isDebug() {
  50. return $this->debug;
  51. }
  52. /**
  53. * Auth using a username and password.
  54. *
  55. * @param $user User to auth with
  56. * @param $pass Password to auth with
  57. * @param $key (Optional) 2FA Key for login.
  58. * @return $this for chaining.
  59. */
  60. public function setAuthUserPass($user, $pass, $key = NULL) {
  61. $this->auth = ['type' => 'userpass', 'user' => $user, 'pass' => $pass, '2fa' => $key];
  62. return $this;
  63. }
  64. /**
  65. * Auth using 2FA Push.
  66. * This isn't a real auth type, but will get us a 2fa code to use.
  67. *
  68. * @param $user User to auth with
  69. * @param $pass Password to auth with
  70. * @return $this for chaining.
  71. */
  72. public function doAuth2FAPush($user, $pass) {
  73. $auth = ['type' => 'userpass', 'user' => $user, 'pass' => $pass, '2fa_push' => true];
  74. return $this->api('/session', 'GET', [], $auth);
  75. }
  76. /**
  77. * Auth using a username and api key.
  78. *
  79. * @param $user User to auth with
  80. * @param $key Key to auth with
  81. * @return $this for chaining.
  82. */
  83. public function setAuthUserKey($user, $key) {
  84. $this->auth = ['type' => 'userkey', 'user' => $user, 'key' => $key];
  85. return $this;
  86. }
  87. /**
  88. * Auth using a domain and domain key.
  89. *
  90. * @param $domain Domain to auth with
  91. * @param $key Key to auth with
  92. * @return $this for chaining.
  93. */
  94. public function setAuthDomainKey($domain, $key) {
  95. $this->auth = ['type' => 'domainkey', 'domain' => $domain, 'key' => $key];
  96. return $this;
  97. }
  98. /**
  99. * Auth using a session ID.
  100. *
  101. * @param $sessionid ID to auth with
  102. * @return $this for chaining.
  103. */
  104. public function setAuthSession($sessionid) {
  105. $this->auth = ['type' => 'session', 'sessionid' => $sessionid];
  106. return $this;
  107. }
  108. /**
  109. * Auth using a custom auth method.
  110. *
  111. * @param $auth Auth data.
  112. * @return $this for chaining.
  113. */
  114. public function setAuth($auth) {
  115. $this->auth = $auth;
  116. return $this;
  117. }
  118. /**
  119. * Set device ID
  120. *
  121. * @param $id Device ID
  122. * @return $this for chaining.
  123. */
  124. public function setDeviceID($id) {
  125. $this->deviceID = $id;
  126. return $this;
  127. }
  128. /**
  129. * Set device Name
  130. *
  131. * @param $name Device Name
  132. * @return $this for chaining.
  133. */
  134. public function setDeviceName($name) {
  135. $this->deviceName = $name;
  136. return $this;
  137. }
  138. /**
  139. * Impersonate a user
  140. *
  141. * @param $user User to impersonate
  142. * @param $type (Default: email) Is $user an email or id?
  143. * @return $this for chaining.
  144. */
  145. public function impersonate($user, $type = 'email') {
  146. $this->impersonate = $user;
  147. $this->impersonateType = $type;
  148. return $this;
  149. }
  150. /**
  151. * Ping the API
  152. *
  153. * @param $time [Optional] Time to send as extra param.
  154. * @return Data from API
  155. */
  156. public function ping($time = NULL) {
  157. $result = $time === NULL ? $this->api('/ping', 'GET') : $this->api('/ping/' . $time, 'GET');
  158. return isset($result['response']) ? $result['response'] : NULL;
  159. }
  160. /**
  161. * Get version info from the API.
  162. *
  163. * @return Array of user data or null if we are not authed.
  164. */
  165. public function getVersion() {
  166. $result = $this->api('/version');
  167. return isset($result['response']) ? $result['response'] : NULL;
  168. }
  169. /**
  170. * Register a new account.
  171. *
  172. * @param $email Email address
  173. * @param $name Real name
  174. * @param $acceptTerms Do we accept the terms of registration?
  175. * @return Data from API
  176. */
  177. public function register($email, $name, $acceptTerms = false) {
  178. $registrationData = ['email' => $email, 'realname' => $name, 'acceptterms' => $acceptTerms];
  179. return $this->api('/register', 'POST', $registrationData);
  180. }
  181. /**
  182. * Confirm account registration
  183. *
  184. * @param $user User ID
  185. * @param $code Verify Code
  186. * @param $password Requested Password
  187. * @return Data from API
  188. */
  189. public function registerConfirm($user, $code, $password) {
  190. return $this->api('/register/confirm/' . $user, 'POST', ['code' => $code, 'password' => $password]);
  191. }
  192. /**
  193. * Resend welcome email.
  194. *
  195. * @param $user User ID
  196. * @return Data from API
  197. */
  198. public function resendWelcome($userid) {
  199. return $this->api('/users/' . $userid . '/resendwelcome', 'POST', []);
  200. }
  201. /**
  202. * Accept the terms of service.
  203. *
  204. * @param $user (Optional) User ID - defaults to self.
  205. * @return Data from API
  206. */
  207. public function acceptTerms($userid = 'self') {
  208. return $this->api('/users/' . $userid . '/acceptterms', 'POST', ['acceptterms' => "true"]);
  209. }
  210. /**
  211. * Submit a password reset request
  212. *
  213. * @param $email Email address
  214. * @return Data from API
  215. */
  216. public function forgotpassword($email) {
  217. return $this->api('/forgotpassword', 'POST', ['email' => $email]);
  218. }
  219. /**
  220. * Confirm a password reset request
  221. *
  222. * @param $user User ID
  223. * @param $code Verify Code
  224. * @param $password Requested Password
  225. * @return Data from API
  226. */
  227. public function forgotpasswordConfirm($user, $code, $password) {
  228. return $this->api('/forgotpassword/confirm/' . $user, 'POST', ['code' => $code, 'password' => $password]);
  229. }
  230. /**
  231. * Check if we have valid auth details.
  232. *
  233. * @return True if we can auth successfully.
  234. */
  235. public function validAuth() {
  236. if ($this->auth === FALSE) {
  237. return FALSE;
  238. }
  239. return $this->getUserData() !== NULL;
  240. }
  241. /**
  242. * Get information about the user we are authed as and our current
  243. * access level.
  244. *
  245. * @return Array of user data or null if we are not authed.
  246. */
  247. public function getUserData() {
  248. if ($this->auth === FALSE) { return NULL; }
  249. $result = $this->api('/userdata');
  250. return isset($result['response']) ? $result['response'] : NULL;
  251. }
  252. /**
  253. * Get information about all users we can see.
  254. *
  255. * @return Result from the API.
  256. */
  257. public function getUsers() {
  258. if ($this->auth === FALSE) { return NULL; }
  259. $result = $this->api('/users');
  260. return $result;
  261. }
  262. /**
  263. * Get system info.
  264. *
  265. * @return Result from the API.
  266. */
  267. public function getSystemDataValue($key) {
  268. if (empty($key)) { return NULL; }
  269. $result = $this->api('/system/datavalue/' . $key);
  270. return isset($result['response'][$key]) ? $result['response'][$key] : NULL;
  271. }
  272. /**
  273. * Get system stats.
  274. *
  275. * @return Result from the API.
  276. */
  277. public function getSystemStats($type, $options) {
  278. if ($this->auth === FALSE) { return []; }
  279. $result = $this->api('/system/stats/' . $type, 'GET', $options);
  280. return isset($result['response']['stats']) ? $result['response']['stats'] : [];
  281. }
  282. /**
  283. * Get users statistics
  284. *
  285. * @param $type Statistics type.
  286. * @param $options Options to pass to statistics.
  287. * @param $userid User ID to get data for (Default: 'self')
  288. * @return Array of stats.
  289. */
  290. public function getUserStats($type, $options = [], $userID = 'self') {
  291. if ($this->auth === FALSE) { return NULL; }
  292. $result = $this->api('/users/' . $userID . '/stats/' . $type, 'GET', $options);
  293. return isset($result['response']['stats']) ? $result['response']['stats'] : [];
  294. }
  295. /**
  296. * Set information a given user id.
  297. *
  298. * @param $userid User ID to get data for (Default: 'self')
  299. * @return Result from the api
  300. */
  301. public function getUserInfo($userID = 'self') {
  302. if ($this->auth === FALSE) { return NULL; }
  303. $result = $this->api('/users/' . $userID);
  304. return isset($result['response']) ? $result['response'] : NULL;
  305. }
  306. /**
  307. * Update information about the user we are authed as
  308. *
  309. * @param $data Data to use for the update
  310. * @param $userid User ID to edit (Default: 'self')
  311. * @return Result from the api
  312. */
  313. public function setUserInfo($data, $userID = 'self') {
  314. if ($this->auth === FALSE) { return []; }
  315. return $this->api('/users/' . $userID, 'POST', $data);
  316. }
  317. /**
  318. * Create a new user
  319. *
  320. * @param $data for the create operation
  321. * @return Result from the api
  322. */
  323. public function createUser($data) {
  324. if ($this->auth === FALSE) { return []; }
  325. return $this->api('/users/create', 'POST', $data);
  326. }
  327. /**
  328. * Delete the given user id.
  329. *
  330. * @param $userid User ID to delete.
  331. * @return Result from the api
  332. */
  333. public function deleteUser($userID) {
  334. if ($this->auth === FALSE) { return []; }
  335. return $this->api('/users/' . $userID, 'DELETE');
  336. }
  337. /**
  338. * Confirm delete the given user id
  339. *
  340. * @param $userid User ID to delete.
  341. * @param $confirmCode Confirmation code.
  342. * @param $twoFactorCode Optional twofactor code.
  343. * @return Result from the api
  344. */
  345. public function deleteUserConfirm($userID, $confirmCode, $twoFactorCode = '') {
  346. if ($this->auth === FALSE) { return []; }
  347. return $this->api('/users/' . $userID . '/confirm/' . $confirmCode . (!empty($twoFactorCode) ? '/' . $twoFactorCode : ''), 'DELETE');
  348. }
  349. /**
  350. * Get API Keys for a user
  351. *
  352. * @param $userid User ID to get keys for
  353. * @return Array of api keys.
  354. */
  355. public function getAPIKeys($userid = 'self') {
  356. if ($this->auth === FALSE) { return NULL; }
  357. $result = $this->api('/users/' . $userid . '/keys');
  358. return isset($result['response']) ? $result['response'] : (isset($result['error']) ? NULL : []);
  359. }
  360. /**
  361. * Create a new API Key.
  362. *
  363. * @param $data Data to use for the create
  364. * @param $userid User ID to create key for
  365. * @return Result of create operation.
  366. */
  367. public function createAPIKey($data, $userid = 'self') {
  368. if ($this->auth === FALSE) { return []; }
  369. return $this->api('/users/' . $userid . '/keys', 'POST', $data);
  370. }
  371. /**
  372. * Update an API Key.
  373. *
  374. * @param $key Key to update
  375. * @param $data Data to use for the update
  376. * @param $userid User ID to update key for
  377. * @return Result of update operation.
  378. */
  379. public function updateAPIKey($key, $data, $userid = 'self') {
  380. if ($this->auth === FALSE) { return []; }
  381. return $this->api('/users/' . $userid . '/keys/' . $key, 'POST', $data);
  382. }
  383. /**
  384. * Delete a new API Key.
  385. *
  386. * @param $key Key to delete
  387. * @param $userid User ID to delete key for
  388. * @return Result of delete operation.
  389. */
  390. public function deleteAPIKey($key, $userid = 'self') {
  391. if ($this->auth === FALSE) { return []; }
  392. return $this->api('/users/' . $userid . '/keys/' . $key, 'DELETE');
  393. }
  394. /**
  395. * Get 2FA Devices for the current user
  396. *
  397. * @param $userid User ID to get devices for
  398. * @return Array of 2FA devices.
  399. */
  400. public function get2FADevices($userid = 'self') {
  401. if ($this->auth === FALSE) { return NULL; }
  402. $result = $this->api('/users/' . $userid . '/2fadevices');
  403. return isset($result['response']) ? $result['response'] : (isset($result['error']) ? NULL : []);
  404. }
  405. /**
  406. * Delete a 2FA Device.
  407. *
  408. * @param $key Device to delete
  409. * @param $userid User ID to delete device for
  410. * @return Result of delete operation.
  411. */
  412. public function delete2FADevice($device, $userid = 'self') {
  413. if ($this->auth === FALSE) { return []; }
  414. return $this->api('/users/' . $userid . '/2fadevices/' . $device, 'DELETE');
  415. }
  416. /**
  417. * Get 2FA Keys for the current user
  418. *
  419. * @param $userid User ID to get keys for
  420. * @return Array of 2FA keys.
  421. */
  422. public function get2FAKeys($userid = 'self') {
  423. if ($this->auth === FALSE) { return NULL; }
  424. $result = $this->api('/users/' . $userid . '/2fa');
  425. return isset($result['response']) ? $result['response'] : (isset($result['error']) ? NULL : []);
  426. }
  427. /**
  428. * Create a new 2FA Key.
  429. *
  430. * @param $data Data to use for the create
  431. * @param $userid User ID to create key for
  432. * @return Result of create operation.
  433. */
  434. public function create2FAKey($data, $userid = 'self') {
  435. if ($this->auth === FALSE) { return []; }
  436. return $this->api('/users/' . $userid . '/2fa', 'POST', $data);
  437. }
  438. /**
  439. * Update a 2FA Key.
  440. *
  441. * @param $key Key to update
  442. * @param $data Data to use for the update
  443. * @param $userid User ID to update key for
  444. * @return Result of update operation.
  445. */
  446. public function update2FAKey($key, $data, $userid = 'self') {
  447. if ($this->auth === FALSE) { return []; }
  448. return $this->api('/users/' . $userid . '/2fa/' . $key, 'POST', $data);
  449. }
  450. /**
  451. * Verify a 2FA Key.
  452. *
  453. * @param $key Key to verify
  454. * @param $code Code to verify with
  455. * @param $userid User ID to verify key for
  456. * @return Result of update operation.
  457. */
  458. public function verify2FAKey($key, $code, $userid = 'self') {
  459. if ($this->auth === FALSE) { return []; }
  460. return $this->api('/users/' . $userid . '/2fa/' . $key . '/verify', 'POST', ['code' => $code]);
  461. }
  462. /**
  463. * Delete a 2FA Key.
  464. *
  465. * @param $key Key to delete
  466. * @param $userid User ID to delete key for
  467. * @return Result of delete operation.
  468. */
  469. public function delete2FAKey($key, $userid = 'self') {
  470. if ($this->auth === FALSE) { return []; }
  471. return $this->api('/users/' . $userid . '/2fa/' . $key, 'DELETE');
  472. }
  473. /**
  474. * Get Custom Data for the current user
  475. *
  476. * @param $userid User ID to get data for
  477. * @return Array of Custom Data.
  478. */
  479. public function getCustomDataList($userid = 'self') {
  480. if ($this->auth === FALSE) { return NULL; }
  481. $result = $this->api('/users/' . $userid . '/customdata');
  482. return isset($result['response']) ? $result['response'] : (isset($result['error']) ? NULL : []);
  483. }
  484. /**
  485. * Create/Update a Custom Data Value.
  486. *
  487. * @param $key Key to create/update
  488. * @param $value Data to use for the create/update
  489. * @param $userid User ID to create/update for
  490. * @return Result of update operation.
  491. */
  492. public function setCustomData($key, $value, $userid = 'self') {
  493. if ($this->auth === FALSE) { return []; }
  494. return $this->api('/users/' . $userid . '/customdata/' . $key, 'POST', ['value' => $value]);
  495. }
  496. /**
  497. * Get a Custom Data Value.
  498. *
  499. * @param $key Key to get
  500. * @param $userid User ID to get for
  501. * @return Result of update operation.
  502. */
  503. public function getCustomData($key, $userid = 'self') {
  504. if ($this->auth === FALSE) { return []; }
  505. $result = $this->api('/users/' . $userid . '/customdata/' . $key, 'GET');
  506. return isset($result['response']['value']) ? $result['response']['value'] : NULL;
  507. }
  508. /**
  509. * Delete a Custom Data Value.
  510. *
  511. * @param $key Key to delete
  512. * @param $userid User ID to delete key for
  513. * @return Result of delete operation.
  514. */
  515. public function deleteCustomData($key, $userid = 'self') {
  516. if ($this->auth === FALSE) { return []; }
  517. return $this->api('/users/' . $userid . '/customdata/' . $key, 'DELETE');
  518. }
  519. /**
  520. * Get a session ID from the backend
  521. *
  522. * @return Backend session ID or null if we are not authed.
  523. */
  524. public function getSessionID() {
  525. if ($this->auth === FALSE) { return NULL; }
  526. $result = $this->api('/session');
  527. return isset($result['response']['session']) ? $result['response']['session'] : NULL;
  528. }
  529. /**
  530. * Delete our session
  531. *
  532. * @return Result of delete operation.
  533. */
  534. public function deleteSession() {
  535. if ($this->auth === FALSE) { return NULL; }
  536. $result = $this->api('/session', 'DELETE');
  537. return isset($result['response']['session']) ? $result['response']['session'] : NULL;
  538. }
  539. /**
  540. * Enable or disable domain admin-override.
  541. *
  542. * @param $value (Default: true) Set value for domain admin override.
  543. */
  544. public function domainAdmin($value = true) {
  545. $this->domainAdmin = true;
  546. return $this;
  547. }
  548. /**
  549. * Get list of our domains.
  550. *
  551. * @return Array of domains or an empty array.
  552. */
  553. public function getDomains() {
  554. if ($this->auth === FALSE) { return []; }
  555. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains');
  556. return isset($result['response']) ? $result['response'] : [];
  557. }
  558. /**
  559. * Create a domain.
  560. *
  561. * @param $domain Domain to create.
  562. * @param $owner (Default: NULL) Who to set as owner (if null, self);
  563. * @return Result from the API
  564. */
  565. public function createDomain($domain, $owner = NULL) {
  566. if ($this->auth === FALSE) { return []; }
  567. $data = ['domain' => $domain];
  568. if ($owner !== null) {
  569. $data['owner'] = $owner;
  570. }
  571. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains', 'POST', $data);
  572. }
  573. /**
  574. * Delete a domain.
  575. *
  576. * @param $domain Domain to delete.
  577. * @return Result from the API
  578. */
  579. public function deleteDomain($domain) {
  580. if ($this->auth === FALSE) { return []; }
  581. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain, 'DELETE');
  582. }
  583. /**
  584. * Get domain data for a given domain.
  585. *
  586. * @param $domain Domain to get data for
  587. * @return Array of domains or an empty array.
  588. */
  589. public function getDomainData($domain) {
  590. if ($this->auth === FALSE) { return []; }
  591. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain);
  592. return isset($result['response']) ? $result['response'] : NULL;
  593. }
  594. /**
  595. * Set domain data for a given domain.
  596. *
  597. * @param $domain Domain to set data for
  598. * @param $data Data to set
  599. * @return Result from the API
  600. */
  601. public function setDomainData($domain, $data) {
  602. if ($this->auth === FALSE) { return []; }
  603. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain, 'POST', $data);
  604. }
  605. /**
  606. * Get domain access for a given domain.
  607. *
  608. * @param $domain Domain to get access-data for
  609. * @return Array of access info or an empty array.
  610. */
  611. public function getDomainAccess($domain) {
  612. if ($this->auth === FALSE) { return []; }
  613. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/access');
  614. return isset($result['response']) ? $result['response'] : [];
  615. }
  616. /**
  617. * Set domain access for a given domain.
  618. *
  619. * @param $domain Domain to set access-data for
  620. * @param $data New access data
  621. * @return Response from the API
  622. */
  623. public function setDomainAccess($domain, $data) {
  624. if ($this->auth === FALSE) { return []; }
  625. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/access', 'POST', $data);
  626. }
  627. /**
  628. * Get domain statistics
  629. *
  630. * @param $domain Domain to get stats for.
  631. * @param $options Options to pass to statistics.
  632. * @return Array of stats.
  633. */
  634. public function getDomainStats($domain, $options = []) {
  635. if ($this->auth === FALSE) { return []; }
  636. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/stats', 'GET', $options);
  637. return isset($result['response']['stats']) ? $result['response']['stats'] : [];
  638. }
  639. /**
  640. * Get domain logs
  641. *
  642. * @param $domain Domain to get logs for.
  643. * @return Array of logs.
  644. */
  645. public function getDomainLogs($domain) {
  646. if ($this->auth === FALSE) { return []; }
  647. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/logs', 'GET', $options);
  648. return isset($result['response']) ? $result['response'] : [];
  649. }
  650. /**
  651. * Attempt to sync the domain to the backends.
  652. *
  653. * @param $domain Domain to export.
  654. * @return Array of records or an empty array.
  655. */
  656. public function syncDomain($domain) {
  657. if ($this->auth === FALSE) { return []; }
  658. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/sync');
  659. }
  660. /**
  661. * Export domain as bind zone file.
  662. *
  663. * @param $domain Domain to export.
  664. * @return Array of records or an empty array.
  665. */
  666. public function exportZone($domain) {
  667. if ($this->auth === FALSE) { return []; }
  668. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/export');
  669. return isset($result['response']['zone']) ? $result['response']['zone'] : [];
  670. }
  671. /**
  672. * Import domain from bind zone file.
  673. *
  674. * @param $domain Domain to import.
  675. * @param $zone Zonefile data
  676. * @return Array of records or an empty array.
  677. */
  678. public function importZone($domain, $zone) {
  679. if ($this->auth === FALSE) { return []; }
  680. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/import', 'POST', ['zone' => $zone]);
  681. }
  682. /**
  683. * Get domain records for a given domain.
  684. *
  685. * @param $domain Domain to get records for
  686. * @return Array of records or an empty array.
  687. */
  688. public function getDomainRecords($domain) {
  689. if ($this->auth === FALSE) { return []; }
  690. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/records');
  691. return isset($result['response']) ? $result['response'] : [];
  692. }
  693. /**
  694. * Get domain record for a given domain by id
  695. *
  696. * @param $domain Domain to get record for
  697. * @param $id Record ID to get
  698. * @return Array of records or an empty array.
  699. */
  700. public function getDomainRecord($domain, $id) {
  701. if ($this->auth === FALSE) { return []; }
  702. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/records/' . $id);
  703. return isset($result['response']) ? $result['response'] : [];
  704. }
  705. /**
  706. * Get domain records for a given domain filtered by name
  707. *
  708. * @param $domain Domain to get records for
  709. * @param $name Record name to get
  710. * @param $type (Optional) optional type to limit to
  711. * @return Array of records or an empty array.
  712. */
  713. public function getDomainRecordsByName($domain, $name, $type = null) {
  714. if ($this->auth === FALSE) { return []; }
  715. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/record/' . $name . ($type != null ? '/' . $type : ''));
  716. return isset($result['response']['records']) ? $result['response']['records'] : [];
  717. }
  718. /**
  719. * Set domain records for a given domain.
  720. *
  721. * @param $domain Domain to set records for
  722. * @param $data Data to set
  723. * @return Result from API
  724. */
  725. public function setDomainRecords($domain, $data) {
  726. if ($this->auth === FALSE) { return []; }
  727. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/records', 'POST', $data);
  728. }
  729. /**
  730. * Set domain records for a given domain.
  731. *
  732. * @param $domain Domain to set record for
  733. * @param $id Record ID to set
  734. * @param $data Data to set
  735. * @return Result from API
  736. */
  737. public function setDomainRecord($domain, $id, $data) {
  738. if ($this->auth === FALSE) { return []; }
  739. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/records/' . $id, 'POST', $data);
  740. }
  741. /**
  742. * Delete records for a given domain.
  743. *
  744. * @param $domain Domain to delete records for
  745. * @return Result from API
  746. */
  747. public function deleteDomainRecords($domain) {
  748. if ($this->auth === FALSE) { return []; }
  749. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/records', 'DELETE');
  750. return $result['response'];
  751. }
  752. /**
  753. * Delete a record for a given domain.
  754. *
  755. * @param $domain Domain to delete record for
  756. * @param $id Record ID to delete
  757. * @return Result from API
  758. */
  759. public function deleteDomainRecord($domain, $id) {
  760. if ($this->auth === FALSE) { return []; }
  761. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/records/' . $id, 'DELETE');
  762. return $result['response'];
  763. }
  764. /**
  765. * Delete records for a given domain.
  766. *
  767. * @param $domain Domain to delete records for
  768. * @param $name Record name to delete
  769. * @param $type (Optional) optional type to limit delete to
  770. * @return Result from API
  771. */
  772. public function deleteDomainRecordsByName($domain, $name, $type = null) {
  773. if ($this->auth === FALSE) { return []; }
  774. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/record/' . $name . ($type != null ? '/' . $type : ''), 'DELETE');
  775. return $result['response'];
  776. }
  777. /**
  778. * Get Domain Keys for the given domain
  779. *
  780. * @param $domain Domain to get keys for
  781. * @return Array of domain keys.
  782. */
  783. public function getDomainKeys($domain) {
  784. if ($this->auth === FALSE) { return NULL; }
  785. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/keys');
  786. return isset($result['response']) ? $result['response'] : (isset($result['error']) ? NULL : []);
  787. }
  788. /**
  789. * Create a new Domain Key.
  790. *
  791. * @param $domain Domain to create key for
  792. * @param $data Data to use for the create
  793. * @return Result of create operation.
  794. */
  795. public function createDomainKey($domain, $data) {
  796. if ($this->auth === FALSE) { return []; }
  797. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/keys', 'POST', $data);
  798. }
  799. /**
  800. * Update a Domain Key.
  801. *
  802. * @param $domain Domain to update key for
  803. * @param $key Key to update
  804. * @param $data Data to use for the update
  805. * @return Result of update operation.
  806. */
  807. public function updateDomainKey($domain, $key, $data) {
  808. if ($this->auth === FALSE) { return []; }
  809. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/keys/' . $key, 'POST', $data);
  810. }
  811. /**
  812. * Delete a Domain Key.
  813. *
  814. * @param $domain Domain to delete key for
  815. * @param $key Key to delete
  816. * @return Result of delete operation.
  817. */
  818. public function deleteDomainKey($domain, $key) {
  819. if ($this->auth === FALSE) { return []; }
  820. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/keys/' . $key, 'DELETE');
  821. }
  822. /**
  823. * Get Domain Hooks for the given domain
  824. *
  825. * @param $domain Domain to get hooks for
  826. * @return Array of domain hooks.
  827. */
  828. public function getDomainHooks($domain) {
  829. if ($this->auth === FALSE) { return NULL; }
  830. $result = $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/hooks');
  831. return isset($result['response']) ? $result['response'] : (isset($result['error']) ? NULL : []);
  832. }
  833. /**
  834. * Create a new Domain Hook.
  835. *
  836. * @param $domain Domain to create hook for
  837. * @param $data Data to use for the create
  838. * @return Result of create operation.
  839. */
  840. public function createDomainHook($domain, $data) {
  841. if ($this->auth === FALSE) { return []; }
  842. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/hooks', 'POST', $data);
  843. }
  844. /**
  845. * Update a Domain Hook.
  846. *
  847. * @param $domain Domain to update hook for
  848. * @param $hookid Hook ID to update
  849. * @param $data Data to use for the update
  850. * @return Result of update operation.
  851. */
  852. public function updateDomainHook($domain, $hookid, $data) {
  853. if ($this->auth === FALSE) { return []; }
  854. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/hooks/' . $hookid, 'POST', $data);
  855. }
  856. /**
  857. * Delete a Domain Hook.
  858. *
  859. * @param $domain Domain to delete hook for
  860. * @param $hookid hookid to delete
  861. * @return Result of delete operation.
  862. */
  863. public function deleteDomainHook($domain, $hookid) {
  864. if ($this->auth === FALSE) { return []; }
  865. return $this->api(($this->domainAdmin ? '/admin' : '') . '/domains/' . $domain . '/hooks/' . $hookid, 'DELETE');
  866. }
  867. /**
  868. * Get articles.
  869. *
  870. * @return Result from the API.
  871. */
  872. public function getArticles() {
  873. $result = $this->api('/articles');
  874. return isset($result['response']) ? $result['response'] : [];
  875. }
  876. /**
  877. * Get all articles.
  878. *
  879. * @return Result from the API.
  880. */
  881. public function getAllArticles() {
  882. if ($this->auth === FALSE) { return []; }
  883. $result = $this->api('/admin/articles');
  884. return isset($result['response']) ? $result['response'] : [];
  885. }
  886. /**
  887. * Crate a new article.
  888. *
  889. * @param $data Data for create
  890. * @return Result from the API.
  891. */
  892. public function createArticle($data) {
  893. if ($this->auth === FALSE) { return []; }
  894. return $this->api('/admin/articles', 'POST', $data);
  895. }
  896. /**
  897. * Get a specific article.
  898. *
  899. * @param $articleid Article to get
  900. * @return Result from the API.
  901. */
  902. public function getArticle($articleid) {
  903. if ($this->auth === FALSE) { return []; }
  904. $result = $this->api('/admin/articles/' . $articleid);
  905. return isset($result['response']) ? $result['response'] : [];
  906. }
  907. /**
  908. * Update a specific article.
  909. *
  910. * @param $articleid Article to update
  911. * @param $data Data for update
  912. * @return Result from the API.
  913. */
  914. public function updateArticle($articleid, $data) {
  915. if ($this->auth === FALSE) { return []; }
  916. return $this->api('/admin/articles/' . $articleid, 'POST', $data);
  917. }
  918. /**
  919. * Delete a specific article.
  920. *
  921. * @param $articleid Article to delete
  922. * @return Result from the API.
  923. */
  924. public function deleteArticle($articleid) {
  925. if ($this->auth === FALSE) { return []; }
  926. return $this->api('/admin/articles/' . $articleid, 'DELETE');
  927. }
  928. /**
  929. * Get the last response from the API
  930. *
  931. * @return Last API Response.
  932. */
  933. public function getLastResponse() {
  934. return $this->lastResponse;
  935. }
  936. /**
  937. * Poke the API.
  938. *
  939. * @param $apimethod API Method to poke
  940. * @param $method Request method to access the API with
  941. * @param $data (Default: []) Data to send if POST
  942. * @param $auth (Default: []) Custom auth data to use for just this request.
  943. * @return Response from the API as an array.
  944. */
  945. public function api($apimethod, $method = 'GET', $data = [], $auth = NULL) {
  946. $headers = [];
  947. $options = [];
  948. if ($auth == NULL) { $auth = $this->auth; }
  949. if ($auth !== FALSE) {
  950. if ($auth['type'] == 'session') {
  951. $headers['X-SESSION-ID'] = $auth['sessionid'];
  952. } else if ($auth['type'] == 'userkey') {
  953. $headers['X-API-USER'] = $auth['user'];
  954. $headers['X-API-KEY'] = $auth['key'];
  955. } else if ($auth['type'] == 'domainkey') {
  956. $headers['X-DOMAIN'] = $auth['domain'];
  957. $headers['X-DOMAIN-KEY'] = $auth['key'];
  958. } else if ($auth['type'] == 'userpass') {
  959. $options['auth'] = [$auth['user'], $auth['pass']];
  960. if (isset($auth['2fa'])) {
  961. $headers['X-2FA-KEY'] = $auth['2fa'];
  962. }
  963. if (isset($auth['2fa_push'])) {
  964. $headers['X-2FA-PUSH'] = $auth['2fa_push'];
  965. }
  966. }
  967. }
  968. if ($this->deviceID !== NULL) { $headers['X-2FA-DEVICE-ID'] = $this->deviceID; }
  969. if ($this->deviceName !== NULL) { $headers['X-2FA-SAVE-DEVICE'] = $this->deviceName; }
  970. if ($this->impersonate !== FALSE) {
  971. if ($this->impersonateType == 'id') {
  972. $headers['X-IMPERSONATE-ID'] = $this->impersonate;
  973. } else {
  974. $headers['X-IMPERSONATE'] = $this->impersonate;
  975. }
  976. }
  977. $url = sprintf('%s/%s/%s', rtrim($this->baseurl, '/'), $this->version, ltrim($apimethod, '/'));
  978. try {
  979. if ($method == 'GET') {
  980. if (count($data) > 0) {
  981. $url = parse_url($url);
  982. if (isset($url['query'])) {
  983. $query = [];
  984. parse_str($url['query'], $query);
  985. $url['query'] = http_build_query(array_merge($query, $data));
  986. } else {
  987. $url['query'] = http_build_query($data);
  988. }
  989. $url = $this->unparse_url($url);
  990. }
  991. $response = Requests::get($url, $headers, $options);
  992. } else if ($method == 'POST') {
  993. $response = Requests::post($url, $headers, json_encode(['data' => $data]), $options);
  994. } else if ($method == 'DELETE') {
  995. $response = Requests::delete($url, $headers, $options);
  996. }
  997. $data = @json_decode($response->body, TRUE);
  998. } catch (Requests_Exception $ex) {
  999. $data = NULL;
  1000. }
  1001. if ($data == NULL) {
  1002. $data = ['error' => 'There was an unknown error.'];
  1003. }
  1004. if ($this->isDebug()) {
  1005. $debug = ['request' => '', 'response' => $response->body];
  1006. if ($method == 'POST') {
  1007. $debug['request'] = json_encode(['data' => $data]);
  1008. }
  1009. $data['__DEBUG'] = $debug;
  1010. }
  1011. $this->lastResponse = $data;
  1012. return $data;
  1013. }
  1014. /**
  1015. * Take an array from parse_url and turn it back into a URL.
  1016. *
  1017. * @param $parsed_url Array from parse_url.
  1018. * @return String representing the URL.
  1019. */
  1020. function unparse_url($parsed_url) {
  1021. $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  1022. $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  1023. $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  1024. $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  1025. $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
  1026. $pass = ($user || $pass) ? "$pass@" : '';
  1027. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  1028. $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
  1029. $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  1030. return "$scheme$user$pass$host$port$path$query$fragment";
  1031. }
  1032. }