dolibarr 24.0.0-beta
document.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005 Simon Tosser <simon@kornog-computing.com>
5 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2010 Pierre Morin <pierre.morin@auguria.net>
7 * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
8 * Copyright (C) 2022 Ferran Marcet <fmarcet@2byte.es>
9 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 * or see https://www.gnu.org/
25 */
26
36define('MAIN_SECURITY_FORCECSP', "default-src 'none'");
37
38//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
39//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
40if (!defined('NOTOKENRENEWAL')) {
41 define('NOTOKENRENEWAL', '1');
42}
43if (!defined('NOREQUIREMENU')) {
44 define('NOREQUIREMENU', '1');
45}
46if (!defined('NOREQUIREHTML')) {
47 define('NOREQUIREHTML', '1');
48}
49if (!defined('NOREQUIREAJAX')) {
50 define('NOREQUIREAJAX', '1');
51}
52
53if (!defined("NOLOGIN")) {
54 define("NOLOGIN", 1);
55}
56if (!defined("NOCSRFCHECK")) {
57 define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
58}
59if (!defined("NOIPCHECK")) {
60 define("NOIPCHECK", 1); // Do not check IP defined into conf $dolibarr_main_restrict_ip
61}
62
86function llxHeader($head = '', $title = '', $help_url = '', $target = '', $disablejs = 0, $disablehead = 0, $arrayofjs = '', $arrayofcss = '', $morequerystring = '', $morecssonbody = '', $replacemainareaby = '', $disablenofollow = 0, $disablenoindex = 0)
87{
88}
101function llxFooter($comment = '', $zone = 'private', $disabledoutputofmessages = 0)
102{
103}
104
105require '../../main.inc.php'; // Load $user and permissions
113require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
114require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
115require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
116
117$encoding = '';
118$original_file = GETPOST('file', 'alphanohtml');
119$modulepart = 'ticket'; // Forced to be sure wrapper is not used for something else
120$entity = GETPOSTISSET('entity') ? GETPOSTINT('entity') : $conf->entity;
121
122// Security check
123$socid = 0;
124if ($user->socid > 0) {
125 $socid = $user->socid;
126}
127
128// Check access to Module(s)
129if (!isModEnabled('ticket')) {
130 httponly_accessforbidden('Module Ticket is not enabled');
131}
132
133if (!getDolGlobalString('TICKET_ENABLE_PUBLIC_INTERFACE')) {
134 print $langs->trans('TicketPublicInterfaceForbidden');
135 exit;
136}
137
138global $dolibarr_main_instance_unique_id;
139$calcsecurekey = dol_hash('dolibarr-'.$original_file.'-'.$dolibarr_main_instance_unique_id, 'sha256');
140
141$securekey = GETPOST('securekey');
142
143if (!hash_equals($calcsecurekey, $securekey)) {
144 httponly_accessforbidden('Invalid securekey');
145}
146
147$object = new Ticket($db);
148
149
150/*
151 * Actions
152 */
153
154// None
155
156
157
158/*
159 * View
160 */
161
162
163// Define attachment (attachment=true to force choice popup 'open'/'save as')
164$attachment = true;
165if (preg_match('/\.(html|htm)$/i', $original_file)) {
166 $attachment = false;
167}
168if (isset($_GET["attachment"])) {
169 $attachment = GETPOST("attachment", 'alpha') ? true : false;
170}
171if (getDolGlobalString('MAIN_DISABLE_FORCE_SAVEAS')) {
172 $attachment = false;
173}
174
175// Define mime type
176$type = 'application/octet-stream'; // By default
177if (GETPOST('type', 'alpha')) {
178 $type = GETPOST('type', 'alpha');
179} else {
180 $type = dol_mimetype($original_file);
181}
182// Security: Force to octet-stream if file is a dangerous file. For example when it is a .noexe file
183// We do not force if file is a javascript to be able to get js from website module with <script src="
184// Note: Force whatever is $modulepart seems ok.
185if (!in_array($type, array('text/x-javascript')) && !dolIsAllowedForPreview($original_file)) {
186 $type = 'application/octet-stream';
187}
188
189// Security: Delete string ../ or ..\ into $original_file
190$original_file = preg_replace('/\.\.+/', '..', $original_file); // Replace '... or more' with '..'
191$original_file = str_replace('../', '/', $original_file);
192$original_file = str_replace('..\\', '/', $original_file);
193
194// Security check
195if (empty($modulepart)) {
196 accessforbidden('Bad value for parameter modulepart');
197}
198
199$accessallowed = 1;
200
201// Use dol_check_secure_access_document(); instead or not ?
202/*
203$sql = "SELECT rowid, src_object_id, src_object_type FROM ".MAIN_DB_PREFIX.'ecm_files';
204$sql .= " WHERE filename = '".$this->db->escape(basename($original_file))."'";
205$sql .= " AND filepath = '".$this->db->escape(basename($tmparray['dir_output']).'/'.dirname($original_file))."'";
206$resql = $this->db->query($sql);
207if ($resql) {
208 $obj = $this->db->fetch_object($resql);
209
210 if ($obj->src_object_id && $obj->src_object_type) {
211 // Create the virtual user
212 $tmpuser = new User($this->db);
213 $tmpuser->socid = $socId;
214
215 include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
216 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
217
218 // Use dol_check_secure_access_document(); instead or not ?
219 $ok = checkUserAccessToObject($tmpuser, array($obj->src_object_type), $obj->src_object_id, '', '', 'fk_soc');
220
221 $accessallowed = ($ok ? 1 : 0);
222 $pathdir = $tmparray['dir_output'];
223 }
224} else {
225 dol_print_error($this->db);
226}
227
228$ok = checkUserAccessToObject($user, array($obj->src_object_type), $obj->src_object_id, '', '', 'fk_soc');
229*/
230
231$fullpath_original_file = getMultidirOutput($object, 'ticket').'/'.$original_file;
232
233
234// Security:
235// Limit access if permissions are wrong
236if (!$accessallowed) { // @phpstan-ignore-line as value is set to 1 just before
238}
239
240// Security:
241// We refuse directory transversal change and pipes in file names
242if (preg_match('/\.\./', $fullpath_original_file) || preg_match('/[<>|]/', $fullpath_original_file)) {
243 dol_syslog("Refused to deliver file ".$fullpath_original_file);
244 print "ErrorFileNameInvalid: ".dol_escape_htmltag($original_file);
245 exit;
246}
247
248
249clearstatcache();
250
251$filename = basename($fullpath_original_file);
252$filename = preg_replace('/\.noexe$/i', '', $filename);
253
254// Output file on browser
255dol_syslog("document.php download $fullpath_original_file filename=$filename content-type=$type");
256$fullpath_original_file_osencoded = dol_osencode($fullpath_original_file); // New file name encoded in OS encoding charset
257
258// This test if file exists should be useless. We keep it to find bug more easily
259if (!file_exists($fullpath_original_file_osencoded)) {
260 dol_syslog("ErrorFileDoesNotExists: ".$fullpath_original_file);
261 print $langs->trans("ErrorFileDoesNotExists") . ' : ' . dol_escape_htmltag($original_file);
262 exit;
263}
264
265// Set this for test
266//$type = 'text/html'; $attachment = -1;
267
268// Permissions are ok and file found, so we return it
269top_httphead($type);
270
271header('Content-Description: File Transfer');
272if ($encoding) { // @phpstan-ignore-line as variable is set to '' and never change
273 header('Content-Encoding: '.$encoding);
274}
275// Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, attachment=need user action to open)
276
277if ($attachment > 0) {
278 header('Content-Disposition: attachment; filename="'.$filename.'"');
279} elseif (empty($attachment)) {
280 header('Content-Disposition: inline; filename="'.$filename.'"');
281}
282// Ajout directives pour resoudre bug IE
283header('Cache-Control: Public, must-revalidate');
284header('Pragma: public');
285$readfile = true;
286
287if (is_object($db)) {
288 $db->close();
289}
290
291// Send file now
292if ($readfile) { // @phpstan-ignore-line as value is set to true just before
293 header('Content-Length: '.dol_filesize($fullpath_original_file));
294
295 readfileLowMemory($fullpath_original_file_osencoded);
296}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_filesize($pathoffile)
Return size of a file.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getMultidirOutput($object, $module='', $forobject=0, $mode='output')
Return the full path of the directory where a module (or an object of a module) stores its files.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Class to generate the form for creating a new ticket.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.