BxDolFile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. class BxDolFile extends BxDol 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) && $this->_isDirectory($sFilePathTo)) {
  78. $this->_mkDirR($sFilePathTo);
  79. $bResult = true;
  80. $aInnerFiles = $this->_readDirectory($sFilePathFrom);
  81. foreach($aInnerFiles as $sFile)
  82. $bResult = $bResult && $this->_copyFile($this->_validatePath($sFilePathFrom, false) . $sFile, $this->_validatePath($sFilePathTo) . $sFile);
  83. return $bResult;
  84. }
  85. return false;
  86. }
  87. protected function _readDirectory($sFilePath)
  88. {
  89. if(!is_dir($sFilePath) || !($rSource = opendir($sFilePath)))
  90. return false;
  91. $aResult = array();
  92. while(($sFile = readdir($rSource)) !== false) {
  93. if($sFile == '.' || $sFile =='..')
  94. continue;
  95. $aResult[] = $sFile;
  96. }
  97. closedir($rSource);
  98. return $aResult;
  99. }
  100. protected function _deleteDirectory($sPath)
  101. {
  102. if(!file_exists($sPath))
  103. return true;
  104. if(is_dir($sPath)) {
  105. $sPath = $this->_validatePath($sPath, false);
  106. $aFiles = $this->_readDirectory($sPath);
  107. if(is_array($aFiles) && !empty($aFiles))
  108. foreach($aFiles as $sFile)
  109. $this->_deleteDirectory($sPath . $sFile);
  110. if(!@rmdir($sPath))
  111. return false;
  112. } else if(!@unlink($sPath))
  113. return false;
  114. return true;
  115. }
  116. protected function _validatePath($sPath, $bAbstract = true)
  117. {
  118. return $sPath . ($sPath && !$this->_isEndWithSlash($sPath) && ($bAbstract ? $this->_isDirectory($sPath) : is_dir($sPath)) ? '/' : '');
  119. }
  120. protected function _parseFile($sFilePath)
  121. {
  122. return array(dirname($sFilePath), basename($sFilePath));
  123. }
  124. protected function _isFile($sFilePath)
  125. {
  126. if($this->_isEndWithSlash($sFilePath))
  127. return false;
  128. $aInfo = pathinfo($sFilePath);
  129. if(!isset($aInfo['extension']))
  130. return false;
  131. return true;
  132. }
  133. protected function _isDirectory($sFilePath)
  134. {
  135. return !$this->_isFile($sFilePath);
  136. }
  137. protected function _isEndWithSlash($sFilePath)
  138. {
  139. return in_array(substr($sFilePath, -1), array('/', '\\'));
  140. }
  141. protected function _getPermissions($sPath)
  142. {
  143. clearstatcache();
  144. $hPerms = @fileperms($sPath);
  145. if($hPerms == false)
  146. return false;
  147. return substr(decoct($hPerms), -3);
  148. }
  149. protected function _setPermissions($sPath, $sMode)
  150. {
  151. $aConvert = array('writable' => 0666, 'executable' => 0777);
  152. return @chmod($sPath, $aConvert[$sMode]);
  153. }
  154. protected function _mkDirR($sPath)
  155. {
  156. return @mkdir($sPath, 0777, true);
  157. }
  158. }
  159. /** @} */