BxDolFile.php 5.4 KB

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