BxDolStorageLocal.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. define ('BX_DOL_STORAGE_DIR_RIGHTS', BX_DOL_DIR_RIGHTS);
  10. define ('BX_DOL_STORAGE_FILE_RIGHTS', BX_DOL_FILE_RIGHTS);
  11. bx_import('BxDolStorage');
  12. /**
  13. * File storage in local folder.
  14. * @see BxDolStorage
  15. */
  16. class BxDolStorageLocal extends BxDolStorage
  17. {
  18. /**
  19. * constructor
  20. */
  21. public function __construct($aObject)
  22. {
  23. parent::__construct($aObject);
  24. }
  25. /**
  26. * Get file url.
  27. * @param $iFileId file
  28. * @return file url or false if file was not found
  29. */
  30. public function getFileUrlById($iFileId)
  31. {
  32. $aFile = $this->_oDb->getFileById($iFileId);
  33. if (!$aFile)
  34. return false;
  35. $sUrl = $this->getObjectBaseUrl($aFile['private']) . $aFile['remote_id'] . '.' . $aFile['ext'];
  36. if ($aFile['private']) {
  37. $sToken = $this->_oDb->genToken($iFileId);
  38. $sUrl = bx_append_url_params($sUrl, array ('t' => $sToken));
  39. }
  40. return $sUrl;
  41. }
  42. /**
  43. * Start file fownloading by remote id. If file is private then token is checked.
  44. */
  45. public function download ($sRemoteId, $sToken = false)
  46. {
  47. $this->setErrorCode(BX_DOL_STORAGE_ERR_OK);
  48. $aFile = $this->_oDb->getFileByRemoteId($sRemoteId);
  49. if (!$aFile) {
  50. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILE_NOT_FOUND);
  51. return false;
  52. }
  53. if ($aFile['private'] && !$this->_oDb->isTokenValid($aFile['id'], $sToken)) {
  54. $this->setErrorCode(BX_DOL_STORAGE_ERR_PERMISSION_DENIED);
  55. return false;
  56. }
  57. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  58. if (!file_exists($sFileLocation)) {
  59. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILE_NOT_FOUND);
  60. return false;
  61. }
  62. header('Content-Type: ' . $aFile['mime_type']);
  63. if ($this->_iCacheControl > 0)
  64. header('Cache-Control: max-age=' . ($aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl));
  65. if (false === readfile($sFileLocation)) {
  66. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  67. return false;
  68. }
  69. return true;
  70. }
  71. // ----------------
  72. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId)
  73. {
  74. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  75. $sNewFileDir = $this->getObjectBaseDir($isPrivate) . $sPath;
  76. $sNewFilePath = $sNewFileDir . $sLocalId;
  77. $this->mkdir($sNewFileDir);
  78. if (!file_exists($sNewFileDir) || !is_writable($sNewFileDir)) {
  79. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILESYSTEM_PERM);
  80. return false;
  81. }
  82. if (!copy($sTmpFile, $sNewFilePath)) {
  83. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  84. return false;
  85. }
  86. if (!chmod ($sNewFilePath, BX_DOL_STORAGE_FILE_RIGHTS)) {
  87. unlink($sNewFilePath);
  88. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  89. return false;
  90. }
  91. return true;
  92. }
  93. protected function deleteFileFromEngine($sFilePath, $isPrivate)
  94. {
  95. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  96. if (!file_exists($sFileLocation))
  97. return true;
  98. if (!unlink($sFileLocation)) {
  99. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  100. return false;
  101. }
  102. return true;
  103. }
  104. protected function getObjectBaseDir ($isPrivate = false)
  105. {
  106. return BX_DIRECTORY_STORAGE . $this->_aObject['object'] . '/';
  107. }
  108. protected function getObjectBaseUrl ($isPrivate = false)
  109. {
  110. bx_import('BxDolPermalinks');
  111. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('storage.php?o=' . $this->_aObject['object'] . '&f=');
  112. }
  113. protected function mkdir($sDirName, $sRights = false)
  114. {
  115. if (false == $sRights)
  116. $sRights = BX_DOL_STORAGE_DIR_RIGHTS;
  117. $aDirs = explode('/', $sDirName);
  118. $sDir='';
  119. foreach ($aDirs as $sPart) {
  120. $sDir .= $sPart.'/';
  121. if (!is_dir($sDir) && strlen($sDir) > 0 && !file_exists($sDir)) {
  122. mkdir($sDir);
  123. chmod($sDir, $sRights);
  124. }
  125. }
  126. }
  127. }
  128. /** @} */