document.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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@capnetworks.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 <http://www.gnu.org/licenses/>.
  21. * or see http://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. * document.php?modulepart=repfichierconcerne&file=pathrelatifdufichier
  28. */
  29. define('NOTOKENRENEWAL',1); // Disables token renewal
  30. // Pour autre que bittorrent, on charge environnement + info issus de logon (comme le user)
  31. if (isset($_GET["modulepart"]) && $_GET["modulepart"] == 'bittorrent' && ! defined("NOLOGIN"))
  32. {
  33. define("NOLOGIN",1);
  34. define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  35. }
  36. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  37. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  38. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  39. /**
  40. * Header empty
  41. *
  42. * @return void
  43. */
  44. function llxHeader() { }
  45. /**
  46. * Footer empty
  47. *
  48. * @return void
  49. */
  50. function llxFooter() { }
  51. require 'main.inc.php'; // Load $user and permissions
  52. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  53. $encoding = '';
  54. $action=GETPOST('action','alpha');
  55. $original_file=GETPOST('file','alpha'); // Do not use urldecode here ($_GET are already decoded by PHP).
  56. $modulepart=GETPOST('modulepart','alpha');
  57. $urlsource=GETPOST('urlsource','alpha');
  58. $entity=GETPOST('entity','int')?GETPOST('entity','int'):$conf->entity;
  59. // Security check
  60. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  61. if ($modulepart == 'fckeditor') $modulepart='medias'; // For backward compatibility
  62. $socid=0;
  63. if ($user->societe_id > 0) $socid = $user->societe_id;
  64. // For some module part, dir may be privates
  65. if (in_array($modulepart,array('facture_paiement','unpaid')))
  66. {
  67. if (! $user->rights->societe->client->voir || $socid) $original_file='private/'.$user->id.'/'.$original_file; // If user has no permission to see all, output dir is specific to user
  68. }
  69. /*
  70. * Action
  71. */
  72. // None
  73. /*
  74. * View
  75. */
  76. // Define mime type
  77. $type = 'application/octet-stream';
  78. if (GETPOST('type','alpha')) $type=GETPOST('type','alpha');
  79. else $type=dol_mimetype($original_file);
  80. // Define attachment (attachment=true to force choice popup 'open'/'save as')
  81. $attachment = true;
  82. if (preg_match('/\.(html|htm)$/i',$original_file)) $attachment = false;
  83. if (isset($_GET["attachment"])) $attachment = GETPOST("attachment",'alpha')?true:false;
  84. if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
  85. // Security: Delete string ../ into $original_file
  86. $original_file = str_replace("../","/", $original_file);
  87. // Find the subdirectory name as the reference
  88. $refname=basename(dirname($original_file)."/");
  89. // Security check
  90. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  91. $check_access = dol_check_secure_access_document($modulepart,$original_file,$entity,$refname);
  92. $accessallowed = $check_access['accessallowed'];
  93. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  94. $original_file = $check_access['original_file']; // original_file is now a full path name
  95. // Basic protection (against external users only)
  96. if ($user->societe_id > 0)
  97. {
  98. if ($sqlprotectagainstexternals)
  99. {
  100. $resql = $db->query($sqlprotectagainstexternals);
  101. if ($resql)
  102. {
  103. $num=$db->num_rows($resql);
  104. $i=0;
  105. while ($i < $num)
  106. {
  107. $obj = $db->fetch_object($resql);
  108. if ($user->societe_id != $obj->fk_soc)
  109. {
  110. $accessallowed=0;
  111. break;
  112. }
  113. $i++;
  114. }
  115. }
  116. }
  117. }
  118. // Security:
  119. // Limite acces si droits non corrects
  120. if (! $accessallowed)
  121. {
  122. accessforbidden();
  123. }
  124. // Security:
  125. // On interdit les remontees de repertoire ainsi que les pipe dans
  126. // les noms de fichiers.
  127. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
  128. {
  129. dol_syslog("Refused to deliver file ".$original_file);
  130. $file=basename($original_file); // Do no show plain path of original_file in shown error message
  131. dol_print_error(0,$langs->trans("ErrorFileNameInvalid",$file));
  132. exit;
  133. }
  134. clearstatcache();
  135. $filename = basename($original_file);
  136. // Output file on browser
  137. dol_syslog("document.php download $original_file $filename content-type=$type");
  138. $original_file_osencoded=dol_osencode($original_file); // New file name encoded in OS encoding charset
  139. // This test if file exists should be useless. We keep it to find bug more easily
  140. if (! file_exists($original_file_osencoded))
  141. {
  142. dol_print_error(0,$langs->trans("ErrorFileDoesNotExists",$original_file));
  143. exit;
  144. }
  145. // Permissions are ok and file found, so we return it
  146. top_httphead($type);
  147. header('Content-Description: File Transfer');
  148. if ($encoding) header('Content-Encoding: '.$encoding);
  149. // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, atachment=need user action to open)
  150. if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
  151. else header('Content-Disposition: inline; filename="'.$filename.'"');
  152. header('Content-Length: ' . dol_filesize($original_file));
  153. // Ajout directives pour resoudre bug IE
  154. header('Cache-Control: Public, must-revalidate');
  155. header('Pragma: public');
  156. //ob_clean();
  157. //flush();
  158. readfile($original_file_osencoded);
  159. if (is_object($db)) $db->close();