PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/viewimage.php

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