fileupload.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /* Copyright (C) 2011-2012 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/ajax/fileupload.php
  20. * \brief File to return Ajax response on file upload
  21. */
  22. if (!defined('NOREQUIREMENU')) {
  23. define('NOREQUIREMENU', '1'); // If there is no menu to show
  24. }
  25. if (!defined('NOREQUIREHTML')) {
  26. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  27. }
  28. if (!defined('NOREQUIREAJAX')) {
  29. define('NOREQUIREAJAX', '1');
  30. }
  31. if (!defined('NOREQUIRESOC')) {
  32. define('NOREQUIRESOC', '1');
  33. }
  34. // Load Dolibarr environment
  35. require '../../main.inc.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/fileupload.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
  38. $id = GETPOST('fk_element', 'int');
  39. $element = GETPOST('element', 'alpha'); // 'myobject' (myobject=mymodule) or 'myobject@mymodule' or 'myobject_mysubobject' (myobject=mymodule)
  40. $elementupload = $element;
  41. // Load object according to $id and $element
  42. $object = fetchObjectByElement($id, $element);
  43. $module = $object->module;
  44. $element = $object->element;
  45. $usesublevelpermission = ($module != $element ? $element : '');
  46. if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
  47. $usesublevelpermission = '';
  48. }
  49. //print 'fileupload.php: '.$object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
  50. // Security check
  51. if (!empty($user->socid)) {
  52. $socid = $user->socid;
  53. if (!empty($object->socid) && $socid != $object->socid) {
  54. httponly_accessforbidden("Access on object not allowed for this external user."); // This includes the exit.
  55. }
  56. }
  57. $result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
  58. if (!$result) {
  59. httponly_accessforbidden('Not allowed by restrictArea (module='.$object->module.' table_element='.$object->table_element.')');
  60. }
  61. /*
  62. * View
  63. */
  64. $upload_handler = new FileUpload(null, $id, $elementupload);
  65. top_httphead();
  66. header('Pragma: no-cache');
  67. header('Cache-Control: no-store, no-cache, must-revalidate');
  68. header('Content-Disposition: inline; filename="files.json"');
  69. header('X-Content-Type-Options: nosniff');
  70. header('Access-Control-Allow-Origin: *');
  71. header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE');
  72. header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');
  73. switch ($_SERVER['REQUEST_METHOD']) {
  74. case 'OPTIONS':
  75. break;
  76. case 'HEAD':
  77. case 'GET':
  78. $upload_handler->get();
  79. break;
  80. case 'POST':
  81. if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
  82. $upload_handler->delete();
  83. } else {
  84. $upload_handler->post();
  85. // Note: even if this return an error on 1 file in post(), we will return http code 200 because error must be managed by the caller (some files may be ok and some in error)
  86. }
  87. break;
  88. case 'DELETE':
  89. $upload_handler->delete();
  90. break;
  91. default:
  92. header('HTTP/1.0 405 Method Not Allowed');
  93. exit;
  94. }
  95. $db->close();