BxDolStorageS3.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 . 'unaio/amazon-s3-php-class-hmac-v2/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 downloading by remote id. If file is private then token is checked.
  67. */
  68. public function download ($sRemoteId, $sToken = false, $bForceDownloadDialog = 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. if ($bForceDownloadDialog) {
  82. // download remote file to tmp
  83. $sTmpFilePath = BX_DIRECTORY_PATH_TMP . 'dwnld_'.$sRemoteId;
  84. if (!file_exists($sTmpFilePath)) {
  85. @file_put_contents($sTmpFilePath, bx_file_get_contents($sUrl));
  86. }
  87. // read from a local storage to be able to send it as attachment and give it a proper name
  88. if (!file_exists($sTmpFilePath) || !bx_smart_readfile($sTmpFilePath, $aFile['file_name'], $aFile['mime_type'], $aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl, $aFile['private'] ? 'private' : 'public', 'attachment')) {
  89. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  90. return false;
  91. }
  92. } else {
  93. header("Location: " . $sUrl);
  94. }
  95. return parent::download($aFile);
  96. }
  97. /**
  98. * Set file private or public.
  99. */
  100. public function setFilePrivate($iFileId, $isPrivate = true)
  101. {
  102. $aFile = $this->_oDb->getFileById($iFileId);
  103. $sFileLocation = $this->getObjectBaseDir($aFile['private']) . $aFile['path'];
  104. if (($aACP = $this->_s3->getAccessControlPolicy($this->_sBucket, $sFileLocation)) === false) {
  105. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  106. return false;
  107. }
  108. if (!is_array($aACP['acl']) || !$aACP['acl']) {
  109. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  110. return false;
  111. }
  112. // check current permissions
  113. $aNewACP = $aACP;
  114. unset($aNewACP['acl']);
  115. $aNewACP['acl'] = array();
  116. $aGroupPublic = false;
  117. $aGroupPrivate = false;
  118. foreach ($aACP['acl'] as $r) {
  119. if ('Group' == $r['type']) {
  120. if (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AllUsers')
  121. $aGroupPublic = $r;
  122. elseif (isset($r['uri']) && $r['uri'] == 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers')
  123. $aGroupPrivate = $r;
  124. else
  125. $aNewACP['acl'][] = $r;
  126. } else {
  127. $aNewACP['acl'][] = $r;
  128. }
  129. }
  130. // determine permissions changing
  131. $aGroupAdd = false;
  132. if ($isPrivate && (!$aGroupPrivate || $aGroupPublic)) {
  133. // make private
  134. $aGroupAdd = array (
  135. 'type' => 'Group',
  136. 'uri' => 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers',
  137. 'permission' => 'READ',
  138. );
  139. } elseif (!$isPrivate && ($aGroupPrivate || !$aGroupPublic)) {
  140. // make public
  141. $aGroupAdd = array (
  142. 'type' => 'Group',
  143. 'uri' => 'http://acs.amazonaws.com/groups/global/AllUsers',
  144. 'permission' => 'READ',
  145. );
  146. }
  147. // change permission if necessary
  148. if ($aGroupAdd) {
  149. $aNewACP['acl'][] = $aGroupAdd;
  150. if (!$this->_s3->setAccessControlPolicy($this->_sBucket, $sFileLocation, $aNewACP)) {
  151. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
  152. return false;
  153. }
  154. }
  155. return parent::setFilePrivate($iFileId, $isPrivate);
  156. }
  157. // ----------------
  158. protected function generateHeaders($sFileName, $isPrivate, $sMimeType = '')
  159. {
  160. if (!$sMimeType)
  161. $sMimeType = $this->getMimeTypeByFileName($sFileName);
  162. $aRequestHeaders = array (
  163. "Content-Type" => $sMimeType,
  164. );
  165. if ($this->_iCacheControl > 0) {
  166. $aRequestHeaders = array_merge ($aRequestHeaders, array (
  167. "Cache-Control" => "max-age=" . ($isPrivate && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl),
  168. ));
  169. }
  170. return $aRequestHeaders;
  171. }
  172. protected function addFileToEngine($sTmpFile, $sLocalId, $sName, $isPrivate, $iProfileId)
  173. {
  174. $sMimeType = $this->getMimeTypeByFileName($sName);
  175. $sExt = $this->getFileExt($sName);
  176. $sPath = $this->genPath($sLocalId, $this->_aObject['levels']);
  177. $sRemoteNamePath = $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  178. $aRequestHeaders = $this->generateHeaders($sName, $isPrivate);
  179. $aMetaHeaders = array();
  180. $sStorageClass = $this->_bReducedRedundancy ? S3::STORAGE_CLASS_RRS : S3::STORAGE_CLASS_STANDARD;
  181. $sACL = $isPrivate ? S3::ACL_AUTHENTICATED_READ : S3::ACL_PUBLIC_READ;
  182. $aInputFile = $this->_s3->inputFile($sTmpFile);
  183. if (!$this->_s3->putObject($aInputFile, $this->_sBucket, $this->getObjectBaseDir($isPrivate) . $sRemoteNamePath, $sACL, $aMetaHeaders, $aRequestHeaders, $sStorageClass)) {
  184. $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_ADD);
  185. return false;
  186. }
  187. return true;
  188. }
  189. protected function deleteFileFromEngine($sFilePath, $isPrivate)
  190. {
  191. $sFileLocation = $this->getObjectBaseDir($isPrivate) . $sFilePath;
  192. if (!$this->_s3->deleteObject($this->_sBucket, $sFileLocation)) {
  193. $this->setErrorCode(BX_DOL_STORAGE_ERR_UNLINK);
  194. return false;
  195. }
  196. return true;
  197. }
  198. protected function genRemoteNamePath ($sPath, $sLocalId, $sExt)
  199. {
  200. return $sPath . $sLocalId . ($sExt ? '.' . $sExt : '');
  201. }
  202. protected function getObjectBaseDir ($isPrivate = false)
  203. {
  204. return $this->_aObject['object'] . '/';
  205. }
  206. protected function getObjectBaseUrl ($isPrivate = false)
  207. {
  208. $sPath = $this->_bSSL ? 'https://' : 'http://';
  209. if ($this->_sDomain)
  210. $sPath .= $this->_sDomain;
  211. elseif ($this->_sEndpoint)
  212. $sPath .= $this->_sEndpoint . '/' . $this->_sBucket;
  213. else
  214. $sPath .= $this->_sBucket . '.s3.amazonaws.com';
  215. return $sPath . '/' . $this->getObjectBaseDir($isPrivate);
  216. }
  217. }
  218. /** @} */