Records.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Description of Records API
  4. * @author Vitalii Aloksa\
  5. *
  6. * Edited: Mateusz Pawłwoski <mateusz.pa@mdouelsgarden.com>
  7. */
  8. namespace MGModule\DNSManager2\mgLibs\ModuleAPI;
  9. use Exception;
  10. use MGModule\DNSManager2\mgLibs\custom\dns\record\Record;
  11. use MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
  12. use MGModule\DNSManager2\models\custom\dns;
  13. use MGModule\DNSManager2\models\custom\zone\Zone as ZoneModel;
  14. class Records extends Api
  15. {
  16. //put your code here
  17. public $data;
  18. protected $module;
  19. protected $zone;
  20. /*
  21. * !IMPORTANT
  22. *
  23. * this array is used to declare the variables, that will be used in API.
  24. * You may declare several api versions, that could have differnet names of api data values.
  25. * DO NOT change the keys, it is used in the code, and should not be changed,
  26. * only the values are the names, that will be looked for in the INPUT DATA (POST/GET)
  27. */
  28. public $dataFields = array(
  29. 'api1' => array(
  30. 'userid' => 'userid',
  31. 'zone_id' => 'zone_id',
  32. 'zone_name' => 'zone_name',
  33. 'zone_ip' => 'zone_ip',
  34. 'type' => 'type',
  35. 'modulename' => 'modulename',
  36. 'config' => 'config',
  37. 'recordSet' => 'recordSet',
  38. )
  39. );
  40. function __construct($data)
  41. {
  42. parent::__construct($data);
  43. $this->processZoneID();
  44. if (!isset($data['internal_use']) && $data['dnsaction'] != 'createZone')
  45. {
  46. $this->loadModule();
  47. }
  48. }
  49. /*
  50. * PREPARE METHODS
  51. */
  52. public function loadModule()
  53. {
  54. $this->laodSubmoduleFromZone();
  55. $this->loadSubmodule();
  56. if (!is_object($this->module))
  57. {
  58. throw new Exception(Response::errorSubmodule('notfound'));
  59. }
  60. }
  61. public function laodSubmoduleFromZone()
  62. {
  63. if ($this->module)
  64. {
  65. return $this->module;
  66. }
  67. if (!isset($this->data['zone_id']))
  68. {
  69. return false;
  70. }
  71. $zoneid = $this->data['zone_id'];
  72. Validator::isValidZoneID($zoneid);
  73. $ZoneModel = new ZoneModel($zoneid);
  74. $this->zone = $ZoneModel;
  75. $this->module = $ZoneModel->getModule();
  76. return $this->module;
  77. }
  78. public function loadSubmodule()
  79. {
  80. try
  81. {
  82. if ($this->module)
  83. {
  84. return $this->module;
  85. }
  86. $modulename = $this->data['modulename'];
  87. $config = $this->data['config'];
  88. Validator::isValidModuleConfig($modulename, $config);
  89. $this->module = dns\Core::getModule($modulename);
  90. $configuration = $this->getConfiguration();
  91. $this->module->setConfiguration($configuration);
  92. }
  93. catch (Exception $ex)
  94. {
  95. Response::proccessExceptionResponse($this->errors, $ex);
  96. }
  97. return $this->module;
  98. }
  99. public function processZoneID()
  100. {
  101. if (isset($this->data['zone_id']))
  102. {
  103. $this->data['zone_id'] = (int) $this->data['zone_id'];
  104. }
  105. }
  106. /*
  107. * API METHODS
  108. *
  109. * @author Mateusz Pawłwoski <mateusz.pa@mdouelsgarden.com>
  110. */
  111. public function update()
  112. {
  113. $this->checkRecords();
  114. $postRecords = $this->data['records'];
  115. $this->checkLines();
  116. foreach ($postRecords as $postRecord)
  117. {
  118. try
  119. {
  120. $newRecord = Record::tryToCreateFromArray($postRecord);
  121. if (is_array($postRecord['data']))
  122. {
  123. $newRecord->rdata->setDataFromArray($postRecord['data']);
  124. }
  125. else
  126. {
  127. $newRecord->rdata->fromString($postRecord['data']);
  128. }
  129. $newRecord->decode();
  130. $this->module->editRecord($newRecord);
  131. LogHelper::addSuccessLogUsingZone('api_updateRecord', 'Record: ' . $record->name . ' added', $this->zone);
  132. }
  133. catch (Exception $ex)
  134. {
  135. LogHelper::addFailLogUsingZone('api_updateRecord', $ex->getMessage(), $this->zone);
  136. throw $ex;
  137. }
  138. }
  139. return Response::successRecordsUpdate();
  140. }
  141. public function create()
  142. {
  143. $this->checkRecords();
  144. $postRecords = $this->data['records'];
  145. foreach ($postRecords as $postRecord)
  146. {
  147. try
  148. {
  149. $newRecord = Record::tryToCreateFromArray($postRecord);
  150. if (is_array($postRecord['data']))
  151. {
  152. $newRecord->rdata->setDataFromArray($postRecord['data']);
  153. }
  154. else
  155. {
  156. $newRecord->rdata->fromString($postRecord['data']);
  157. }
  158. $newRecord->decode();
  159. $record = $this->module->addRecord($newRecord);
  160. LogHelper::addSuccessLogUsingZone('api_addRecord', 'Record: ' . $record->name . ' added', $this->zone);
  161. }
  162. catch (Exception $ex)
  163. {
  164. LogHelper::addFailLogUsingZone('api_addRecord', $ex->getMessage(), $this->zone);
  165. throw $ex;
  166. }
  167. }
  168. return Response::successRecordsCreate();
  169. }
  170. public function remove()
  171. {
  172. $this->checkRecords();
  173. $postRecords = $this->data['records'];
  174. $this->checkLines();
  175. foreach ($postRecords as $postRecord)
  176. {
  177. try
  178. {
  179. $recordToRemove = new Record();
  180. $recordToRemove->line = ($postRecord['line']) ?: $postRecord['id'];
  181. $this->module->deleteRecord($recordToRemove);
  182. LogHelper::addSuccessLogUsingZone('api_deleteRecord', 'Record: ' . $record->name . ' removed', $this->zone);
  183. }
  184. catch (Exception $ex)
  185. {
  186. LogHelper::addFailLogUsingZone('api_deleteRecord', $ex->getMessage(), $this->zone);
  187. throw $ex;
  188. }
  189. }
  190. return Response::successRecordsRemove();
  191. }
  192. /*
  193. * API Validators
  194. *
  195. * @author Mateusz Pawłwoski <mateusz.pa@mdouelsgarden.com>
  196. */
  197. public function checkLines()
  198. {
  199. $postRecords = $this->data['records'];
  200. $recordsLine = $this->getExistedRecordsLines();
  201. foreach ($postRecords as $postRecord)
  202. {
  203. if (isset($postRecord['line']) || isset($postRecord['id']))
  204. {
  205. $recordLine = ($postRecord['line']) ?: $postRecord['id'];
  206. if (in_array($recordLine, $recordsLine))
  207. {
  208. continue;
  209. }
  210. throw new Exception('Record #' . $recordLine . ' does not exist.');
  211. }
  212. throw new Exception('Missing id parameter in one of the records.');
  213. }
  214. return true;
  215. }
  216. private function getExistedRecordsLines()
  217. {
  218. $records = $this->module->getRecords();
  219. $recordsLine = [];
  220. foreach ($records as $record)
  221. {
  222. $recordsLine[] = $record->line;
  223. }
  224. return $recordsLine;
  225. }
  226. private function checkRecords(){
  227. $postRecords = $this->data['records'];
  228. if(empty($postRecords)){
  229. throw new Exception('Records field cannot be empty.');
  230. }
  231. if(!is_array($postRecords)){
  232. throw new Exception('Records field, must be an array.');
  233. }
  234. }
  235. }