ajax.attachment.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Handles various ajax requests
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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. echo $e->GetMessage();
  95. IssueLog::Error($e->getMessage());
  96. }
  97. ?>