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');
145 $type = dol_mimetype($original_file);
150 if (!in_array($type, array(
'text/x-javascript')) && !dolIsAllowedForPreview($original_file)) {
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);
162 $tmparray = getElementProperties($modulepart);
164 $moduleName = $modulepart;
165 $moduleNameEn = $moduleName;
167 if ($moduleName ==
'commande') {
168 $moduleNameEn =
'order';
169 } elseif ($moduleName ==
'facture') {
170 $moduleNameEn =
'invoice';
172 $moduleNameUpperEn = strtoupper($moduleNameEn);
175 $hookmanager->initHooks(array(
'document'));
176 $parameters = array(
'ecmfile' => $ecmfile,
'modulepart' => $modulepart,
'original_file' => &$original_file,
'socId' => $socId,
177 'entity' => $entity,
'accessallowed' => &$accessallowed);
179 $reshook = $hookmanager->executeHooks(
'accessDownloadDocument', $parameters,
$object, $action);
181 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
182 dol_syslog(
"document.php - Errors when executing the hook 'accessDownloadDocument' : " . $errors);
183 print
"ErrorDownloadDocumentHooks: " . $errors;
186 if (empty($reshook)) {
190 if (
getDolGlobalInt(
'WEBPORTAL_' . $moduleNameUpperEn .
'_LIST_ACCESS')
191 && in_array($type, array(
'application/pdf'))
193 &&
$context->logged_thirdparty->id == $socId
195 if (
isModEnabled($moduleName) && isset(
$conf->{$moduleName}->multidir_output[$entity])) {
197 if (in_array($moduleName, array(
'facture',
'invoice',
'commande',
'order',
'propal',
'ticket'))) {
198 $sql =
"SELECT rowid, src_object_id, src_object_type FROM ".MAIN_DB_PREFIX.
'ecm_files';
199 $sql .=
" WHERE filename = '".$this->db->escape(basename($original_file)).
"'";
200 $sql .=
" AND filepath = '".$this->db->escape(basename($tmparray[
'dir_output']).
'/'.dirname($original_file)).
"'";
201 $resql = $this->db->query($sql);
203 $obj = $this->db->fetch_object($resql);
205 if ($obj->src_object_id && $obj->src_object_type) {
207 $tmpuser =
new User($this->db);
208 $tmpuser->socid = $socId;
210 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security.lib.php';
211 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
216 $accessallowed = ($ok ? 1 : 0);
217 $pathdir = $tmparray[
'dir_output'];
226 $pathdir = $hookmanager->resArray[
'pathdir'];
231 if (!$accessallowed) {
235 if (empty($pathdir)) {
236 print
"ErrorDownloadDocument: No path defined to find files";
239 $fullpath_original_file = $pathdir .
'/' . $original_file;
243 if (preg_match(
'/\.\./', $fullpath_original_file) || preg_match(
'/[<>|]/', $fullpath_original_file)) {
244 dol_syslog(
"Refused to deliver file " . $fullpath_original_file);
250 $refname = basename(dirname($original_file) .
"/");
252 $filename = basename($fullpath_original_file);
253 $filename = preg_replace(
'/\.noexe$/i',
'', $filename);
256 dol_syslog(
"document controller download $fullpath_original_file filename=$filename content-type=$type");
257 $fullpath_original_file_osencoded =
dol_osencode($fullpath_original_file);
260 if (!file_exists($fullpath_original_file_osencoded)) {
261 dol_syslog(
"ErrorFileDoesNotExists: " . $fullpath_original_file);
262 print
"ErrorFileDoesNotExists: " . $original_file;
267 $fileSizeMax =
getDolGlobalInt(
'MAIN_SECURITY_MAXFILESIZE_DOWNLOADED');
268 if ($fileSizeMax && $fileSize > ($fileSizeMax * 1024)) {
270 $fileSizeKb = round($fileSize / 1024, 2);
271 dol_syslog(
'ErrorFileSizeTooLarge: ' . $fileSize .
' bytes (' . $fileSizeKb .
' Kb) - max allowed: ' . $fileSizeMax .
' Kb');
272 print
'ErrorFileSizeTooLarge: ' . $fileSizeKb .
' Kb (max ' . $fileSizeMax .
' Kb)';
277 $hookmanager->initHooks(array(
'document'));
278 $parameters = array(
'ecmfile' => $ecmfile,
'modulepart' => $modulepart,
'original_file' => $original_file,
279 'entity' => $entity,
'refname' => $refname,
'fullpath_original_file' => $fullpath_original_file,
280 'filename' => $filename,
'fullpath_original_file_osencoded' => $fullpath_original_file_osencoded);
282 $reshook = $hookmanager->executeHooks(
'downloadDocument', $parameters,
$object, $action);
284 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
285 dol_syslog(
"document.php - Errors when executing the hook 'downloadDocument' : " . $errors);
286 print
"ErrorDownloadDocumentHooks: " . $errors;
291 $this->attachment = $attachment;
292 $this->encoding = $encoding;
293 $this->entity = $entity;
294 $this->filename = $filename;
295 $this->fullpath_original_file = $fullpath_original_file;
296 $this->fullpath_original_file_osencoded = $fullpath_original_file_osencoded;
297 $this->modulepart = $modulepart;
298 $this->original_file = $original_file;