| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\helpers;
- use \MGModule\DNSManager2 as main;
- use \MGModule\DNSManager2\models\custom\zone;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- class ImportExportFileHelper
- {
- public static $skipFileList = array(
- '.htaccess',
- 'index.html'
- );
-
- public static function exportZoneToFile($zoneId, $path, $fileName = false)
- {
- $zone = new zone\Zone($zoneId);
- $newFileName = $fileName ? $fileName : self::generateNewExportFileName($zone->name);
- $fileManager = new main\mgLibs\custom\FileManager($path);
-
- $importInfo = array();
- $importInfo['zoneName'] = $zone->name;
-
- $module = $zone->getModule();
- if(!$module->zoneExists())
- {
- throw new \Exception(main\mgLibs\lang::T('you_cannot_edit_this_zone_because_it_is_terminated_on_server'));
- }
- else
- {
- $importInfo['zoneRecords'] = $module->getRecords();
- }
-
- return $fileManager->saveDataToFile(json_encode($importInfo, JSON_PRETTY_PRINT), $newFileName);
- }
-
- public static function listFilesForZone($zone, $fileManager)
- {
- $files = $fileManager->getFilesFromStorage();
- foreach($files as $key => $filename)
- {
- $nameParts = explode('_', $filename);
- if($nameParts[0] !== $zone->name || in_array($filename, self::$skipFileList))
- {
- unset($files[$key]);
- }
- }
-
- return $files;
- }
-
- public static function loadFileContent($path, $fileName)
- {
- if(!$fileName)
- {
- throw new \Exception('You have to select a file');
- }
-
- $fileManager = new main\mgLibs\custom\FileManager($path);
-
- $cont = $fileManager->loadFileContent($fileName);
- if(!$cont)
- {
- return false;
- }
-
- $backupZone = json_decode($cont);
- return $backupZone;
- }
- public static function generateClientFileName($clientId)
- {
- return date("Y.m.d_H.i.s").'_'. $clientId;
- }
- public static function generateNewExportFileName($associateName)
- {
- return $associateName.'_'.date("Y.m.d_H.i.s");
- }
-
- public static function bulkImportZoneToFile($zone, $path, $fileName = false)
- {
-
- $newFileName = $fileName ? $fileName : self::generateNewExportFileName($zone->name);
- $fileManager = new main\mgLibs\custom\FileManager($path);
-
- $importInfo = array();
- $importInfo['zoneName'] = $zone->name;
-
- $module = $zone->getModule();
- if(!$module->zoneExists())
- {
- throw new \Exception(main\mgLibs\lang::T('you_cannot_edit_this_zone_because_it_is_terminated_on_server'));
- }
- else
- {
- $importInfo['zoneRecords'] = $module->getRecords();
- }
-
- $content = $fileManager->loadFileContent($newFileName);
- $decodedContent = json_decode($content);
- if(!$decodedContent)
- {
- $newContent = array();
- $newContent[$zone->name] = $importInfo;
- }
- else
- {
- $decodedContent->{$zone->name} = $importInfo;
- $newContent = $decodedContent;
- }
-
-
- return $fileManager->saveDataToFile(json_encode($newContent, JSON_PRETTY_PRINT), $newFileName, true);
- }
-
- public static function listFilesForBulkExport($fileManager, $serverName = false, $order = 0)
- {
- $files = $fileManager->getFilesFromStorage($order);
- foreach($files as $key => $filename)
- {
- $nameParts = explode('_', $filename);
- if($serverName && $nameParts[0] !== $serverName)
- {
- unset($files[$key]);
- }
-
- if($filename === '.' || $filename === '..' || in_array($filename, self::$skipFileList))
- {
- unset($files[$key]);
- }
- }
-
- return $files;
- }
-
- public static function listBulkExportZones($fileName, $path)
- {
- $fileManager = new main\mgLibs\custom\FileManager($path);
-
- $content = $fileManager->loadFileContent($fileName);
- $decodedContent = json_decode($content);
-
- return $decodedContent;
- }
- }
|