viewimage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. define('MAIN_SECURITY_FORCECSP', "default-src: 'none'");
  28. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  29. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  30. if (!defined('NOREQUIRESOC')) {
  31. define('NOREQUIRESOC', '1');
  32. }
  33. if (!defined('NOREQUIRETRAN')) {
  34. define('NOREQUIRETRAN', '1');
  35. }
  36. if (!defined('NOCSRFCHECK')) {
  37. define('NOCSRFCHECK', '1');
  38. }
  39. if (!defined('NOTOKENRENEWAL')) {
  40. define('NOTOKENRENEWAL', '1');
  41. }
  42. if (!defined('NOREQUIREMENU')) {
  43. define('NOREQUIREMENU', '1');
  44. }
  45. if (!defined('NOREQUIREHTML')) {
  46. define('NOREQUIREHTML', '1');
  47. }
  48. if (!defined('NOREQUIREAJAX')) {
  49. define('NOREQUIREAJAX', '1');
  50. }
  51. // Some value of modulepart can be used to get resources that are public so no login are required.
  52. // Note that only directory logo is free to access without login.
  53. $needlogin = 1;
  54. if (isset($_GET["modulepart"])) {
  55. // For logo of company
  56. if ($_GET["modulepart"] == 'mycompany' && preg_match('/^\/?logos\//', $_GET['file'])) {
  57. $needlogin = 0;
  58. }
  59. // For barcode live generation
  60. if ($_GET["modulepart"] == 'barcode') {
  61. $needlogin = 0;
  62. }
  63. // Some value of modulepart can be used to get resources that are public so no login are required.
  64. if ($_GET["modulepart"] == 'medias') {
  65. $needlogin = 0;
  66. }
  67. if ($_GET["modulepart"] == 'userphotopublic') {
  68. $needlogin = 0;
  69. }
  70. // Used by TakePOS Auto Order
  71. if ($_GET["modulepart"] == 'product' && isset($_GET["publictakepos"])) {
  72. $needlogin = 0;
  73. }
  74. }
  75. // For direct external download link, we don't need to load/check we are into a login session
  76. if (isset($_GET["hashp"])) {
  77. $needlogin = 0;
  78. }
  79. // If nologin required
  80. if (!$needlogin) {
  81. if (!defined("NOLOGIN")) {
  82. define("NOLOGIN", 1);
  83. }
  84. if (!defined("NOCSRFCHECK")) {
  85. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  86. }
  87. if (!defined("NOIPCHECK")) {
  88. define("NOIPCHECK", 1); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  89. }
  90. }
  91. // For multicompany
  92. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  93. if (is_numeric($entity)) {
  94. define("DOLENTITY", $entity);
  95. }
  96. /**
  97. * Header empty
  98. *
  99. * @ignore
  100. * @return void
  101. */
  102. function llxHeader()
  103. {
  104. }
  105. /**
  106. * Footer empty
  107. *
  108. * @ignore
  109. * @return void
  110. */
  111. function llxFooter()
  112. {
  113. }
  114. require 'main.inc.php'; // Load $user and permissions
  115. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  116. $action = GETPOST('action', 'aZ09');
  117. $original_file = GETPOST('file', 'alphanohtml'); // Do not use urldecode here ($_GET are already decoded by PHP).
  118. $hashp = GETPOST('hashp', 'aZ09', 1); // Must be read only by GET
  119. $modulepart = GETPOST('modulepart', 'alpha', 1); // Must be read only by GET
  120. $urlsource = GETPOST('urlsource', 'alpha');
  121. $entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : $conf->entity);
  122. // Security check
  123. if (empty($modulepart) && empty($hashp)) {
  124. httponly_accessforbidden('Bad link. Bad value for parameter modulepart', 400);
  125. }
  126. if (empty($original_file) && empty($hashp) && $modulepart != 'barcode') {
  127. httponly_accessforbidden('Bad link. Missing identification to find file (param file or hashp)', 400);
  128. }
  129. if ($modulepart == 'fckeditor') {
  130. $modulepart = 'medias'; // For backward compatibility
  131. }
  132. /*
  133. * Actions
  134. */
  135. // None
  136. /*
  137. * View
  138. */
  139. if (GETPOST("cache", 'alpha')) {
  140. // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
  141. // Add param cache=abcdef
  142. if (empty($dolibarr_nocache)) {
  143. header('Cache-Control: max-age=3600, public, must-revalidate');
  144. header('Pragma: cache'); // This is to avoid to have Pragma: no-cache set by proxy or web server
  145. header('Expires: '.gmdate('D, d M Y H:i:s', time() + 3600).' GMT'); // This is to avoid to have Expires set by proxy or web server
  146. //header('Expires: '.strtotime('+1 hour');
  147. } else {
  148. header('Cache-Control: no-cache');
  149. }
  150. //print $dolibarr_nocache; exit;
  151. }
  152. // If we have a hash public (hashp), we guess the original_file.
  153. if (!empty($hashp)) {
  154. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  155. $ecmfile = new EcmFiles($db);
  156. $result = $ecmfile->fetch(0, '', '', '', $hashp);
  157. if ($result > 0) {
  158. $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory
  159. // filepath can be 'users/X' or 'X/propale/PR11111'
  160. if (is_numeric($tmp[0])) { // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
  161. $tmp = explode('/', $tmp[1], 2);
  162. }
  163. $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path
  164. if ($modulepart) { // Not required, so often not defined, for link using public hashp parameter.
  165. if ($moduleparttocheck == $modulepart) {
  166. // We remove first level of directory
  167. $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
  168. //var_dump($original_file); exit;
  169. } else {
  170. httponly_accessforbidden('Bad link. File is from another module part.', 403);
  171. }
  172. } else {
  173. $modulepart = $moduleparttocheck;
  174. $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
  175. }
  176. } else {
  177. $langs->load("errors");
  178. httponly_accessforbidden($langs->trans("ErrorFileNotFoundWithSharedLink"), 403, 1);
  179. }
  180. }
  181. // Define mime type
  182. $type = 'application/octet-stream';
  183. if (GETPOST('type', 'alpha')) {
  184. $type = GETPOST('type', 'alpha');
  185. } else {
  186. $type = dol_mimetype($original_file);
  187. }
  188. // Security: This wrapper is for images. We do not allow type/html
  189. if (preg_match('/html/i', $type)) {
  190. httponly_accessforbidden('Error: Using the image wrapper to output a file with a mime type HTML is not possible.');
  191. }
  192. // Security: This wrapper is for images. We do not allow files ending with .noexe
  193. if (preg_match('/\.noexe$/i', $original_file)) {
  194. httponly_accessforbidden('Error: Using the image wrapper to output a file ending with .noexe is not allowed.');
  195. }
  196. // Security: Delete string ../ or ..\ into $original_file
  197. $original_file = preg_replace('/\.\.+/', '..', $original_file); // Replace '... or more' with '..'
  198. $original_file = str_replace('../', '/', $original_file);
  199. $original_file = str_replace('..\\', '/', $original_file);
  200. // Find the subdirectory name as the reference
  201. $refname = basename(dirname($original_file)."/");
  202. if ($refname == 'thumbs') {
  203. // If we get the thumbs directory, we must go one step higher. For example original_file='10/thumbs/myfile_small.jpg' -> refname='10'
  204. $refname = basename(dirname(dirname($original_file))."/");
  205. }
  206. // Check that file is allowed for view with viewimage.php
  207. if (!empty($original_file) && !dolIsAllowedForPreview($original_file)) {
  208. httponly_accessforbidden('This file is not qualified for preview', 403);
  209. }
  210. // Security check
  211. if (empty($modulepart)) {
  212. httponly_accessforbidden('Bad value for parameter modulepart', 400);
  213. }
  214. // When logged in a different entity, medias cannot be accessed because $conf->$module->multidir_output
  215. // is not set on the requested entity, but they are public documents, so reset entity
  216. if ($modulepart === 'medias' && $entity != $conf->entity) {
  217. $conf->entity = $entity;
  218. $conf->setValues($db);
  219. }
  220. $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, $user, $refname);
  221. $accessallowed = $check_access['accessallowed'];
  222. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  223. $fullpath_original_file = $check_access['original_file']; // $fullpath_original_file is now a full path name
  224. if (!empty($hashp)) {
  225. $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
  226. $sqlprotectagainstexternals = '';
  227. } elseif (isset($_GET["publictakepos"])) {
  228. if (!empty($conf->global->TAKEPOS_AUTO_ORDER)) {
  229. $accessallowed = 1; // Only if TakePOS Public Auto Order is enabled and received publictakepos variable
  230. }
  231. } else {
  232. // Basic protection (against external users only)
  233. if ($user->socid > 0) {
  234. if ($sqlprotectagainstexternals) {
  235. $resql = $db->query($sqlprotectagainstexternals);
  236. if ($resql) {
  237. $num = $db->num_rows($resql);
  238. $i = 0;
  239. while ($i < $num) {
  240. $obj = $db->fetch_object($resql);
  241. if ($user->socid != $obj->fk_soc) {
  242. $accessallowed = 0;
  243. break;
  244. }
  245. $i++;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. // Security:
  252. // Limit access if permissions are wrong
  253. if (!$accessallowed) {
  254. accessforbidden();
  255. }
  256. // Security:
  257. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  258. if (preg_match('/\.\./', $fullpath_original_file) || preg_match('/[<>|]/', $fullpath_original_file)) {
  259. dol_syslog("Refused to deliver file ".$fullpath_original_file);
  260. print "ErrorFileNameInvalid: ".dol_escape_htmltag($original_file);
  261. exit;
  262. }
  263. if ($modulepart == 'barcode') {
  264. $generator = GETPOST("generator", "aZ09");
  265. $encoding = GETPOST("encoding", "aZ09");
  266. $readable = GETPOST("readable", 'aZ09') ? GETPOST("readable", "aZ09") : "Y";
  267. if (in_array($encoding, array('EAN8', 'EAN13'))) {
  268. $code = GETPOST("code", 'alphanohtml');
  269. } else {
  270. $code = GETPOST("code", 'restricthtml'); // This can be rich content (qrcode, datamatrix, ...)
  271. }
  272. if (empty($generator) || empty($encoding)) {
  273. print 'Error: Parameter "generator" or "encoding" not defined';
  274. exit;
  275. }
  276. $dirbarcode = array_merge(array("/core/modules/barcode/doc/"), $conf->modules_parts['barcode']);
  277. $result = 0;
  278. foreach ($dirbarcode as $reldir) {
  279. $dir = dol_buildpath($reldir, 0);
  280. $newdir = dol_osencode($dir);
  281. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  282. if (!is_dir($newdir)) {
  283. continue;
  284. }
  285. $result = @include_once $newdir.$generator.'.modules.php';
  286. if ($result) {
  287. break;
  288. }
  289. }
  290. // Load barcode class
  291. $classname = "mod".ucfirst($generator);
  292. $module = new $classname($db);
  293. if ($module->encodingIsSupported($encoding)) {
  294. $result = $module->buildBarCode($code, $encoding, $readable);
  295. }
  296. } else {
  297. // Open and return file
  298. clearstatcache();
  299. $filename = basename($fullpath_original_file);
  300. // Output files on browser
  301. dol_syslog("viewimage.php return file $fullpath_original_file filename=$filename content-type=$type");
  302. // This test is to avoid error images when image is not available (for example thumbs).
  303. if (!dol_is_file($fullpath_original_file) && empty($_GET["noalt"])) {
  304. $fullpath_original_file = DOL_DOCUMENT_ROOT.'/public/theme/common/nophoto.png';
  305. /*$error='Error: File '.$_GET["file"].' does not exists or filesystems permissions are not allowed';
  306. print $error;
  307. exit;*/
  308. }
  309. // Permissions are ok and file found, so we return it
  310. if ($type) {
  311. top_httphead($type);
  312. header('Content-Disposition: inline; filename="'.basename($fullpath_original_file).'"');
  313. } else {
  314. top_httphead('image/png');
  315. header('Content-Disposition: inline; filename="'.basename($fullpath_original_file).'"');
  316. }
  317. $fullpath_original_file_osencoded = dol_osencode($fullpath_original_file);
  318. readfile($fullpath_original_file_osencoded);
  319. }
  320. if (is_object($db)) {
  321. $db->close();
  322. }