BxDolStorageS3.php 6.7 KB

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