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; } }