ajax.attachment.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // Copyright (C) 2010-2012 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-2012 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::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  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. 'msg' => ''
  44. );
  45. $sObjClass = stripslashes(utils::ReadParam('obj_class', '', false, 'class'));
  46. $sTempId = utils::ReadParam('temp_id', '');
  47. if (empty($sObjClass))
  48. {
  49. $aResult['error'] = "Missing argument 'obj_class'";
  50. }
  51. elseif (empty($sTempId))
  52. {
  53. $aResult['error'] = "Missing argument 'temp_id'";
  54. }
  55. else
  56. {
  57. try
  58. {
  59. $oDoc = utils::ReadPostedDocument('file');
  60. $oAttachment = MetaModel::NewObject('Attachment');
  61. $oAttachment->Set('expire', time() + 3600); // one hour...
  62. $oAttachment->Set('temp_id', $sTempId);
  63. $oAttachment->Set('item_class', $sObjClass);
  64. $oAttachment->SetDefaultOrgId();
  65. $oAttachment->Set('contents', $oDoc);
  66. $iAttId = $oAttachment->DBInsert();
  67. $aResult['msg'] = $oDoc->GetFileName();
  68. $aResult['icon'] = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($oDoc->GetFileName());
  69. $aResult['att_id'] = $iAttId;
  70. }
  71. catch (FileUploadException $e)
  72. {
  73. $aResult['error'] = $e->GetMessage();
  74. }
  75. }
  76. $oPage->add(json_encode($aResult));
  77. break;
  78. case 'remove':
  79. $iAttachmentId = utils::ReadParam('att_id', '');
  80. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE id = :id");
  81. $oSet = new DBObjectSet($oSearch, array(), array('id' => $iAttachmentId));
  82. while ($oAttachment = $oSet->Fetch())
  83. {
  84. $oAttachment->DBDelete();
  85. }
  86. break;
  87. default:
  88. $oPage->p("Missing argument 'operation'");
  89. }
  90. $oPage->output();
  91. }
  92. catch (Exception $e)
  93. {
  94. // note: transform to cope with XSS attacks
  95. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  96. IssueLog::Error($e->getMessage());
  97. }
  98. ?>