1
0

BxDolFile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolFile extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_sPathFrom;
  12. protected $_sPathTo;
  13. protected function __construct()
  14. {
  15. parent::__construct();
  16. $this->_sPathFrom = BX_DIRECTORY_PATH_ROOT;
  17. $this->_sPathTo = BX_DIRECTORY_PATH_ROOT;
  18. }
  19. /**
  20. * Prevent cloning the instance
  21. */
  22. public function __clone()
  23. {
  24. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  25. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  26. }
  27. /**
  28. * Get singleton instance of the class
  29. */
  30. public static function getInstance()
  31. {
  32. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  33. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolFile();
  34. return $GLOBALS['bxDolClasses'][__CLASS__];
  35. }
  36. function copy($sFilePathFrom, $sFilePathTo)
  37. {
  38. if(substr($sFilePathFrom, 0, strlen($this->_sPathFrom)) != $this->_sPathFrom)
  39. $sFilePathFrom = $this->_sPathFrom . $sFilePathFrom;
  40. if(substr($sFilePathTo, 0, strlen($this->_sPathTo)) != $this->_sPathTo)
  41. $sFilePathTo = $this->_sPathTo . $sFilePathTo;
  42. return $this->_copyFile($sFilePathFrom, $sFilePathTo);
  43. }
  44. function delete($sPath)
  45. {
  46. $sPath = $this->_sPathTo . $sPath;
  47. return $this->_deleteDirectory($sPath);
  48. }
  49. function getPermissions($sPath)
  50. {
  51. $sPath = $this->_sPathTo . $sPath;
  52. return $this->_getPermissions($sPath);
  53. }
  54. function setPermissions($sPath, $sMode)
  55. {
  56. $sPath = $this->_sPathTo . $sPath;
  57. return $this->_setPermissions($sPath, $sMode);
  58. }
  59. protected function _copyFile($sFilePathFrom, $sFilePathTo)
  60. {
  61. if(substr($sFilePathFrom, -1) == '*')
  62. $sFilePathFrom = substr($sFilePathFrom, 0, -1);
  63. //--- Copy file to file/folder
  64. if(is_file($sFilePathFrom)) {
  65. if(is_dir($sFilePathTo)) {
  66. $aFileParts = $this->_parseFile($sFilePathFrom);
  67. return @copy($sFilePathFrom, $this->_validatePath($sFilePathTo) . $aFileParts[1]);
  68. }
  69. else {
  70. $aFileParts = $this->_parseFile($sFilePathTo);
  71. if(isset($aFileParts[0]))
  72. $this->_mkDirR($aFileParts[0]);
  73. return @copy($sFilePathFrom, $sFilePathTo);
  74. }
  75. }
  76. //--- Copy directory to directory
  77. if(is_dir($sFilePathFrom)) {
  78. $bFilePathTo = file_exists($sFilePathTo);
  79. if($bFilePathTo && !is_dir($sFilePathTo))
  80. return false;
  81. if(!$bFilePathTo && !$this->_mkDirR($sFilePathTo))
  82. return false;
  83. $bResult = true;
  84. $aInnerFiles = $this->_readDirectory($sFilePathFrom);
  85. foreach($aInnerFiles as $sFile)
  86. $bResult = $bResult && $this->_copyFile($this->_validatePath($sFilePathFrom, false) . $sFile, $this->_validatePath($sFilePathTo, false) . $sFile);
  87. return $bResult;
  88. }
  89. return false;
  90. }
  91. protected function _readDirectory($sFilePath)
  92. {
  93. if(!is_dir($sFilePath) || !($rSource = opendir($sFilePath)))
  94. return false;
  95. $aResult = array();
  96. while(($sFile = readdir($rSource)) !== false) {
  97. if($sFile == '.' || $sFile =='..')
  98. continue;
  99. $aResult[] = $sFile;
  100. }
  101. closedir($rSource);
  102. return $aResult;
  103. }
  104. protected function _deleteDirectory($sPath)
  105. {
  106. if(!file_exists($sPath))
  107. return true;
  108. if(is_dir($sPath)) {
  109. $sPath = $this->_validatePath($sPath, false);
  110. $aFiles = $this->_readDirectory($sPath);
  111. if(is_array($aFiles) && !empty($aFiles))
  112. foreach($aFiles as $sFile)
  113. $this->_deleteDirectory($sPath . $sFile);
  114. if(!@rmdir($sPath))
  115. return false;
  116. } else if(!@unlink($sPath))
  117. return false;
  118. return true;
  119. }
  120. protected function _validatePath($sPath, $bAbstract = true)
  121. {
  122. return $sPath . ($sPath && !$this->_isEndWithSlash($sPath) && ($bAbstract ? $this->_isDirectory($sPath) : is_dir($sPath)) ? '/' : '');
  123. }
  124. protected function _parseFile($sFilePath)
  125. {
  126. return array(dirname($sFilePath), basename($sFilePath));
  127. }
  128. protected function _isFile($sFilePath)
  129. {
  130. if($this->_isEndWithSlash($sFilePath))
  131. return false;
  132. $aInfo = pathinfo($sFilePath);
  133. if(!isset($aInfo['extension']))
  134. return false;
  135. return true;
  136. }
  137. protected function _isDirectory($sFilePath)
  138. {
  139. return !$this->_isFile($sFilePath);
  140. }
  141. protected function _isEndWithSlash($sFilePath)
  142. {
  143. return in_array(substr($sFilePath, -1), array('/', '\\'));
  144. }
  145. protected function _getPermissions($sPath)
  146. {
  147. clearstatcache();
  148. $hPerms = @fileperms($sPath);
  149. if($hPerms == false)
  150. return false;
  151. return substr(decoct($hPerms), -3);
  152. }
  153. protected function _setPermissions($sPath, $sMode)
  154. {
  155. $aConvert = array('writable' => 0666, 'executable' => 0777);
  156. return @chmod($sPath, $aConvert[$sMode]);
  157. }
  158. protected function _mkDirR($sPath)
  159. {
  160. return @mkdir($sPath, 0777, true);
  161. }
  162. }
  163. /** @} */