storage.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  30. if (!$sUrl) {
  31. $sFile = preg_replace("/\.[A-Za-z0-9]+$/", '', $sFile);
  32. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  33. }
  34. // Tmp fix for storages renaming in the past
  35. if (!$sUrl && 'bx_posts_files' == $sStorageObject && ($oStorage = BxDolStorage::getObjectInstance('bx_posts_covers')))
  36. $sUrl = $oStorage->getFileUrlByRemoteId($sFile);
  37. if (!$sUrl)
  38. bx_storage_download_error_occured();
  39. else
  40. header("Location: " . $sUrl);
  41. exit;
  42. }
  43. if (!$oStorage->download($sRemoteId, $sToken)) {
  44. $iError = $oStorage->getErrorCode();
  45. switch ($iError) {
  46. case BX_DOL_STORAGE_ERR_FILE_NOT_FOUND:
  47. bx_storage_download_error_occured();
  48. exit;
  49. case BX_DOL_STORAGE_ERR_PERMISSION_DENIED:
  50. bx_storage_download_error_occured('displayAccessDenied');
  51. exit;
  52. default:
  53. bx_storage_download_error_occured('displayErrorOccured');
  54. exit;
  55. }
  56. }
  57. function bx_storage_download_error_occured($sMethod = 'displayPageNotFound')
  58. {
  59. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  60. $oTemplate = BxDolTemplate::getInstance();
  61. $oTemplate->$sMethod ();
  62. }
  63. /** @} */