82 global $conf, $hookmanager, $dolibarr_nocache, $user;
84 define(
'MAIN_SECURITY_FORCECSP',
"default-src 'none'");
86 if (!defined(
'NOREQUIRESOC')) {
87 define(
'NOREQUIRESOC',
'1');
89 if (!defined(
'NOREQUIRETRAN')) {
90 define(
'NOREQUIRETRAN',
'1');
92 if (!defined(
'NOCSRFCHECK')) {
93 define(
'NOCSRFCHECK',
'1');
95 if (!defined(
'NOTOKENRENEWAL')) {
96 define(
'NOTOKENRENEWAL',
'1');
98 if (!defined(
'NOREQUIREMENU')) {
99 define(
'NOREQUIREMENU',
'1');
101 if (!defined(
'NOREQUIREHTML')) {
102 define(
'NOREQUIREHTML',
'1');
104 if (!defined(
'NOREQUIREAJAX')) {
105 define(
'NOREQUIREAJAX',
'1');
111 $entity = (!empty($_GET[
'entity']) ? (int) $_GET[
'entity'] : (!empty($_POST[
'entity']) ? (int) $_POST[
'entity'] : 1));
112 define(
"DOLENTITY", $entity);
116 $action =
GETPOST(
'action',
'aZ09');
117 $original_file =
GETPOST(
'file',
'alphanohtml');
118 $hashp =
GETPOST(
'hashp',
'aZ09', 1);
119 $extname =
GETPOST(
'extname',
'alpha', 1);
120 $modulepart =
GETPOST(
'modulepart',
'alpha', 1);
121 $urlsource =
GETPOST(
'urlsource',
'alpha');
125 if (empty($modulepart) && empty($hashp)) {
128 if (empty($original_file) && empty($hashp) && $modulepart !=
'barcode') {
131 if ($modulepart ==
'fckeditor') {
132 $modulepart =
'medias';
135 $cachestring =
GETPOST(
"cache",
'aZ09');
141 if (empty($dolibarr_nocache)) {
142 $delaycache = ((is_numeric($cachestring) && (int) $cachestring > 1 && (
int) $cachestring < 999999) ? $cachestring :
'3600');
143 header(
'Cache-Control: max-age=' . $delaycache .
', public, must-revalidate');
144 header(
'Pragma: cache');
145 header(
'Expires: ' . gmdate(
'D, d M Y H:i:s', time() + (
int) $delaycache) .
' GMT');
148 header(
'Cache-Control: no-cache');
154 $type =
'application/octet-stream';
155 if (
GETPOST(
'type',
'alpha')) {
156 $type =
GETPOST(
'type',
'alpha');
162 if (preg_match(
'/html/i', $type)) {
166 if (preg_match(
'/\.noexe$/i', $original_file)) {
171 $original_file = preg_replace(
'/\.\.+/',
'..', $original_file);
172 $original_file = str_replace(
'../',
'/', $original_file);
173 $original_file = str_replace(
'..\\',
'/', $original_file);
176 $refname = basename(dirname($original_file) .
"/");
177 if ($refname ==
'thumbs') {
179 $refname = basename(dirname(dirname($original_file)) .
"/");
188 if (empty($modulepart)) {
194 if ($modulepart ===
'medias' && $entity != $conf->entity) {
195 $conf->entity = $entity;
196 $conf->setValues($this->db);
199 $sqlprotectagainstexternals =
'';
200 $fullpath_original_file =
'';
204 $hookmanager->initHooks(array(
'viewimage'));
205 $parameters = array(
'modulepart' => $modulepart,
'original_file' => &$original_file,
206 'sqlprotectagainstexternals' => &$sqlprotectagainstexternals,
'fullpath_original_file' => &$fullpath_original_file,
207 'entity' => $entity,
'accessallowed' => &$accessallowed);
209 $reshook = $hookmanager->executeHooks(
'accessViewImage', $parameters,
$object, $action);
211 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
212 dol_syslog(
"document.php - Errors when executing the hook 'accessViewImage' : " . $errors);
213 print
"ErrorViewImageHooks: " . $errors;
215 } elseif (empty($reshook)) {
217 $accessallowed = $check_access[
'accessallowed'];
218 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
219 $fullpath_original_file = $check_access[
'original_file'];
222 if (!empty($hashp)) {
224 $sqlprotectagainstexternals =
'';
227 if ($user->socid > 0) {
228 if ($sqlprotectagainstexternals) {
229 $resql = $this->db->query($sqlprotectagainstexternals);
231 $num = $this->db->num_rows($resql);
234 $obj = $this->db->fetch_object($resql);
235 if ($user->socid != $obj->fk_soc) {
248 if (!$accessallowed) {
254 if (preg_match(
'/\.\./', $fullpath_original_file) || preg_match(
'/[<>|]/', $fullpath_original_file)) {
255 dol_syslog(
"Refused to deliver file " . $fullpath_original_file);
261 $refname = basename(dirname($original_file) .
"/");
263 $filename = basename($fullpath_original_file);
264 $filename = preg_replace(
'/\.noexe$/i',
'', $filename);
267 dol_syslog(
"document controller download $fullpath_original_file filename=$filename content-type=$type");
268 $fullpath_original_file_osencoded =
dol_osencode($fullpath_original_file);
271 if (!file_exists($fullpath_original_file_osencoded)) {
272 dol_syslog(
"ErrorFileDoesNotExists: " . $fullpath_original_file);
273 print
"ErrorFileDoesNotExists: " . $original_file;
278 $fileSizeMax =
getDolGlobalInt(
'MAIN_SECURITY_MAXFILESIZE_DOWNLOADED');
279 if ($fileSizeMax && $fileSize > $fileSizeMax) {
280 dol_syslog(
'ErrorFileSizeTooLarge: ' . $fileSize);
281 print
'ErrorFileSizeTooLarge: ' . $fileSize .
' (max ' . $fileSizeMax .
' Kb)';
286 $hookmanager->initHooks(array(
'document'));
287 $parameters = array(
'modulepart' => $modulepart,
'original_file' => $original_file,
288 'entity' => $entity,
'refname' => $refname,
'fullpath_original_file' => $fullpath_original_file,
289 'filename' => $filename,
'fullpath_original_file_osencoded' => $fullpath_original_file_osencoded);
291 $reshook = $hookmanager->executeHooks(
'viewImage', $parameters,
$object, $action);
293 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
294 dol_syslog(
"document.php - Errors when executing the hook 'viewImage' : " . $errors);
295 print
"ErrorViewImageHooks: " . $errors;
300 $this->entity = $entity;
301 $this->filename = $filename;
302 $this->fullpath_original_file = $fullpath_original_file;
303 $this->fullpath_original_file_osencoded = $fullpath_original_file_osencoded;
304 $this->modulepart = $modulepart;
305 $this->original_file = $original_file;