viewimage.php 12 KB

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