BxDolStorageLocal.php 4.5 KB

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