BxDolStorageLocal.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA 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. /**
  12. * File storage in local folder.
  13. * @see BxDolStorage
  14. */
  15. class BxDolStorageLocal extends BxDolStorage
  16. {
  17. /**
  18. * constructor
  19. */
  20. public function __construct($aObject)
  21. {
  22. parent::__construct($aObject);
  23. }
  24. /**
  25. * Get file url.
  26. * @param $iFileId file
  27. * @return file url or false if file was not found
  28. */
  29. public function getFileUrlById($iFileId)
  30. {
  31. $aFile = $this->_oDb->getFileById($iFileId);
  32. if (!$aFile)
  33. return false;
  34. $sUrl = $this->getObjectBaseUrl($aFile['private']) . $aFile['remote_id'] . '.' . $aFile['ext'];
  35. if ($aFile['private']) {
  36. $sToken = $this->_oDb->genToken($iFileId);
  37. $sUrl = bx_append_url_params($sUrl, array ('t' => $sToken));
  38. }
  39. return $sUrl;
  40. }
  41. /**
  42. * Start file downloading by remote id. If file is private then token is checked.
  43. */
  44. public function download ($sRemoteId, $sToken = false, $mixedForceDownloadDialog = 'auto')
  45. {
  46. $this->setErrorCode(BX_DOL_STORAGE_ERR_OK);
  47. $aFile = $this->_oDb->getFileByRemoteId($sRemoteId);
  48. if (!$aFile) {
  49. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILE_NOT_FOUND);
  50. return false;
  51. }
  52. if ($aFile['private'] && !$this->_oDb->isTokenValid($aFile['id'], $sToken)) {
  53. $this->setErrorCode(BX_DOL_STORAGE_ERR_PERMISSION_DENIED);
  54. return false;
  55. }
  56. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  57. if (!file_exists($sFileLocation)) {
  58. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILE_NOT_FOUND);
  59. return false;
  60. }
  61. if ('auto' === $mixedForceDownloadDialog) {
  62. $bForceDownloadDialog = true;
  63. foreach ($this->_aMimeTypesViewable as $sType) {
  64. if (0 === strpos($aFile['mime_type'], $sType)) {
  65. $bForceDownloadDialog = false;
  66. break;
  67. }
  68. }
  69. }
  70. else {
  71. $bForceDownloadDialog = $mixedForceDownloadDialog;
  72. }
  73. if (!bx_smart_readfile($sFileLocation, $aFile['file_name'], $aFile['mime_type'], $aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl, $aFile['private'] ? 'private' : 'public', $bForceDownloadDialog ? 'attachment' : 'inline')) {
  74. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  75. return false;
  76. }
  77. return parent::download($aFile);
  78. }
  79. // ----------------
  80. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId)
  81. {
  82. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  83. $sNewFileDir = $this->getObjectBaseDir($isPrivate) . $sPath;
  84. $sNewFilePath = $sNewFileDir . $sLocalId;
  85. $this->mkdir($sNewFileDir);
  86. if (!file_exists($sNewFileDir) || !is_writable($sNewFileDir)) {
  87. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILESYSTEM_PERM);
  88. return false;
  89. }
  90. if (!copy($sTmpFile, $sNewFilePath)) {
  91. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  92. return false;
  93. }
  94. if (!chmod ($sNewFilePath, BX_DOL_STORAGE_FILE_RIGHTS)) {
  95. unlink($sNewFilePath);
  96. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  97. return false;
  98. }
  99. return true;
  100. }
  101. protected function deleteFileFromEngine($sFilePath, $isPrivate)
  102. {
  103. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  104. if (!file_exists($sFileLocation))
  105. return true;
  106. if (!unlink($sFileLocation)) {
  107. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  108. return false;
  109. }
  110. return true;
  111. }
  112. protected function getObjectBaseDir ($isPrivate = false)
  113. {
  114. return BX_DIRECTORY_STORAGE . $this->_aObject['object'] . '/';
  115. }
  116. protected function getObjectBaseUrl ($isPrivate = false)
  117. {
  118. $o = BxDolPermalinks::getInstance();
  119. return bx_absolute_url($o->permalink('storage.php?o=') . $this->_aObject['object'] . ($o->_isEnabled('storage.php?o=') ? '/' : '&f='));
  120. }
  121. protected function mkdir($sDirName, $sRights = false)
  122. {
  123. if (false == $sRights)
  124. $sRights = BX_DOL_STORAGE_DIR_RIGHTS;
  125. $sDirName = bx_ltrim_str($sDirName, BX_DIRECTORY_PATH_ROOT);
  126. $aDirs = explode('/', $sDirName);
  127. $sDir = BX_DIRECTORY_PATH_ROOT;
  128. foreach ($aDirs as $sPart) {
  129. $sDir .= $sPart.'/';
  130. if (!is_dir($sDir) && strlen($sDir) > 0 && !file_exists($sDir)) {
  131. mkdir($sDir);
  132. chmod($sDir, $sRights);
  133. }
  134. }
  135. }
  136. }
  137. /** @} */