* Copyright (C) 2004-2013 Laurent Destailleur * Copyright (C) 2005 Simon Tosser * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010 Pierre Morin * Copyright (C) 2010 Juanjo Menent * Copyright (C) 2022 Ferran Marcet * Copyright (C) 2024-2025 Frédéric France * Copyright (C) 2025 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * or see https://www.gnu.org/ */ /** * \file htdocs/document.php * \brief Wrapper to download data files * \remarks Call of this wrapper is made with URL: * DOL_URL_ROOT.'/document.php?modulepart=repfichierconcerne&file=relativepathoffile' * DOL_URL_ROOT.'/document.php?modulepart=logs&file=dolibarr.log' * DOL_URL_ROOT.'/document.php?hashp=sharekey' */ define('MAIN_SECURITY_FORCECSP', "default-src: 'none'"); //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language if (!defined('NOTOKENRENEWAL')) { define('NOTOKENRENEWAL', '1'); } if (!defined('NOREQUIREMENU')) { define('NOREQUIREMENU', '1'); } if (!defined('NOREQUIREHTML')) { define('NOREQUIREHTML', '1'); } if (!defined('NOREQUIREAJAX')) { define('NOREQUIREAJAX', '1'); } // For direct external download link, we don't need to load/check we are into a login session if (isset($_GET["hashp"]) && !defined("NOLOGIN")) { if (!defined("NOLOGIN")) { define("NOLOGIN", 1); } if (!defined("NOCSRFCHECK")) { define("NOCSRFCHECK", 1); // We accept to go on this page from external web site. } if (!defined("NOIPCHECK")) { define("NOIPCHECK", 1); // Do not check IP defined into conf $dolibarr_main_restrict_ip } } // Some value of modulepart can be used to get resources that are public so no login are required. // Keep $_GET here, GETPOST is not available yet if ((isset($_GET["modulepart"]) && $_GET["modulepart"] == 'medias')) { if (!defined("NOLOGIN")) { define("NOLOGIN", 1); } if (!defined("NOCSRFCHECK")) { define("NOCSRFCHECK", 1); // We accept to go on this page from external web site. } if (!defined("NOIPCHECK")) { define("NOIPCHECK", 1); // Do not check IP defined into conf $dolibarr_main_restrict_ip } } /** * Header empty * * Note: also called by functions.lib:recordNotFound * * @param string $head Optional head lines * @param string $title HTML title * @param string $help_url Url links to help page * Syntax is: For a wiki page: EN:EnglishPage|FR:FrenchPage|ES:SpanishPage|DE:GermanPage * For other external page: http://server/url * @param string $target Target to use on links * @param int $disablejs More content into html header * @param int $disablehead More content into html header * @param string[]|string $arrayofjs Array of complementary js files * @param string[]|string $arrayofcss Array of complementary css files * @param string $morequerystring Query string to add to the link "print" to get same parameters (use only if autodetect fails) * @param string $morecssonbody More CSS on body tag. For example 'classforhorizontalscrolloftabs'. * @param string $replacemainareaby Replace call to main_area() by a print of this string * @param int $disablenofollow Disable the "nofollow" on meta robot header * @param int $disablenoindex Disable the "noindex" on meta robot header * @return void * @phan-suppress PhanRedefineFunction */ function llxHeader($head = '', $title = '', $help_url = '', $target = '', $disablejs = 0, $disablehead = 0, $arrayofjs = '', $arrayofcss = '', $morequerystring = '', $morecssonbody = '', $replacemainareaby = '', $disablenofollow = 0, $disablenoindex = 0) { } /** * Footer empty * * Note: also called by functions.lib:recordNotFound * * @ignore * @param string $comment A text to add as HTML comment into HTML generated page * @param string $zone 'private' (for private pages) or 'public' (for public pages) * @param int $disabledoutputofmessages Clear all messages stored into session without displaying them * @return void * @phan-suppress PhanRedefineFunction */ function llxFooter($comment = '', $zone = 'private', $disabledoutputofmessages = 0) { } require 'main.inc.php'; // Load $user and permissions require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php'; /** * @var Conf $conf * @var DoliDB $db * @var HookManager $hookmanager * @var Translate $langs * @var User $user */ $encoding = ''; $action = GETPOST('action', 'aZ09'); $original_file = GETPOST('file', 'alphanohtml'); $hashp = GETPOST('hashp', 'aZ09'); $modulepart = GETPOST('modulepart', 'alpha'); $urlsource = GETPOST('urlsource', 'alpha'); $entity = GETPOSTISSET('entity') ? GETPOSTINT('entity') : $conf->entity; // Security check if (empty($modulepart) && empty($hashp)) { httponly_accessforbidden('Bad link. Bad value for parameter modulepart', 400); } if (empty($original_file) && empty($hashp)) { httponly_accessforbidden('Bad link. Missing identification to find file (original_file or hashp)', 400); } if ($modulepart == 'fckeditor') { $modulepart = 'medias'; // For backward compatibility } $socid = 0; if ($user->socid > 0) { $socid = $user->socid; } // For some module part, dir may be privates if (in_array($modulepart, array('facture_paiement', 'unpaid'))) { if (!$user->hasRight('societe', 'client', 'voir')) { $original_file = 'private/'.$user->id.'/'.$original_file; // If user has no permission to see all, output dir is specific to user } } /* * Actions */ // None /* * View */ // If we have a hash public (hashp), we guess the original_file. $ecmfile = ''; if (!empty($hashp)) { if (GETPOST('type', 'alpha')=='link') { require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php'; $link = new Link($db); $result = $link->fetch(0, $hashp); if ($result > 0 && !empty($link->url)) { header('Location: '.$link->url); exit; } else { $langs->load("errors"); httponly_accessforbidden($langs->trans("ErrorLinkNotFoundWithSharedLink"), 403, 1); } } else { include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php'; $ecmfile = new EcmFiles($db); $result = $ecmfile->fetch(0, '', '', '', $hashp); if ($result > 0) { $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory // filepath can be 'users/X' or 'X/propale/PR11111' if (is_numeric($tmp[0])) { // If first tmp is numeric, it is subdir of company for multicompany, we take next part. $tmp = explode('/', $tmp[1], 2); } $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path if ($modulepart) { // Not required, so often not defined, for link using public hashp parameter. if ($moduleparttocheck == $modulepart) { // We remove first level of directory $original_file = (($tmp[1] ? $tmp[1] . '/' : '') . $ecmfile->filename); // this is relative to module dir //var_dump($original_file); exit; } else { httponly_accessforbidden('Bad link. File is from another module part.', 403); } } else { $modulepart = $moduleparttocheck; $original_file = (($tmp[1] ? $tmp[1] . '/' : '') . $ecmfile->filename); // this is relative to module dir } $entity = $ecmfile->entity; if (isModEnabled('multicompany') && !empty($ecmfile->src_object_type) && $ecmfile->src_object_id > 0) { $object = fetchObjectByElement($ecmfile->src_object_id, $ecmfile->src_object_type); if (is_object($object) && $object->id > 0) { $entity = $object->entity; } } if ($entity != $conf->entity) { $conf->entity = $entity; $conf->setValues($db); } } else { $langs->load("errors"); httponly_accessforbidden($langs->trans("ErrorFileNotFoundWithSharedLink"), 403, 1); } } } // Define attachment (attachment=true to force choice popup 'open'/'save as') $attachment = true; if (preg_match('/\.(html|htm)$/i', $original_file)) { $attachment = false; } if (isset($_GET["attachment"])) { $attachment = GETPOST("attachment", 'alpha') ? true : false; } if (getDolGlobalString('MAIN_DISABLE_FORCE_SAVEAS')) { $attachment = false; } // Define mime type $type = 'application/octet-stream'; // By default if (GETPOST('type', 'alpha')) { $type = GETPOST('type', 'alpha'); } else { $type = dol_mimetype($original_file); } // Security: Force to octet-stream if file is a dangerous file. For example when it is a .noexe file // We do not force if file is a javascript to be able to get js from website module with