BxDolFile.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. var $_sPathFrom;
  11. var $_sPathTo;
  12. function BxDolFile() {
  13. parent::BxDol();
  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. 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. protected function _copyFile($sFilePathFrom, $sFilePathTo) {
  42. if(file_exists($sFilePathTo))
  43. return true;
  44. if(substr($sFilePathFrom, -1) == '*')
  45. $sFilePathFrom = substr($sFilePathFrom, 0, -1);
  46. $bResult = true;
  47. if(is_file($sFilePathFrom)) {
  48. if($this->_isFile($sFilePathTo)) {
  49. $aFileParts = $this->_parseFile($sFilePathTo);
  50. if(isset($aFileParts[0]))
  51. @mkdir($this->_rStream, $aFileParts[0]);
  52. $bResult = @copy($sFilePathFrom, $sFilePathTo);
  53. }
  54. else if($this->_isDirectory($sFilePathTo)) {
  55. @mkdir($sFilePathTo);
  56. $aFileParts = $this->_parseFile($sFilePathFrom);
  57. if(isset($aFileParts[1]))
  58. $bResult = @copy($sFilePathFrom, $this->_validatePath($sFilePathTo) . $aFileParts[1]);
  59. }
  60. }
  61. else if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
  62. @mkdir($sFilePathTo);
  63. $aInnerFiles = $this->_readDirectory($sFilePathFrom);
  64. foreach($aInnerFiles as $sFile)
  65. $bResult = $this->_copyFile($this->_validatePath($sFilePathFrom) . $sFile, $this->_validatePath($sFilePathTo) . $sFile);
  66. }
  67. else
  68. $bResult = false;
  69. return $bResult;
  70. }
  71. protected function _readDirectory($sFilePath) {
  72. if(!is_dir($sFilePath) || !($rSource = opendir($sFilePath)))
  73. return false;
  74. $aResult = array();
  75. while(($sFile = readdir($rSource)) !== false) {
  76. if($sFile == '.' || $sFile =='..' || $sFile[0] == '.')
  77. continue;
  78. $aResult[] = $sFile;
  79. }
  80. closedir($rSource);
  81. return $aResult;
  82. }
  83. protected function _deleteDirectory($sPath) {
  84. if(!file_exists($sPath))
  85. return true;
  86. if($this->_isDirectory($sPath)) {
  87. if(substr($sPath, -1) != '/')
  88. $sPath .= '/';
  89. $aFiles = $this->_readDirectory($sPath);
  90. if(is_array($aFiles) && !empty($aFiles))
  91. foreach($aFiles as $sFile)
  92. $this->_deleteDirectory($sPath . $sFile);
  93. if(!@rmdir($sPath))
  94. return false;
  95. }
  96. else if(!@unlink($sPath))
  97. return false;
  98. return true;
  99. }
  100. protected function _validatePath($sPath) {
  101. if($sPath && substr($sPath, -1) != '/' && $this->_isDirectory($sPath))
  102. $sPath .= '/';
  103. return $sPath;
  104. }
  105. protected function _parseFile($sFilePath) {
  106. $aParts = array();
  107. preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+[\\\\\/])([a-zA-Z0-9~_-]+\.[a-zA-Z]{2,8})$/", $sFilePath, $aParts) ? true : false;
  108. return count($aParts) > 1 ? array_slice($aParts, 1) : false;
  109. }
  110. protected function _isFile($sFilePath) {
  111. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)\.([a-zA-Z]){2,8}$/", $sFilePath) ? true : false;
  112. }
  113. protected function _isDirectory($sFilePath) {
  114. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)[\\\\\/]([a-zA-Z0-9~_-]+)[\\\\\/]?$/", $sFilePath) ? true : false;
  115. }
  116. }
  117. /** @} */