2004-10-20 21:06:45 +00:00
< ? php
2007-04-13 12:55:55 +00:00
/* Copyright ( C ) 2004 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2013-06-04 17:36:41 +00:00
* Copyright ( C ) 2004 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2005-11-01 15:58:51 +00:00
* Copyright ( C ) 2005 Simon Tosser < simon @ kornog - computing . com >
2012-12-30 14:13:49 +00:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2010-04-21 08:47:58 +00:00
* Copyright ( C ) 2010 Pierre Morin < pierre . morin @ auguria . net >
2010-10-08 16:56:14 +00:00
* Copyright ( C ) 2010 Juanjo Menent < jmenent @ 2 byte . es >
2004-07-12 09:46:13 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 14:36:08 +00:00
* the Free Software Foundation ; either version 3 of the License , or
2004-07-12 09:46:13 +00:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-07-31 23:19:04 +00:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2004-07-12 09:46:13 +00:00
* or see http :// www . gnu . org /
*/
2007-04-13 12:55:55 +00:00
/**
2008-11-24 20:09:23 +00:00
* \file htdocs / document . php
* \brief Wrapper to download data files
2010-02-28 14:16:46 +00:00
* \remarks Call of this wrapper is made with URL :
2009-02-24 02:41:21 +00:00
* document . php ? modulepart = repfichierconcerne & file = pathrelatifdufichier
2008-11-24 20:09:23 +00:00
*/
2004-12-28 15:02:48 +00:00
2009-12-30 12:09:24 +00:00
define ( 'NOTOKENRENEWAL' , 1 ); // Disables token renewal
2010-01-12 10:26:05 +00:00
// Pour autre que bittorrent, on charge environnement + info issus de logon (comme le user)
2011-04-27 17:10:29 +00:00
if ( isset ( $_GET [ " modulepart " ]) && $_GET [ " modulepart " ] == 'bittorrent' && ! defined ( " NOLOGIN " ))
2010-02-28 14:16:46 +00:00
{
define ( " NOLOGIN " , 1 );
define ( " NOCSRFCHECK " , 1 ); // We accept to go on this page from external web site.
}
2009-12-30 12:09:24 +00:00
if ( ! defined ( 'NOREQUIREMENU' )) define ( 'NOREQUIREMENU' , '1' );
if ( ! defined ( 'NOREQUIREHTML' )) define ( 'NOREQUIREHTML' , '1' );
if ( ! defined ( 'NOREQUIREAJAX' )) define ( 'NOREQUIREAJAX' , '1' );
2009-02-18 17:04:00 +00:00
2011-07-09 00:15:16 +00:00
/**
2013-04-15 13:43:25 +00:00
* Header empty
2011-07-09 00:15:16 +00:00
*
2013-04-15 13:43:25 +00:00
* @ return void
2011-07-09 00:15:16 +00:00
*/
2007-11-19 18:34:53 +00:00
function llxHeader () { }
2013-04-15 13:43:25 +00:00
/**
* Footer empty
*
* @ return void
*/
function llxFooter () { }
2007-11-19 18:34:53 +00:00
2013-04-28 16:46:34 +00:00
2012-08-22 23:43:44 +00:00
require 'main.inc.php' ; // Load $user and permissions
2012-08-22 21:11:24 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2011-04-27 17:10:29 +00:00
$encoding = '' ;
2012-04-07 08:18:09 +00:00
$action = GETPOST ( 'action' , 'alpha' );
$original_file = GETPOST ( 'file' , 'alpha' ); // Do not use urldecode here ($_GET are already decoded by PHP).
$modulepart = GETPOST ( 'modulepart' , 'alpha' );
$urlsource = GETPOST ( 'urlsource' , 'alpha' );
2012-04-17 23:01:46 +00:00
$entity = GETPOST ( 'entity' ) ? GETPOST ( 'entity' , 'int' ) : $conf -> entity ;
2011-04-27 17:10:29 +00:00
2011-06-15 11:35:33 +00:00
// Security check
if ( empty ( $modulepart )) accessforbidden ( 'Bad value for parameter modulepart' );
2016-05-01 14:34:50 +00:00
if ( $modulepart == 'fckeditor' ) $modulepart = 'medias' ; // For backward compatibility
2011-06-15 11:35:33 +00:00
2014-01-30 10:44:31 +00:00
$socid = 0 ;
if ( $user -> societe_id > 0 ) $socid = $user -> societe_id ;
// For some module part, dir may be privates
if ( in_array ( $modulepart , array ( 'facture_paiement' , 'unpaid' )))
{
if ( ! $user -> rights -> societe -> client -> voir || $socid ) $original_file = 'private/' . $user -> id . '/' . $original_file ; // If user has no permission to see all, output dir is specific to user
}
2010-12-18 03:27:01 +00:00
/*
2011-04-27 17:10:29 +00:00
* Action
2010-12-18 03:27:01 +00:00
*/
2011-04-27 17:10:29 +00:00
// None
2008-08-03 22:50:32 +00:00
2011-04-27 17:10:29 +00:00
/*
* View
*/
2011-04-26 21:06:45 +00:00
2008-08-03 22:50:32 +00:00
// Define mime type
$type = 'application/octet-stream' ;
2012-02-27 16:02:56 +00:00
if ( GETPOST ( 'type' , 'alpha' )) $type = GETPOST ( 'type' , 'alpha' );
2008-08-03 22:50:32 +00:00
else $type = dol_mimetype ( $original_file );
2012-09-07 15:23:16 +00:00
// Define attachment (attachment=true to force choice popup 'open'/'save as')
$attachment = true ;
2012-09-03 16:01:07 +00:00
if ( preg_match ( '/\.(html|htm)$/i' , $original_file )) $attachment = false ;
2012-09-07 15:23:16 +00:00
if ( isset ( $_GET [ " attachment " ])) $attachment = GETPOST ( " attachment " ) ? true : false ;
if ( ! empty ( $conf -> global -> MAIN_DISABLE_FORCE_SAVEAS )) $attachment = false ;
2012-09-03 15:01:03 +00:00
2016-05-01 14:34:50 +00:00
// Security: Delete string ../ into $original_file
2012-09-07 15:23:16 +00:00
$original_file = str_replace ( " ../ " , " / " , $original_file );
// Find the subdirectory name as the reference
2007-01-31 17:52:54 +00:00
$refname = basename ( dirname ( $original_file ) . " / " );
2005-08-25 10:20:13 +00:00
2010-11-07 12:33:32 +00:00
// Security check
2013-04-25 08:06:54 +00:00
if ( empty ( $modulepart )) accessforbidden ( 'Bad value for parameter modulepart' );
2013-05-16 13:46:54 +00:00
$check_access = dol_check_secure_access_document ( $modulepart , $original_file , $entity , $refname );
2013-04-25 08:06:54 +00:00
$accessallowed = $check_access [ 'accessallowed' ];
2013-04-22 13:00:29 +00:00
$sqlprotectagainstexternals = $check_access [ 'sqlprotectagainstexternals' ];
2013-06-04 20:03:11 +00:00
$original_file = $check_access [ 'original_file' ];
2012-04-17 23:01:46 +00:00
2007-01-31 17:52:54 +00:00
// Basic protection (against external users only)
2007-11-19 18:34:53 +00:00
if ( $user -> societe_id > 0 )
2007-01-31 17:52:54 +00:00
{
if ( $sqlprotectagainstexternals )
{
$resql = $db -> query ( $sqlprotectagainstexternals );
if ( $resql )
{
2009-04-29 20:16:13 +00:00
$num = $db -> num_rows ( $resql );
2010-04-21 18:35:40 +00:00
$i = 0 ;
while ( $i < $num )
{
$obj = $db -> fetch_object ( $resql );
if ( $user -> societe_id != $obj -> fk_soc )
{
$accessallowed = 0 ;
break ;
}
$i ++ ;
}
2007-01-31 17:52:54 +00:00
}
}
}
2007-04-12 19:47:50 +00:00
// Security:
2008-01-20 23:18:18 +00:00
// Limite acces si droits non corrects
2005-02-08 12:03:12 +00:00
if ( ! $accessallowed )
{
2009-04-29 20:16:13 +00:00
accessforbidden ();
2005-02-08 12:03:12 +00:00
}
2004-12-28 15:02:48 +00:00
2007-04-12 19:47:50 +00:00
// Security:
2009-02-18 17:04:00 +00:00
// On interdit les remontees de repertoire ainsi que les pipe dans
2007-04-12 19:47:50 +00:00
// les noms de fichiers.
2009-10-24 06:10:00 +00:00
if ( preg_match ( '/\.\./' , $original_file ) || preg_match ( '/[<>|]/' , $original_file ))
2007-04-12 19:47:50 +00:00
{
2009-02-20 22:53:15 +00:00
dol_syslog ( " Refused to deliver file " . $original_file );
2011-04-27 17:10:29 +00:00
$file = basename ( $original_file ); // Do no show plain path of original_file in shown error message
dol_print_error ( 0 , $langs -> trans ( " ErrorFileNameInvalid " , $file ));
2007-04-12 19:47:50 +00:00
exit ;
}
2009-02-18 17:04:00 +00:00
2012-09-03 15:01:03 +00:00
clearstatcache ();
2006-06-10 18:43:11 +00:00
2012-09-03 15:01:03 +00:00
$filename = basename ( $original_file );
2006-04-27 21:25:56 +00:00
2012-09-03 15:01:03 +00:00
// Output file on browser
dol_syslog ( " document.php download $original_file $filename content-type= $type " );
$original_file_osencoded = dol_osencode ( $original_file ); // New file name encoded in OS encoding charset
2007-01-31 17:52:54 +00:00
2012-09-03 15:01:03 +00:00
// This test if file exists should be useless. We keep it to find bug more easily
if ( ! file_exists ( $original_file_osencoded ))
2012-05-30 02:11:00 +00:00
{
2012-09-03 15:01:03 +00:00
dol_print_error ( 0 , $langs -> trans ( " ErrorFileDoesNotExists " , $original_file ));
exit ;
}
2009-02-18 17:04:00 +00:00
2014-02-08 01:02:01 +00:00
// Permissions are ok and file found, so we return it
2012-09-03 15:01:03 +00:00
header ( 'Content-Description: File Transfer' );
if ( $encoding ) header ( 'Content-Encoding: ' . $encoding );
if ( $type ) header ( 'Content-Type: ' . $type . ( preg_match ( '/text/' , $type ) ? '; charset="' . $conf -> file -> character_set_client : '' ));
2012-09-03 16:01:07 +00:00
// Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, atachment=need user action to open)
2012-09-03 15:01:03 +00:00
if ( $attachment ) header ( 'Content-Disposition: attachment; filename="' . $filename . '"' );
else header ( 'Content-Disposition: inline; filename="' . $filename . '"' );
header ( 'Content-Length: ' . dol_filesize ( $original_file ));
// Ajout directives pour resoudre bug IE
header ( 'Cache-Control: Public, must-revalidate' );
header ( 'Pragma: public' );
2009-02-18 17:04:00 +00:00
2012-09-03 15:01:03 +00:00
//ob_clean();
//flush();
2016-05-01 14:34:50 +00:00
2012-09-03 15:01:03 +00:00
readfile ( $original_file_osencoded );
2004-12-28 15:02:48 +00:00
2013-06-26 14:45:42 +00:00
if ( is_object ( $db )) $db -> close ();