BxDolStorageS3.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. bx_import('BxDolStorage');
  10. require_once(BX_DIRECTORY_PATH_PLUGINS . 'amazon-s3/S3.php');
  11. /**
  12. * File storage in Amazon S3.
  13. * @see BxDolStorage
  14. */
  15. class BxDolStorageS3 extends BxDolStorage
  16. {
  17. protected $_s3;
  18. protected $_sBucket;
  19. protected $_sDomain;
  20. protected $_bSSL;
  21. protected $_bReducedRedundancy;
  22. /**
  23. * constructor
  24. */
  25. public function __construct($aObject)
  26. {
  27. parent::__construct($aObject);
  28. $sAccessKey = getParam('sys_storage_s3_access_key');
  29. $sSecretKey = getParam('sys_storage_s3_secret_key');
  30. $this->_s3 = new S3($sAccessKey, $sSecretKey);
  31. $this->_sBucket = getParam('sys_storage_s3_bucket');
  32. $this->_sDomain = getParam('sys_storage_s3_domain');
  33. $this->_bSSL = isset($this->_aParams['ssl']) && $this->_aParams['ssl'] ? true : false;
  34. $this->_bReducedRedundancy = isset($this->_aParams['reduced_redundancy']) && $this->_aParams['reduced_redundancy'] ? true : false;
  35. }
  36. /**
  37. * Get file url.
  38. * @param $iFileId file
  39. * @return file url
  40. */
  41. public function getFileUrlById($iFileId)
  42. {
  43. $aFile = $this->_oDb->getFileById($iFileId);
  44. if (!$aFile)
  45. return false;
  46. if ($aFile['private']) {
  47. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  48. if ($this->_sDomain)
  49. return $this->_s3->getAuthenticatedURL($this->_sDomain, $sFileLocation, $this->_aObject['token_life'], true, $this->_bSSL);
  50. else
  51. return $this->_s3->getAuthenticatedURL($this->_sBucket, $sFileLocation, $this->_aObject['token_life'], false, $this->_bSSL);
  52. }
  53. return $this->getObjectBaseUrl($aFile['private']) . $aFile['path'];
  54. }
  55. /**
  56. * Set file private or public.
  57. */
  58. public function setFilePrivate($iFileId, $isPrivate = true)
  59. {
  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. {
  118. $sMimeType = $this->getMimeTypeByFileName($sName);
  119. $sExt = $this->getFileExt($sName);
  120. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  121. $sRemoteNamePath = $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  122. $aMetaHeaders = array();
  123. $aRequestHeaders = array (
  124. "Content-Type" => $sMimeType,
  125. );
  126. if ($this->_iCacheControl > 0) {
  127. $aRequestHeaders = array_merge ($aRequestHeaders, array (
  128. "Cache-Control" => "max-age=" . ($isPrivate && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl),
  129. ));
  130. }
  131. $sStorageClass = $this->_bReducedRedundancy ? S3::STORAGE_CLASS_RRS : S3::STORAGE_CLASS_STANDARD;
  132. $sACL = $isPrivate ? S3::ACL_AUTHENTICATED_READ : S3::ACL_PUBLIC_READ;
  133. $aInputFile = $this->_s3->inputFile($sTmpFile);
  134. if (!$this->_s3->putObject($aInputFile, $this->_sBucket, $this->getObjectBaseDir($isPrivate) . $sRemoteNamePath, $sACL, $aMetaHeaders, $aRequestHeaders, $sStorageClass)) {
  135. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  136. return false;
  137. }
  138. return true;
  139. }
  140. protected function deleteFileFromEngine($sFilePath, $isPrivate)
  141. {
  142. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  143. if (!$this->_s3->deleteObject($this->_sBucket, $sFileLocation)) {
  144. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  145. return false;
  146. }
  147. return true;
  148. }
  149. protected function genRemoteNamePath ($sPath, $sLocalId, $sExt)
  150. {
  151. return $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  152. }
  153. protected function getObjectBaseDir ($isPrivate = false)
  154. {
  155. return $this->_aObject['object'] . '/';
  156. }
  157. protected function getObjectBaseUrl ($isPrivate = false)
  158. {
  159. $sProto = $this->_bSSL ? 'https://' : 'http://';
  160. return $sProto . $this->_sBucket . '.s3.amazonaws.com/' . $this->getObjectBaseDir($isPrivate);
  161. }
  162. }
  163. /** @} */