document.php 8.9 KB

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