26require_once DOL_DOCUMENT_ROOT .
'/core/lib/files.lib.php';
62 public $fullpath_original_file;
67 public $fullpath_original_file_osencoded;
77 public $original_file;
92 global $conf, $hookmanager;
94 define(
'MAIN_SECURITY_FORCECSP',
"default-src 'none'");
96 if (!defined(
'NOTOKENRENEWAL')) {
97 define(
'NOTOKENRENEWAL',
'1');
99 if (!defined(
'NOREQUIREMENU')) {
100 define(
'NOREQUIREMENU',
'1');
102 if (!defined(
'NOREQUIREHTML')) {
103 define(
'NOREQUIREHTML',
'1');
105 if (!defined(
'NOREQUIREAJAX')) {
106 define(
'NOREQUIREAJAX',
'1');
112 $action =
GETPOST(
'action',
'aZ09');
113 $original_file =
GETPOST(
'file',
'alphanohtml');
114 $modulepart =
GETPOST(
'modulepart',
'alpha');
119 if (empty($modulepart)) {
122 if (empty($original_file)) {
131 if (preg_match(
'/\.(html|htm)$/i', $original_file)) {
134 if (GETPOSTISSET(
"attachment")) {
135 $attachment =
GETPOST(
"attachment",
'alpha') ? true :
false;
142 if (
GETPOST(
'type',
'alpha')) {
143 $type =
GETPOST(
'type',
'alpha');
151 $type =
'application/octet-stream';
155 $original_file = preg_replace(
'/\.\.+/',
'..', $original_file);
156 $original_file = str_replace(
'../',
'/', $original_file);
157 $original_file = str_replace(
'..\\',
'/', $original_file);
161 $moduleName = $modulepart;
162 $moduleNameEn = $moduleName;
163 if ($moduleName ==
'commande') {
164 $moduleNameEn =
'order';
165 } elseif ($moduleName ==
'facture') {
166 $moduleNameEn =
'invoice';
168 $moduleNameUpperEn = strtoupper($moduleNameEn);
170 $hookmanager->initHooks(array(
'document'));
171 $parameters = array(
'ecmfile' => $ecmfile,
'modulepart' => $modulepart,
'original_file' => &$original_file,
'socId' => $socId,
172 'entity' => $entity,
'accessallowed' => &$accessallowed);
174 $reshook = $hookmanager->executeHooks(
'accessDownloadDocument', $parameters,
$object, $action);
176 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
177 dol_syslog(
"document.php - Errors when executing the hook 'accessDownloadDocument' : " . $errors);
178 print
"ErrorDownloadDocumentHooks: " . $errors;
180 } elseif (empty($reshook)) {
184 if (
getDolGlobalInt(
'WEBPORTAL_' . $moduleNameUpperEn .
'_LIST_ACCESS')
185 && in_array($type, array(
'application/pdf'))
187 &&
$context->logged_thirdparty->id == $socId
189 if (
isModEnabled($moduleName) && isset($conf->{$moduleName}->multidir_output[$entity])) {
190 $original_file = $conf->{$moduleName}->multidir_output[$entity] .
'/' . $original_file;
195 $fullpath_original_file = $original_file;
199 if (!$accessallowed) {
205 if (preg_match(
'/\.\./', $fullpath_original_file) || preg_match(
'/[<>|]/', $fullpath_original_file)) {
206 dol_syslog(
"Refused to deliver file " . $fullpath_original_file);
212 $refname = basename(dirname($original_file) .
"/");
214 $filename = basename($fullpath_original_file);
215 $filename = preg_replace(
'/\.noexe$/i',
'', $filename);
218 dol_syslog(
"document controller download $fullpath_original_file filename=$filename content-type=$type");
219 $fullpath_original_file_osencoded =
dol_osencode($fullpath_original_file);
222 if (!file_exists($fullpath_original_file_osencoded)) {
223 dol_syslog(
"ErrorFileDoesNotExists: " . $fullpath_original_file);
224 print
"ErrorFileDoesNotExists: " . $original_file;
229 $fileSizeMax =
getDolGlobalInt(
'MAIN_SECURITY_MAXFILESIZE_DOWNLOADED');
230 if ($fileSizeMax && $fileSize > ($fileSizeMax * 1024)) {
232 $fileSizeKb = round($fileSize / 1024, 2);
233 dol_syslog(
'ErrorFileSizeTooLarge: ' . $fileSize .
' bytes (' . $fileSizeKb .
' Kb) - max allowed: ' . $fileSizeMax .
' Kb');
234 print
'ErrorFileSizeTooLarge: ' . $fileSizeKb .
' Kb (max ' . $fileSizeMax .
' Kb)';
239 $hookmanager->initHooks(array(
'document'));
240 $parameters = array(
'ecmfile' => $ecmfile,
'modulepart' => $modulepart,
'original_file' => $original_file,
241 'entity' => $entity,
'refname' => $refname,
'fullpath_original_file' => $fullpath_original_file,
242 'filename' => $filename,
'fullpath_original_file_osencoded' => $fullpath_original_file_osencoded);
244 $reshook = $hookmanager->executeHooks(
'downloadDocument', $parameters,
$object, $action);
246 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
247 dol_syslog(
"document.php - Errors when executing the hook 'downloadDocument' : " . $errors);
248 print
"ErrorDownloadDocumentHooks: " . $errors;
252 $this->action = $action;
253 $this->attachment = $attachment;
254 $this->encoding = $encoding;
255 $this->entity = $entity;
256 $this->filename = $filename;
257 $this->fullpath_original_file = $fullpath_original_file;
258 $this->fullpath_original_file_osencoded = $fullpath_original_file_osencoded;
259 $this->modulepart = $modulepart;
260 $this->original_file = $original_file;
271 $this->accessRight =
true;
273 return parent::checkAccess();
285 if (!
$context->controllerInstance->checkAccess()) {
307 if (!
$context->controllerInstance->checkAccess()) {
313 $attachment = $this->attachment;
314 $encoding = $this->encoding;
315 $filename = $this->filename;
316 $fullpath_original_file = $this->fullpath_original_file;
317 $fullpath_original_file_osencoded = $this->fullpath_original_file_osencoded;
324 header(
'Content-Description: File Transfer');
326 header(
'Content-Encoding: ' . $encoding);
330 header(
'Content-Disposition: attachment; filename="' . $filename .
'"');
332 header(
'Content-Disposition: inline; filename="' . $filename .
'"');
334 header(
'Cache-Control: Public, must-revalidate');
335 header(
'Pragma: public');
338 header(
'Content-Length: ' .
dol_filesize($fullpath_original_file));
if(! $sortfield) if(! $sortorder) $object
static getInstance()
Singleton method to create one instance of this object.
Class for DocumentController.
action()
Action method is called before html output can be used to manage security and change context.
checkAccess()
Check current access to controller.
dol_filesize($pathoffile)
Return size of a file.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
readfileLowMemory($fullpath_original_file_osencoded, $method=-1)
Return a file on output using a low memory.
dolIsAllowedForPreview($file)
Return if a file is qualified for preview.
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.
$context
@method int call_trigger(string $triggerName, ?User $user)
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
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.