*/ 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 */ 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 */ 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.'); } } }