BxDolStorageS3.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. if (!defined('CURL_SSLVERSION_TLSv1'))
  10. define('CURL_SSLVERSION_TLSv1', 1);
  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 $_sEndpoint;
  21. protected $_bSSL;
  22. protected $_bReducedRedundancy;
  23. /**
  24. * constructor
  25. */
  26. public function __construct($aObject)
  27. {
  28. parent::__construct($aObject);
  29. $this->_bSSL = (isset($this->_aParams['ssl']) && $this->_aParams['ssl']) || (!isset($this->_aParams['ssl']) && strncmp(BX_DOL_URL_ROOT, 'https://', 8) === 0) ? true : false;
  30. $this->_sEndpoint = getParam('sys_storage_s3_endpoint');
  31. $this->_sBucket = getParam('sys_storage_s3_bucket');
  32. $this->_sDomain = getParam('sys_storage_s3_domain');
  33. $this->_bReducedRedundancy = isset($this->_aParams['reduced_redundancy']) && $this->_aParams['reduced_redundancy'] ? true : false;
  34. $this->init($aObject);
  35. }
  36. protected function init ($aObject)
  37. {
  38. require_once(BX_DIRECTORY_PATH_PLUGINS . 'amazon-s3/S3.php');
  39. $this->_s3 = new S3(
  40. getParam('sys_storage_s3_access_key'),
  41. getParam('sys_storage_s3_secret_key'),
  42. $this->_bSSL,
  43. $this->_sEndpoint ? $this->_sEndpoint : 's3.amazonaws.com'
  44. );
  45. }
  46. /**
  47. * Get file url.
  48. * @param $iFileId file
  49. * @return file url
  50. */
  51. public function getFileUrlById($iFileId)
  52. {
  53. $aFile = $this->_oDb->getFileById($iFileId);
  54. if (!$aFile)
  55. return false;
  56. if ($aFile['private']) {
  57. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  58. if ($this->_sDomain)
  59. return $this->_s3->getAuthenticatedURL($this->_sDomain, $sFileLocation, $this->_aObject['token_life'], true, $this->_bSSL);
  60. else
  61. return $this->_s3->getAuthenticatedURL($this->_sBucket, $sFileLocation, $this->_aObject['token_life'], false, $this->_bSSL);
  62. }
  63. return $this->getObjectBaseUrl($aFile['private']) . $aFile['path'];
  64. }
  65. /**
  66. * Start file fownloading by remote id. If file is private then token is checked.
  67. */
  68. public function download ($sRemoteId, $sToken = false)
  69. {
  70. $this->setErrorCode(BX_DOL_STORAGE_ERR_OK);
  71. $aFile = $this->_oDb->getFileByRemoteId($sRemoteId);
  72. if (!$aFile) {
  73. $this->setErrorCode(BX_DOL_STORAGE_ERR_FILE_NOT_FOUND);
  74. return false;
  75. }
  76. if ($aFile['private'] && !$this->_oDb->isTokenValid($aFile['id'], $sToken)) {
  77. $this->setErrorCode(BX_DOL_STORAGE_ERR_PERMISSION_DENIED);
  78. return false;
  79. }
  80. $sUrl = $this->getFileUrlById($aFile['id']);
  81. header("Location: " . $sUrl);
  82. return parent::download($aFile);
  83. }
  84. /**
  85. * Set file private or public.
  86. */
  87. public function setFilePrivate($iFileId, $isPrivate = true)
  88. {
  89. $aFile = $this->_oDb->getFileById($iFileId);
  90. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  91. if (($aACP = $this->_s3->getAccessControlPolicy($this->_sBucket, $sFileLocation)) === false) {
  92. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  93. return false;
  94. }
  95. if (!is_array($aACP['acl']) || !$aACP['acl']) {
  96. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  97. return false;
  98. }
  99. // check current permissions
  100. $aNewACP = $aACP;
  101. unset($aNewACP['acl']);
  102. $aNewACP['acl'] = array();
  103. $aGroupPublic = false;
  104. $aGroupPrivate = false;
  105. foreach ($aACP['acl'] as $r) {
  106. if ('Group' == $r['type']) {
  107. if (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AllUsers')
  108. $aGroupPublic = $r;
  109. elseif (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers')
  110. $aGroupPrivate = $r;
  111. else
  112. $aNewACP['acl'][] = $r;
  113. } else {
  114. $aNewACP['acl'][] = $r;
  115. }
  116. }
  117. // determine permissions changing
  118. $aGroupAdd = false;
  119. if ($isPrivate && (!$aGroupPrivate || $aGroupPublic)) {
  120. // make private
  121. $aGroupAdd = array (
  122. 'type' => 'Group',
  123. 'uri' => 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers',
  124. 'permission' => 'READ',
  125. );
  126. } elseif (!$isPrivate && ($aGroupPrivate || !$aGroupPublic)) {
  127. // make public
  128. $aGroupAdd = array (
  129. 'type' => 'Group',
  130. 'uri' => 'http://acs.amazonaws.com/groups/global/AllUsers',
  131. 'permission' => 'READ',
  132. );
  133. }
  134. // change permission if necessary
  135. if ($aGroupAdd) {
  136. $aNewACP['acl'][] = $aGroupAdd;
  137. if (!$this->_s3->setAccessControlPolicy($this->_sBucket, $sFileLocation, $aNewACP)) {
  138. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  139. return false;
  140. }
  141. }
  142. return parent::setFilePrivate($iFileId, $isPrivate);
  143. }
  144. // ----------------
  145. protected function generateHeaders($sFileName, $isPrivate, $sMimeType = '')
  146. {
  147. if (!$sMimeType)
  148. $sMimeType = $this->getMimeTypeByFileName($sFileName);
  149. $aRequestHeaders = array (
  150. "Content-Type" => $sMimeType,
  151. );
  152. if ($this->_iCacheControl > 0) {
  153. $aRequestHeaders = array_merge ($aRequestHeaders, array (
  154. "Cache-Control" => "max-age=" . ($isPrivate && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl),
  155. ));
  156. }
  157. return $aRequestHeaders;
  158. }
  159. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId)
  160. {
  161. $sMimeType = $this->getMimeTypeByFileName($sName);
  162. $sExt = $this->getFileExt($sName);
  163. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  164. $sRemoteNamePath = $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  165. $aRequestHeaders = $this->generateHeaders($sName, $isPrivate);
  166. $aMetaHeaders = array();
  167. $sStorageClass = $this->_bReducedRedundancy ? S3::STORAGE_CLASS_RRS : S3::STORAGE_CLASS_STANDARD;
  168. $sACL = $isPrivate ? S3::ACL_AUTHENTICATED_READ : S3::ACL_PUBLIC_READ;
  169. $aInputFile = $this->_s3->inputFile($sTmpFile);
  170. if (!$this->_s3->putObject($aInputFile, $this->_sBucket, $this->getObjectBaseDir($isPrivate) . $sRemoteNamePath, $sACL, $aMetaHeaders, $aRequestHeaders, $sStorageClass)) {
  171. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  172. return false;
  173. }
  174. return true;
  175. }
  176. protected function deleteFileFromEngine($sFilePath, $isPrivate)
  177. {
  178. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  179. if (!$this->_s3->deleteObject($this->_sBucket, $sFileLocation)) {
  180. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  181. return false;
  182. }
  183. return true;
  184. }
  185. protected function genRemoteNamePath ($sPath, $sLocalId, $sExt)
  186. {
  187. return $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  188. }
  189. protected function getObjectBaseDir ($isPrivate = false)
  190. {
  191. return $this->_aObject['object'] . '/';
  192. }
  193. protected function getObjectBaseUrl ($isPrivate = false)
  194. {
  195. $sProto = $this->_bSSL ? 'https://' : 'http://';
  196. return $sProto . $this->_sBucket . '.' . ($this->_sEndpoint ? $this->_sEndpoint : 's3.amazonaws.com'). '/' . $this->getObjectBaseDir($isPrivate);
  197. }
  198. }
  199. /** @} */