82 global
$conf, $hookmanager, $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 $delaycache = ((is_numeric($cachestring) && (int) $cachestring > 1 && (
int) $cachestring < 999999) ? $cachestring :
'3600');
142 header(
'Cache-Control: max-age=' . $delaycache .
', public, must-revalidate');
143 header(
'Pragma: cache');
144 header(
'Expires: ' . gmdate(
'D, d M Y H:i:s', time() + (
int) $delaycache) .
' GMT');
148 if (!empty($hashp) && $hashp !=
'shared') {
149 include_once DOL_DOCUMENT_ROOT .
'/ecm/class/ecmfiles.class.php';
150 include_once DOL_DOCUMENT_ROOT .
'/core/lib/images.lib.php';
152 $result = $ecmfile->fetch(0,
'',
'',
'', $hashp);
154 $tmp = explode(
'/', $ecmfile->filepath, 2);
156 if (is_numeric($tmp[0])) {
157 $tmp = explode(
'/', $tmp[1], 2);
159 $moduleparttocheck = $tmp[0];
162 if ($moduleparttocheck != $modulepart) {
166 $modulepart = $moduleparttocheck;
168 $original_file = ((!empty($tmp[1]) ? $tmp[1] .
'/' :
'') . $ecmfile->filename);
179 $type =
'application/octet-stream';
180 if (
GETPOST(
'type',
'alpha')) {
181 $type =
GETPOST(
'type',
'alpha');
187 if (preg_match(
'/html/i', $type)) {
191 if (preg_match(
'/\.noexe$/i', $original_file)) {
196 $original_file = preg_replace(
'/\.\.+/',
'..', $original_file);
197 $original_file = str_replace(
'../',
'/', $original_file);
198 $original_file = str_replace(
'..\\',
'/', $original_file);
201 $refname = basename(dirname($original_file) .
"/");
202 if ($refname ==
'thumbs') {
204 $refname = basename(dirname(dirname($original_file)) .
"/");
213 if (empty($modulepart)) {
219 if ($modulepart ===
'medias' && $entity !=
$conf->entity) {
220 $conf->entity = $entity;
221 $conf->setValues($this->db);
224 $sqlprotectagainstexternals =
'';
225 $fullpath_original_file =
'';
229 $hookmanager->initHooks(array(
'viewimage'));
230 $parameters = array(
'modulepart' => $modulepart,
'original_file' => &$original_file,
231 'sqlprotectagainstexternals' => &$sqlprotectagainstexternals,
'fullpath_original_file' => &$fullpath_original_file,
232 'entity' => $entity,
'accessallowed' => &$accessallowed);
234 $reshook = $hookmanager->executeHooks(
'accessViewImage', $parameters,
$object, $action);
236 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
237 dol_syslog(
"document.php - Errors when executing the hook 'accessViewImage' : " . $errors);
238 print
"ErrorViewImageHooks: " . $errors;
240 } elseif (empty($reshook)) {
242 $accessallowed = $check_access[
'accessallowed'];
243 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
244 $fullpath_original_file = $check_access[
'original_file'];
247 if (!empty($hashp) && $hashp !=
'shared') {
249 $sqlprotectagainstexternals =
'';
252 if ($user->socid > 0) {
253 if ($sqlprotectagainstexternals) {
254 $resql = $this->db->query($sqlprotectagainstexternals);
256 $num = $this->db->num_rows($resql);
259 $obj = $this->db->fetch_object($resql);
260 if ($user->socid != $obj->fk_soc) {
273 if (!$accessallowed) {
279 if (preg_match(
'/\.\./', $fullpath_original_file) || preg_match(
'/[<>|]/', $fullpath_original_file)) {
280 dol_syslog(
"Refused to deliver file " . $fullpath_original_file);
286 $refname = basename(dirname($original_file) .
"/");
288 $filename = basename($fullpath_original_file);
289 $filename = preg_replace(
'/\.noexe$/i',
'', $filename);
292 dol_syslog(
"document controller download $fullpath_original_file filename=$filename content-type=$type");
293 $fullpath_original_file_osencoded =
dol_osencode($fullpath_original_file);
296 if (!file_exists($fullpath_original_file_osencoded)) {
297 dol_syslog(
"ErrorFileDoesNotExists: " . $fullpath_original_file);
298 print
"ErrorFileDoesNotExists: " . $original_file;
303 $fileSizeMax =
getDolGlobalInt(
'MAIN_SECURITY_MAXFILESIZE_DOWNLOADED');
304 if ($fileSizeMax && $fileSize > $fileSizeMax) {
305 dol_syslog(
'ErrorFileSizeTooLarge: ' . $fileSize);
306 print
'ErrorFileSizeTooLarge: ' . $fileSize .
' (max ' . $fileSizeMax .
' Kb)';
311 $hookmanager->initHooks(array(
'document'));
312 $parameters = array(
'modulepart' => $modulepart,
'original_file' => $original_file,
313 'entity' => $entity,
'refname' => $refname,
'fullpath_original_file' => $fullpath_original_file,
314 'filename' => $filename,
'fullpath_original_file_osencoded' => $fullpath_original_file_osencoded);
316 $reshook = $hookmanager->executeHooks(
'viewImage', $parameters,
$object, $action);
318 $errors = $hookmanager->error . (is_array($hookmanager->errors) ? (!empty($hookmanager->error) ?
', ' :
'') . implode(
', ', $hookmanager->errors) :
'');
319 dol_syslog(
"document.php - Errors when executing the hook 'viewImage' : " . $errors);
320 print
"ErrorViewImageHooks: " . $errors;
325 $this->entity = $entity;
326 $this->filename = $filename;
327 $this->fullpath_original_file = $fullpath_original_file;
328 $this->fullpath_original_file_osencoded = $fullpath_original_file_osencoded;
329 $this->modulepart = $modulepart;
330 $this->original_file = $original_file;