dolibarr 19.0.3
fileupload.php
Go to the documentation of this file.
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
24if (!defined('NOREQUIREMENU')) {
25 define('NOREQUIREMENU', '1'); // If there is no menu to show
26}
27if (!defined('NOREQUIREHTML')) {
28 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
29}
30if (!defined('NOREQUIREAJAX')) {
31 define('NOREQUIREAJAX', '1');
32}
33if (!defined('NOREQUIRESOC')) {
34 define('NOREQUIRESOC', '1');
35}
36
37// Load Dolibarr environment
38require '../../main.inc.php';
39require_once DOL_DOCUMENT_ROOT.'/core/class/fileupload.class.php'; // Class to upload common files
40require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
42
43$id = GETPOST('fk_element', 'int');
44$element = GETPOST('element', 'alpha'); // 'myobject' (myobject=mymodule) or 'myobject@mymodule' or 'myobject_mysubobject' (myobject=mymodule)
45$elementupload = $element;
46
47// Load object according to $id and $element
48$object = fetchObjectByElement($id, $element);
49
50$module = $object->module;
51$element = $object->element;
52
53$usesublevelpermission = ($module != $element ? $element : '');
54if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
55 $usesublevelpermission = '';
56}
57
58//print 'fileupload.php: '.$object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
59
60// Security check
61if (!empty($user->socid)) {
62 $socid = $user->socid;
63 if (!empty($object->socid) && $socid != $object->socid) {
64 httponly_accessforbidden("Access on object not allowed for this external user."); // This includes the exit.
65 }
66}
67
68$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
69if (!$result) {
70 httponly_accessforbidden('Not allowed by restrictArea (module='.$object->module.' table_element='.$object->table_element.')');
71}
72
73
74/*
75 * View
76 */
77
78$upload_handler = new FileUpload(null, $id, $elementupload);
79
80
82
83header('Pragma: no-cache');
84header('Cache-Control: no-store, no-cache, must-revalidate');
85header('Content-Disposition: inline; filename="files.json"');
86header('X-Content-Type-Options: nosniff');
87header('Access-Control-Allow-Origin: '.getRootURLFromURL(DOL_MAIN_URL_ROOT));
88header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE');
89header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');
90
91switch ($_SERVER['REQUEST_METHOD']) {
92 case 'OPTIONS':
93 break;
94 case 'HEAD':
95 case 'GET':
96 $upload_handler->get();
97 break;
98 case 'POST':
99 if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
100 $upload_handler->delete();
101 } else {
102 $upload_handler->post();
103 // 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)
104 }
105 break;
106 case 'DELETE':
107 $upload_handler->delete();
108 break;
109 default:
110 header('HTTP/1.0 405 Method Not Allowed');
111 exit;
112}
113
114$db->close();
This class is used to manage file upload using ajax.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
fetchObjectByElement($element_id, $element_type, $element_ref='')
Fetch an object from its id and element_type Inclusion of classes is automatic.
getRootURLFromURL($url)
Function root url from a long url For example: https://www.abc.mydomain.com/dir/page....
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
httponly_accessforbidden($message=1, $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.