FileManager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom;
  3. use MGModule\DNSManager2 as main;
  4. class FileManager
  5. {
  6. private $storagePath;
  7. public function __construct($storagePath)
  8. {
  9. if($storagePath && file_exists($storagePath))
  10. {
  11. $this->storagePath = $storagePath.DIRECTORY_SEPARATOR;
  12. }
  13. else
  14. {
  15. $this->storagePath = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'storage';
  16. if($storagePath)
  17. {
  18. $relativePath = $this->storagePath.DIRECTORY_SEPARATOR.trim($storagePath, DIRECTORY_SEPARATOR);
  19. if(file_exists($relativePath))
  20. {
  21. $this->storagePath = $relativePath.DIRECTORY_SEPARATOR;
  22. }
  23. }
  24. }
  25. }
  26. public function isStorageWritable()
  27. {
  28. return is_writable($this->storagePath);
  29. }
  30. public function isStorageReadable()
  31. {
  32. return is_readable($this->storagePath);
  33. }
  34. public function getStoragePath()
  35. {
  36. return $this->storagePath;
  37. }
  38. public function saveDataToFile($data, $fileName, $allowOverwrite = false)
  39. {
  40. $newFilename = $allowOverwrite ? $fileName : $this->parseFileName($fileName);
  41. return file_put_contents($this->storagePath.$newFilename, $data);
  42. }
  43. public function parseFileName($basicName)
  44. {
  45. if(!file_exists($this->storagePath.$basicName))
  46. {
  47. return $basicName;
  48. }
  49. $vNumber = 1;
  50. while(file_exists($this->storagePath.$basicName.'_'.$vNumber))
  51. {
  52. $vNumber++;
  53. }
  54. return $basicName.'_'.$vNumber;
  55. }
  56. public function getFilesFromStorage($order = 0)
  57. {
  58. return scandir($this->storagePath,$order);
  59. }
  60. public function loadFileContent($fileName)
  61. {
  62. if(!file_exists($this->storagePath.$fileName))
  63. {
  64. return false;
  65. }
  66. return file_get_contents($this->storagePath.$fileName);
  67. }
  68. public function fileExists($fileName)
  69. {
  70. if(file_exists($this->storagePath.$fileName))
  71. {
  72. return true;
  73. }
  74. return false;
  75. }
  76. public function deleteFile($fileName)
  77. {
  78. if(file_exists($this->storagePath.$fileName) && $this->checkIfProperBackupStoragePatch($this->storagePath.$fileName))
  79. {
  80. return unlink($this->storagePath.$fileName);
  81. }
  82. return false;
  83. }
  84. private function checkIfProperBackupStoragePatch($patch)
  85. {
  86. if(strpos($patch, 'DNSManager2'.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'zonesFilesStorage'))
  87. {
  88. return true;
  89. }
  90. return false;
  91. }
  92. public static function removeFakePath($fileName)
  93. {
  94. if(stripos($fileName, '\\'))
  95. {
  96. $parts = explode('\\', $fileName);
  97. return end($parts);
  98. }
  99. if(stripos($fileName, DIRECTORY_SEPARATOR))
  100. {
  101. $parts = explode(DIRECTORY_SEPARATOR, $fileName);
  102. return end($parts);
  103. }
  104. return $fileName;
  105. }
  106. public function uploadFile($fileName)
  107. {
  108. $tmpFile = self::findUploadedFile($fileName);
  109. return move_uploaded_file($tmpFile["tmp_name"], $this->storagePath.$fileName);
  110. }
  111. public static function checkIfFileWasSent($fileName)
  112. {
  113. return self::findUploadedFile($fileName) ? true : false;
  114. }
  115. public static function findUploadedFile($fileName)
  116. {
  117. foreach($_FILES as $file)
  118. {
  119. if($file['name'] === $fileName)
  120. {
  121. return $file;
  122. }
  123. }
  124. return false;
  125. }
  126. public static function checkTypeByContent($fileName)
  127. {
  128. $tmpFile = self::findUploadedFile($fileName);
  129. $cont = file_get_contents($tmpFile["tmp_name"]);
  130. $parsedCont = json_decode($cont, true);
  131. if($parsedCont['zoneName'])
  132. {
  133. return 'single';
  134. }
  135. $first = array_shift($parsedCont);
  136. if($first['zoneName'])
  137. {
  138. return 'bulk';
  139. }
  140. return false;
  141. }
  142. }