| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom;
- use MGModule\DNSManager2 as main;
- class FileManager
- {
- private $storagePath;
-
- public function __construct($storagePath)
- {
- if($storagePath && file_exists($storagePath))
- {
- $this->storagePath = $storagePath.DIRECTORY_SEPARATOR;
- }
- else
- {
- $this->storagePath = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'storage';
- if($storagePath)
- {
- $relativePath = $this->storagePath.DIRECTORY_SEPARATOR.trim($storagePath, DIRECTORY_SEPARATOR);
- if(file_exists($relativePath))
- {
- $this->storagePath = $relativePath.DIRECTORY_SEPARATOR;
- }
- }
- }
- }
-
- public function isStorageWritable()
- {
- return is_writable($this->storagePath);
- }
-
- public function isStorageReadable()
- {
- return is_readable($this->storagePath);
- }
-
- public function getStoragePath()
- {
- return $this->storagePath;
- }
-
- public function saveDataToFile($data, $fileName, $allowOverwrite = false)
- {
- $newFilename = $allowOverwrite ? $fileName : $this->parseFileName($fileName);
- return file_put_contents($this->storagePath.$newFilename, $data);
- }
-
- public function parseFileName($basicName)
- {
- if(!file_exists($this->storagePath.$basicName))
- {
- return $basicName;
- }
-
- $vNumber = 1;
- while(file_exists($this->storagePath.$basicName.'_'.$vNumber))
- {
- $vNumber++;
- }
-
- return $basicName.'_'.$vNumber;
- }
-
- public function getFilesFromStorage($order = 0)
- {
- return scandir($this->storagePath,$order);
- }
-
- public function loadFileContent($fileName)
- {
- if(!file_exists($this->storagePath.$fileName))
- {
- return false;
- }
-
- return file_get_contents($this->storagePath.$fileName);
- }
-
- public function fileExists($fileName)
- {
- if(file_exists($this->storagePath.$fileName))
- {
- return true;
- }
- return false;
- }
-
- public function deleteFile($fileName)
- {
- if(file_exists($this->storagePath.$fileName) && $this->checkIfProperBackupStoragePatch($this->storagePath.$fileName))
- {
- return unlink($this->storagePath.$fileName);
- }
- return false;
- }
- private function checkIfProperBackupStoragePatch($patch)
- {
- if(strpos($patch, 'DNSManager2'.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'zonesFilesStorage'))
- {
- return true;
- }
-
- return false;
- }
-
- public static function removeFakePath($fileName)
- {
- if(stripos($fileName, '\\'))
- {
- $parts = explode('\\', $fileName);
-
- return end($parts);
- }
- if(stripos($fileName, DIRECTORY_SEPARATOR))
- {
- $parts = explode(DIRECTORY_SEPARATOR, $fileName);
-
- return end($parts);
- }
-
- return $fileName;
- }
-
- public function uploadFile($fileName)
- {
- $tmpFile = self::findUploadedFile($fileName);
- return move_uploaded_file($tmpFile["tmp_name"], $this->storagePath.$fileName);
- }
-
- public static function checkIfFileWasSent($fileName)
- {
- return self::findUploadedFile($fileName) ? true : false;
- }
-
- public static function findUploadedFile($fileName)
- {
- foreach($_FILES as $file)
- {
- if($file['name'] === $fileName)
- {
- return $file;
- }
- }
-
- return false;
- }
-
- public static function checkTypeByContent($fileName)
- {
- $tmpFile = self::findUploadedFile($fileName);
- $cont = file_get_contents($tmpFile["tmp_name"]);
- $parsedCont = json_decode($cont, true);
- if($parsedCont['zoneName'])
- {
- return 'single';
- }
- $first = array_shift($parsedCont);
- if($first['zoneName'])
- {
- return 'bulk';
- }
- return false;
- }
- }
|