BxDolFile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. {
  11. protected $_sPathFrom;
  12. protected $_sPathTo;
  13. 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. $sFilePathFrom = $this->_sPathFrom . $sFilePathFrom;
  39. $sFilePathTo = $this->_sPathTo . $sFilePathTo;
  40. return $this->_copyFile($sFilePathFrom, $sFilePathTo);
  41. }
  42. function delete($sPath)
  43. {
  44. $sPath = $this->_sPathTo . $sPath;
  45. return $this->_deleteDirectory($sPath);
  46. }
  47. function getPermissions($sPath)
  48. {
  49. $sPath = $this->_sPathTo . $sPath;
  50. return $this->_getPermissions($sPath);
  51. }
  52. function setPermissions($sPath, $sMode)
  53. {
  54. $sPath = $this->_sPathTo . $sPath;
  55. return $this->_setPermissions($sPath, $sMode);
  56. }
  57. protected function _copyFile($sFilePathFrom, $sFilePathTo)
  58. {
  59. if(file_exists($sFilePathTo))
  60. return true;
  61. if(substr($sFilePathFrom, -1) == '*')
  62. $sFilePathFrom = substr($sFilePathFrom, 0, -1);
  63. $bResult = true;
  64. if(is_file($sFilePathFrom)) {
  65. if($this->_isFile($sFilePathTo)) {
  66. $aFileParts = $this->_parseFile($sFilePathTo);
  67. if(isset($aFileParts[0]))
  68. @mkdir($this->_rStream, $aFileParts[0]);
  69. $bResult = @copy($sFilePathFrom, $sFilePathTo);
  70. } else if($this->_isDirectory($sFilePathTo)) {
  71. @mkdir($sFilePathTo);
  72. $aFileParts = $this->_parseFile($sFilePathFrom);
  73. if(isset($aFileParts[1]))
  74. $bResult = @copy($sFilePathFrom, $this->_validatePath($sFilePathTo) . $aFileParts[1]);
  75. }
  76. } else if(is_dir($sFilePathFrom) && $this->_isDirectory($sFilePathTo)) {
  77. @mkdir($sFilePathTo);
  78. $aInnerFiles = $this->_readDirectory($sFilePathFrom);
  79. foreach($aInnerFiles as $sFile)
  80. $bResult = $this->_copyFile($this->_validatePath($sFilePathFrom) . $sFile, $this->_validatePath($sFilePathTo) . $sFile);
  81. } else
  82. $bResult = false;
  83. return $bResult;
  84. }
  85. protected function _readDirectory($sFilePath)
  86. {
  87. if(!is_dir($sFilePath) || !($rSource = opendir($sFilePath)))
  88. return false;
  89. $aResult = array();
  90. while(($sFile = readdir($rSource)) !== false) {
  91. if($sFile == '.' || $sFile =='..' || $sFile[0] == '.')
  92. continue;
  93. $aResult[] = $sFile;
  94. }
  95. closedir($rSource);
  96. return $aResult;
  97. }
  98. protected function _deleteDirectory($sPath)
  99. {
  100. if(!file_exists($sPath))
  101. return true;
  102. if($this->_isDirectory($sPath)) {
  103. if(substr($sPath, -1) != '/')
  104. $sPath .= '/';
  105. $aFiles = $this->_readDirectory($sPath);
  106. if(is_array($aFiles) && !empty($aFiles))
  107. foreach($aFiles as $sFile)
  108. $this->_deleteDirectory($sPath . $sFile);
  109. if(!@rmdir($sPath))
  110. return false;
  111. } else if(!@unlink($sPath))
  112. return false;
  113. return true;
  114. }
  115. protected function _validatePath($sPath)
  116. {
  117. if($sPath && substr($sPath, -1) != '/' && $this->_isDirectory($sPath))
  118. $sPath .= '/';
  119. return $sPath;
  120. }
  121. protected function _parseFile($sFilePath)
  122. {
  123. $aParts = array();
  124. preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+[\\\\\/])([a-zA-Z0-9~_-]+\.[a-zA-Z]{2,8})$/", $sFilePath, $aParts) ? true : false;
  125. return count($aParts) > 1 ? array_slice($aParts, 1) : false;
  126. }
  127. protected function _isFile($sFilePath)
  128. {
  129. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)\.([a-zA-Z]){2,8}$/", $sFilePath) ? true : false;
  130. }
  131. protected function _isDirectory($sFilePath)
  132. {
  133. return preg_match("/^([a-zA-Z0-9@~_\.\\\\\/:-]+)[\\\\\/]([a-zA-Z0-9~_-]+)[\\\\\/]?$/", $sFilePath) ? true : false;
  134. }
  135. protected function _getPermissions($sPath)
  136. {
  137. clearstatcache();
  138. $hPerms = @fileperms($sPath);
  139. if($hPerms == false)
  140. return false;
  141. return substr(decoct($hPerms), -3);
  142. }
  143. protected function _setPermissions($sPath, $sMode)
  144. {
  145. $aConvert = array('writable' => 0666, 'executable' => 0777);
  146. return chmod($sPath, $aConvert[$sMode]);
  147. }
  148. }
  149. /** @} */