viewimage.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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@capnetworks.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 <http://www.gnu.org/licenses/>.
  18. * or see http://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 '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=diroffile&file=relativepathofofile&cache=0">'
  24. */
  25. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  26. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  27. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  28. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  29. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  30. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  31. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  32. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  33. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  34. if (! defined('NOREQUIREHOOK')) define('NOREQUIREHOOK','1'); // Disable "main.inc.php" hooks
  35. // Some value of modulepart can be used to get resources that are public so no login are required.
  36. if ((isset($_GET["modulepart"]) && ($_GET["modulepart"] == 'mycompany' || $_GET["modulepart"] == 'companylogo')) && ! defined("NOLOGIN")) define("NOLOGIN",'1');
  37. if ((isset($_GET["modulepart"]) && $_GET["modulepart"] == 'medias') && ! defined("NOLOGIN"))
  38. {
  39. define("NOLOGIN",'1');
  40. // For multicompany
  41. $entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  42. if (is_numeric($entity)) define("DOLENTITY", $entity);
  43. }
  44. /**
  45. * Header empty
  46. *
  47. * @return void
  48. */
  49. function llxHeader() { }
  50. /**
  51. * Footer empty
  52. *
  53. * @return void
  54. */
  55. function llxFooter() { }
  56. require 'main.inc.php';
  57. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  58. $action=GETPOST('action','alpha');
  59. $original_file=GETPOST("file",'alpha');
  60. $modulepart=GETPOST('modulepart','alpha');
  61. $urlsource=GETPOST("urlsource",'alpha');
  62. $entity=GETPOST('entity','int')?GETPOST('entity','int'):$conf->entity;
  63. // Security check
  64. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  65. if ($modulepart == 'fckeditor') $modulepart='medias'; // For backward compatibility
  66. /*
  67. * Actions
  68. */
  69. // None
  70. /*
  71. * View
  72. */
  73. if (GETPOST("cache",'alpha'))
  74. {
  75. // Important: Following code is to avoid page request by browser and PHP CPU at
  76. // each Dolibarr page access.
  77. if (empty($dolibarr_nocache))
  78. {
  79. header('Cache-Control: max-age=3600, public, must-revalidate');
  80. header('Pragma: cache'); // This is to avoid having Pragma: no-cache
  81. }
  82. else header('Cache-Control: no-cache');
  83. //print $dolibarr_nocache; exit;
  84. }
  85. // Define mime type
  86. $type = 'application/octet-stream';
  87. if (! empty($_GET["type"])) $type=$_GET["type"];
  88. else $type=dol_mimetype($original_file);
  89. // Security: Delete string ../ into $original_file
  90. $original_file = str_replace("../","/", $original_file);
  91. // Find the subdirectory name as the reference
  92. $refname=basename(dirname($original_file)."/");
  93. // Security check
  94. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  95. $check_access = dol_check_secure_access_document($modulepart,$original_file,$entity,$refname);
  96. $accessallowed = $check_access['accessallowed'];
  97. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  98. $original_file = $check_access['original_file'];
  99. // Security:
  100. // Limit access if permissions are wrong
  101. if (! $accessallowed)
  102. {
  103. accessforbidden();
  104. }
  105. // Security:
  106. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  107. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
  108. {
  109. dol_syslog("Refused to deliver file ".$original_file, LOG_WARNING);
  110. // Do no show plain path in shown error message
  111. dol_print_error(0,'Error: File '.$_GET["file"].' does not exists');
  112. exit;
  113. }
  114. if ($modulepart == 'barcode')
  115. {
  116. $generator=GETPOST("generator","alpha");
  117. $code=GETPOST("code",'alpha');
  118. $encoding=GETPOST("encoding","alpha");
  119. $readable=GETPOST("readable",'alpha')?GETPOST("readable","alpha"):"Y";
  120. if (empty($generator) || empty($encoding))
  121. {
  122. dol_print_error(0,'Error, parameter "generator" or "encoding" not defined');
  123. exit;
  124. }
  125. $dirbarcode=array_merge(array("/core/modules/barcode/doc/"),$conf->modules_parts['barcode']);
  126. $result=0;
  127. foreach($dirbarcode as $reldir)
  128. {
  129. $dir=dol_buildpath($reldir,0);
  130. $newdir=dol_osencode($dir);
  131. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
  132. if (! is_dir($newdir)) continue;
  133. $result=@include_once $newdir.$generator.'.modules.php';
  134. if ($result) break;
  135. }
  136. // Load barcode class
  137. $classname = "mod".ucfirst($generator);
  138. $module = new $classname($db);
  139. if ($module->encodingIsSupported($encoding))
  140. {
  141. $result=$module->buildBarCode($code,$encoding,$readable);
  142. }
  143. }
  144. else // Open and return file
  145. {
  146. clearstatcache();
  147. // Output files on browser
  148. dol_syslog("viewimage.php return file $original_file content-type=$type");
  149. // This test is to avoid error images when image is not available (for example thumbs).
  150. if (! dol_is_file($original_file) && empty($_GET["noalt"]))
  151. {
  152. $original_file=DOL_DOCUMENT_ROOT.'/public/theme/common/nophoto.png';
  153. /*$error='Error: File '.$_GET["file"].' does not exists or filesystems permissions are not allowed';
  154. dol_print_error(0,$error);
  155. print $error;
  156. exit;*/
  157. }
  158. // Les drois sont ok et fichier trouve
  159. if ($type)
  160. {
  161. top_httphead($type);
  162. header('Content-Disposition: inline; filename="'.basename($original_file).'"');
  163. }
  164. else
  165. {
  166. top_httphead('image/png');
  167. header('Content-Disposition: inline; filename="'.basename($original_file).'"');
  168. }
  169. $original_file_osencoded=dol_osencode($original_file);
  170. readfile($original_file_osencoded);
  171. }
  172. if (is_object($db)) $db->close();