dolibarr 25.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU 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 * This program 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25if (!defined('NOREQUIREMENU')) {
26 define('NOREQUIREMENU', '1'); // If there is no menu to show
27}
28if (!defined('NOREQUIREHTML')) {
29 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
30}
31if (!defined('NOREQUIREAJAX')) {
32 define('NOREQUIREAJAX', '1');
33}
34if (!defined('NOREQUIRESOC')) {
35 define('NOREQUIRESOC', '1');
36}
37
38// Load Dolibarr environment
39require '../../main.inc.php';
40require_once DOL_DOCUMENT_ROOT.'/core/class/fileupload.class.php'; // Class to upload common files
41require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
42require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
43
52$id = GETPOSTINT('fk_element');
53$element = GETPOST('element', 'alpha'); // 'myobject' (myobject=mymodule) or 'myobject@mymodule' or 'myobject_mysubobject' (myobject=mymodule)
54$elementupload = $element;
55
56// Load object according to $id and $element
57$object = fetchObjectByElement($id, $element);
58
59// fetchObjectByElement() returns an object even when the record was not found, and it returns a
60// GenericObject with no module when the element is unknown. In both cases restrictedArea() is then called
61// with an empty feature, and it grants the access without checking any permission, so we must stop here.
62// Note: fetchObjectByElement() may also return an int instead of an object when the module is disabled.
63// We answer the same http code and the same message than a refusal by restrictedArea() below, so that a
64// user can't tell an object that exists but is not allowed from an object that does not exist.
65if (!is_object($object) || empty($object->id) || empty($object->module)) {
66 dol_syslog("fileupload.php object ".$element." with id ".$id." was not found or its element is not supported", LOG_WARNING);
67 httponly_accessforbidden('Not allowed');
68}
69
70$module = $object->module;
71$element = $object->element;
72
73$usesublevelpermission = ($module != $element ? $element : '');
74if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
75 $usesublevelpermission = '';
76}
77
78//print 'fileupload.php: '.$object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
79
80// Security check
81if (!empty($user->socid)) {
82 $socid = $user->socid;
83 // socid is not declared on CommonObject, which is the type fetchObjectByElement() answers, and an object
84 // that has no third party does not own the property at all.
85 if (property_exists($object, 'socid') && !empty($object->socid) && $socid != $object->socid) { // @phan-suppress-current-line PhanUndeclaredProperty
86 // Same message than every other refusal of this page: a distinct one tells an external user that the
87 // object exists but belongs to another third party, which lets him enumerate the records of the others.
88 dol_syslog("fileupload.php object ".$element." with id ".$id." belongs to another third party than the external user", LOG_WARNING);
89 httponly_accessforbidden('Not allowed'); // This includes the exit.
90 }
91}
92
93$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
94if (!$result) {
95 // The module and the table are reported into the log only, they must not be disclosed to the caller
96 dol_syslog("fileupload.php not allowed by restrictedArea (module=".$object->module." table_element=".$object->table_element.")", LOG_WARNING);
97 httponly_accessforbidden('Not allowed');
98}
99
100
101/*
102 * View
103 */
104
106
107header('Pragma: no-cache');
108header('Cache-Control: no-store, no-cache, must-revalidate');
109header('Content-Disposition: inline; filename="files.json"');
110header('X-Content-Type-Options: nosniff');
111header('Access-Control-Allow-Origin: '.getRootURLFromURL(DOL_MAIN_URL_ROOT));
112header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE');
113header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');
114
115switch ($_SERVER['REQUEST_METHOD']) {
116 case 'OPTIONS':
117 break;
118 /*case 'HEAD':
119 case 'GET':
120 $upload_handler->get();
121 break;
122 */
123 case 'POST':
124 // The constructor throws an exception when the element does not support file uploading, or when the
125 // object was not found. Answer with the same json contract than post() so the caller can show the
126 // error, instead of letting a fatal error return an http code 500 with no usable content.
127 try {
128 $upload_handler = new FileUpload(null, $id, $elementupload);
129 } catch (Exception $e) {
130 // Same http code 200 and same content type negotiation than post(), the error is reported into the
131 // json content as the caller expects. Forcing 'application/json' would make jQuery parse the content
132 // by itself, and the JSON.parse() of the caller would then fail on an already parsed array.
133 if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
134 header('Content-type: application/json');
135 } else {
136 header('Content-type: text/plain');
137 }
138 echo json_encode(array(array('name' => '', 'error' => $e->getMessage())));
139 $db->close();
140 exit;
141 }
142
143 /*if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
144 $file = GETPOST('file');
145 $upload_handler->delete($file);
146 } else {*/
147 $upload_handler->post();
148 // 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)
149 //}
150 break;
151 /*case 'DELETE':
152 $file = GETPOST('file');
153 $upload_handler->delete($file);
154 break;*/
155 default:
156 header('HTTP/1.0 405 Method Not Allowed');
157 exit;
158}
159
160$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
This class is used to manage file upload using ajax.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
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.