PageRenderTime 441ms CodeModel.GetById 39ms RepoModel.GetById 3ms app.codeStats 0ms

/htdocs/document.php

https://bitbucket.org/speedealing/speedealing
PHP | 511 lines | 346 code | 64 blank | 101 comment | 190 complexity | d24598d5108da904c6af0e56cd06da3d MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Simon Tosser <simon@kornog-computing.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2010 Pierre Morin <pierre.morin@auguria.net>
  7. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. * or see http://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/document.php
  25. * \brief Wrapper to download data files
  26. * \remarks Call of this wrapper is made with URL:
  27. * document.php?modulepart=repfichierconcerne&file=pathrelatifdufichier
  28. */
  29. define('NOTOKENRENEWAL',1); // Disables token renewal
  30. // Pour autre que bittorrent, on charge environnement + info issus de logon (comme le user)
  31. if (isset($_GET["modulepart"]) && $_GET["modulepart"] == 'bittorrent' && ! defined("NOLOGIN"))
  32. {
  33. define("NOLOGIN",1);
  34. define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  35. }
  36. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  37. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  38. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  39. /**
  40. * Wrapper, donc header vierge
  41. *
  42. * @return null
  43. */
  44. function llxHeader() { }
  45. require 'main.inc.php'; // Load $user and permissions
  46. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  47. $encoding = '';
  48. $action=GETPOST('action','alpha');
  49. $original_file=GETPOST('file','alpha'); // Do not use urldecode here ($_GET are already decoded by PHP).
  50. $modulepart=GETPOST('modulepart','alpha');
  51. $urlsource=GETPOST('urlsource','alpha');
  52. $entity=GETPOST('entity')?GETPOST('entity','int'):$conf->entity;
  53. // Security check
  54. if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
  55. /*
  56. * Action
  57. */
  58. // None
  59. /*
  60. * View
  61. */
  62. // Define mime type
  63. $type = 'application/octet-stream';
  64. if (GETPOST('type','alpha')) $type=GETPOST('type','alpha');
  65. else $type=dol_mimetype($original_file);
  66. // Define attachment (attachment=true to force choice popup 'open'/'save as')
  67. $attachment = true;
  68. if (preg_match('/\.(html|htm)$/i',$original_file)) $attachment = false;
  69. if (isset($_GET["attachment"])) $attachment = GETPOST("attachment")?true:false;
  70. if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
  71. // Suppression de la chaine de caractere ../ dans $original_file
  72. $original_file = str_replace("../","/", $original_file);
  73. // Find the subdirectory name as the reference
  74. $refname=basename(dirname($original_file)."/");
  75. // Security check
  76. $accessallowed=0;
  77. $sqlprotectagainstexternals='';
  78. if ($modulepart)
  79. {
  80. // On fait une verification des droits et on definit le repertoire concerne
  81. // Wrapping for third parties
  82. if ($modulepart == 'company' || $modulepart == 'societe')
  83. {
  84. if ($user->rights->societe->lire || preg_match('/^specimen/i',$original_file))
  85. {
  86. $accessallowed=1;
  87. }
  88. $original_file=$conf->societe->multidir_output[$entity].'/'.$original_file;
  89. $sqlprotectagainstexternals = "SELECT rowid as fk_soc FROM ".MAIN_DB_PREFIX."societe WHERE rowid='".$refname."' AND entity IN (".getEntity('societe', 1).")";
  90. }
  91. // Wrapping for invoices
  92. else if ($modulepart == 'facture' || $modulepart == 'invoice')
  93. {
  94. if ($user->rights->facture->lire || preg_match('/^specimen/i',$original_file))
  95. {
  96. $accessallowed=1;
  97. }
  98. $original_file=$conf->facture->dir_output.'/'.$original_file;
  99. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."facture WHERE ref='".$refname."' AND entity=".$conf->entity;
  100. }
  101. else if ($modulepart == 'unpaid')
  102. {
  103. if ($user->rights->facture->lire || preg_match('/^specimen/i',$original_file))
  104. {
  105. $accessallowed=1;
  106. }
  107. $original_file=$conf->facture->dir_output.'/unpaid/temp/'.$original_file;
  108. }
  109. // Wrapping pour les fiches intervention
  110. else if ($modulepart == 'ficheinter')
  111. {
  112. if ($user->rights->ficheinter->lire || preg_match('/^specimen/i',$original_file))
  113. {
  114. $accessallowed=1;
  115. }
  116. $original_file=$conf->ficheinter->dir_output.'/'.$original_file;
  117. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."fichinter WHERE ref='".$refname."' AND entity=".$conf->entity;
  118. }
  119. // Wrapping pour les prelevements
  120. else if ($modulepart == 'prelevement')
  121. {
  122. if ($user->rights->prelevement->bons->lire || preg_match('/^specimen/i',$original_file))
  123. {
  124. $accessallowed=1;
  125. }
  126. $original_file=$conf->prelevement->dir_output.'/'.$original_file;
  127. }
  128. // Wrapping pour les propales
  129. else if ($modulepart == 'propal')
  130. {
  131. if ($user->rights->propal->lire || preg_match('/^specimen/i',$original_file))
  132. {
  133. $accessallowed=1;
  134. }
  135. $original_file=$conf->propal->dir_output.'/'.$original_file;
  136. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."propal WHERE ref='".$refname."' AND entity=".$conf->entity;
  137. }
  138. // Wrapping pour les commandes
  139. else if ($modulepart == 'commande' || $modulepart == 'order')
  140. {
  141. if ($user->rights->commande->lire || preg_match('/^specimen/i',$original_file))
  142. {
  143. $accessallowed=1;
  144. }
  145. $original_file=$conf->commande->dir_output.'/'.$original_file;
  146. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."commande WHERE ref='".$refname."' AND entity=".$conf->entity;
  147. }
  148. // Wrapping pour les projets
  149. else if ($modulepart == 'project')
  150. {
  151. if ($user->rights->projet->lire || preg_match('/^specimen/i',$original_file))
  152. {
  153. $accessallowed=1;
  154. }
  155. $original_file=$conf->projet->dir_output.'/'.$original_file;
  156. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."projet WHERE ref='".$refname."' AND entity=".$conf->entity;
  157. }
  158. // Wrapping pour les commandes fournisseurs
  159. else if ($modulepart == 'commande_fournisseur' || $modulepart == 'order_supplier')
  160. {
  161. if ($user->rights->fournisseur->commande->lire || preg_match('/^specimen/i',$original_file))
  162. {
  163. $accessallowed=1;
  164. }
  165. $original_file=$conf->fournisseur->commande->dir_output.'/'.$original_file;
  166. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."commande_fournisseur WHERE ref='".$refname."' AND entity=".$conf->entity;
  167. }
  168. // Wrapping pour les factures fournisseurs
  169. else if ($modulepart == 'facture_fournisseur' || $modulepart == 'invoice_supplier')
  170. {
  171. if ($user->rights->fournisseur->facture->lire || preg_match('/^specimen/i',$original_file))
  172. {
  173. $accessallowed=1;
  174. }
  175. $original_file=$conf->fournisseur->facture->dir_output.'/'.$original_file;
  176. $sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."facture_fourn WHERE facnumber='".$refname."' AND entity=".$conf->entity;
  177. }
  178. // Wrapping pour les rapport de paiements
  179. else if ($modulepart == 'facture_paiement')
  180. {
  181. if ($user->rights->facture->lire || preg_match('/^specimen/i',$original_file))
  182. {
  183. $accessallowed=1;
  184. }
  185. if ($user->societe_id > 0) $original_file=$conf->facture->dir_output.'/payments/private/'.$user->id.'/'.$original_file;
  186. else $original_file=$conf->facture->dir_output.'/payments/'.$original_file;
  187. }
  188. // Wrapping pour les exports de compta
  189. else if ($modulepart == 'export_compta')
  190. {
  191. if ($user->rights->compta->ventilation->creer || preg_match('/^specimen/i',$original_file))
  192. {
  193. $accessallowed=1;
  194. }
  195. $original_file=$conf->compta->dir_output.'/'.$original_file;
  196. }
  197. // Wrapping pour les expedition
  198. else if ($modulepart == 'expedition')
  199. {
  200. if ($user->rights->expedition->lire || preg_match('/^specimen/i',$original_file))
  201. {
  202. $accessallowed=1;
  203. }
  204. $original_file=$conf->expedition->dir_output."/sending/".$original_file;
  205. }
  206. // Wrapping pour les bons de livraison
  207. else if ($modulepart == 'livraison')
  208. {
  209. if ($user->rights->expedition->livraison->lire || preg_match('/^specimen/i',$original_file))
  210. {
  211. $accessallowed=1;
  212. }
  213. $original_file=$conf->expedition->dir_output."/receipt/".$original_file;
  214. }
  215. // Wrapping pour les actions
  216. else if ($modulepart == 'actions')
  217. {
  218. if ($user->rights->agenda->myactions->read || preg_match('/^specimen/i',$original_file))
  219. {
  220. $accessallowed=1;
  221. }
  222. $original_file=$conf->agenda->dir_output.'/'.$original_file;
  223. }
  224. // Wrapping pour les actions
  225. else if ($modulepart == 'actionsreport')
  226. {
  227. if ($user->rights->agenda->allactions->read || preg_match('/^specimen/i',$original_file))
  228. {
  229. $accessallowed=1;
  230. }
  231. $original_file = $conf->agenda->dir_temp."/".$original_file;
  232. }
  233. // Wrapping pour les produits et services
  234. else if ($modulepart == 'produit' || $modulepart == 'service')
  235. {
  236. if (($user->rights->produit->lire || $user->rights->service->lire) || preg_match('/^specimen/i',$original_file))
  237. {
  238. $accessallowed=1;
  239. }
  240. if (! empty($conf->product->enabled)) $original_file=$conf->product->multidir_output[$entity].'/'.$original_file;
  241. elseif (! empty($conf->service->enabled)) $original_file=$conf->service->multidir_output[$entity].'/'.$original_file;
  242. }
  243. // Wrapping pour les contrats
  244. else if ($modulepart == 'contract')
  245. {
  246. if ($user->rights->contrat->lire || preg_match('/^specimen/i',$original_file))
  247. {
  248. $accessallowed=1;
  249. }
  250. $original_file=$conf->contrat->dir_output.'/'.$original_file;
  251. }
  252. // Wrapping pour les dons
  253. else if ($modulepart == 'donation')
  254. {
  255. if ($user->rights->don->lire || preg_match('/^specimen/i',$original_file))
  256. {
  257. $accessallowed=1;
  258. }
  259. $original_file=$conf->don->dir_output.'/'.$original_file;
  260. }
  261. // Wrapping pour les remises de cheques
  262. else if ($modulepart == 'remisecheque')
  263. {
  264. if ($user->rights->banque->lire || preg_match('/^specimen/i',$original_file))
  265. {
  266. $accessallowed=1;
  267. }
  268. $original_file=$conf->banque->dir_output.'/bordereau/'.get_exdir(basename($original_file,".pdf"),2,1).$original_file;
  269. }
  270. // Wrapping for export module
  271. else if ($modulepart == 'export')
  272. {
  273. // Aucun test necessaire car on force le rep de download sur
  274. // le rep export qui est propre a l'utilisateur
  275. $accessallowed=1;
  276. $original_file=$conf->export->dir_temp.'/'.$user->id.'/'.$original_file;
  277. }
  278. // Wrapping for import module
  279. else if ($modulepart == 'import')
  280. {
  281. // Aucun test necessaire car on force le rep de download sur
  282. // le rep export qui est propre a l'utilisateur
  283. $accessallowed=1;
  284. $original_file=$conf->import->dir_temp.'/'.$original_file;
  285. }
  286. // Wrapping pour l'editeur wysiwyg
  287. else if ($modulepart == 'editor')
  288. {
  289. // Aucun test necessaire car on force le rep de download sur
  290. // le rep export qui est propre a l'utilisateur
  291. $accessallowed=1;
  292. $original_file=$conf->fckeditor->dir_output.'/'.$original_file;
  293. }
  294. // Wrapping pour les backups
  295. else if ($modulepart == 'systemtools')
  296. {
  297. if ($user->admin)
  298. {
  299. $accessallowed=1;
  300. }
  301. $original_file=$conf->admin->dir_output.'/'.$original_file;
  302. }
  303. // Model de contract
  304. else if ($modulepart == 'modelcontract')
  305. {
  306. if ($user->admin)
  307. {
  308. $accessallowed=1;
  309. }
  310. $tmpdir=trim($conf->global->CONTRAT_ADDON_PDF_ODT_PATH);
  311. if($conf->multicompany->enabled && $conf->entity > 1)
  312. $upload_dir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT."/".$conf->entity,$tmpdir);
  313. else
  314. $upload_dir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
  315. $original_file=$upload_dir.'/'.$original_file;
  316. }
  317. // Wrapping for upload file test
  318. else if ($modulepart == 'admin_temp')
  319. {
  320. if ($user->admin)
  321. $accessallowed=1;
  322. $original_file=$conf->admin->dir_temp.'/'.$original_file;
  323. }
  324. // Wrapping pour BitTorrent
  325. else if ($modulepart == 'bittorrent')
  326. {
  327. $accessallowed=1;
  328. $dir='files';
  329. if ($type == 'application/x-bittorrent') $dir='torrents';
  330. $original_file=$conf->bittorrent->dir_output.'/'.$dir.'/'.$original_file;
  331. }
  332. // Wrapping pour Foundation module
  333. else if ($modulepart == 'member')
  334. {
  335. if ($user->rights->adherent->lire || preg_match('/^specimen/i',$original_file))
  336. {
  337. $accessallowed=1;
  338. }
  339. $original_file=$conf->adherent->dir_output.'/'.$original_file;
  340. }
  341. // Wrapping for Scanner
  342. else if ($modulepart == 'scanner_user_temp')
  343. {
  344. $accessallowed=1;
  345. $original_file=$conf->scanner->dir_temp.'/'.$user->id.'/'.$original_file;
  346. }
  347. // Generic wrapping
  348. else
  349. {
  350. // For dir temp
  351. $dir_temp=false;
  352. if (preg_match('/\_temp$/i', $modulepart)) {
  353. $modulepart = str_replace('_temp', '', $modulepart);
  354. $dir_temp=true;
  355. }
  356. // Define $accessallowed
  357. if (($user->rights->$modulepart->lire) || ($user->rights->$modulepart->read) || ($user->rights->$modulepart->download)) $accessallowed=1; // No subpermission, we have checked on main permission
  358. elseif (preg_match('/^specimen/i',$original_file)) $accessallowed=1; // If link to a specimen
  359. elseif ($user->admin) $accessallowed=1; // If user is admin
  360. // For modules who wants to manage different levels of permissions for documents
  361. $subPermCategoryConstName = strtoupper($modulepart).'_SUBPERMCATEGORY_FOR_DOCUMENTS';
  362. if (! empty($conf->global->$subPermCategoryConstName))
  363. {
  364. $subPermCategory = $conf->global->$subPermCategoryConstName;
  365. if (! empty($subPermCategory) && (($user->rights->$modulepart->$subPermCategory->lire) || ($user->rights->$modulepart->$subPermCategory->read) || ($user->rights->$modulepart->$subPermCategory->download)))
  366. {
  367. $accessallowed=1;
  368. }
  369. }
  370. // Define $original_file
  371. $dir = $conf->$modulepart->dir_output;
  372. if ($dir_temp) $dir = $conf->$modulepart->dir_temp;
  373. $original_file = $dir.'/'.$original_file;
  374. // Define $sqlprotectagainstexternals for modules who want to protect access using a SQL query.
  375. $sqlProtectConstName = strtoupper($modulepart).'_SQLPROTECTAGAINSTEXTERNALS_FOR_DOCUMENTS';
  376. if (! empty($conf->global->$sqlProtectConstName)) // If module want to define its own $sqlprotectagainstexternals
  377. {
  378. // Example: mymodule__SQLPROTECTAGAINSTEXTERNALS_FOR_DOCUMENTS = "SELECT fk_soc FROM ".MAIN_DB_PREFIX.$modulepart." WHERE ref='".$refname."' AND entity=".$conf->entity;
  379. eval('$sqlprotectagainstexternals = "'.$conf->global->$sqlProtectConstName.'";');
  380. }
  381. }
  382. }
  383. // Basic protection (against external users only)
  384. if ($user->societe_id > 0)
  385. {
  386. if ($sqlprotectagainstexternals)
  387. {
  388. $resql = $db->query($sqlprotectagainstexternals);
  389. if ($resql)
  390. {
  391. $num=$db->num_rows($resql);
  392. $i=0;
  393. while ($i < $num)
  394. {
  395. $obj = $db->fetch_object($resql);
  396. if ($user->societe_id != $obj->fk_soc)
  397. {
  398. $accessallowed=0;
  399. break;
  400. }
  401. $i++;
  402. }
  403. }
  404. }
  405. }
  406. // Security:
  407. // Limite acces si droits non corrects
  408. if (! $accessallowed)
  409. {
  410. accessforbidden();
  411. }
  412. // Security:
  413. // On interdit les remontees de repertoire ainsi que les pipe dans
  414. // les noms de fichiers.
  415. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
  416. {
  417. dol_syslog("Refused to deliver file ".$original_file);
  418. $file=basename($original_file); // Do no show plain path of original_file in shown error message
  419. dol_print_error(0,$langs->trans("ErrorFileNameInvalid",$file));
  420. exit;
  421. }
  422. clearstatcache();
  423. $filename = basename($original_file);
  424. // Output file on browser
  425. dol_syslog("document.php download $original_file $filename content-type=$type");
  426. $original_file_osencoded=dol_osencode($original_file); // New file name encoded in OS encoding charset
  427. // This test if file exists should be useless. We keep it to find bug more easily
  428. if (! file_exists($original_file_osencoded))
  429. {
  430. dol_print_error(0,$langs->trans("ErrorFileDoesNotExists",$original_file));
  431. exit;
  432. }
  433. // Les drois sont ok et fichier trouve, on l'envoie
  434. header('Content-Description: File Transfer');
  435. if ($encoding) header('Content-Encoding: '.$encoding);
  436. if ($type) header('Content-Type: '.$type.(preg_match('/text/',$type)?'; charset="'.$conf->file->character_set_client:''));
  437. // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, atachment=need user action to open)
  438. if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
  439. else header('Content-Disposition: inline; filename="'.$filename.'"');
  440. header('Content-Length: ' . dol_filesize($original_file));
  441. // Ajout directives pour resoudre bug IE
  442. header('Cache-Control: Public, must-revalidate');
  443. header('Pragma: public');
  444. //ob_clean();
  445. //flush();
  446. readfile($original_file_osencoded);
  447. ?>