BxDolStorageLocal.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, $bForceDownloadDialog = false)
  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 (!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')) {
  62. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  63. return false;
  64. }
  65. return parent::download($aFile);
  66. }
  67. // ----------------
  68. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId)
  69. {
  70. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  71. $sNewFileDir = $this->getObjectBaseDir($isPrivate) . $sPath;
  72. $sNewFilePath = $sNewFileDir . $sLocalId;
  73. $this->mkdir($sNewFileDir);
  74. if (!file_exists($sNewFileDir) || !is_writable($sNewFileDir)) {
  75. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILESYSTEM_PERM);
  76. return false;
  77. }
  78. if (!copy($sTmpFile, $sNewFilePath)) {
  79. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  80. return false;
  81. }
  82. if (!chmod ($sNewFilePath, BX_DOL_STORAGE_FILE_RIGHTS)) {
  83. unlink($sNewFilePath);
  84. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  85. return false;
  86. }
  87. return true;
  88. }
  89. protected function deleteFileFromEngine($sFilePath, $isPrivate)
  90. {
  91. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  92. if (!file_exists($sFileLocation))
  93. return true;
  94. if (!unlink($sFileLocation)) {
  95. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  96. return false;
  97. }
  98. return true;
  99. }
  100. protected function getObjectBaseDir ($isPrivate = false)
  101. {
  102. return BX_DIRECTORY_STORAGE . $this->_aObject['object'] . '/';
  103. }
  104. protected function getObjectBaseUrl ($isPrivate = false)
  105. {
  106. $o = BxDolPermalinks::getInstance();
  107. return BX_DOL_URL_ROOT . $o->permalink('storage.php?o=') . $this->_aObject['object'] . ($o->_isEnabled('storage.php?o=') ? '/' : '&f=');
  108. }
  109. protected function mkdir($sDirName, $sRights = false)
  110. {
  111. if (false == $sRights)
  112. $sRights = BX_DOL_STORAGE_DIR_RIGHTS;
  113. $aDirs = explode('/', $sDirName);
  114. $sDir='';
  115. foreach ($aDirs as $sPart) {
  116. $sDir .= $sPart.'/';
  117. if (!is_dir($sDir) && strlen($sDir) > 0 && !file_exists($sDir)) {
  118. mkdir($sDir);
  119. chmod($sDir, $sRights);
  120. }
  121. }
  122. }
  123. }
  124. /** @} */