2004-10-20 23:15:17 +02:00
< ? php
2005-07-22 17:39:59 +02:00
/* Copyright ( C ) 2004 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2016-02-08 15:49:38 +01:00
* Copyright ( C ) 2005 - 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2016 Regis Houssin < regis . houssin @ inodbox . com >
2004-10-07 11:36:56 +02: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 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2004-10-07 11:36:56 +02: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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2004-10-07 11:36:56 +02:00
*/
2006-09-03 15:57:44 +02:00
/**
2009-07-19 18:34:13 +02:00
* \file htdocs / viewimage . php
2018-02-13 13:55:36 +01:00
* \brief Wrapper to show images into Dolibarr screens .
* \remarks Call to wrapper is :
* DOL_URL_ROOT . ' / viewimage . php ? modulepart = diroffile & file = relativepathofofile & cache = 0
* DOL_URL_ROOT . ' / viewimage . php ? hashp = sharekey
2009-07-19 18:34:13 +02:00
*/
2005-02-05 00:02:46 +01:00
2016-01-06 16:18:52 +01:00
//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
2020-04-10 10:59:32 +02:00
if ( ! defined ( 'NOREQUIRESOC' )) define ( 'NOREQUIRESOC' , '1' );
if ( ! defined ( 'NOREQUIRETRAN' )) define ( 'NOREQUIRETRAN' , '1' );
if ( ! defined ( 'NOCSRFCHECK' )) define ( 'NOCSRFCHECK' , '1' );
if ( ! defined ( 'NOTOKENRENEWAL' )) define ( 'NOTOKENRENEWAL' , '1' );
if ( ! defined ( 'NOREQUIREMENU' )) define ( 'NOREQUIREMENU' , '1' );
if ( ! defined ( 'NOREQUIREHTML' )) define ( 'NOREQUIREHTML' , '1' );
2021-02-20 11:34:11 +01:00
if ( ! defined ( 'NOREQUIREAJAX' )) define ( 'NOREQUIREAJAX' , '1' );
2018-04-21 12:00:55 +02:00
2016-07-03 20:50:28 +02:00
// Some value of modulepart can be used to get resources that are public so no login are required.
2018-09-09 12:53:30 +02:00
// Note that only directory logo is free to access without login.
if ( isset ( $_GET [ " modulepart " ]) && $_GET [ " modulepart " ] == 'mycompany' && preg_match ( '/^\/?logos\//' , $_GET [ 'file' ]))
2018-02-13 13:55:36 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! defined ( " NOLOGIN " )) define ( " NOLOGIN " , 1 );
if ( ! defined ( " NOCSRFCHECK " )) define ( " NOCSRFCHECK " , 1 ); // We accept to go on this page from external web site.
if ( ! defined ( " NOIPCHECK " )) define ( " NOIPCHECK " , 1 ); // Do not check IP defined into conf $dolibarr_main_restrict_ip
2018-02-13 13:55:36 +01:00
}
// For direct external download link, we don't need to load/check we are into a login session
2020-04-10 10:59:32 +02:00
if ( isset ( $_GET [ " hashp " ]) && ! defined ( " NOLOGIN " ))
2018-02-13 13:55:36 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! defined ( " NOLOGIN " )) define ( " NOLOGIN " , 1 );
if ( ! defined ( " NOCSRFCHECK " )) define ( " NOCSRFCHECK " , 1 ); // We accept to go on this page from external web site.
if ( ! defined ( " NOIPCHECK " )) define ( " NOIPCHECK " , 1 ); // Do not check IP defined into conf $dolibarr_main_restrict_ip
2018-02-13 13:55:36 +01:00
}
// Some value of modulepart can be used to get resources that are public so no login are required.
2020-05-15 14:34:24 +02:00
if ( isset ( $_GET [ " modulepart " ]) && $_GET [ " modulepart " ] == 'medias' )
2016-11-11 15:03:05 +01:00
{
2020-04-10 10:59:32 +02:00
if ( ! defined ( " NOLOGIN " )) define ( " NOLOGIN " , 1 );
if ( ! defined ( " NOCSRFCHECK " )) define ( " NOCSRFCHECK " , 1 ); // We accept to go on this page from external web site.
if ( ! defined ( " NOIPCHECK " )) define ( " NOIPCHECK " , 1 ); // Do not check IP defined into conf $dolibarr_main_restrict_ip
2016-11-04 10:06:53 +01:00
}
2012-01-11 20:31:40 +01:00
2020-05-14 18:03:22 +02:00
// Used by TakePOS Auto Order
2020-05-15 14:34:24 +02:00
if ( isset ( $_GET [ " modulepart " ]) && $_GET [ " modulepart " ] == 'product' && isset ( $_GET [ " publictakepos " ]))
2020-05-14 18:03:22 +02:00
{
if ( ! defined ( " NOLOGIN " )) define ( " NOLOGIN " , 1 );
if ( ! defined ( " NOCSRFCHECK " )) define ( " NOCSRFCHECK " , 1 ); // We accept to go on this page from external web site.
if ( ! defined ( " NOIPCHECK " )) define ( " NOIPCHECK " , 1 ); // Do not check IP defined into conf $dolibarr_main_restrict_ip
}
2018-08-30 02:33:22 +02:00
// For multicompany
2020-04-10 10:59:32 +02:00
$entity = ( ! empty ( $_GET [ 'entity' ]) ? ( int ) $_GET [ 'entity' ] : ( ! empty ( $_POST [ 'entity' ]) ? ( int ) $_POST [ 'entity' ] : 1 ));
2018-08-30 02:33:22 +02:00
if ( is_numeric ( $entity )) define ( " DOLENTITY " , $entity );
2011-07-09 02:15:16 +02:00
/**
2013-04-15 15:43:25 +02:00
* Header empty
2011-07-09 02:15:16 +02:00
*
2019-06-22 18:06:14 +02:00
* @ ignore
2013-04-15 15:43:25 +02:00
* @ return void
2011-07-09 02:15:16 +02:00
*/
2018-08-15 14:28:34 +02:00
function llxHeader ()
{
}
2013-04-15 15:43:25 +02:00
/**
* Footer empty
*
2019-06-22 18:06:14 +02:00
* @ ignore
2013-04-15 15:43:25 +02:00
* @ return void
*/
2018-08-15 14:28:34 +02:00
function llxFooter ()
{
}
2008-01-02 23:11:50 +01:00
2020-04-10 10:59:32 +02:00
require 'main.inc.php' ; // Load $user and permissions
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2008-01-02 23:11:50 +01:00
2020-09-16 19:39:50 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2020-04-10 10:59:32 +02:00
$original_file = GETPOST ( 'file' , 'alphanohtml' ); // Do not use urldecode here ($_GET are already decoded by PHP).
$hashp = GETPOST ( 'hashp' , 'aZ09' );
$modulepart = GETPOST ( 'modulepart' , 'alpha' );
$urlsource = GETPOST ( 'urlsource' , 'alpha' );
$entity = GETPOST ( 'entity' , 'int' ) ? GETPOST ( 'entity' , 'int' ) : $conf -> entity ;
2012-01-11 20:31:40 +01:00
2011-06-15 13:35:33 +02:00
// Security check
2019-01-27 11:55:16 +01:00
if ( empty ( $modulepart ) && empty ( $hashp )) accessforbidden ( 'Bad link. Bad value for parameter modulepart' , 0 , 0 , 1 );
2019-08-12 22:19:24 +02:00
if ( empty ( $original_file ) && empty ( $hashp ) && $modulepart != 'barcode' ) accessforbidden ( 'Bad link. Missing identification to find file (param file or hashp)' , 0 , 0 , 1 );
2020-04-10 10:59:32 +02:00
if ( $modulepart == 'fckeditor' ) $modulepart = 'medias' ; // For backward compatibility
2011-06-15 13:35:33 +02:00
2012-01-11 20:31:40 +01:00
2018-09-09 12:53:30 +02:00
2011-06-15 13:35:33 +02:00
/*
* Actions
*/
// None
/*
* View
*/
2011-04-10 22:42:18 +02:00
2019-01-27 11:55:16 +01:00
if ( GETPOST ( " cache " , 'alpha' ))
2011-04-10 22:42:18 +02:00
{
2020-10-31 14:32:18 +01:00
// Important: Following code is to avoid page request by browser and PHP CPU at
// each Dolibarr page access.
if ( empty ( $dolibarr_nocache ))
{
header ( 'Cache-Control: max-age=3600, public, must-revalidate' );
header ( 'Pragma: cache' ); // This is to avoid having Pragma: no-cache
} else header ( 'Cache-Control: no-cache' );
//print $dolibarr_nocache; exit;
2011-04-10 22:42:18 +02:00
}
2018-02-13 13:55:36 +01:00
// If we have a hash public (hashp), we guess the original_file.
2020-04-10 10:59:32 +02:00
if ( ! empty ( $hashp ))
2018-02-13 13:55:36 +01:00
{
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
2020-04-10 10:59:32 +02:00
$ecmfile = new EcmFiles ( $db );
2018-02-13 13:55:36 +01:00
$result = $ecmfile -> fetch ( 0 , '' , '' , '' , $hashp );
if ( $result > 0 )
{
2020-04-10 10:59:32 +02:00
$tmp = explode ( '/' , $ecmfile -> filepath , 2 ); // $ecmfile->filepath is relative to document directory
2018-12-17 15:35:20 +01:00
// filepath can be 'users/X' or 'X/propale/PR11111'
if ( is_numeric ( $tmp [ 0 ])) // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
{
$tmp = explode ( '/' , $tmp [ 1 ], 2 );
}
2020-04-10 10:59:32 +02:00
$moduleparttocheck = $tmp [ 0 ]; // moduleparttocheck is first part of path
2018-12-17 15:35:20 +01:00
if ( $modulepart ) // Not required, so often not defined, for link using public hashp parameter.
2018-02-13 13:55:36 +01:00
{
if ( $moduleparttocheck == $modulepart )
{
// We remove first level of directory
2020-04-10 10:59:32 +02:00
$original_file = (( $tmp [ 1 ] ? $tmp [ 1 ] . '/' : '' ) . $ecmfile -> filename ); // this is relative to module dir
2018-02-13 13:55:36 +01:00
//var_dump($original_file); exit;
2020-05-21 01:03:03 +02:00
} else {
2019-01-27 11:55:16 +01:00
accessforbidden ( 'Bad link. File is from another module part.' , 0 , 0 , 1 );
2018-02-13 13:55:36 +01:00
}
2020-05-21 01:03:03 +02:00
} else {
2018-02-13 13:55:36 +01:00
$modulepart = $moduleparttocheck ;
2020-04-10 10:59:32 +02:00
$original_file = (( $tmp [ 1 ] ? $tmp [ 1 ] . '/' : '' ) . $ecmfile -> filename ); // this is relative to module dir
2018-02-13 13:55:36 +01:00
}
2020-05-21 01:03:03 +02:00
} else {
2018-02-13 13:55:36 +01:00
$langs -> load ( " errors " );
2019-01-27 11:55:16 +01:00
accessforbidden ( $langs -> trans ( " ErrorFileNotFoundWithSharedLink " ), 0 , 0 , 1 );
2018-02-13 13:55:36 +01:00
}
}
2009-07-16 02:43:59 +02:00
// Define mime type
$type = 'application/octet-stream' ;
2020-04-10 10:59:32 +02:00
if ( GETPOST ( 'type' , 'alpha' )) $type = GETPOST ( 'type' , 'alpha' );
else $type = dol_mimetype ( $original_file );
2008-01-02 23:11:50 +01:00
2019-04-25 23:36:19 +02:00
// Security: This wrapper is for images. We do not allow type/html
2019-07-30 20:55:15 +02:00
if ( preg_match ( '/html/i' , $type )) accessforbidden ( 'Error: Using the image wrapper to output a file with a mime type HTML is not possible.' , 0 , 0 , 1 );
2019-07-30 14:00:43 +02:00
// Security: This wrapper is for images. We do not allow files ending with .noexe
2019-07-30 20:55:15 +02:00
if ( preg_match ( '/\.noexe$/i' , $original_file )) accessforbidden ( 'Error: Using the image wrapper to output a file ending with .noexe is not allowed.' , 0 , 0 , 1 );
2019-04-25 23:36:19 +02:00
2016-05-01 16:34:50 +02:00
// Security: Delete string ../ into $original_file
2019-01-27 11:55:16 +01:00
$original_file = str_replace ( " ../ " , " / " , $original_file );
2004-10-07 11:36:56 +02:00
2013-06-05 16:12:07 +02:00
// Find the subdirectory name as the reference
2020-04-10 10:59:32 +02:00
$refname = basename ( dirname ( $original_file ) . " / " );
2013-06-05 16:12:07 +02:00
// Security check
2019-06-04 12:21:27 +02:00
if ( empty ( $modulepart )) accessforbidden ( 'Bad value for parameter modulepart' , 0 , 0 , 1 );
2018-02-13 13:55:36 +01:00
2022-02-22 17:13:26 +01:00
$check_access = dol_check_secure_access_document ( $modulepart , $original_file , $entity , $user , $refname );
2013-06-05 16:12:07 +02:00
$accessallowed = $check_access [ 'accessallowed' ];
$sqlprotectagainstexternals = $check_access [ 'sqlprotectagainstexternals' ];
2020-04-10 10:59:32 +02:00
$fullpath_original_file = $check_access [ 'original_file' ]; // $fullpath_original_file is now a full path name
2018-02-13 13:55:36 +01:00
2020-05-21 15:05:19 +02:00
if ( ! empty ( $hashp )) {
2020-04-10 10:59:32 +02:00
$accessallowed = 1 ; // When using hashp, link is public so we force $accessallowed
2018-02-13 13:55:36 +01:00
$sqlprotectagainstexternals = '' ;
2020-05-21 15:05:19 +02:00
} elseif ( isset ( $_GET [ " publictakepos " ])) {
2020-10-31 14:32:18 +01:00
if ( ! empty ( $conf -> global -> TAKEPOS_AUTO_ORDER )) {
2020-05-15 14:34:24 +02:00
$accessallowed = 1 ; // Only if TakePOS Public Auto Order is enabled and received publictakepos variable
}
2020-05-21 01:03:03 +02:00
} else {
2018-02-13 13:55:36 +01:00
// Basic protection (against external users only)
2019-10-31 20:46:31 +01:00
if ( $user -> socid > 0 )
2018-02-13 13:55:36 +01:00
{
if ( $sqlprotectagainstexternals )
{
$resql = $db -> query ( $sqlprotectagainstexternals );
if ( $resql )
{
2020-04-10 10:59:32 +02:00
$num = $db -> num_rows ( $resql );
$i = 0 ;
2018-02-13 13:55:36 +01:00
while ( $i < $num )
{
$obj = $db -> fetch_object ( $resql );
2019-10-31 20:46:31 +01:00
if ( $user -> socid != $obj -> fk_soc )
2018-02-13 13:55:36 +01:00
{
2020-04-10 10:59:32 +02:00
$accessallowed = 0 ;
2018-02-13 13:55:36 +01:00
break ;
}
$i ++ ;
}
}
}
}
}
2004-10-07 11:36:56 +02:00
2007-04-12 21:47:50 +02:00
// Security:
2008-01-27 18:22:41 +01:00
// Limit access if permissions are wrong
2020-04-10 10:59:32 +02:00
if ( ! $accessallowed )
2006-05-14 04:51:11 +02:00
{
2018-02-13 13:55:36 +01:00
accessforbidden ();
2006-05-14 04:51:11 +02:00
}
2004-10-07 11:36:56 +02:00
2007-04-12 21:47:50 +02:00
// Security:
2011-07-06 18:56:01 +02:00
// On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
2019-01-27 11:55:16 +01:00
if ( preg_match ( '/\.\./' , $fullpath_original_file ) || preg_match ( '/[<>|]/' , $fullpath_original_file ))
2007-04-12 21:47:50 +02:00
{
2017-09-06 11:39:30 +02:00
dol_syslog ( " Refused to deliver file " . $fullpath_original_file );
2020-09-16 16:34:19 +02:00
print " ErrorFileNameInvalid: " . dol_escape_htmltag ( $original_file );
2018-02-13 13:55:36 +01:00
exit ;
2007-04-12 21:47:50 +02:00
}
2004-10-07 11:36:56 +02:00
2008-01-03 19:41:32 +01:00
if ( $modulepart == 'barcode' )
2006-05-14 04:51:11 +02:00
{
2020-10-31 14:32:18 +01:00
$generator = GETPOST ( " generator " , " alpha " );
$code = GETPOST ( " code " , 'none' ); // This can be rich content (qrcode, datamatrix, ...)
$encoding = GETPOST ( " encoding " , " alpha " );
$readable = GETPOST ( " readable " , 'alpha' ) ? GETPOST ( " readable " , " alpha " ) : " Y " ;
if ( empty ( $generator ) || empty ( $encoding ))
{
print 'Error: Parameter "generator" or "encoding" not defined' ;
exit ;
}
$dirbarcode = array_merge ( array ( " /core/modules/barcode/doc/ " ), $conf -> modules_parts [ 'barcode' ]);
$result = 0 ;
foreach ( $dirbarcode as $reldir )
{
$dir = dol_buildpath ( $reldir , 0 );
$newdir = dol_osencode ( $dir );
// Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
if ( ! is_dir ( $newdir )) continue ;
$result = @ include_once $newdir . $generator . '.modules.php' ;
if ( $result ) break ;
}
// Load barcode class
$classname = " mod " . ucfirst ( $generator );
$module = new $classname ( $db );
if ( $module -> encodingIsSupported ( $encoding ))
{
$result = $module -> buildBarCode ( $code , $encoding , $readable );
}
2020-05-21 01:03:03 +02:00
} else {
2020-10-31 14:32:18 +01:00
// Open and return file
clearstatcache ();
2011-06-11 16:14:27 +02:00
2020-10-31 14:32:18 +01:00
$filename = basename ( $fullpath_original_file );
2018-02-13 13:55:36 +01:00
2020-10-31 14:32:18 +01:00
// Output files on browser
dol_syslog ( " viewimage.php return file $fullpath_original_file filename= $filename content-type= $type " );
2011-06-11 16:14:27 +02:00
2020-10-31 14:32:18 +01:00
// This test is to avoid error images when image is not available (for example thumbs).
if ( ! dol_is_file ( $fullpath_original_file ) && empty ( $_GET [ " noalt " ]))
{
$fullpath_original_file = DOL_DOCUMENT_ROOT . '/public/theme/common/nophoto.png' ;
/* $error = 'Error: File ' . $_GET [ " file " ] . ' does not exists or filesystems permissions are not allowed' ;
2011-07-06 18:56:01 +02:00
print $error ;
2012-05-30 00:05:42 +02:00
exit ; */
2020-10-31 14:32:18 +01:00
}
2011-06-11 16:14:27 +02:00
2020-10-31 14:32:18 +01:00
// Permissions are ok and file found, so we return it
if ( $type )
{
top_httphead ( $type );
header ( 'Content-Disposition: inline; filename="' . basename ( $fullpath_original_file ) . '"' );
} else {
top_httphead ( 'image/png' );
header ( 'Content-Disposition: inline; filename="' . basename ( $fullpath_original_file ) . '"' );
}
2011-06-11 16:14:27 +02:00
2020-10-31 14:32:18 +01:00
$fullpath_original_file_osencoded = dol_osencode ( $fullpath_original_file );
2017-09-06 11:39:30 +02:00
2020-10-31 14:32:18 +01:00
readfile ( $fullpath_original_file_osencoded );
2005-02-05 00:02:46 +01:00
}
2011-11-01 15:06:03 +01:00
if ( is_object ( $db )) $db -> close ();