ajax.document.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Handles various ajax requests
  20. *
  21. * @copyright Copyright (C) 2010-2016 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once('../approot.inc.php');
  25. require_once(APPROOT.'application/utils.inc.php');
  26. if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER) && (strlen($_SERVER['HTTP_IF_MODIFIED_SINCE']) > 0))
  27. {
  28. // The content is garanteed to be unmodified since the URL includes a signature based on the contents of the document
  29. header('not modified', true, 304);
  30. exit;
  31. }
  32. try
  33. {
  34. require_once(APPROOT.'/application/application.inc.php');
  35. require_once(APPROOT.'/application/webpage.class.inc.php');
  36. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  37. require_once(APPROOT.'/application/startup.inc.php');
  38. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  39. $oPage = new ajax_page("");
  40. $oPage->no_cache();
  41. $operation = utils::ReadParam('operation', '');
  42. $sClass = utils::ReadParam('class', 'MissingAjaxParam', false, 'class');
  43. switch($operation)
  44. {
  45. case 'download_document':
  46. LoginWebPage::DoLoginEx(null /* any portal */, false);
  47. $id = utils::ReadParam('id', '');
  48. $sField = utils::ReadParam('field', '');
  49. if ($sClass == 'Attachment')
  50. {
  51. $iCacheSec = 31556926; // One year ahead: an attachment cannot change
  52. }
  53. else
  54. {
  55. $iCacheSec = (int)utils::ReadParam('cache', 0);
  56. }
  57. if (!empty($sClass) && ($sClass != 'InlineImage') && !empty($id) && !empty($sField))
  58. {
  59. ormDocument::DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  60. if ($iCacheSec > 0)
  61. {
  62. $oPage->add_header("Expires: "); // Reset the value set in ajax_page
  63. $oPage->add_header("Cache-Control: no-transform,public,max-age=$iCacheSec,s-maxage=$iCacheSec");
  64. $oPage->add_header("Pragma: cache"); // Reset the value set .... where ?
  65. $oPage->add_header("Last-Modified: Wed, 15 Jun 2015 13:21:15 GMT"); // An arbitrary date in the past is ok
  66. }
  67. }
  68. break;
  69. case 'download_inlineimage':
  70. // No login is required because the "secret" protects us
  71. // Benefit: the inline image can be inserted into any HTML (templating = $this->html(public_log)$)
  72. $id = utils::ReadParam('id', '');
  73. $sSecret = utils::ReadParam('s', '');
  74. $iCacheSec = 31556926; // One year ahead: an inline image cannot change
  75. if (!empty($id) && !empty($sSecret))
  76. {
  77. ormDocument::DownloadDocument($oPage, 'InlineImage', $id, 'contents', 'inline', 'secret', $sSecret);
  78. $oPage->add_header("Expires: "); // Reset the value set in ajax_page
  79. $oPage->add_header("Cache-Control: no-transform,public,max-age=$iCacheSec,s-maxage=$iCacheSec");
  80. $oPage->add_header("Pragma: cache"); // Reset the value set .... where ?
  81. $oPage->add_header("Last-Modified: Wed, 15 Jun 2016 13:21:15 GMT"); // An arbitrary date in the past is ok
  82. }
  83. break;
  84. default:
  85. $oPage->p("Invalid query.");
  86. }
  87. $oPage->output();
  88. }
  89. catch (Exception $e)
  90. {
  91. // note: transform to cope with XSS attacks
  92. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  93. IssueLog::Error($e->getMessage()."\nDebug trace:\n".$e->getTraceAsString());
  94. }