BxDolStorageS3.php 9.1 KB

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