BxDolStorageS3.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_PUBLIC', 'public');
  10. define ('BX_DOL_STORAGE_PRIVATE', 'private');
  11. define ('BX_DOL_STORAGE_DIR_RIGHT', 0700);
  12. define ('BX_DOL_STORAGE_FILE_RIGHT', 0400);
  13. bx_import('BxDolStorage');
  14. require_once(BX_DIRECTORY_PATH_PLUGINS . 'amazon-s3/S3.php');
  15. /**
  16. * File storage in Amazon S3.
  17. * @see BxDolStorage
  18. */
  19. class BxDolStorageS3 extends BxDolStorage {
  20. protected $_s3;
  21. protected $_sBucket;
  22. protected $_sDomain;
  23. protected $_bSSL;
  24. protected $_bReducedRedundancy;
  25. /**
  26. * constructor
  27. */
  28. public function __construct($aObject) {
  29. parent::__construct($aObject);
  30. $sAccessKey = getParam('sys_storage_s3_access_key');
  31. $sSecretKey = getParam('sys_storage_s3_secret_key');
  32. $this->_s3 = new S3($sAccessKey, $sSecretKey);
  33. $this->_sBucket = getParam('sys_storage_s3_bucket');
  34. $this->_sDomain = getParam('sys_storage_s3_domain');
  35. $this->_bSSL = isset($this->_aParams['ssl']) && $this->_aParams['ssl'] ? true : false;
  36. $this->_bReducedRedundancy = isset($this->_aParams['reduced_redundancy']) && $this->_aParams['reduced_redundancy'] ? true : false;
  37. }
  38. /**
  39. * Get file url.
  40. * @param $iFileId file
  41. * @return file url
  42. */
  43. public function getFileUrlById($iFileId) {
  44. $aFile = $this->_oDb->getFileById($iFileId);
  45. if (!$aFile)
  46. return false;
  47. if ($aFile['private']) {
  48. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  49. if ($this->_sDomain)
  50. return $this->_s3->getAuthenticatedURL($this->_sDomain, $sFileLocation, $this->_aObject['token_life'], true, $this->_bSSL);
  51. else
  52. return $this->_s3->getAuthenticatedURL($this->_sBucket, $sFileLocation, $this->_aObject['token_life'], false, $this->_bSSL);
  53. }
  54. return $this->getObjectBaseUrl($aFile['private']) . $aFile['path'];
  55. }
  56. /**
  57. * Set file private or public.
  58. */
  59. public function setFilePrivate($iFileId, $isPrivate = true) {
  60. $aFile = $this->_oDb->getFileById($iFileId);
  61. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  62. if (($aACP = $this->_s3->getAccessControlPolicy($this->_sBucket, $sFileLocation)) === false) {
  63. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  64. return false;
  65. }
  66. if (!is_array($aACP['acl']) || !$aACP['acl']) {
  67. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  68. return false;
  69. }
  70. // check current permissions
  71. $aNewACP = $aACP;
  72. unset($aNewACP['acl']);
  73. $aNewACP['acl'] = array();
  74. $aGroupPublic = false;
  75. $aGroupPrivate = false;
  76. foreach ($aACP['acl'] as $r) {
  77. if ('Group' == $r['type']) {
  78. if (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AllUsers')
  79. $aGroupPublic = $r;
  80. elseif (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers')
  81. $aGroupPrivate = $r;
  82. else
  83. $aNewACP['acl'][] = $r;
  84. } else {
  85. $aNewACP['acl'][] = $r;
  86. }
  87. }
  88. // determine permissions changing
  89. $aGroupAdd = false;
  90. if ($isPrivate && (!$aGroupPrivate || $aGroupPublic)) {
  91. // make private
  92. $aGroupAdd = array (
  93. 'type' => 'Group',
  94. 'uri' => 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers',
  95. 'permission' => 'READ',
  96. );
  97. } elseif (!$isPrivate && ($aGroupPrivate || !$aGroupPublic)) {
  98. // make public
  99. $aGroupAdd = array (
  100. 'type' => 'Group',
  101. 'uri' => 'http://acs.amazonaws.com/groups/global/AllUsers',
  102. 'permission' => 'READ',
  103. );
  104. }
  105. // change permission if necessary
  106. if ($aGroupAdd) {
  107. $aNewACP['acl'][] = $aGroupAdd;
  108. if (!$this->_s3->setAccessControlPolicy($this->_sBucket, $sFileLocation, $aNewACP)) {
  109. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  110. return false;
  111. }
  112. }
  113. return parent::setFilePrivate($iFileId, $isPrivate);
  114. }
  115. // ----------------
  116. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId) {
  117. $sMimeType = $this->getMimeTypeByFileName($sName);
  118. $sExt = $this->getFileExt($sName);
  119. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  120. $sRemoteNamePath = $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  121. $aMetaHeaders = array();
  122. $aRequestHeaders = array (
  123. "Content-Type" => $sMimeType,
  124. );
  125. if ($this->_iCacheControl > 0) {
  126. $aRequestHeaders = array_merge ($aRequestHeaders, array (
  127. "Cache-Control" => "max-age=" . ($isPrivate && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl),
  128. ));
  129. }
  130. $sStorageClass = $this->_bReducedRedundancy ? S3::STORAGE_CLASS_RRS : S3::STORAGE_CLASS_STANDARD;
  131. $sACL = $isPrivate ? S3::ACL_AUTHENTICATED_READ : S3::ACL_PUBLIC_READ;
  132. $aInputFile = $this->_s3->inputFile($sTmpFile);
  133. if (!$this->_s3->putObject($aInputFile, $this->_sBucket, $this->getObjectBaseDir($isPrivate) . $sRemoteNamePath, $sACL, $aMetaHeaders, $aRequestHeaders, $sStorageClass)) {
  134. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  135. return false;
  136. }
  137. return true;
  138. }
  139. protected function deleteFileFromEngine($sFilePath, $isPrivate) {
  140. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  141. if (!$this->_s3->deleteObject($this->_sBucket, $sFileLocation)) {
  142. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  143. return false;
  144. }
  145. return true;
  146. }
  147. protected function genRemoteNamePath ($sPath, $sLocalId, $sExt) {
  148. return $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  149. }
  150. protected function getObjectBaseDir ($isPrivate = false) {
  151. return $this->_aObject['object'] . '/';
  152. }
  153. protected function getObjectBaseUrl ($isPrivate = false) {
  154. $sProto = $this->_bSSL ? 'https://' : 'http://';
  155. return $sProto . $this->_sBucket . '.s3.amazonaws.com/' . $this->getObjectBaseDir($isPrivate);
  156. }
  157. }
  158. /** @} */