storage.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  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. require_once('./inc/header.inc.php');
  10. $sStorageObject = bx_process_input(bx_get('o'));
  11. $sFile = bx_process_input(bx_get('f'));
  12. $sToken = bx_process_input(bx_get('t'));
  13. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  14. // upload action implementation
  15. if ($oStorage && bx_get('a') == 'upload' && bx_get('t')) {
  16. header('Content-Type: application/json; charset=utf-8');
  17. $iProfileId = bx_get_logged_profile_id();
  18. if (!($iId = $oStorage->storeFileFromForm($_FILES['file'], false, $iProfileId))) {
  19. echo json_encode(array('error' => '1'));
  20. exit;
  21. }
  22. $oStorage->afterUploadCleanup($iId, $iProfileId);
  23. $aFileInfo = $oStorage->getFile($iId);
  24. if ($aFileInfo && in_array($aFileInfo['ext'], array('jpg', 'jpeg', 'jpe', 'png'))) {
  25. $oTranscoder = BxDolTranscoderImage::getObjectInstance(bx_get('t'));
  26. $sUrl = $oTranscoder->getFileUrl($iId);
  27. }
  28. else {
  29. $sUrl = $oStorage->getFileUrlById($iId);
  30. }
  31. echo json_encode(array('link' => $sUrl));
  32. exit;
  33. }
  34. if (!$oStorage || !method_exists($oStorage, 'download')) {
  35. ob_end_clean();
  36. bx_storage_download_error_occured();
  37. exit;
  38. }
  39. $i = strrpos($sFile, '.');
  40. $sRemoteId = ($i !== false) ? substr($sFile, 0, $i) : $sFile;
  41. if (!$sRemoteId) {
  42. ob_end_clean();
  43. bx_storage_download_error_occured();
  44. exit;
  45. }
  46. // redirect for remote storage in case if some references still pointing to local storage
  47. $aObject = $oStorage->getObjectData();
  48. if ('Local' != $aObject['engine']) {
  49. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  50. if (!$sUrl) {
  51. $sFile = preg_replace("/\.[A-Za-z0-9]+$/", '', $sFile);
  52. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  53. }
  54. // Tmp fix for storages renaming in the past
  55. if (!$sUrl && 'bx_posts_files' == $sStorageObject && ($oStorage = BxDolStorage::getObjectInstance('bx_posts_covers')))
  56. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  57. if (!$sUrl)
  58. bx_storage_download_error_occured();
  59. else
  60. header("Location: " . $sUrl);
  61. exit;
  62. }
  63. if (!$oStorage->download($sRemoteId, $sToken)) {
  64. $iError = $oStorage->getErrorCode();
  65. switch ($iError) {
  66. case BX_DOL_STORAGE_ERR_FILE_NOT_FOUND:
  67. bx_storage_download_error_occured();
  68. exit;
  69. case BX_DOL_STORAGE_ERR_PERMISSION_DENIED:
  70. bx_storage_download_error_occured('displayAccessDenied');
  71. exit;
  72. default:
  73. bx_storage_download_error_occured('displayErrorOccured');
  74. exit;
  75. }
  76. }
  77. function bx_storage_download_error_occured($sMethod = 'displayPageNotFound')
  78. {
  79. bx_import('BxDolLanguages');
  80. if ('displayPageNotFound' == $sMethod) {
  81. header('HTTP/1.0 404 Not Found');
  82. header('Status: 404 Not Found');
  83. echo json_encode(['status' => 404, 'error' => _t("_sys_request_page_not_found_cpt")]);
  84. }
  85. if ('displayAccessDenied' == $sMethod) {
  86. header('HTTP/1.0 403 Forbidden');
  87. header('Status: 403 Forbidden');
  88. echo json_encode(['status' => 403, 'error' => _t("_Access denied")]);
  89. }
  90. if ('displayErrorOccured' == $sMethod) {
  91. header('HTTP/1.0 500 Internal Server Error');
  92. header('Status: 500 Internal Server Error');
  93. echo json_encode(['status' => 500, 'error' => _t("_error occured")]);
  94. }
  95. }
  96. /** @} */