BxDolStorageLocal.php 4.5 KB

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