viewimage.php 12 KB

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