BxDolStorageS3.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 TridentCore Trident Core
  7. * @{
  8. */
  9. require_once(BX_DIRECTORY_PATH_PLUGINS . 'amazon-s3/S3.php');
  10. /**
  11. * File storage in Amazon S3.
  12. * @see BxDolStorage
  13. */
  14. class BxDolStorageS3 extends BxDolStorage
  15. {
  16. protected $_s3;
  17. protected $_sBucket;
  18. protected $_sDomain;
  19. protected $_bSSL;
  20. protected $_bReducedRedundancy;
  21. /**
  22. * constructor
  23. */
  24. public function __construct($aObject)
  25. {
  26. parent::__construct($aObject);
  27. $sAccessKey = getParam('sys_storage_s3_access_key');
  28. $sSecretKey = getParam('sys_storage_s3_secret_key');
  29. $this->_s3 = new S3($sAccessKey, $sSecretKey);
  30. $this->_sBucket = getParam('sys_storage_s3_bucket');
  31. $this->_sDomain = getParam('sys_storage_s3_domain');
  32. $this->_bSSL = isset($this->_aParams['ssl']) && $this->_aParams['ssl'] ? true : false;
  33. $this->_bReducedRedundancy = isset($this->_aParams['reduced_redundancy']) && $this->_aParams['reduced_redundancy'] ? true : false;
  34. }
  35. /**
  36. * Get file url.
  37. * @param $iFileId file
  38. * @return file url
  39. */
  40. public function getFileUrlById($iFileId)
  41. {
  42. $aFile = $this->_oDb->getFileById($iFileId);
  43. if (!$aFile)
  44. return false;
  45. if ($aFile['private']) {
  46. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  47. if ($this->_sDomain)
  48. return $this->_s3->getAuthenticatedURL($this->_sDomain, $sFileLocation, $this->_aObject['token_life'], true, $this->_bSSL);
  49. else
  50. return $this->_s3->getAuthenticatedURL($this->_sBucket, $sFileLocation, $this->_aObject['token_life'], false, $this->_bSSL);
  51. }
  52. return $this->getObjectBaseUrl($aFile['private']) . $aFile['path'];
  53. }
  54. /**
  55. * Set file private or public.
  56. */
  57. public function setFilePrivate($iFileId, $isPrivate = true)
  58. {
  59. $aFile = $this->_oDb->getFileById($iFileId);
  60. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  61. if (($aACP = $this->_s3->getAccessControlPolicy($this->_sBucket, $sFileLocation)) === false) {
  62. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  63. return false;
  64. }
  65. if (!is_array($aACP['acl']) || !$aACP['acl']) {
  66. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  67. return false;
  68. }
  69. // check current permissions
  70. $aNewACP = $aACP;
  71. unset($aNewACP['acl']);
  72. $aNewACP['acl'] = array();
  73. $aGroupPublic = false;
  74. $aGroupPrivate = false;
  75. foreach ($aACP['acl'] as $r) {
  76. if ('Group' == $r['type']) {
  77. if (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AllUsers')
  78. $aGroupPublic = $r;
  79. elseif (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers')
  80. $aGroupPrivate = $r;
  81. else
  82. $aNewACP['acl'][] = $r;
  83. } else {
  84. $aNewACP['acl'][] = $r;
  85. }
  86. }
  87. // determine permissions changing
  88. $aGroupAdd = false;
  89. if ($isPrivate && (!$aGroupPrivate || $aGroupPublic)) {
  90. // make private
  91. $aGroupAdd = array (
  92. 'type' => 'Group',
  93. 'uri' => 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers',
  94. 'permission' => 'READ',
  95. );
  96. } elseif (!$isPrivate && ($aGroupPrivate || !$aGroupPublic)) {
  97. // make public
  98. $aGroupAdd = array (
  99. 'type' => 'Group',
  100. 'uri' => 'http://acs.amazonaws.com/groups/global/AllUsers',
  101. 'permission' => 'READ',
  102. );
  103. }
  104. // change permission if necessary
  105. if ($aGroupAdd) {
  106. $aNewACP['acl'][] = $aGroupAdd;
  107. if (!$this->_s3->setAccessControlPolicy($this->_sBucket, $sFileLocation, $aNewACP)) {
  108. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  109. return false;
  110. }
  111. }
  112. return parent::setFilePrivate($iFileId, $isPrivate);
  113. }
  114. // ----------------
  115. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId)
  116. {
  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. {
  141. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  142. if (!$this->_s3->deleteObject($this->_sBucket, $sFileLocation)) {
  143. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  144. return false;
  145. }
  146. return true;
  147. }
  148. protected function genRemoteNamePath ($sPath, $sLocalId, $sExt)
  149. {
  150. return $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  151. }
  152. protected function getObjectBaseDir ($isPrivate = false)
  153. {
  154. return $this->_aObject['object'] . '/';
  155. }
  156. protected function getObjectBaseUrl ($isPrivate = false)
  157. {
  158. $sProto = $this->_bSSL ? 'https://' : 'http://';
  159. return $sProto . $this->_sBucket . '.s3.amazonaws.com/' . $this->getObjectBaseDir($isPrivate);
  160. }
  161. }
  162. /** @} */