document.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Simon Tosser <simon@kornog-computing.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010 Pierre Morin <pierre.morin@auguria.net>
  7. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. * or see https://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/document.php
  25. * \brief Wrapper to download data files
  26. * \remarks Call of this wrapper is made with URL:
  27. * DOL_URL_ROOT.'/document.php?modulepart=repfichierconcerne&file=relativepathoffile'
  28. * DOL_URL_ROOT.'/document.php?modulepart=logs&file=dolibarr.log'
  29. * DOL_URL_ROOT.'/document.php?hashp=sharekey'
  30. */
  31. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  32. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  33. if (!defined('NOTOKENRENEWAL')) {
  34. define('NOTOKENRENEWAL', '1');
  35. }
  36. if (!defined('NOREQUIREMENU')) {
  37. define('NOREQUIREMENU', '1');
  38. }
  39. if (!defined('NOREQUIREHTML')) {
  40. define('NOREQUIREHTML', '1');
  41. }
  42. if (!defined('NOREQUIREAJAX')) {
  43. define('NOREQUIREAJAX', '1');
  44. }
  45. // For direct external download link, we don't need to load/check we are into a login session
  46. if (isset($_GET["hashp"]) && !defined("NOLOGIN")) {
  47. if (!defined("NOLOGIN")) {
  48. define("NOLOGIN", 1);
  49. }
  50. if (!defined("NOCSRFCHECK")) {
  51. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  52. }
  53. if (!defined("NOIPCHECK")) {
  54. define("NOIPCHECK", 1); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  55. }
  56. }
  57. // Some value of modulepart can be used to get resources that are public so no login are required.
  58. if ((isset($_GET["modulepart"]) && $_GET["modulepart"] == 'medias')) {
  59. if (!defined("NOLOGIN")) {
  60. define("NOLOGIN", 1);
  61. }
  62. if (!defined("NOCSRFCHECK")) {
  63. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  64. }
  65. if (!defined("NOIPCHECK")) {
  66. define("NOIPCHECK", 1); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  67. }
  68. }
  69. /**
  70. * Header empty
  71. *
  72. * @ignore
  73. * @return void
  74. */
  75. function llxHeader()
  76. {
  77. }
  78. /**
  79. * Footer empty
  80. *
  81. * @ignore
  82. * @return void
  83. */
  84. function llxFooter()
  85. {
  86. }
  87. require 'main.inc.php'; // Load $user and permissions
  88. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  89. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  90. $encoding = '';
  91. $action = GETPOST('action', 'aZ09');
  92. $original_file = GETPOST('file', 'alphanohtml'); // Do not use urldecode here ($_GET are already decoded by PHP).
  93. $hashp = GETPOST('hashp', 'aZ09');
  94. $modulepart = GETPOST('modulepart', 'alpha');
  95. $urlsource = GETPOST('urlsource', 'alpha');
  96. $entity = GETPOST('entity', 'int') ?GETPOST('entity', 'int') : $conf->entity;
  97. // Security check
  98. if (empty($modulepart) && empty($hashp)) {
  99. accessforbidden('Bad link. Bad value for parameter modulepart', 0, 0, 1);
  100. }
  101. if (empty($original_file) && empty($hashp)) {
  102. accessforbidden('Bad link. Missing identification to find file (original_file or hashp)', 0, 0, 1);
  103. }
  104. if ($modulepart == 'fckeditor') {
  105. $modulepart = 'medias'; // For backward compatibility
  106. }
  107. $socid = 0;
  108. if ($user->socid > 0) {
  109. $socid = $user->socid;
  110. }
  111. // For some module part, dir may be privates
  112. if (in_array($modulepart, array('facture_paiement', 'unpaid'))) {
  113. if (!$user->rights->societe->client->voir || $socid) {
  114. $original_file = 'private/'.$user->id.'/'.$original_file; // If user has no permission to see all, output dir is specific to user
  115. }
  116. }
  117. /*
  118. * Actions
  119. */
  120. // None
  121. /*
  122. * View
  123. */
  124. // If we have a hash public (hashp), we guess the original_file.
  125. if (!empty($hashp)) {
  126. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  127. $ecmfile = new EcmFiles($db);
  128. $result = $ecmfile->fetch(0, '', '', '', $hashp);
  129. if ($result > 0) {
  130. $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory
  131. // filepath can be 'users/X' or 'X/propale/PR11111'
  132. if (is_numeric($tmp[0])) { // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
  133. $tmp = explode('/', $tmp[1], 2);
  134. }
  135. $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path
  136. if ($modulepart) { // Not required, so often not defined, for link using public hashp parameter.
  137. if ($moduleparttocheck == $modulepart) {
  138. // We remove first level of directory
  139. $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
  140. //var_dump($original_file); exit;
  141. } else {
  142. accessforbidden('Bad link. File is from another module part.', 0, 0, 1);
  143. }
  144. } else {
  145. $modulepart = $moduleparttocheck;
  146. $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
  147. }
  148. } else {
  149. $langs->load("errors");
  150. accessforbidden($langs->trans("ErrorFileNotFoundWithSharedLink"), 0, 0, 1);
  151. }
  152. }
  153. // Define attachment (attachment=true to force choice popup 'open'/'save as')
  154. $attachment = true;
  155. if (preg_match('/\.(html|htm)$/i', $original_file)) {
  156. $attachment = false;
  157. }
  158. if (isset($_GET["attachment"])) {
  159. $attachment = GETPOST("attachment", 'alpha') ?true:false;
  160. }
  161. if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) {
  162. $attachment = false;
  163. }
  164. // Define mime type
  165. $type = 'application/octet-stream'; // By default
  166. if (GETPOST('type', 'alpha')) {
  167. $type = GETPOST('type', 'alpha');
  168. } else {
  169. $type = dol_mimetype($original_file);
  170. }
  171. // Security: Force to octet-stream if file is a dangerous file. For example when it is a .noexe file
  172. // We do not force if file is a javascript to be able to get js from website module with <script src="
  173. // Note: Force whatever is $modulepart seems ok.
  174. if (!in_array($type, array('text/x-javascript')) && !dolIsAllowedForPreview($original_file)) {
  175. $type = 'application/octet-stream';
  176. }
  177. // Security: Delete string ../ into $original_file
  178. $original_file = str_replace("../", "/", $original_file);
  179. // Find the subdirectory name as the reference
  180. $refname = basename(dirname($original_file)."/");
  181. // Security check
  182. if (empty($modulepart)) {
  183. accessforbidden('Bad value for parameter modulepart');
  184. }
  185. // Check security and set return info with full path of file
  186. $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, $user, $refname);
  187. $accessallowed = $check_access['accessallowed'];
  188. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  189. $fullpath_original_file = $check_access['original_file']; // $fullpath_original_file is now a full path name
  190. //var_dump($fullpath_original_file);exit;
  191. if (!empty($hashp)) {
  192. $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
  193. $sqlprotectagainstexternals = '';
  194. } else {
  195. // Basic protection (against external users only)
  196. if ($user->socid > 0) {
  197. if ($sqlprotectagainstexternals) {
  198. $resql = $db->query($sqlprotectagainstexternals);
  199. if ($resql) {
  200. $num = $db->num_rows($resql);
  201. $i = 0;
  202. while ($i < $num) {
  203. $obj = $db->fetch_object($resql);
  204. if ($user->socid != $obj->fk_soc) {
  205. $accessallowed = 0;
  206. break;
  207. }
  208. $i++;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. // Security:
  215. // Limit access if permissions are wrong
  216. if (!$accessallowed) {
  217. accessforbidden();
  218. }
  219. // Security:
  220. // We refuse directory transversal change and pipes in file names
  221. if (preg_match('/\.\./', $fullpath_original_file) || preg_match('/[<>|]/', $fullpath_original_file)) {
  222. dol_syslog("Refused to deliver file ".$fullpath_original_file);
  223. print "ErrorFileNameInvalid: ".dol_escape_htmltag($original_file);
  224. exit;
  225. }
  226. clearstatcache();
  227. $filename = basename($fullpath_original_file);
  228. $filename = preg_replace('/\.noexe$/i', '', $filename);
  229. // Output file on browser
  230. dol_syslog("document.php download $fullpath_original_file filename=$filename content-type=$type");
  231. $fullpath_original_file_osencoded = dol_osencode($fullpath_original_file); // New file name encoded in OS encoding charset
  232. // This test if file exists should be useless. We keep it to find bug more easily
  233. if (!file_exists($fullpath_original_file_osencoded)) {
  234. dol_syslog("ErrorFileDoesNotExists: ".$fullpath_original_file);
  235. print "ErrorFileDoesNotExists: ".$original_file;
  236. exit;
  237. }
  238. // Hooks
  239. if (!is_object($hookmanager)) {
  240. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  241. $hookmanager = new HookManager($this->db);
  242. }
  243. $hookmanager->initHooks(array('document'));
  244. $parameters = array('ecmfile' => $ecmfile, 'modulepart' => $modulepart, 'original_file' => $original_file,
  245. 'entity' => $entity, 'refname' => $refname, 'fullpath_original_file' => $fullpath_original_file,
  246. 'filename' => $filename, 'fullpath_original_file_osencoded' => $fullpath_original_file_osencoded);
  247. $reshook = $hookmanager->executeHooks('downloadDocument', $parameters); // Note that $action and $object may have been
  248. if ($reshook < 0) {
  249. $errors = $hookmanager->error.(is_array($hookmanager->errors) ? (!empty($hookmanager->error) ? ', ' : '').join($separator, $hookmanager->errors) : '');
  250. dol_syslog("document.php - Errors when executing the hook 'downloadDocument' : ".$errors);
  251. print "ErrorDownloadDocumentHooks: ".$errors;
  252. exit;
  253. }
  254. // Permissions are ok and file found, so we return it
  255. top_httphead($type);
  256. header('Content-Description: File Transfer');
  257. if ($encoding) {
  258. header('Content-Encoding: '.$encoding);
  259. }
  260. // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, attachment=need user action to open)
  261. if ($attachment) {
  262. header('Content-Disposition: attachment; filename="'.$filename.'"');
  263. } else {
  264. header('Content-Disposition: inline; filename="'.$filename.'"');
  265. }
  266. // Ajout directives pour resoudre bug IE
  267. header('Cache-Control: Public, must-revalidate');
  268. header('Pragma: public');
  269. $readfile = true;
  270. // on view document, can output images with good orientation according to exif infos
  271. if (!$attachment && !empty($conf->global->MAIN_USE_EXIF_ROTATION) && image_format_supported($fullpath_original_file_osencoded) == 1) {
  272. $imgres = correctExifImageOrientation($fullpath_original_file_osencoded, null);
  273. $readfile = !$imgres;
  274. }
  275. if (is_object($db)) {
  276. $db->close();
  277. }
  278. // Send file now
  279. if ($readfile) {
  280. header('Content-Length: '.dol_filesize($fullpath_original_file));
  281. readfileLowMemory($fullpath_original_file_osencoded);
  282. }