document.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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=relativepathoffile
  28. * document.php?modulepart=logs&file=dolibarr.log
  29. * document.php?modulepart=logs&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('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  34. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  35. //if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  36. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  37. //if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  38. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  39. //if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  40. //if (! defined('NOREQUIREHOOK')) define('NOREQUIREHOOK','1'); // Disable "main.inc.php" hooks
  41. // For bittorent link, we don't need to load/check we are into a login session
  42. if (isset($_GET["modulepart"]) && $_GET["modulepart"] == 'bittorrent' && ! defined("NOLOGIN"))
  43. {
  44. define("NOLOGIN",1);
  45. define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  46. }
  47. // For direct external download link, we don't need to load/check we are into a login session
  48. if (isset($_GET["hashp"]) && ! defined("NOLOGIN"))
  49. {
  50. define("NOLOGIN",1);
  51. define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  52. }
  53. // Some value of modulepart can be used to get resources that are public so no login are required.
  54. if ((isset($_GET["modulepart"]) && $_GET["modulepart"] == 'medias') && ! defined("NOLOGIN"))
  55. {
  56. define("NOLOGIN",1);
  57. define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  58. }
  59. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  60. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  61. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  62. /**
  63. * Header empty
  64. *
  65. * @return void
  66. */
  67. function llxHeader() { }
  68. /**
  69. * Footer empty
  70. *
  71. * @return void
  72. */
  73. function llxFooter() { }
  74. require 'main.inc.php'; // Load $user and permissions
  75. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  76. $encoding = '';
  77. $action=GETPOST('action','alpha');
  78. $original_file=GETPOST('file','alphanohtml'); // Do not use urldecode here ($_GET are already decoded by PHP).
  79. $hashp=GETPOST('hashp','aZ09');
  80. $modulepart=GETPOST('modulepart','alpha');
  81. $urlsource=GETPOST('urlsource','alpha');
  82. $entity=GETPOST('entity','int')?GETPOST('entity','int'):$conf->entity;
  83. // Security check
  84. if (empty($modulepart) && empty($hashp)) accessforbidden('Bad link. Bad value for parameter modulepart',0,0,1);
  85. if (empty($original_file) && empty($hashp)) accessforbidden('Bad link. Missing identification to find file (original_file or hashp)',0,0,1);
  86. if ($modulepart == 'fckeditor') $modulepart='medias'; // For backward compatibility
  87. $socid=0;
  88. if ($user->societe_id > 0) $socid = $user->societe_id;
  89. // For some module part, dir may be privates
  90. if (in_array($modulepart, array('facture_paiement','unpaid')))
  91. {
  92. 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
  93. }
  94. /*
  95. * Action
  96. */
  97. // None
  98. /*
  99. * View
  100. */
  101. // Define mime type
  102. $type = 'application/octet-stream';
  103. if (GETPOST('type','alpha')) $type=GETPOST('type','alpha');
  104. else $type=dol_mimetype($original_file);
  105. // Define attachment (attachment=true to force choice popup 'open'/'save as')
  106. $attachment = true;
  107. if (preg_match('/\.(html|htm)$/i',$original_file)) $attachment = false;
  108. if (isset($_GET["attachment"])) $attachment = GETPOST("attachment",'alpha')?true:false;
  109. if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
  110. // If we have a hash public (hashp), we guess the original_file.
  111. if (! empty($hashp))
  112. {
  113. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  114. $ecmfile=new EcmFiles($db);
  115. $result = $ecmfile->fetch(0, '', '', '', $hashp);
  116. if ($result > 0)
  117. {
  118. $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory
  119. $moduleparttocheck = $tmp[0];
  120. if ($modulepart) // Not required for link using public hashp
  121. {
  122. if ($moduleparttocheck == $modulepart)
  123. {
  124. // We remove first level of directory
  125. $original_file = (($tmp[1]?$tmp[1].'/':'').$ecmfile->filename); // this is relative to module dir
  126. //var_dump($original_file); exit;
  127. }
  128. else
  129. {
  130. accessforbidden('Bad link. File is from another module part.',0,0,1);
  131. }
  132. }
  133. else
  134. {
  135. $modulepart = $moduleparttocheck;
  136. $original_file = (($tmp[1]?$tmp[1].'/':'').$ecmfile->filename); // this is relative to module dir
  137. }
  138. }
  139. else
  140. {
  141. $langs->load("errors");
  142. accessforbidden($langs->trans("ErrorFileNotFoundWithSharedLink"),0,0,1);
  143. }
  144. }
  145. // Security: Delete string ../ into $original_file
  146. $original_file = str_replace("../","/", $original_file);
  147. // Find the subdirectory name as the reference
  148. $refname=basename(dirname($original_file)."/");
  149. // Security check
  150. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  151. $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, $refname);
  152. $accessallowed = $check_access['accessallowed'];
  153. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  154. $fullpath_original_file = $check_access['original_file']; // $fullpath_original_file is now a full path name
  155. if (! empty($hashp))
  156. {
  157. $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
  158. $sqlprotectagainstexternals = '';
  159. }
  160. else
  161. {
  162. // Basic protection (against external users only)
  163. if ($user->societe_id > 0)
  164. {
  165. if ($sqlprotectagainstexternals)
  166. {
  167. $resql = $db->query($sqlprotectagainstexternals);
  168. if ($resql)
  169. {
  170. $num=$db->num_rows($resql);
  171. $i=0;
  172. while ($i < $num)
  173. {
  174. $obj = $db->fetch_object($resql);
  175. if ($user->societe_id != $obj->fk_soc)
  176. {
  177. $accessallowed=0;
  178. break;
  179. }
  180. $i++;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. // Security:
  187. // Limit access if permissions are wrong
  188. if (! $accessallowed)
  189. {
  190. accessforbidden();
  191. }
  192. // Security:
  193. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  194. if (preg_match('/\.\./',$fullpath_original_file) || preg_match('/[<>|]/',$fullpath_original_file))
  195. {
  196. dol_syslog("Refused to deliver file ".$fullpath_original_file);
  197. print "ErrorFileNameInvalid: ".$original_file;
  198. exit;
  199. }
  200. clearstatcache();
  201. $filename = basename($fullpath_original_file);
  202. // Output file on browser
  203. dol_syslog("document.php download $fullpath_original_file filename=$filename content-type=$type");
  204. $fullpath_original_file_osencoded=dol_osencode($fullpath_original_file); // New file name encoded in OS encoding charset
  205. // This test if file exists should be useless. We keep it to find bug more easily
  206. if (! file_exists($fullpath_original_file_osencoded))
  207. {
  208. dol_syslog("ErrorFileDoesNotExists: ".$fullpath_original_file);
  209. print "ErrorFileDoesNotExists: ".$original_file;
  210. exit;
  211. }
  212. // Permissions are ok and file found, so we return it
  213. top_httphead($type);
  214. header('Content-Description: File Transfer');
  215. if ($encoding) header('Content-Encoding: '.$encoding);
  216. // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, attachment=need user action to open)
  217. if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
  218. else header('Content-Disposition: inline; filename="'.$filename.'"');
  219. header('Content-Length: ' . dol_filesize($fullpath_original_file));
  220. // Ajout directives pour resoudre bug IE
  221. header('Cache-Control: Public, must-revalidate');
  222. header('Pragma: public');
  223. //ob_clean();
  224. //flush();
  225. readfile($fullpath_original_file_osencoded);
  226. if (is_object($db)) $db->close();