ajax.attachment.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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'] = $oDoc->GetFileName();
  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. case 'cke_img_upload':
  90. // Image uploaded via CKEditor
  91. $aResult = array(
  92. 'uploaded' => 0,
  93. 'fileName' => '',
  94. 'url' => '',
  95. 'icon' => '',
  96. 'msg' => '',
  97. 'att_id' => 0,
  98. 'preview' => 'false',
  99. );
  100. $sObjClass = stripslashes(utils::ReadParam('obj_class', '', false, 'class'));
  101. $sTempId = utils::ReadParam('temp_id', '');
  102. if (empty($sObjClass))
  103. {
  104. $aResult['error'] = "Missing argument 'obj_class'";
  105. }
  106. elseif (empty($sTempId))
  107. {
  108. $aResult['error'] = "Missing argument 'temp_id'";
  109. }
  110. else
  111. {
  112. try
  113. {
  114. $oDoc = utils::ReadPostedDocument('upload');
  115. $oAttachment = MetaModel::NewObject('Attachment');
  116. $oAttachment->Set('expire', time() + 3600); // one hour...
  117. $oAttachment->Set('temp_id', $sTempId);
  118. $oAttachment->Set('item_class', $sObjClass);
  119. $oAttachment->SetDefaultOrgId();
  120. $oAttachment->Set('contents', $oDoc);
  121. $iAttId = $oAttachment->DBInsert();
  122. $aResult['uploaded'] = 1;
  123. $aResult['msg'] = $oDoc->GetFileName();
  124. $aResult['fileName'] = $oDoc->GetFileName();
  125. $aResult['url'] = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId;
  126. $aResult['icon'] = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($oDoc->GetFileName());
  127. $aResult['att_id'] = $iAttId;
  128. $aResult['preview'] = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  129. }
  130. catch (FileUploadException $e)
  131. {
  132. $aResult['error'] = $e->GetMessage();
  133. }
  134. }
  135. $oPage->add(json_encode($aResult));
  136. break;
  137. case 'cke_browse':
  138. $oPage = new NiceWebPage('Browse for image...');
  139. $oPage->add_linked_stylesheet(utils::GetAbsoluteUrlModulesRoot().'itop-attachments/css/magnific-popup.css');
  140. $oPage->add_linked_script(utils::GetAbsoluteUrlModulesRoot().'itop-attachments/js/jquery.magnific-popup.min.js');
  141. $sImgUrl = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL;
  142. $oPage->add_script(
  143. <<<EOF
  144. // Helper function to get parameters from the query string.
  145. function getUrlParam( paramName ) {
  146. var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' );
  147. var match = window.location.search.match( reParam );
  148. return ( match && match.length > 1 ) ? match[1] : null;
  149. }
  150. // Simulate user action of selecting a file to be returned to CKEditor.
  151. function returnFileUrl(iAttId, sAltText) {
  152. var funcNum = getUrlParam( 'CKEditorFuncNum' );
  153. var fileUrl = '$sImgUrl'+iAttId;
  154. window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl, function() {
  155. // Get the reference to a dialog window.
  156. var dialog = this.getDialog();
  157. // Check if this is the Image Properties dialog window.
  158. if ( dialog.getName() == 'image' ) {
  159. // Get the reference to a text field that stores the "alt" attribute.
  160. var element = dialog.getContentElement( 'info', 'txtAlt' );
  161. // Assign the new value.
  162. if ( element )
  163. element.setValue(sAltText);
  164. }
  165. // Return "false" to stop further execution. In such case CKEditor will ignore the second argument ("fileUrl")
  166. // and the "onSelect" function assigned to the button that called the file manager (if defined).
  167. // return false;
  168. } );
  169. window.close();
  170. }
  171. EOF
  172. );
  173. $oPage->add_ready_script(
  174. <<<EOF
  175. $('.img-picker').magnificPopup({type: 'image', closeOnContentClick: true });
  176. EOF
  177. );
  178. $sTempId = utils::ReadParam('temp_id');
  179. $sClass = utils::ReadParam('obj_class', '', false, 'class');
  180. $iObjectId = utils::ReadParam('obj_key', 0, false, 'integer');
  181. $sOQL = "SELECT Attachment WHERE ((temp_id = :temp_id) OR (item_class = :obj_class AND item_id = :obj_id))";
  182. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array(), array('temp_id' => $sTempId, 'obj_class' => $sClass, 'obj_id' => $iObjectId));
  183. while($oAttachment = $oSet->Fetch())
  184. {
  185. $oDoc = $oAttachment->Get('contents');
  186. if ($oDoc->GetMainMimeType() == 'image')
  187. {
  188. $sDocName = addslashes(htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8'));
  189. $iAttId = $oAttachment->GetKey();
  190. $oPage->add("<div style=\"float:left;margin:1em;text-align:center;\"><img class=\"img-picker\" style=\"max-width:300px;cursor:zoom-in\" href=\"{$sImgUrl}{$iAttId}\" alt=\"$sDocName\" title=\"$sDocName\" src=\"{$sImgUrl}{$iAttId}\"><br/><button onclick=\"returnFileUrl($iAttId, '$sDocName')\">Insert</button></div>");
  191. }
  192. }
  193. break;
  194. default:
  195. $oPage->p("Missing argument 'operation'");
  196. }
  197. $oPage->output();
  198. }
  199. catch (Exception $e)
  200. {
  201. // note: transform to cope with XSS attacks
  202. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  203. IssueLog::Error($e->getMessage());
  204. }
  205. ?>