PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/viewimage.php

https://github.com/asterix14/dolibarr
PHP | 450 lines | 303 code | 57 blank | 90 comment | 93 complexity | 20a010db78c0f4abae53a13b4e3af377 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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. // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  26. $action = isset($_GET["action"])?$_GET["action"]:'';
  27. $original_file = isset($_GET["file"])?$_GET["file"]:'';
  28. $modulepart = isset($_GET["modulepart"])?$_GET["modulepart"]:'';
  29. $urlsource = isset($_GET["urlsource"])?$_GET["urlsource"]:'';
  30. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  31. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  32. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  33. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  34. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  35. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  36. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  37. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  38. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  39. // Pour autre que companylogo, on charge environnement + info issus de logon comme le user
  40. if (($modulepart == 'companylogo') && ! defined("NOLOGIN")) define("NOLOGIN",'1');
  41. /**
  42. * Wrapper, donc header vierge
  43. *
  44. * @return null
  45. */
  46. function llxHeader() { }
  47. require("./main.inc.php");
  48. require_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
  49. // Security check
  50. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  51. /*
  52. * Actions
  53. */
  54. // None
  55. /*
  56. * View
  57. */
  58. if (GETPOST("cache"))
  59. {
  60. // Important: Following code is to avoid page request by browser and PHP CPU at
  61. // each Dolibarr page access.
  62. if (empty($dolibarr_nocache))
  63. {
  64. header('Cache-Control: max-age=3600, public, must-revalidate');
  65. header('Pragma: cache'); // This is to avoid having Pragma: no-cache
  66. }
  67. else header('Cache-Control: no-cache');
  68. //print $dolibarr_nocache; exit;
  69. }
  70. // Define mime type
  71. $type = 'application/octet-stream';
  72. if (! empty($_GET["type"])) $type=$_GET["type"];
  73. else $type=dol_mimetype($original_file);
  74. // Suppression de la chaine de caractere ../ dans $original_file
  75. $original_file = str_replace("../","/", $original_file);
  76. // Security checks
  77. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  78. $accessallowed=0;
  79. if ($modulepart)
  80. {
  81. // Check permissions and define directory
  82. // Wrapping for company logo
  83. if ($modulepart == 'companylogo')
  84. {
  85. $accessallowed=1;
  86. $original_file=$conf->mycompany->dir_output.'/logos/'.$original_file;
  87. }
  88. // Wrapping for users photos
  89. elseif ($modulepart == 'userphoto')
  90. {
  91. $accessallowed=1;
  92. $original_file=$conf->user->dir_output.'/'.$original_file;
  93. }
  94. // Wrapping for members photos
  95. elseif ($modulepart == 'memberphoto')
  96. {
  97. $accessallowed=1;
  98. $original_file=$conf->adherent->dir_output.'/'.$original_file;
  99. }
  100. // Wrapping pour les images des societes
  101. elseif ($modulepart == 'societe')
  102. {
  103. $accessallowed=1;
  104. $original_file=$conf->societe->dir_output.'/'.$original_file;
  105. }
  106. // Wrapping pour les apercu factures
  107. elseif ($modulepart == 'apercufacture')
  108. {
  109. if ($user->rights->facture->lire)
  110. {
  111. $accessallowed=1;
  112. }
  113. $original_file=$conf->facture->dir_output.'/'.$original_file;
  114. }
  115. // Wrapping pour les apercu propal
  116. elseif ($modulepart == 'apercupropal')
  117. {
  118. if ($user->rights->propale->lire)
  119. {
  120. $accessallowed=1;
  121. }
  122. $original_file=$conf->propale->dir_output.'/'.$original_file;
  123. }
  124. // Wrapping pour les apercu commande
  125. elseif ($modulepart == 'apercucommande')
  126. {
  127. if ($user->rights->commande->lire)
  128. {
  129. $accessallowed=1;
  130. }
  131. $original_file=$conf->commande->dir_output.'/'.$original_file;
  132. }
  133. // Wrapping pour les apercu intervention
  134. elseif ($modulepart == 'apercufichinter')
  135. {
  136. if ($user->rights->ficheinter->lire)
  137. {
  138. $accessallowed=1;
  139. }
  140. $original_file=$conf->ficheinter->dir_output.'/'.$original_file;
  141. }
  142. // Wrapping pour les images des stats propales
  143. elseif ($modulepart == 'propalstats')
  144. {
  145. if ($user->rights->propale->lire)
  146. {
  147. $accessallowed=1;
  148. }
  149. $original_file=$conf->propale->dir_temp.'/'.$original_file;
  150. }
  151. // Wrapping pour les images des stats commandes
  152. elseif ($modulepart == 'orderstats')
  153. {
  154. if ($user->rights->commande->lire)
  155. {
  156. $accessallowed=1;
  157. }
  158. $original_file=$conf->commande->dir_temp.'/'.$original_file;
  159. }
  160. elseif ($modulepart == 'orderstatssupplier')
  161. {
  162. if ($user->rights->fournisseur->commande->lire)
  163. {
  164. $accessallowed=1;
  165. }
  166. $original_file=$conf->fournisseur->dir_output.'/commande/temp/'.$original_file;
  167. }
  168. // Wrapping pour les images des stats factures
  169. elseif ($modulepart == 'billstats')
  170. {
  171. if ($user->rights->facture->lire)
  172. {
  173. $accessallowed=1;
  174. }
  175. $original_file=$conf->facture->dir_temp.'/'.$original_file;
  176. }
  177. elseif ($modulepart == 'billstatssupplier')
  178. {
  179. if ($user->rights->fournisseur->facture->lire)
  180. {
  181. $accessallowed=1;
  182. }
  183. $original_file=$conf->fournisseur->dir_output.'/facture/temp/'.$original_file;
  184. }
  185. // Wrapping pour les images des stats expeditions
  186. elseif ($modulepart == 'expeditionstats')
  187. {
  188. if ($user->rights->expedition->lire)
  189. {
  190. $accessallowed=1;
  191. }
  192. $original_file=$conf->expedition->dir_temp.'/'.$original_file;
  193. }
  194. // Wrapping pour les images des stats expeditions
  195. elseif ($modulepart == 'tripsexpensesstats')
  196. {
  197. if ($user->rights->deplacement->lire)
  198. {
  199. $accessallowed=1;
  200. }
  201. $original_file=$conf->deplacement->dir_temp.'/'.$original_file;
  202. }
  203. // Wrapping pour les images des stats expeditions
  204. elseif ($modulepart == 'memberstats')
  205. {
  206. if ($user->rights->adherent->lire)
  207. {
  208. $accessallowed=1;
  209. }
  210. $original_file=$conf->adherent->dir_temp.'/'.$original_file;
  211. }
  212. // Wrapping pour les images des stats produits
  213. elseif (preg_match('/^productstats_/i',$modulepart))
  214. {
  215. if ($user->rights->produit->lire || $user->rights->service->lire)
  216. {
  217. $accessallowed=1;
  218. }
  219. $original_file=(!empty($conf->product->dir_temp)?$conf->product->dir_temp:$conf->service->dir_temp).'/'.$original_file;
  220. }
  221. // Wrapping for products or services
  222. elseif ($modulepart == 'product')
  223. {
  224. if ($user->rights->produit->lire || $user->rights->service->lire)
  225. {
  226. $accessallowed=1;
  227. }
  228. $original_file=(!empty($conf->product->dir_output)?$conf->product->dir_output:$conf->service->dir_output).'/'.$original_file;
  229. }
  230. // Wrapping for categories
  231. elseif ($modulepart == 'category')
  232. {
  233. if ($user->rights->categorie->lire)
  234. {
  235. $accessallowed=1;
  236. }
  237. $original_file=$conf->categorie->dir_output.'/'.$original_file;
  238. }
  239. // Wrapping pour les prelevements
  240. elseif ($modulepart == 'prelevement')
  241. {
  242. if ($user->rights->prelevement->bons->lire) $accessallowed=1;
  243. $original_file=$conf->prelevement->dir_output.'/receipts/'.$original_file;
  244. }
  245. // Wrapping pour les graph energie
  246. elseif ($modulepart == 'graph_stock')
  247. {
  248. $accessallowed=1;
  249. $original_file=$conf->stock->dir_temp.'/'.$original_file;
  250. }
  251. // Wrapping pour les graph fournisseurs
  252. elseif ($modulepart == 'graph_fourn')
  253. {
  254. $accessallowed=1;
  255. $original_file=$conf->fournisseur->dir_temp.'/'.$original_file;
  256. }
  257. // Wrapping pour les graph des produits
  258. elseif ($modulepart == 'graph_product')
  259. {
  260. $accessallowed=1;
  261. $original_file=$conf->product->dir_temp.'/'.$original_file;
  262. }
  263. // Wrapping pour les code barre
  264. elseif ($modulepart == 'barcode')
  265. {
  266. $accessallowed=1;
  267. // If viewimage is called for barcode, we try to output an image on the fly,
  268. // with not build of file on disk.
  269. //$original_file=$conf->barcode->dir_temp.'/'.$original_file;
  270. $original_file='';
  271. }
  272. // Wrapping pour les icones de background des mailings
  273. elseif ($modulepart == 'iconmailing')
  274. {
  275. $accessallowed=1;
  276. $original_file=$conf->mailing->dir_temp.'/'.$original_file;
  277. }
  278. // Wrapping pour les icones de background des mailings
  279. elseif ($modulepart == 'scanner_user_temp')
  280. {
  281. $accessallowed=1;
  282. $original_file=$conf->scanner->dir_temp.'/'.$user->id.'/'.$original_file;
  283. }
  284. // Wrapping pour les images fckeditor
  285. elseif ($modulepart == 'fckeditor')
  286. {
  287. $accessallowed=1;
  288. $original_file=$conf->fckeditor->dir_output.'/'.$original_file;
  289. }
  290. // GENERIC Wrapping
  291. // If modulepart=module_user_temp Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/temp/iduser
  292. // If modulepart=module_temp Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/temp
  293. // If modulepart=module_user Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/iduser
  294. // If modulepart=module Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart
  295. else
  296. {
  297. if (preg_match('/^([a-z]+)_user_temp$/i',$modulepart,$reg))
  298. {
  299. if ($user->rights->$reg[1]->lire || $user->rights->$reg[1]->read) $accessallowed=1;
  300. $original_file=$conf->$reg[1]->dir_temp.'/'.$user->id.'/'.$original_file;
  301. }
  302. else if (preg_match('/^([a-z]+)_temp$/i',$modulepart,$reg))
  303. {
  304. if ($user->rights->$reg[1]->lire || $user->rights->$reg[1]->read) $accessallowed=1;
  305. $original_file=$conf->$reg[1]->dir_temp.'/'.$original_file;
  306. }
  307. else if (preg_match('/^([a-z]+)_user$/i',$modulepart,$reg))
  308. {
  309. if ($user->rights->$reg[1]->lire || $user->rights->$reg[1]->read) $accessallowed=1;
  310. $original_file=$conf->$reg[1]->dir_output.'/'.$user->id.'/'.$original_file;
  311. }
  312. else
  313. {
  314. $perm=GETPOST('perm');
  315. $subperm=GETPOST('subperm');
  316. if ($perm || $subperm)
  317. {
  318. if (($perm && $user->rights->$modulepart->$perm) || ($perm && $subperm && $user->rights->$modulepart->$perm->$subperm)) $accessallowed=1;
  319. $original_file=$conf->$modulepart->dir_output.'/'.$original_file;
  320. }
  321. else
  322. {
  323. if ($user->rights->$modulepart->lire || $user->rights->$modulepart->read) $accessallowed=1;
  324. $original_file=$conf->$modulepart->dir_output.'/'.$original_file;
  325. }
  326. }
  327. }
  328. }
  329. // Security:
  330. // Limit access if permissions are wrong
  331. if (! $accessallowed)
  332. {
  333. accessforbidden();
  334. }
  335. // Security:
  336. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  337. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
  338. {
  339. dol_syslog("Refused to deliver file ".$original_file, LOG_WARNING);
  340. // Do no show plain path in shown error message
  341. dol_print_error(0,'Error: File '.$_GET["file"].' does not exists');
  342. exit;
  343. }
  344. if ($modulepart == 'barcode')
  345. {
  346. $generator=$_GET["generator"];
  347. $code=$_GET["code"];
  348. $encoding=$_GET["encoding"];
  349. $readable=$_GET["readable"]?$_GET["readable"]:"Y";
  350. // Output files with barcode generators
  351. foreach ($conf->file->dol_document_root as $dirroot)
  352. {
  353. $dir=$dirroot . "/core/modules/barcode/";
  354. $result=@include_once($dir.$generator.".modules.php");
  355. if ($result) break;
  356. }
  357. // Chargement de la classe de codage
  358. $classname = "mod".ucfirst($generator);
  359. $module = new $classname($db);
  360. if ($module->encodingIsSupported($encoding))
  361. {
  362. $result=$module->buildBarCode($code,$encoding,$readable);
  363. }
  364. }
  365. else // Open and return file
  366. {
  367. clearstatcache();
  368. // Output files on browser
  369. dol_syslog("viewimage.php return file $original_file content-type=$type");
  370. $original_file_osencoded=dol_osencode($original_file);
  371. // This test if file exists should be useless. We keep it to find bug more easily
  372. if (! dol_is_file($original_file_osencoded))
  373. {
  374. $error='Error: File '.$_GET["file"].' does not exists or filesystems permissions are not allowed';
  375. dol_print_error(0,$error);
  376. print $error;
  377. exit;
  378. }
  379. // Les drois sont ok et fichier trouve
  380. if ($type)
  381. {
  382. header('Content-Disposition: inline; filename="'.basename($original_file).'"');
  383. header('Content-type: '.$type);
  384. }
  385. else
  386. {
  387. header('Content-Disposition: inline; filename="'.basename($original_file).'"');
  388. header('Content-type: image/png');
  389. }
  390. readfile($original_file_osencoded);
  391. }
  392. if (is_object($db)) $db->close();
  393. ?>