viewimage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2016 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/viewimage.php
  22. * \brief Wrapper to show images into Dolibarr screens.
  23. * \remarks Call to wrapper is :
  24. * DOL_URL_ROOT.'/viewimage.php?modulepart=diroffile&file=relativepathofofile&cache=0
  25. * DOL_URL_ROOT.'/viewimage.php?hashp=sharekey
  26. */
  27. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  28. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  29. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  30. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
  31. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
  32. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
  33. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
  34. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
  35. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
  36. // Some value of modulepart can be used to get resources that are public so no login are required.
  37. // Note that only directory logo is free to access without login.
  38. if (isset($_GET["modulepart"]) && $_GET["modulepart"] == 'mycompany' && preg_match('/^\/?logos\//', $_GET['file']))
  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"]) && ! defined("NOLOGIN"))
  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. // For multicompany
  59. $entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  60. if (is_numeric($entity)) define("DOLENTITY", $entity);
  61. /**
  62. * Header empty
  63. *
  64. * @ignore
  65. * @return void
  66. */
  67. function llxHeader()
  68. {
  69. }
  70. /**
  71. * Footer empty
  72. *
  73. * @ignore
  74. * @return void
  75. */
  76. function llxFooter()
  77. {
  78. }
  79. require 'main.inc.php'; // Load $user and permissions
  80. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  81. $action=GETPOST('action', 'alpha');
  82. $original_file=GETPOST('file', 'alphanohtml'); // Do not use urldecode here ($_GET are already decoded by PHP).
  83. $hashp=GETPOST('hashp', 'aZ09');
  84. $modulepart=GETPOST('modulepart', 'alpha');
  85. $urlsource=GETPOST('urlsource', 'alpha');
  86. $entity=GETPOST('entity', 'int')?GETPOST('entity', 'int'):$conf->entity;
  87. // Security check
  88. if (empty($modulepart) && empty($hashp)) accessforbidden('Bad link. Bad value for parameter modulepart', 0, 0, 1);
  89. if (empty($original_file) && empty($hashp) && $modulepart != 'barcode') accessforbidden('Bad link. Missing identification to find file (param file or hashp)', 0, 0, 1);
  90. if ($modulepart == 'fckeditor') $modulepart='medias'; // For backward compatibility
  91. /*
  92. * Actions
  93. */
  94. // None
  95. /*
  96. * View
  97. */
  98. if (GETPOST("cache", 'alpha'))
  99. {
  100. // Important: Following code is to avoid page request by browser and PHP CPU at
  101. // each Dolibarr page access.
  102. if (empty($dolibarr_nocache))
  103. {
  104. header('Cache-Control: max-age=3600, public, must-revalidate');
  105. header('Pragma: cache'); // This is to avoid having Pragma: no-cache
  106. }
  107. else header('Cache-Control: no-cache');
  108. //print $dolibarr_nocache; exit;
  109. }
  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. // filepath can be 'users/X' or 'X/propale/PR11111'
  120. if (is_numeric($tmp[0])) // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
  121. {
  122. $tmp = explode('/', $tmp[1], 2);
  123. }
  124. $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path
  125. if ($modulepart) // Not required, so often not defined, for link using public hashp parameter.
  126. {
  127. if ($moduleparttocheck == $modulepart)
  128. {
  129. // We remove first level of directory
  130. $original_file = (($tmp[1]?$tmp[1].'/':'').$ecmfile->filename); // this is relative to module dir
  131. //var_dump($original_file); exit;
  132. }
  133. else
  134. {
  135. accessforbidden('Bad link. File is from another module part.', 0, 0, 1);
  136. }
  137. }
  138. else
  139. {
  140. $modulepart = $moduleparttocheck;
  141. $original_file = (($tmp[1]?$tmp[1].'/':'').$ecmfile->filename); // this is relative to module dir
  142. }
  143. }
  144. else
  145. {
  146. $langs->load("errors");
  147. accessforbidden($langs->trans("ErrorFileNotFoundWithSharedLink"), 0, 0, 1);
  148. }
  149. }
  150. // Define mime type
  151. $type = 'application/octet-stream';
  152. if (GETPOST('type', 'alpha')) $type=GETPOST('type', 'alpha');
  153. else $type=dol_mimetype($original_file);
  154. // Security: This wrapper is for images. We do not allow type/html
  155. if (preg_match('/html/i', $type)) accessforbidden('Error: Using the image wrapper to output a file with a mime type HTML is not possible.', 0, 0, 1);
  156. // Security: This wrapper is for images. We do not allow files ending with .noexe
  157. if (preg_match('/\.noexe$/i', $original_file)) accessforbidden('Error: Using the image wrapper to output a file ending with .noexe is not allowed.', 0, 0, 1);
  158. // Security: Delete string ../ into $original_file
  159. $original_file = str_replace("../", "/", $original_file);
  160. // Find the subdirectory name as the reference
  161. $refname=basename(dirname($original_file)."/");
  162. // Security check
  163. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart', 0, 0, 1);
  164. $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, $refname);
  165. $accessallowed = $check_access['accessallowed'];
  166. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  167. $fullpath_original_file = $check_access['original_file']; // $fullpath_original_file is now a full path name
  168. if (! empty($hashp))
  169. {
  170. $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
  171. $sqlprotectagainstexternals = '';
  172. }
  173. else
  174. {
  175. // Basic protection (against external users only)
  176. if ($user->socid > 0)
  177. {
  178. if ($sqlprotectagainstexternals)
  179. {
  180. $resql = $db->query($sqlprotectagainstexternals);
  181. if ($resql)
  182. {
  183. $num=$db->num_rows($resql);
  184. $i=0;
  185. while ($i < $num)
  186. {
  187. $obj = $db->fetch_object($resql);
  188. if ($user->socid != $obj->fk_soc)
  189. {
  190. $accessallowed=0;
  191. break;
  192. }
  193. $i++;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. // Security:
  200. // Limit access if permissions are wrong
  201. if (! $accessallowed)
  202. {
  203. accessforbidden();
  204. }
  205. // Security:
  206. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  207. if (preg_match('/\.\./', $fullpath_original_file) || preg_match('/[<>|]/', $fullpath_original_file))
  208. {
  209. dol_syslog("Refused to deliver file ".$fullpath_original_file);
  210. print "ErrorFileNameInvalid: ".$original_file;
  211. exit;
  212. }
  213. if ($modulepart == 'barcode')
  214. {
  215. $generator=GETPOST("generator", "alpha");
  216. $code=GETPOST("code", 'none'); // This can be rich content (qrcode, datamatrix, ...)
  217. $encoding=GETPOST("encoding", "alpha");
  218. $readable=GETPOST("readable", 'alpha')?GETPOST("readable", "alpha"):"Y";
  219. if (empty($generator) || empty($encoding))
  220. {
  221. print 'Error: Parameter "generator" or "encoding" not defined';
  222. exit;
  223. }
  224. $dirbarcode=array_merge(array("/core/modules/barcode/doc/"), $conf->modules_parts['barcode']);
  225. $result=0;
  226. foreach($dirbarcode as $reldir)
  227. {
  228. $dir=dol_buildpath($reldir, 0);
  229. $newdir=dol_osencode($dir);
  230. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  231. if (! is_dir($newdir)) continue;
  232. $result=@include_once $newdir.$generator.'.modules.php';
  233. if ($result) break;
  234. }
  235. // Load barcode class
  236. $classname = "mod".ucfirst($generator);
  237. $module = new $classname($db);
  238. if ($module->encodingIsSupported($encoding))
  239. {
  240. $result=$module->buildBarCode($code, $encoding, $readable);
  241. }
  242. }
  243. else // Open and return file
  244. {
  245. clearstatcache();
  246. $filename = basename($fullpath_original_file);
  247. // Output files on browser
  248. dol_syslog("viewimage.php return file $fullpath_original_file filename=$filename content-type=$type");
  249. // This test is to avoid error images when image is not available (for example thumbs).
  250. if (! dol_is_file($fullpath_original_file) && empty($_GET["noalt"]))
  251. {
  252. $fullpath_original_file=DOL_DOCUMENT_ROOT.'/public/theme/common/nophoto.png';
  253. /*$error='Error: File '.$_GET["file"].' does not exists or filesystems permissions are not allowed';
  254. print $error;
  255. exit;*/
  256. }
  257. // Permissions are ok and file found, so we return it
  258. if ($type)
  259. {
  260. top_httphead($type);
  261. header('Content-Disposition: inline; filename="'.basename($fullpath_original_file).'"');
  262. }
  263. else
  264. {
  265. top_httphead('image/png');
  266. header('Content-Disposition: inline; filename="'.basename($fullpath_original_file).'"');
  267. }
  268. $fullpath_original_file_osencoded=dol_osencode($fullpath_original_file);
  269. readfile($fullpath_original_file_osencoded);
  270. }
  271. if (is_object($db)) $db->close();