| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /**
- * Description of Records API
- * @author Vitalii Aloksa\
- *
- * Edited: Mateusz Pawłwoski <mateusz.pa@mdouelsgarden.com>
- */
- namespace MGModule\DNSManager2\mgLibs\ModuleAPI;
- use Exception;
- use MGModule\DNSManager2\mgLibs\custom\dns\record\Record;
- use MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
- use MGModule\DNSManager2\models\custom\dns;
- use MGModule\DNSManager2\models\custom\zone\Zone as ZoneModel;
- class Records extends Api
- {
- //put your code here
- public $data;
- protected $module;
- protected $zone;
- /*
- * !IMPORTANT
- *
- * this array is used to declare the variables, that will be used in API.
- * You may declare several api versions, that could have differnet names of api data values.
- * DO NOT change the keys, it is used in the code, and should not be changed,
- * only the values are the names, that will be looked for in the INPUT DATA (POST/GET)
- */
- public $dataFields = array(
- 'api1' => array(
- 'userid' => 'userid',
- 'zone_id' => 'zone_id',
- 'zone_name' => 'zone_name',
- 'zone_ip' => 'zone_ip',
- 'type' => 'type',
- 'modulename' => 'modulename',
- 'config' => 'config',
- 'recordSet' => 'recordSet',
- )
- );
- function __construct($data)
- {
- parent::__construct($data);
- $this->processZoneID();
- if (!isset($data['internal_use']) && $data['dnsaction'] != 'createZone')
- {
- $this->loadModule();
- }
- }
- /*
- * PREPARE METHODS
- */
- public function loadModule()
- {
- $this->laodSubmoduleFromZone();
- $this->loadSubmodule();
- if (!is_object($this->module))
- {
- throw new Exception(Response::errorSubmodule('notfound'));
- }
- }
- public function laodSubmoduleFromZone()
- {
- if ($this->module)
- {
- return $this->module;
- }
- if (!isset($this->data['zone_id']))
- {
- return false;
- }
- $zoneid = $this->data['zone_id'];
- Validator::isValidZoneID($zoneid);
- $ZoneModel = new ZoneModel($zoneid);
- $this->zone = $ZoneModel;
- $this->module = $ZoneModel->getModule();
- return $this->module;
- }
- public function loadSubmodule()
- {
- try
- {
- if ($this->module)
- {
- return $this->module;
- }
- $modulename = $this->data['modulename'];
- $config = $this->data['config'];
- Validator::isValidModuleConfig($modulename, $config);
- $this->module = dns\Core::getModule($modulename);
- $configuration = $this->getConfiguration();
- $this->module->setConfiguration($configuration);
- }
- catch (Exception $ex)
- {
- Response::proccessExceptionResponse($this->errors, $ex);
- }
- return $this->module;
- }
- public function processZoneID()
- {
- if (isset($this->data['zone_id']))
- {
- $this->data['zone_id'] = (int) $this->data['zone_id'];
- }
- }
- /*
- * API METHODS
- *
- * @author Mateusz Pawłwoski <mateusz.pa@mdouelsgarden.com>
- */
- public function update()
- {
- $this->checkRecords();
- $postRecords = $this->data['records'];
- $this->checkLines();
- foreach ($postRecords as $postRecord)
- {
- try
- {
- $newRecord = Record::tryToCreateFromArray($postRecord);
- if (is_array($postRecord['data']))
- {
- $newRecord->rdata->setDataFromArray($postRecord['data']);
- }
- else
- {
- $newRecord->rdata->fromString($postRecord['data']);
- }
- $newRecord->decode();
- $this->module->editRecord($newRecord);
- LogHelper::addSuccessLogUsingZone('api_updateRecord', 'Record: ' . $record->name . ' added', $this->zone);
- }
- catch (Exception $ex)
- {
- LogHelper::addFailLogUsingZone('api_updateRecord', $ex->getMessage(), $this->zone);
- throw $ex;
- }
- }
- return Response::successRecordsUpdate();
- }
- public function create()
- {
- $this->checkRecords();
- $postRecords = $this->data['records'];
- foreach ($postRecords as $postRecord)
- {
- try
- {
- $newRecord = Record::tryToCreateFromArray($postRecord);
- if (is_array($postRecord['data']))
- {
- $newRecord->rdata->setDataFromArray($postRecord['data']);
- }
- else
- {
- $newRecord->rdata->fromString($postRecord['data']);
- }
- $newRecord->decode();
- $record = $this->module->addRecord($newRecord);
- LogHelper::addSuccessLogUsingZone('api_addRecord', 'Record: ' . $record->name . ' added', $this->zone);
- }
- catch (Exception $ex)
- {
- LogHelper::addFailLogUsingZone('api_addRecord', $ex->getMessage(), $this->zone);
- throw $ex;
- }
- }
- return Response::successRecordsCreate();
- }
- public function remove()
- {
- $this->checkRecords();
- $postRecords = $this->data['records'];
- $this->checkLines();
- foreach ($postRecords as $postRecord)
- {
- try
- {
- $recordToRemove = new Record();
- $recordToRemove->line = ($postRecord['line']) ?: $postRecord['id'];
- $this->module->deleteRecord($recordToRemove);
- LogHelper::addSuccessLogUsingZone('api_deleteRecord', 'Record: ' . $record->name . ' removed', $this->zone);
- }
- catch (Exception $ex)
- {
- LogHelper::addFailLogUsingZone('api_deleteRecord', $ex->getMessage(), $this->zone);
- throw $ex;
- }
- }
- return Response::successRecordsRemove();
- }
- /*
- * API Validators
- *
- * @author Mateusz Pawłwoski <mateusz.pa@mdouelsgarden.com>
- */
- public function checkLines()
- {
- $postRecords = $this->data['records'];
- $recordsLine = $this->getExistedRecordsLines();
- foreach ($postRecords as $postRecord)
- {
- if (isset($postRecord['line']) || isset($postRecord['id']))
- {
- $recordLine = ($postRecord['line']) ?: $postRecord['id'];
- if (in_array($recordLine, $recordsLine))
- {
- continue;
- }
- throw new Exception('Record #' . $recordLine . ' does not exist.');
- }
- throw new Exception('Missing id parameter in one of the records.');
- }
- return true;
- }
- private function getExistedRecordsLines()
- {
- $records = $this->module->getRecords();
- $recordsLine = [];
- foreach ($records as $record)
- {
- $recordsLine[] = $record->line;
- }
- return $recordsLine;
- }
-
- private function checkRecords(){
- $postRecords = $this->data['records'];
-
- if(empty($postRecords)){
- throw new Exception('Records field cannot be empty.');
- }
- if(!is_array($postRecords)){
- throw new Exception('Records field, must be an array.');
- }
- }
- }
|