ajax.attachment.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/application.inc.php');
  26. require_once(APPROOT.'/application/webpage.class.inc.php');
  27. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  28. try
  29. {
  30. require_once(APPROOT.'/application/startup.inc.php');
  31. // require_once(APPROOT.'/application/user.preferences.class.inc.php');
  32. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  33. LoginWebPage::DoLoginEx(null /* any portal */, false);
  34. $oPage = new ajax_page("");
  35. $oPage->no_cache();
  36. $sOperation = utils::ReadParam('operation', '');
  37. switch($sOperation)
  38. {
  39. case 'add':
  40. $aResult = array(
  41. 'error' => '',
  42. 'att_id' => 0,
  43. 'preview' => 'false',
  44. 'msg' => ''
  45. );
  46. $sObjClass = stripslashes(utils::ReadParam('obj_class', '', false, 'class'));
  47. $sTempId = utils::ReadParam('temp_id', '');
  48. if (empty($sObjClass))
  49. {
  50. $aResult['error'] = "Missing argument 'obj_class'";
  51. }
  52. elseif (empty($sTempId))
  53. {
  54. $aResult['error'] = "Missing argument 'temp_id'";
  55. }
  56. else
  57. {
  58. try
  59. {
  60. $oDoc = utils::ReadPostedDocument('file');
  61. $oAttachment = MetaModel::NewObject('Attachment');
  62. $oAttachment->Set('expire', time() + 3600); // one hour...
  63. $oAttachment->Set('temp_id', $sTempId);
  64. $oAttachment->Set('item_class', $sObjClass);
  65. $oAttachment->SetDefaultOrgId();
  66. $oAttachment->Set('contents', $oDoc);
  67. $iAttId = $oAttachment->DBInsert();
  68. $aResult['msg'] = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8');
  69. $aResult['icon'] = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($oDoc->GetFileName());
  70. $aResult['att_id'] = $iAttId;
  71. $aResult['preview'] = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  72. }
  73. catch (FileUploadException $e)
  74. {
  75. $aResult['error'] = $e->GetMessage();
  76. }
  77. }
  78. $oPage->add(json_encode($aResult));
  79. break;
  80. case 'remove':
  81. $iAttachmentId = utils::ReadParam('att_id', '');
  82. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE id = :id");
  83. $oSet = new DBObjectSet($oSearch, array(), array('id' => $iAttachmentId));
  84. while ($oAttachment = $oSet->Fetch())
  85. {
  86. $oAttachment->DBDelete();
  87. }
  88. break;
  89. default:
  90. $oPage->p("Missing argument 'operation'");
  91. }
  92. $oPage->output();
  93. }
  94. catch (Exception $e)
  95. {
  96. // note: transform to cope with XSS attacks
  97. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  98. IssueLog::Error($e->getMessage());
  99. }
  100. ?>