BxDolFile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. class BxDolFile extends BxDol implements iBxDolSingleton {
  10. protected $_sPathFrom;
  11. protected $_sPathTo;
  12. function __construct() {
  13. parent::__construct();
  14. $this->_sPathFrom = BX_DIRECTORY_PATH_ROOT;
  15. $this->_sPathTo = BX_DIRECTORY_PATH_ROOT;
  16. }
  17. /**
  18. * Prevent cloning the instance
  19. */
  20. public function __clone() {
  21. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  22. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  23. }
  24. /**
  25. * Get singleton instance of the class
  26. */
  27. public static function getInstance() {
  28. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  29. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolFile();
  30. return $GLOBALS['bxDolClasses'][__CLASS__];
  31. }
  32. function copy($sFilePathFrom, $sFilePathTo) {
  33. $sFilePathFrom = $this->_sPathFrom . $sFilePathFrom;
  34. $sFilePathTo = $this->_sPathTo . $sFilePathTo;
  35. return $this->_copyFile($sFilePathFrom, $sFilePathTo);
  36. }
  37. function delete($sPath) {
  38. $sPath = $this->_sPathTo . $sPath;
  39. return $this->_deleteDirectory($sPath);
  40. }
  41. function getPermissions($sPath) {
  42. $sPath = $this->_sPathTo . $sPath;
  43. return $this->_getPermissions($sPath);
  44. }
  45. function setPermissions($sPath, $sMode) {
  46. $sPath = $this->_sPathTo . $sPath;
  47. return $this->_setPermissions($sPath, $sMode);
  48. }
  49. protected function _copyFile($sFilePathFrom, $sFilePathTo) {
  50. if(file_exists($sFilePathTo))
  51. return true;
  52. if(substr($sFilePathFrom, -1) == '*')
  53. $sFilePathFrom = substr($sFilePathFrom, 0, -1);
  54. $bResult = true;
  55. if(is_file($sFilePathFrom)) {
  56. if($this->_isFile($sFilePathTo)) {
  57. $aFileParts = $this->_parseFile($sFilePathTo);
  58. if(isset($aFileParts[0]))
  59. @mkdir($this->_rStream, $aFileParts[0]);
  60. $bResult = @copy($sFilePathFrom, $sFilePathTo);
  61. }
  62. else if($this->_isDirectory($sFilePathTo)) {
  63. @mkdir($sFilePathTo);
  64. $aFileParts = $this->_parseFile($sFilePathFrom);
  65. if(isset($aFileParts[1]))
  66. $bResult = @copy($sFilePathFrom, $this->_validatePath($sFilePathTo) . $aFileParts[1]);
  67. }
  68. }
  69. else if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
  70. @mkdir($sFilePathTo);
  71. $aInnerFiles = $this->_readDirectory($sFilePathFrom);
  72. foreach($aInnerFiles as $sFile)
  73. $bResult = $this->_copyFile($this->_validatePath($sFilePathFrom) . $sFile, $this->_validatePath($sFilePathTo) . $sFile);
  74. }
  75. else
  76. $bResult = false;
  77. return $bResult;
  78. }
  79. protected function _readDirectory($sFilePath) {
  80. if(!is_dir($sFilePath) || !($rSource = opendir($sFilePath)))
  81. return false;
  82. $aResult = array();
  83. while(($sFile = readdir($rSource)) !== false) {
  84. if($sFile == '.' || $sFile =='..' || $sFile[0] == '.')
  85. continue;
  86. $aResult[] = $sFile;
  87. }
  88. closedir($rSource);
  89. return $aResult;
  90. }
  91. protected function _deleteDirectory($sPath) {
  92. if(!file_exists($sPath))
  93. return true;
  94. if($this->_isDirectory($sPath)) {
  95. if(substr($sPath, -1) != '/')
  96. $sPath .= '/';
  97. $aFiles = $this->_readDirectory($sPath);
  98. if(is_array($aFiles) && !empty($aFiles))
  99. foreach($aFiles as $sFile)
  100. $this->_deleteDirectory($sPath . $sFile);
  101. if(!@rmdir($sPath))
  102. return false;
  103. }
  104. else if(!@unlink($sPath))
  105. return false;
  106. return true;
  107. }
  108. protected function _validatePath($sPath) {
  109. if($sPath && substr($sPath, -1) != '/' && $this->_isDirectory($sPath))
  110. $sPath .= '/';
  111. return $sPath;
  112. }
  113. protected function _parseFile($sFilePath) {
  114. $aParts = array();
  115. preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+[\\\\\/])([a-zA-Z0-9~_-]+\.[a-zA-Z]{2,8})$/", $sFilePath, $aParts) ? true : false;
  116. return count($aParts) > 1 ? array_slice($aParts, 1) : false;
  117. }
  118. protected function _isFile($sFilePath) {
  119. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)\.([a-zA-Z]){2,8}$/", $sFilePath) ? true : false;
  120. }
  121. protected function _isDirectory($sFilePath) {
  122. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)[\\\\\/]([a-zA-Z0-9~_-]+)[\\\\\/]?$/", $sFilePath) ? true : false;
  123. }
  124. protected function _getPermissions($sPath) {
  125. clearstatcache();
  126. $hPerms = @fileperms($sPath);
  127. if($hPerms == false)
  128. return false;
  129. return substr(decoct($hPerms), -3);
  130. }
  131. protected function _setPermissions($sPath, $sMode) {
  132. $aConvert = array('writable' => 0666, 'executable' => 0777);
  133. return chmod($sPath, $aConvert[$sMode]);
  134. }
  135. }
  136. /** @} */