storage.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if (!$oStorage || !method_exists($oStorage, 'download')) {
  15. ob_end_clean();
  16. bx_storage_download_error_occured();
  17. exit;
  18. }
  19. $i = strrpos($sFile, '.');
  20. $sRemoteId = ($i !== false) ? substr($sFile, 0, $i) : $sFile;
  21. if (!$sRemoteId) {
  22. ob_end_clean();
  23. bx_storage_download_error_occured();
  24. exit;
  25. }
  26. // redirect for remote storage in case if some references still pointing to local storage
  27. $aObject = $oStorage->getObjectData();
  28. if ('Local' != $aObject['engine']) {
  29. if (!($sUrl = $oStorage->getFileUrlByRemoteId($sFile))) {
  30. $sFile = preg_replace("/\.[A-Za-z0-9]+$/", '', $sFile);
  31. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  32. // Tmp fix for storages renaming in the past
  33. if (!$sUrl && 'bx_posts_files' == $sStorageObject && ($oStorage = BxDolStorage::getObjectInstance('bx_posts_covers')))
  34. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  35. if (!$sUrl) {
  36. bx_storage_download_error_occured();
  37. exit;
  38. }
  39. }
  40. header("Location: " . $sUrl);
  41. }
  42. if (!$oStorage->download($sRemoteId, $sToken)) {
  43. $iError = $oStorage->getErrorCode();
  44. switch ($iError) {
  45. case BX_DOL_STORAGE_ERR_FILE_NOT_FOUND:
  46. bx_storage_download_error_occured();
  47. exit;
  48. case BX_DOL_STORAGE_ERR_PERMISSION_DENIED:
  49. bx_storage_download_error_occured('displayAccessDenied');
  50. exit;
  51. default:
  52. bx_storage_download_error_occured('displayErrorOccured');
  53. exit;
  54. }
  55. }
  56. function bx_storage_download_error_occured($sMethod = 'displayPageNotFound')
  57. {
  58. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  59. $oTemplate = BxDolTemplate::getInstance();
  60. $oTemplate->$sMethod ();
  61. }
  62. /** @} */