2008-04-28 22:49:59 +02:00
< ? php
2016-02-11 20:01:51 +01:00
/* Copyright ( C ) 2008 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2021-10-17 11:59:25 +02:00
* Copyright ( C ) 2012 - 2021 Regis Houssin < regis . houssin @ inodbox . com >
2016-12-10 14:47:18 +01:00
* Copyright ( C ) 2012 - 2016 Juanjo Menent < jmenent @ 2 byte . es >
2016-02-11 20:01:51 +01:00
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2016 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2019-02-02 08:59:37 +01:00
* Copyright ( C ) 2019 Frédéric France < frederic . france @ netlogic . fr >
2008-04-28 22:49:59 +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
2008-04-28 22:49:59 +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 /
2008-04-28 22:49:59 +02:00
*/
/**
2011-10-24 12:59:44 +02:00
* \file htdocs / core / lib / files . lib . php
2008-11-16 02:09:04 +01:00
* \brief Library for file managing functions
*/
2008-04-28 22:49:59 +02:00
2012-06-24 19:32:19 +02:00
/**
* Make a basename working with all page code ( default PHP basenamed fails with cyrillic ) .
* We supose dir separator for input is '/' .
*
* @ param string $pathfile String to find basename .
* @ return string Basename of input
*/
function dol_basename ( $pathfile )
{
2019-01-27 11:55:16 +01:00
return preg_replace ( '/^.*\/([^\/]+)$/' , '$1' , rtrim ( $pathfile , '/' ));
2012-06-24 19:32:19 +02:00
}
2011-06-01 16:05:08 +02:00
2008-04-29 23:13:49 +02:00
/**
2010-10-03 17:42:01 +02:00
* Scan a directory and return a list of files / directories .
* Content for string is UTF8 and dir separator is " / " .
2011-12-17 01:16:33 +01:00
*
2016-12-13 00:15:21 +01:00
* @ param string $path Starting path from which to search . This is a full path .
2014-04-23 15:53:45 +02:00
* @ param string $types Can be " directories " , " files " , or " all "
* @ param int $recursive Determines whether subdirectories are searched
2017-06-01 12:11:45 +02:00
* @ param string $filter Regex filter to restrict list . This regex value must be escaped for '/' by doing preg_quote ( $var , '/' ), since this char is used for preg_match function ,
2017-05-11 13:01:49 +02:00
* but must not contains the start and end '/' . Filter is checked into basename only .
2017-11-19 16:26:39 +01:00
* @ param array $excludefilter Array of Regex for exclude filter ( example : array ( '(\.meta|_preview.*\.png)$' , '^\.' )) . Exclude is checked both into fullpath and into basename ( So '^xxx' may exclude 'xxx/dirscanned/...' and dirscanned / xxx ' ) .
2017-10-16 08:52:00 +02:00
* @ param string $sortcriteria Sort criteria ( '' , 'fullname' , 'relativename' , 'name' , 'date' , 'size' )
2014-04-23 15:53:45 +02:00
* @ param string $sortorder Sort order ( SORT_ASC , SORT_DESC )
2021-04-06 13:56:33 +02:00
* @ param int $mode 0 = Return array minimum keys loaded ( faster ), 1 = Force all keys like date and size to be loaded ( slower ), 2 = Force load of date only , 3 = Force load of size only , 4 = Force load of perm
2014-04-23 15:53:45 +02:00
* @ param int $nohook Disable all hooks
2017-07-17 10:26:36 +02:00
* @ param string $relativename For recursive purpose only . Must be " " at first call .
2018-03-03 11:22:15 +01:00
* @ param string $donotfollowsymlinks Do not follow symbolic links
2016-12-13 00:15:21 +01:00
* @ return array Array of array ( 'name' => 'xxx' , 'fullname' => '/abc/xxx' , 'date' => 'yyy' , 'size' => 99 , 'type' => 'dir|file' , ... )
2019-03-11 01:01:15 +01:00
* @ see dol_dir_list_in_database ()
2008-04-29 23:13:49 +02:00
*/
2019-01-27 15:20:16 +01:00
function dol_dir_list ( $path , $types = " all " , $recursive = 0 , $filter = " " , $excludefilter = null , $sortcriteria = " name " , $sortorder = SORT_ASC , $mode = 0 , $nohook = 0 , $relativename = " " , $donotfollowsymlinks = 0 )
2008-04-29 23:13:49 +02:00
{
2012-09-05 10:17:02 +02:00
global $db , $hookmanager ;
2012-09-11 17:01:54 +02:00
global $object ;
2012-09-05 10:17:02 +02:00
2021-05-01 13:22:01 +02:00
if ( $recursive <= 1 ) { // Avoid too verbose log
dol_syslog ( " files.lib.php::dol_dir_list path= " . $path . " types= " . $types . " recursive= " . $recursive . " filter= " . $filter . " excludefilter= " . json_encode ( $excludefilter ));
//print 'xxx'."files.lib.php::dol_dir_list path=".$path." types=".$types." recursive=".$recursive." filter=".$filter." excludefilter=".json_encode($excludefilter);
}
2008-04-29 23:13:49 +02:00
2019-11-13 19:37:08 +01:00
$loaddate = ( $mode == 1 || $mode == 2 ) ? true : false ;
$loadsize = ( $mode == 1 || $mode == 3 ) ? true : false ;
2021-04-06 13:56:33 +02:00
$loadperm = ( $mode == 1 || $mode == 4 ) ? true : false ;
2009-02-24 23:52:55 +01:00
2008-04-29 23:13:49 +02:00
// Clean parameters
2019-11-13 19:37:08 +01:00
$path = preg_replace ( '/([\\/]+)$/i' , '' , $path );
$newpath = dol_osencode ( $path );
2009-02-24 23:52:55 +01:00
2017-02-27 02:04:47 +01:00
$reshook = 0 ;
$file_list = array ();
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( is_object ( $hookmanager ) && ! $nohook ) {
2019-11-13 19:37:08 +01:00
$hookmanager -> resArray = array ();
2017-06-09 00:27:19 +02:00
2017-10-07 13:09:31 +02:00
$hookmanager -> initHooks ( array ( 'fileslib' ));
2012-09-17 19:32:45 +02:00
2019-11-13 19:37:08 +01:00
$parameters = array (
2020-01-20 14:29:02 +01:00
'path' => $newpath ,
'types' => $types ,
'recursive' => $recursive ,
'filter' => $filter ,
'excludefilter' => $excludefilter ,
'sortcriteria' => $sortcriteria ,
'sortorder' => $sortorder ,
'loaddate' => $loaddate ,
'loadsize' => $loadsize ,
'mode' => $mode
2012-09-17 19:32:45 +02:00
);
2019-11-13 19:37:08 +01:00
$reshook = $hookmanager -> executeHooks ( 'getDirList' , $parameters , $object );
2012-09-17 19:32:45 +02:00
}
2012-09-05 10:17:02 +02:00
2012-09-05 14:30:12 +02:00
// $hookmanager->resArray may contain array stacked by other modules
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook )) {
if ( ! is_dir ( $newpath )) {
return array ();
}
2012-09-07 17:23:16 +02:00
2021-02-23 22:03:23 +01:00
if ( $dir = opendir ( $newpath )) {
2019-11-13 19:37:08 +01:00
$filedate = '' ;
$filesize = '' ;
2021-10-23 16:12:44 +02:00
$fileperm = '' ;
2021-02-23 22:03:23 +01:00
while ( false !== ( $file = readdir ( $dir ))) { // $file is always a basename (into directory $newpath)
if ( ! utf8_check ( $file )) {
$file = utf8_encode ( $file ); // To be sure data is stored in utf8 in memory
}
2019-11-13 19:37:08 +01:00
$fullpathfile = ( $newpath ? $newpath . '/' : '' ) . $file ;
2012-09-07 17:23:16 +02:00
2019-11-13 19:37:08 +01:00
$qualified = 1 ;
2012-09-07 17:23:16 +02:00
// Define excludefilterarray
2019-11-13 19:37:08 +01:00
$excludefilterarray = array ( '^\.' );
2021-02-23 22:03:23 +01:00
if ( is_array ( $excludefilter )) {
2019-11-13 19:37:08 +01:00
$excludefilterarray = array_merge ( $excludefilterarray , $excludefilter );
2021-02-23 22:03:23 +01:00
} elseif ( $excludefilter ) {
$excludefilterarray [] = $excludefilter ;
}
2012-09-07 17:23:16 +02:00
// Check if file is qualified
2021-02-23 22:03:23 +01:00
foreach ( $excludefilterarray as $filt ) {
2017-11-19 16:26:39 +01:00
if ( preg_match ( '/' . $filt . '/i' , $file ) || preg_match ( '/' . $filt . '/i' , $fullpathfile )) {
2021-03-01 20:37:16 +01:00
$qualified = 0 ;
break ;
2012-09-07 17:23:16 +02:00
}
}
2017-11-19 16:26:39 +01:00
//print $fullpathfile.' '.$file.' '.$qualified.'<br>';
2012-09-07 17:23:16 +02:00
2021-02-23 22:03:23 +01:00
if ( $qualified ) {
2019-11-13 19:37:08 +01:00
$isdir = is_dir ( dol_osencode ( $path . " / " . $file ));
2012-09-07 17:23:16 +02:00
// Check whether this is a file or directory and whether we're interested in that type
2021-05-01 13:22:01 +02:00
if ( $isdir && (( $types == " directories " ) || ( $types == " all " ) || $recursive > 0 )) {
2012-09-07 17:23:16 +02:00
// Add entry into file_list array
2021-02-23 22:03:23 +01:00
if (( $types == " directories " ) || ( $types == " all " )) {
if ( $loaddate || $sortcriteria == 'date' ) {
$filedate = dol_filemtime ( $path . " / " . $file );
}
if ( $loadsize || $sortcriteria == 'size' ) {
$filesize = dol_filesize ( $path . " / " . $file );
}
2021-04-06 13:56:33 +02:00
if ( $loadperm || $sortcriteria == 'perm' ) {
$fileperm = dol_fileperm ( $path . " / " . $file );
}
2012-09-07 17:23:16 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $filter || preg_match ( '/' . $filter . '/i' , $file )) { // We do not search key $filter into all $path, only into $file part
2020-09-19 21:19:04 +02:00
$reg = array ();
2019-01-27 11:55:16 +01:00
preg_match ( '/([^\/]+)\/[^\/]+$/' , $path . '/' . $file , $reg );
2019-11-13 19:37:08 +01:00
$level1name = ( isset ( $reg [ 1 ]) ? $reg [ 1 ] : '' );
2012-09-07 17:23:16 +02:00
$file_list [] = array (
2020-01-20 14:29:02 +01:00
" name " => $file ,
" path " => $path ,
" level1name " => $level1name ,
" relativename " => ( $relativename ? $relativename . '/' : '' ) . $file ,
" fullname " => $path . '/' . $file ,
" date " => $filedate ,
" size " => $filesize ,
2021-04-06 13:56:33 +02:00
" perm " => $fileperm ,
2020-01-20 14:29:02 +01:00
" type " => 'dir'
2012-09-07 17:23:16 +02:00
);
}
}
// if we're in a directory and we want recursive behavior, call this function again
2021-05-01 13:22:01 +02:00
if ( $recursive > 0 ) {
2021-02-23 22:03:23 +01:00
if ( empty ( $donotfollowsymlinks ) || ! is_link ( $path . " / " . $file )) {
2018-03-03 11:22:15 +01:00
//var_dump('eee '. $path."/".$file. ' '.is_dir($path."/".$file).' '.is_link($path."/".$file));
2021-05-01 13:22:01 +02:00
$file_list = array_merge ( $file_list , dol_dir_list ( $path . " / " . $file , $types , $recursive + 1 , $filter , $excludefilter , $sortcriteria , $sortorder , $mode , $nohook , ( $relativename != '' ? $relativename . '/' : '' ) . $file , $donotfollowsymlinks ));
2018-03-03 11:22:15 +01:00
}
2012-09-07 17:23:16 +02:00
}
2021-02-23 22:03:23 +01:00
} elseif ( ! $isdir && (( $types == " files " ) || ( $types == " all " ))) {
2012-09-07 17:23:16 +02:00
// Add file into file_list array
2021-02-23 22:03:23 +01:00
if ( $loaddate || $sortcriteria == 'date' ) {
$filedate = dol_filemtime ( $path . " / " . $file );
}
if ( $loadsize || $sortcriteria == 'size' ) {
$filesize = dol_filesize ( $path . " / " . $file );
}
2012-09-07 17:23:16 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $filter || preg_match ( '/' . $filter . '/i' , $file )) { // We do not search key $filter into $path, only into $file
2019-01-27 11:55:16 +01:00
preg_match ( '/([^\/]+)\/[^\/]+$/' , $path . '/' . $file , $reg );
2019-11-13 19:37:08 +01:00
$level1name = ( isset ( $reg [ 1 ]) ? $reg [ 1 ] : '' );
2012-09-07 17:23:16 +02:00
$file_list [] = array (
2020-01-20 14:29:02 +01:00
" name " => $file ,
" path " => $path ,
" level1name " => $level1name ,
" relativename " => ( $relativename ? $relativename . '/' : '' ) . $file ,
" fullname " => $path . '/' . $file ,
" date " => $filedate ,
" size " => $filesize ,
" type " => 'file'
2012-09-07 17:23:16 +02:00
);
}
}
}
}
closedir ( $dir );
// Obtain a list of columns
2021-02-23 22:03:23 +01:00
if ( ! empty ( $sortcriteria ) && $sortorder ) {
2020-01-20 14:29:02 +01:00
$file_list = dol_sort_array ( $file_list , $sortcriteria , ( $sortorder == SORT_ASC ? 'asc' : 'desc' ));
2012-09-07 17:23:16 +02:00
}
2012-09-05 10:17:02 +02:00
}
2008-04-29 23:13:49 +02:00
}
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( is_object ( $hookmanager ) && is_array ( $hookmanager -> resArray )) {
$file_list = array_merge ( $file_list , $hookmanager -> resArray );
}
2017-06-01 12:11:45 +02:00
2017-02-27 02:04:47 +01:00
return $file_list ;
2008-04-29 23:13:49 +02:00
}
2015-08-25 17:05:08 +02:00
2016-12-13 00:15:21 +01:00
/**
* Scan a directory and return a list of files / directories .
* Content for string is UTF8 and dir separator is " / " .
*
2016-12-31 16:16:20 +01:00
* @ param string $path Starting path from which to search . Example : 'produit/MYPROD'
2016-12-13 00:15:21 +01:00
* @ param string $filter Regex filter to restrict list . This regex value must be escaped for '/' , since this char is used for preg_match function
2017-04-12 11:30:33 +02:00
* @ param array | null $excludefilter Array of Regex for exclude filter ( example : array ( '(\.meta|_preview.*\.png)$' , '^\.' ))
2016-12-13 00:15:21 +01:00
* @ param string $sortcriteria Sort criteria ( " " , " fullname " , " name " , " date " , " size " )
* @ param string $sortorder Sort order ( SORT_ASC , SORT_DESC )
* @ param int $mode 0 = Return array minimum keys loaded ( faster ), 1 = Force all keys like description
* @ return array Array of array ( 'name' => 'xxx' , 'fullname' => '/abc/xxx' , 'type' => 'dir|file' , ... )
2019-03-11 01:01:15 +01:00
* @ see dol_dir_list ()
2016-12-13 00:15:21 +01:00
*/
2019-01-27 15:20:16 +01:00
function dol_dir_list_in_database ( $path , $filter = " " , $excludefilter = null , $sortcriteria = " name " , $sortorder = SORT_ASC , $mode = 0 )
2016-12-13 00:15:21 +01:00
{
2017-10-07 13:09:31 +02:00
global $conf , $db ;
2020-08-23 15:15:47 +02:00
$sql = " SELECT rowid, label, entity, filename, filepath, fullpath_orig, keywords, cover, gen_or_uploaded, extraparams, " ;
$sql .= " date_c, tms as date_m, fk_user_c, fk_user_m, acl, position, share " ;
2021-02-23 22:03:23 +01:00
if ( $mode ) {
$sql .= " , description " ;
}
2019-11-13 19:37:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " ecm_files " ;
2020-11-11 22:47:06 +01:00
$sql .= " WHERE entity = " . $conf -> entity ;
if ( preg_match ( '/%$/' , $path )) {
$sql .= " AND filepath LIKE ' " . $db -> escape ( $path ) . " ' " ;
} else {
$sql .= " AND filepath = ' " . $db -> escape ( $path ) . " ' " ;
}
2017-10-07 13:09:31 +02:00
$resql = $db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:37:08 +01:00
$file_list = array ();
2017-10-07 13:09:31 +02:00
$num = $db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
2020-11-11 22:47:06 +01:00
$reg = array ();
2019-01-27 11:55:16 +01:00
preg_match ( '/([^\/]+)\/[^\/]+$/' , DOL_DATA_ROOT . '/' . $obj -> filepath . '/' . $obj -> filename , $reg );
2019-11-13 19:37:08 +01:00
$level1name = ( isset ( $reg [ 1 ]) ? $reg [ 1 ] : '' );
2017-10-07 13:09:31 +02:00
$file_list [] = array (
" rowid " => $obj -> rowid ,
2019-11-13 19:37:08 +01:00
" label " => $obj -> label , // md5
2017-10-07 13:09:31 +02:00
" name " => $obj -> filename ,
" path " => DOL_DATA_ROOT . '/' . $obj -> filepath ,
" level1name " => $level1name ,
" fullname " => DOL_DATA_ROOT . '/' . $obj -> filepath . '/' . $obj -> filename ,
" fullpath_orig " => $obj -> fullpath_orig ,
" date_c " => $db -> jdate ( $obj -> date_c ),
" date_m " => $db -> jdate ( $obj -> date_m ),
" type " => 'file' ,
" keywords " => $obj -> keywords ,
" cover " => $obj -> cover ,
" position " => ( int ) $obj -> position ,
2018-02-13 13:55:36 +01:00
" acl " => $obj -> acl ,
" share " => $obj -> share
2017-10-07 13:09:31 +02:00
);
}
$i ++ ;
}
// Obtain a list of columns
2021-02-23 22:03:23 +01:00
if ( ! empty ( $sortcriteria )) {
2019-11-13 19:37:08 +01:00
$myarray = array ();
2021-02-23 22:03:23 +01:00
foreach ( $file_list as $key => $row ) {
2019-11-13 19:37:08 +01:00
$myarray [ $key ] = ( isset ( $row [ $sortcriteria ]) ? $row [ $sortcriteria ] : '' );
2017-10-07 13:09:31 +02:00
}
// Sort the data
2021-02-23 22:03:23 +01:00
if ( $sortorder ) {
array_multisort ( $myarray , $sortorder , $file_list );
}
2017-10-07 13:09:31 +02:00
}
return $file_list ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $db );
return array ();
}
2016-12-13 00:15:21 +01:00
}
2017-06-01 12:11:45 +02:00
2016-12-13 00:15:21 +01:00
2017-08-18 16:10:21 +02:00
/**
* Complete $filearray with data from database .
* This will call doldir_list_indatabase to complate filearray .
2017-08-21 00:40:32 +02:00
*
2020-11-11 22:47:06 +01:00
* @ param array $filearray Array of files obtained using dol_dir_list
2017-08-22 09:47:33 +02:00
* @ param string $relativedir Relative dir from DOL_DATA_ROOT
2017-08-18 16:10:21 +02:00
* @ return void
*/
function completeFileArrayWithDatabaseInfo ( & $filearray , $relativedir )
{
2018-03-08 16:33:51 +01:00
global $conf , $db , $user ;
2017-08-21 00:40:32 +02:00
2017-08-18 16:10:21 +02:00
$filearrayindatabase = dol_dir_list_in_database ( $relativedir , '' , null , 'name' , SORT_ASC );
2018-03-08 16:33:51 +01:00
// TODO Remove this when PRODUCT_USE_OLD_PATH_FOR_PHOTO will be removed
global $modulepart ;
2019-11-13 19:37:08 +01:00
if ( $modulepart == 'produit' && ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) {
2018-03-08 16:33:51 +01:00
global $object ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $object -> id )) {
if ( ! empty ( $conf -> product -> enabled )) {
$upload_dirold = $conf -> product -> multidir_output [ $object -> entity ] . '/' . substr ( substr ( " 000 " . $object -> id , - 2 ), 1 , 1 ) . '/' . substr ( substr ( " 000 " . $object -> id , - 2 ), 0 , 1 ) . '/' . $object -> id . " /photos " ;
} else {
$upload_dirold = $conf -> service -> multidir_output [ $object -> entity ] . '/' . substr ( substr ( " 000 " . $object -> id , - 2 ), 1 , 1 ) . '/' . substr ( substr ( " 000 " . $object -> id , - 2 ), 0 , 1 ) . '/' . $object -> id . " /photos " ;
}
2018-03-08 16:33:51 +01:00
2019-01-27 11:55:16 +01:00
$relativedirold = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $upload_dirold );
$relativedirold = preg_replace ( '/^[\\/]/' , '' , $relativedirold );
2018-03-08 16:33:51 +01:00
$filearrayindatabase = array_merge ( $filearrayindatabase , dol_dir_list_in_database ( $relativedirold , '' , null , 'name' , SORT_ASC ));
}
}
2020-11-11 22:47:06 +01:00
/* var_dump ( $relativedir );
var_dump ( $filearray );
var_dump ( $filearrayindatabase ); */
2017-08-18 16:10:21 +02:00
// Complete filearray with properties found into $filearrayindatabase
2021-02-23 22:03:23 +01:00
foreach ( $filearray as $key => $val ) {
2020-06-17 14:45:15 +02:00
$tmpfilename = preg_replace ( '/\.noexe$/' , '' , $filearray [ $key ][ 'name' ]);
2019-11-13 19:37:08 +01:00
$found = 0 ;
2017-08-18 16:10:21 +02:00
// Search if it exists into $filearrayindatabase
2021-02-23 22:03:23 +01:00
foreach ( $filearrayindatabase as $key2 => $val2 ) {
if (( $filearrayindatabase [ $key2 ][ 'path' ] == $filearray [ $key ][ 'path' ]) && ( $filearrayindatabase [ $key2 ][ 'name' ] == $tmpfilename )) {
2019-11-13 19:37:08 +01:00
$filearray [ $key ][ 'position_name' ] = ( $filearrayindatabase [ $key2 ][ 'position' ] ? $filearrayindatabase [ $key2 ][ 'position' ] : '0' ) . '_' . $filearrayindatabase [ $key2 ][ 'name' ];
$filearray [ $key ][ 'position' ] = $filearrayindatabase [ $key2 ][ 'position' ];
$filearray [ $key ][ 'cover' ] = $filearrayindatabase [ $key2 ][ 'cover' ];
$filearray [ $key ][ 'acl' ] = $filearrayindatabase [ $key2 ][ 'acl' ];
$filearray [ $key ][ 'rowid' ] = $filearrayindatabase [ $key2 ][ 'rowid' ];
$filearray [ $key ][ 'label' ] = $filearrayindatabase [ $key2 ][ 'label' ];
$filearray [ $key ][ 'share' ] = $filearrayindatabase [ $key2 ][ 'share' ];
$found = 1 ;
2017-08-18 16:10:21 +02:00
break ;
}
}
2021-02-23 22:03:23 +01:00
if ( ! $found ) { // This happen in transition toward version 6, or if files were added manually into os dir.
2019-11-13 19:37:08 +01:00
$filearray [ $key ][ 'position' ] = '999999' ; // File not indexed are at end. So if we add a file, it will not replace an existing position
$filearray [ $key ][ 'cover' ] = 0 ;
$filearray [ $key ][ 'acl' ] = '' ;
2017-08-18 16:10:21 +02:00
2019-01-27 11:55:16 +01:00
$rel_filename = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $filearray [ $key ][ 'fullname' ]);
2020-11-11 22:47:06 +01:00
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/([\\/]temp[\\/]|[\\/]thumbs|\.meta$)/' , $rel_filename )) { // If not a tmp file
2017-08-18 16:10:21 +02:00
dol_syslog ( " list_of_documents We found a file called ' " . $filearray [ $key ][ 'name' ] . " ' not indexed into database. We add it " );
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
2019-11-13 19:37:08 +01:00
$ecmfile = new EcmFiles ( $db );
2017-08-18 16:10:21 +02:00
// Add entry into database
$filename = basename ( $rel_filename );
$rel_dir = dirname ( $rel_filename );
$rel_dir = preg_replace ( '/[\\/]$/' , '' , $rel_dir );
$rel_dir = preg_replace ( '/^[\\/]/' , '' , $rel_dir );
$ecmfile -> filepath = $rel_dir ;
$ecmfile -> filename = $filename ;
2019-11-13 19:37:08 +01:00
$ecmfile -> label = md5_file ( dol_osencode ( $filearray [ $key ][ 'fullname' ])); // $destfile is a full path to file
2017-08-18 16:10:21 +02:00
$ecmfile -> fullpath_orig = $filearray [ $key ][ 'fullname' ];
$ecmfile -> gen_or_uploaded = 'unknown' ;
2019-11-13 19:37:08 +01:00
$ecmfile -> description = '' ; // indexed content
2021-06-29 19:49:26 +02:00
$ecmfile -> keywords = '' ; // keyword content
2017-08-18 16:10:21 +02:00
$result = $ecmfile -> create ( $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2017-08-18 16:10:21 +02:00
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$filearray [ $key ][ 'rowid' ] = $result ;
2017-08-18 16:10:21 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$filearray [ $key ][ 'rowid' ] = 0 ; // Should not happened
2017-08-18 16:10:21 +02:00
}
}
}
2020-11-11 22:47:06 +01:00
//var_dump($filearray); var_dump($relativedir.' - tmpfilename='.$tmpfilename.' - found='.$found);
2017-08-18 16:10:21 +02:00
}
2008-04-29 23:13:49 +02:00
/**
2010-07-28 00:38:28 +02:00
* Fast compare of 2 files identified by their properties -> name , -> date and -> size
2011-12-17 01:16:33 +01:00
*
2011-11-12 12:59:06 +01:00
* @ param string $a File 1
* @ param string $b File 2
* @ return int 1 , 0 , 1
2008-04-29 23:13:49 +02:00
*/
2008-04-28 22:49:59 +02:00
function dol_compare_file ( $a , $b )
{
global $sortorder ;
global $sortfield ;
2009-02-24 23:52:55 +01:00
2019-11-13 19:37:08 +01:00
$sortorder = strtoupper ( $sortorder );
2009-02-24 23:52:55 +01:00
2021-02-23 22:03:23 +01:00
if ( $sortorder == 'ASC' ) {
2021-03-01 20:37:16 +01:00
$retup = - 1 ;
$retdown = 1 ;
2021-02-23 22:03:23 +01:00
} else {
2021-03-01 20:37:16 +01:00
$retup = 1 ;
$retdown = - 1 ;
2021-02-23 22:03:23 +01:00
}
2009-02-24 23:52:55 +01:00
2021-02-23 22:03:23 +01:00
if ( $sortfield == 'name' ) {
if ( $a -> name == $b -> name ) {
return 0 ;
}
2008-04-28 22:49:59 +02:00
return ( $a -> name < $b -> name ) ? $retup : $retdown ;
}
2021-02-23 22:03:23 +01:00
if ( $sortfield == 'date' ) {
if ( $a -> date == $b -> date ) {
return 0 ;
}
2008-04-28 22:49:59 +02:00
return ( $a -> date < $b -> date ) ? $retup : $retdown ;
}
2021-02-23 22:03:23 +01:00
if ( $sortfield == 'size' ) {
if ( $a -> size == $b -> size ) {
return 0 ;
}
2008-04-28 22:49:59 +02:00
return ( $a -> size < $b -> size ) ? $retup : $retdown ;
}
}
2008-11-16 02:09:04 +01:00
2010-08-21 17:30:17 +02:00
/**
2011-11-12 12:59:06 +01:00
* Test if filename is a directory
2011-08-26 19:59:14 +02:00
*
2011-11-12 12:59:06 +01:00
* @ param string $folder Name of folder
* @ return boolean True if it ' s a directory , False if not found
2010-08-21 17:30:17 +02:00
*/
function dol_is_dir ( $folder )
{
2019-11-13 19:37:08 +01:00
$newfolder = dol_osencode ( $folder );
2021-02-23 22:03:23 +01:00
if ( is_dir ( $newfolder )) {
return true ;
} else {
return false ;
}
2010-08-21 17:30:17 +02:00
}
2019-03-10 19:33:28 +01:00
/**
* Return if path is empty
*
* @ param string $dir Path of Directory
* @ return boolean True or false
*/
2019-03-11 01:01:15 +01:00
function dol_is_dir_empty ( $dir )
{
2021-02-23 22:03:23 +01:00
if ( ! is_readable ( $dir )) {
return false ;
}
2020-01-20 14:29:02 +01:00
return ( count ( scandir ( $dir )) == 2 );
2019-03-10 19:33:28 +01:00
}
2010-09-29 15:14:48 +02:00
/**
* Return if path is a file
2011-08-26 19:59:14 +02:00
*
2011-11-12 12:59:06 +01:00
* @ param string $pathoffile Path of file
* @ return boolean True or false
2010-09-29 15:14:48 +02:00
*/
function dol_is_file ( $pathoffile )
{
2019-11-13 19:37:08 +01:00
$newpathoffile = dol_osencode ( $pathoffile );
2017-10-07 13:09:31 +02:00
return is_file ( $newpathoffile );
2010-09-29 15:14:48 +02:00
}
2010-08-21 17:30:17 +02:00
2018-04-25 17:59:05 +02:00
/**
* Return if path is a symbolic link
*
* @ param string $pathoffile Path of file
* @ return boolean True or false
*/
function dol_is_link ( $pathoffile )
{
2019-11-13 19:37:08 +01:00
$newpathoffile = dol_osencode ( $pathoffile );
2018-04-25 17:59:05 +02:00
return is_link ( $newpathoffile );
}
2011-08-26 19:59:14 +02:00
/**
* Return if path is an URL
*
2011-11-12 12:59:06 +01:00
* @ param string $url Url
* @ return boolean True or false
2011-08-26 19:59:14 +02:00
*/
function dol_is_url ( $url )
{
2019-11-13 19:37:08 +01:00
$tmpprot = array ( 'file' , 'http' , 'https' , 'ftp' , 'zlib' , 'data' , 'ssh' , 'ssh2' , 'ogg' , 'expect' );
2021-02-23 22:03:23 +01:00
foreach ( $tmpprot as $prot ) {
if ( preg_match ( '/^' . $prot . ':/i' , $url )) {
return true ;
}
2017-10-07 13:09:31 +02:00
}
return false ;
2011-08-26 19:59:14 +02:00
}
2008-11-16 02:09:04 +01:00
/**
2010-09-29 15:14:48 +02:00
* Test if a folder is empty
2011-08-26 19:59:14 +02:00
*
2011-11-12 12:59:06 +01:00
* @ param string $folder Name of folder
* @ return boolean True if dir is empty or non - existing , False if it contains files
2008-11-16 02:09:04 +01:00
*/
function dol_dir_is_emtpy ( $folder )
{
2019-11-13 19:37:08 +01:00
$newfolder = dol_osencode ( $folder );
2021-02-23 22:03:23 +01:00
if ( is_dir ( $newfolder )) {
2009-10-04 17:52:16 +02:00
$handle = opendir ( $newfolder );
2017-10-07 13:09:31 +02:00
$folder_content = '' ;
2021-02-23 22:03:23 +01:00
while (( gettype ( $name = readdir ( $handle )) != " boolean " )) {
2008-11-16 02:09:04 +01:00
$name_array [] = $name ;
}
2021-02-23 22:03:23 +01:00
foreach ( $name_array as $temp ) {
$folder_content .= $temp ;
}
2008-11-16 02:09:04 +01:00
2017-10-07 13:09:31 +02:00
closedir ( $handle );
2013-11-08 14:42:28 +01:00
2021-02-23 22:03:23 +01:00
if ( $folder_content == " ... " ) {
return true ;
} else {
return false ;
}
} else {
return true ; // Dir does not exists
}
2008-11-16 02:09:04 +01:00
}
2009-09-30 18:02:54 +02:00
/**
2010-09-29 15:14:48 +02:00
* Count number of lines in a file
2011-12-17 01:16:33 +01:00
*
2011-11-12 12:59:06 +01:00
* @ param string $file Filename
* @ return int < 0 if KO , Number of lines in files if OK
2019-03-11 01:01:15 +01:00
* @ see dol_nboflines ()
2009-09-30 18:02:54 +02:00
*/
function dol_count_nb_of_line ( $file )
{
2019-11-13 19:37:08 +01:00
$nb = 0 ;
2009-10-04 17:52:16 +02:00
2019-11-13 19:37:08 +01:00
$newfile = dol_osencode ( $file );
2009-09-30 18:02:54 +02:00
//print 'x'.$file;
2019-11-13 19:37:08 +01:00
$fp = fopen ( $newfile , 'r' );
2021-02-23 22:03:23 +01:00
if ( $fp ) {
while ( ! feof ( $fp )) {
2019-11-13 19:37:08 +01:00
$line = fgets ( $fp );
2017-10-07 13:09:31 +02:00
// We increase count only if read was success. We need test because feof return true only after fgets so we do n+1 fgets for a file with n lines.
2021-02-23 22:03:23 +01:00
if ( ! $line === false ) {
$nb ++ ;
}
2009-10-04 17:52:16 +02:00
}
fclose ( $fp );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$nb = - 1 ;
2009-09-30 18:02:54 +02:00
}
return $nb ;
}
2009-10-04 19:18:09 +02:00
/**
* Return size of a file
2011-12-17 01:16:33 +01:00
*
2013-11-07 21:24:01 +01:00
* @ param string $pathoffile Path of file
2015-04-06 12:25:30 +02:00
* @ return integer File size
2019-08-12 22:19:24 +02:00
* @ see dol_print_size ()
2009-10-04 19:18:09 +02:00
*/
function dol_filesize ( $pathoffile )
{
2019-11-13 19:37:08 +01:00
$newpathoffile = dol_osencode ( $pathoffile );
2009-10-13 23:46:09 +02:00
return filesize ( $newpathoffile );
2009-10-04 19:18:09 +02:00
}
/**
* Return time of a file
2011-12-17 01:16:33 +01:00
*
2011-11-12 12:59:06 +01:00
* @ param string $pathoffile Path of file
2013-11-07 21:12:11 +01:00
* @ return int Time of file
2009-10-04 19:18:09 +02:00
*/
2009-10-04 20:01:30 +02:00
function dol_filemtime ( $pathoffile )
2009-10-04 19:18:09 +02:00
{
2019-11-13 19:37:08 +01:00
$newpathoffile = dol_osencode ( $pathoffile );
2011-06-10 22:32:22 +02:00
return @ filemtime ( $newpathoffile ); // @Is to avoid errors if files does not exists
2009-10-04 19:18:09 +02:00
}
2010-02-10 18:44:25 +01:00
2021-04-06 13:56:33 +02:00
/**
* Return permissions of a file
*
* @ param string $pathoffile Path of file
* @ return integer File permissions
*/
function dol_fileperm ( $pathoffile )
{
$newpathoffile = dol_osencode ( $pathoffile );
return fileperms ( $newpathoffile );
}
2017-05-11 13:01:49 +02:00
/**
* Make replacement of strings into a file .
*
2019-03-17 19:33:25 +01:00
* @ param string $srcfile Source file ( can ' t be a directory )
* @ param array $arrayreplacement Array with strings to replace . Example : array ( 'valuebefore' => 'valueafter' , ... )
* @ param string $destfile Destination file ( can ' t be a directory ) . If empty , will be same than source file .
* @ param int $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK ) . Example : '0666'
* @ param int $indexdatabase 1 = index new file into database .
* @ param int $arrayreplacementisregex 1 = Array of replacement is regex
* @ return int < 0 if error , 0 if nothing done ( dest file already exists ), > 0 if OK
* @ see dol_copy ()
2017-05-11 13:01:49 +02:00
*/
2019-03-17 19:33:25 +01:00
function dolReplaceInFile ( $srcfile , $arrayreplacement , $destfile = '' , $newmask = 0 , $indexdatabase = 0 , $arrayreplacementisregex = 0 )
2017-05-11 13:01:49 +02:00
{
2017-10-07 13:09:31 +02:00
global $conf ;
2017-05-11 13:01:49 +02:00
2019-03-17 19:33:25 +01:00
dol_syslog ( " files.lib.php::dolReplaceInFile srcfile= " . $srcfile . " destfile= " . $destfile . " newmask= " . $newmask . " indexdatabase= " . $indexdatabase . " arrayreplacementisregex= " . $arrayreplacementisregex );
2017-05-11 13:01:49 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $srcfile )) {
return - 1 ;
}
if ( empty ( $destfile )) {
$destfile = $srcfile ;
}
2017-06-01 12:11:45 +02:00
2019-11-13 19:37:08 +01:00
$destexists = dol_is_file ( $destfile );
2021-02-23 22:03:23 +01:00
if (( $destfile != $srcfile ) && $destexists ) {
return 0 ;
}
2017-06-01 12:11:45 +02:00
2019-11-13 19:37:08 +01:00
$tmpdestfile = $destfile . '.tmp' ;
2017-05-11 13:01:49 +02:00
2019-11-13 19:37:08 +01:00
$newpathofsrcfile = dol_osencode ( $srcfile );
$newpathoftmpdestfile = dol_osencode ( $tmpdestfile );
$newpathofdestfile = dol_osencode ( $destfile );
$newdirdestfile = dirname ( $newpathofdestfile );
2017-05-11 13:01:49 +02:00
2021-02-23 22:03:23 +01:00
if ( $destexists && ! is_writable ( $newpathofdestfile )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dolReplaceInFile failed Permission denied to overwrite target file " , LOG_WARNING );
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! is_writable ( $newdirdestfile )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dolReplaceInFile failed Permission denied to write into target directory " . $newdirdestfile , LOG_WARNING );
return - 2 ;
}
2017-06-01 12:11:45 +02:00
2017-10-07 13:09:31 +02:00
dol_delete_file ( $tmpdestfile );
2017-06-01 12:11:45 +02:00
2017-10-07 13:09:31 +02:00
// Create $newpathoftmpdestfile from $newpathofsrcfile
2018-08-16 21:48:16 +02:00
$content = file_get_contents ( $newpathofsrcfile , 'r' );
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $arrayreplacementisregex )) {
2020-01-20 14:29:02 +01:00
$content = make_substitutions ( $content , $arrayreplacement , null );
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
foreach ( $arrayreplacement as $key => $value ) {
2020-01-20 14:29:02 +01:00
$content = preg_replace ( $key , $value , $content );
}
2019-03-17 19:33:25 +01:00
}
2017-06-01 12:11:45 +02:00
2017-10-07 13:09:31 +02:00
file_put_contents ( $newpathoftmpdestfile , $content );
@ chmod ( $newpathoftmpdestfile , octdec ( $newmask ));
2017-06-01 12:11:45 +02:00
2017-10-07 13:09:31 +02:00
// Rename
2019-11-13 19:37:08 +01:00
$result = dol_move ( $newpathoftmpdestfile , $newpathofdestfile , $newmask , (( $destfile == $srcfile ) ? 1 : 0 ), 0 , $indexdatabase );
2021-02-23 22:03:23 +01:00
if ( ! $result ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dolReplaceInFile failed to move tmp file to final dest " , LOG_WARNING );
return - 3 ;
}
2021-02-23 22:03:23 +01:00
if ( empty ( $newmask ) && ! empty ( $conf -> global -> MAIN_UMASK )) {
$newmask = $conf -> global -> MAIN_UMASK ;
}
if ( empty ( $newmask )) { // This should no happen
2017-10-07 13:09:31 +02:00
dol_syslog ( " Warning: dolReplaceInFile called with empty value for newmask and no default value defined " , LOG_WARNING );
2019-11-13 19:37:08 +01:00
$newmask = '0664' ;
2017-10-07 13:09:31 +02:00
}
2017-05-11 13:01:49 +02:00
2017-10-07 13:09:31 +02:00
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
2017-05-11 13:01:49 +02:00
2017-10-07 13:09:31 +02:00
return 1 ;
2017-05-11 13:01:49 +02:00
}
2017-07-22 01:00:43 +02:00
2009-12-15 21:45:21 +01:00
/**
2012-02-05 19:37:52 +01:00
* Copy a file to another file .
2012-02-04 14:39:47 +01:00
*
* @ param string $srcfile Source file ( can ' t be a directory )
* @ param string $destfile Destination file ( can ' t be a directory )
2015-04-03 23:42:56 +02:00
* @ param int $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK ) . Example : '0666'
2012-02-04 14:39:47 +01:00
* @ param int $overwriteifexists Overwrite file if exists ( 1 by default )
2012-02-05 19:37:52 +01:00
* @ return int < 0 if error , 0 if nothing done ( dest file already exists and overwriteifexists = 0 ), > 0 if OK
2019-03-11 01:01:15 +01:00
* @ see dol_delete_file () dolCopyDir ()
2009-12-15 21:45:21 +01:00
*/
2019-01-27 15:20:16 +01:00
function dol_copy ( $srcfile , $destfile , $newmask = 0 , $overwriteifexists = 1 )
2009-12-15 21:45:21 +01:00
{
2010-02-13 22:26:52 +01:00
global $conf ;
2012-12-29 12:16:45 +01:00
dol_syslog ( " files.lib.php::dol_copy srcfile= " . $srcfile . " destfile= " . $destfile . " newmask= " . $newmask . " overwriteifexists= " . $overwriteifexists );
2015-04-03 23:42:56 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $srcfile ) || empty ( $destfile )) {
return - 1 ;
}
2015-04-03 23:42:56 +02:00
2019-11-13 19:37:08 +01:00
$destexists = dol_is_file ( $destfile );
2021-02-23 22:03:23 +01:00
if ( ! $overwriteifexists && $destexists ) {
return 0 ;
}
2012-02-05 19:37:52 +01:00
2019-11-13 19:37:08 +01:00
$newpathofsrcfile = dol_osencode ( $srcfile );
$newpathofdestfile = dol_osencode ( $destfile );
$newdirdestfile = dirname ( $newpathofdestfile );
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( $destexists && ! is_writable ( $newpathofdestfile )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_copy failed Permission denied to overwrite target file " , LOG_WARNING );
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! is_writable ( $newdirdestfile )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_copy failed Permission denied to write into target directory " . $newdirdestfile , LOG_WARNING );
return - 2 ;
}
// Copy with overwriting if exists
2019-11-13 19:37:08 +01:00
$result = @ copy ( $newpathofsrcfile , $newpathofdestfile );
2012-02-05 19:37:52 +01:00
//$result=copy($newpathofsrcfile, $newpathofdestfile); // To see errors, remove @
2021-02-23 22:03:23 +01:00
if ( ! $result ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_copy failed to copy " , LOG_WARNING );
return - 3 ;
2010-03-21 21:29:29 +01:00
}
2021-02-23 22:03:23 +01:00
if ( empty ( $newmask ) && ! empty ( $conf -> global -> MAIN_UMASK )) {
$newmask = $conf -> global -> MAIN_UMASK ;
}
if ( empty ( $newmask )) { // This should no happen
2014-04-02 13:17:20 +02:00
dol_syslog ( " Warning: dol_copy called with empty value for newmask and no default value defined " , LOG_WARNING );
2019-11-13 19:37:08 +01:00
$newmask = '0664' ;
2014-04-02 13:17:20 +02:00
}
2014-04-30 13:50:25 +02:00
2012-02-05 19:37:52 +01:00
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
2010-02-13 22:26:52 +01:00
2012-02-05 19:37:52 +01:00
return 1 ;
2009-12-15 21:45:21 +01:00
}
2009-10-04 19:18:09 +02:00
2015-04-03 23:42:56 +02:00
/**
2017-08-21 00:40:32 +02:00
* Copy a dir to another dir . This include recursive subdirectories .
2015-04-03 23:42:56 +02:00
*
* @ param string $srcfile Source file ( a directory )
* @ param string $destfile Destination file ( a directory )
* @ param int $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK ) . Example : '0666'
* @ param int $overwriteifexists Overwrite file if exists ( 1 by default )
2017-05-28 14:43:17 +02:00
* @ param array $arrayreplacement Array to use to replace filenames with another one during the copy ( works only on file names , not on directory names ) .
2020-05-03 14:49:06 +02:00
* @ param int $excludesubdir 0 = Do not exclude subdirectories , 1 = Exclude subdirectories , 2 = Exclude subdirectories if name is not a 2 chars ( used for country codes subdirectories ) .
2017-05-28 14:43:17 +02:00
* @ return int < 0 if error , 0 if nothing done ( all files already exists and overwriteifexists = 0 ), > 0 if OK
2019-03-11 01:01:15 +01:00
* @ see dol_copy ()
2015-04-03 23:42:56 +02:00
*/
2020-05-03 14:49:06 +02:00
function dolCopyDir ( $srcfile , $destfile , $newmask , $overwriteifexists , $arrayreplacement = null , $excludesubdir = 0 )
2015-04-03 23:42:56 +02:00
{
global $conf ;
2019-11-13 19:37:08 +01:00
$result = 0 ;
2015-04-03 23:42:56 +02:00
2017-02-11 15:40:25 +01:00
dol_syslog ( " files.lib.php::dolCopyDir srcfile= " . $srcfile . " destfile= " . $destfile . " newmask= " . $newmask . " overwriteifexists= " . $overwriteifexists );
2015-04-03 23:42:56 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $srcfile ) || empty ( $destfile )) {
return - 1 ;
}
2015-04-03 23:42:56 +02:00
2019-11-13 19:37:08 +01:00
$destexists = dol_is_dir ( $destfile );
2017-05-30 01:32:36 +02:00
//if (! $overwriteifexists && $destexists) return 0; // The overwriteifexists is for files only, so propagated to dol_copy only.
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $destexists ) {
2017-10-07 13:09:31 +02:00
// We must set mask just before creating dir, becaause it can be set differently by dol_copy
umask ( 0 );
2019-11-13 19:37:08 +01:00
$dirmaskdec = octdec ( $newmask );
2021-02-23 22:03:23 +01:00
if ( empty ( $newmask ) && ! empty ( $conf -> global -> MAIN_UMASK )) {
$dirmaskdec = octdec ( $conf -> global -> MAIN_UMASK );
}
2019-11-13 19:37:08 +01:00
$dirmaskdec |= octdec ( '0200' ); // Set w bit required to be able to create content for recursive subdirs files
2017-10-07 13:09:31 +02:00
dol_mkdir ( $destfile , '' , decoct ( $dirmaskdec ));
}
2017-06-01 12:11:45 +02:00
2019-11-13 19:37:08 +01:00
$ossrcfile = dol_osencode ( $srcfile );
$osdestfile = dol_osencode ( $destfile );
2015-04-03 23:42:56 +02:00
2017-10-07 13:09:31 +02:00
// Recursive function to copy all subdirectories and contents:
2021-02-23 22:03:23 +01:00
if ( is_dir ( $ossrcfile )) {
2019-11-13 19:37:08 +01:00
$dir_handle = opendir ( $ossrcfile );
2021-02-23 22:03:23 +01:00
while ( $file = readdir ( $dir_handle )) {
if ( $file != " . " && $file != " .. " && ! is_link ( $ossrcfile . " / " . $file )) {
if ( is_dir ( $ossrcfile . " / " . $file )) {
2020-05-03 14:49:06 +02:00
if ( empty ( $excludesubdir ) || ( $excludesubdir == 2 && strlen ( $file ) == 2 )) {
2020-05-05 21:45:10 +02:00
$newfile = $file ;
// Replace destination filename with a new one
2021-02-23 22:03:23 +01:00
if ( is_array ( $arrayreplacement )) {
foreach ( $arrayreplacement as $key => $val ) {
2020-05-05 21:45:10 +02:00
$newfile = str_replace ( $key , $val , $newfile );
}
}
2020-05-03 14:49:06 +02:00
//var_dump("xxx dolCopyDir $srcfile/$file, $destfile/$file, $newmask, $overwriteifexists");
2020-05-05 21:45:10 +02:00
$tmpresult = dolCopyDir ( $srcfile . " / " . $file , $destfile . " / " . $newfile , $newmask , $overwriteifexists , $arrayreplacement , $excludesubdir );
2020-05-03 14:49:06 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-05-28 14:43:17 +02:00
$newfile = $file ;
// Replace destination filename with a new one
2021-02-23 22:03:23 +01:00
if ( is_array ( $arrayreplacement )) {
foreach ( $arrayreplacement as $key => $val ) {
2017-05-28 14:43:17 +02:00
$newfile = str_replace ( $key , $val , $newfile );
}
}
2019-11-13 19:37:08 +01:00
$tmpresult = dol_copy ( $srcfile . " / " . $file , $destfile . " / " . $newfile , $newmask , $overwriteifexists );
2017-10-07 13:09:31 +02:00
}
// Set result
2021-02-23 22:03:23 +01:00
if ( $result > 0 && $tmpresult >= 0 ) {
2017-10-07 13:09:31 +02:00
// Do nothing, so we don't set result to 0 if tmpresult is 0 and result was success in a previous pass
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$result = $tmpresult ;
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
break ;
}
2017-10-07 13:09:31 +02:00
}
}
closedir ( $dir_handle );
2020-05-21 15:05:19 +02:00
} else {
2017-05-28 14:43:17 +02:00
// Source directory does not exists
2017-10-07 13:09:31 +02:00
$result = - 2 ;
}
2015-04-03 23:42:56 +02:00
2017-10-07 13:09:31 +02:00
return $result ;
2015-04-03 23:42:56 +02:00
}
2010-10-03 18:59:24 +02:00
/**
2012-09-07 10:30:02 +02:00
* Move a file into another name .
2017-06-01 12:11:45 +02:00
* Note :
2017-05-21 14:06:43 +02:00
* - This function differs from dol_move_uploaded_file , because it can be called in any context .
2017-08-18 16:10:21 +02:00
* - Database indexes for files are updated .
2017-06-01 12:11:45 +02:00
* - Test on antivirus is done only if param testvirus is provided and an antivirus was set .
2011-12-17 01:16:33 +01:00
*
2015-04-03 23:42:56 +02:00
* @ param string $srcfile Source file ( can ' t be a directory . use native php @ rename () to move a directory )
* @ param string $destfile Destination file ( can ' t be a directory . use native php @ rename () to move a directory )
2015-10-14 23:50:36 +02:00
* @ param integer $newmask Mask in octal string for new file ( 0 by default means $conf -> global -> MAIN_UMASK )
2011-11-12 12:59:06 +01:00
* @ param int $overwriteifexists Overwrite file if exists ( 1 by default )
2017-05-21 18:30:43 +02:00
* @ param int $testvirus Do an antivirus test . Move is canceled if a virus is found .
2017-05-28 14:43:17 +02:00
* @ param int $indexdatabase Index new file into database .
2011-11-12 12:59:06 +01:00
* @ return boolean True if OK , false if KO
2019-03-11 01:01:15 +01:00
* @ see dol_move_uploaded_file ()
2010-10-03 18:59:24 +02:00
*/
2019-01-27 15:20:16 +01:00
function dol_move ( $srcfile , $destfile , $newmask = 0 , $overwriteifexists = 1 , $testvirus = 0 , $indexdatabase = 1 )
2010-10-03 18:59:24 +02:00
{
2017-10-07 13:09:31 +02:00
global $user , $db , $conf ;
2019-11-13 19:37:08 +01:00
$result = false ;
2017-06-01 12:11:45 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_move srcfile= " . $srcfile . " destfile= " . $destfile . " newmask= " . $newmask . " overwritifexists= " . $overwriteifexists );
2019-11-13 19:37:08 +01:00
$srcexists = dol_is_file ( $srcfile );
$destexists = dol_is_file ( $destfile );
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $srcexists ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_move srcfile does not exists. we ignore the move request. " );
return false ;
}
2021-02-23 22:03:23 +01:00
if ( $overwriteifexists || ! $destexists ) {
2019-11-13 19:37:08 +01:00
$newpathofsrcfile = dol_osencode ( $srcfile );
$newpathofdestfile = dol_osencode ( $destfile );
2017-10-07 13:09:31 +02:00
// Check virus
2019-11-13 19:37:08 +01:00
$testvirusarray = array ();
2021-02-23 22:03:23 +01:00
if ( $testvirus ) {
2019-11-13 19:37:08 +01:00
$testvirusarray = dolCheckVirus ( $newpathofsrcfile );
2021-02-23 22:03:23 +01:00
if ( count ( $testvirusarray )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_move canceled because a virus was found into source file. we ignore the move request. " , LOG_WARNING );
return false ;
}
}
2019-11-13 19:37:08 +01:00
$result = @ rename ( $newpathofsrcfile , $newpathofdestfile ); // To see errors, remove @
2021-02-23 22:03:23 +01:00
if ( ! $result ) {
if ( $destexists ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " files.lib.php::dol_move Failed. We try to delete target first and move after. " , LOG_WARNING );
// We force delete and try again. Rename function sometimes fails to replace dest file with some windows NTFS partitions.
dol_delete_file ( $destfile );
2019-11-13 19:37:08 +01:00
$result = @ rename ( $newpathofsrcfile , $newpathofdestfile ); // To see errors, remove @
2021-02-23 22:03:23 +01:00
} else {
dol_syslog ( " files.lib.php::dol_move Failed. " , LOG_WARNING );
}
2017-10-07 13:09:31 +02:00
}
// Move ok
2021-02-23 22:03:23 +01:00
if ( $result && $indexdatabase ) {
2017-10-07 13:09:31 +02:00
// Rename entry into ecm database
2019-01-27 11:55:16 +01:00
$rel_filetorenamebefore = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $srcfile );
$rel_filetorenameafter = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $destfile );
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/([\\/]temp[\\/]|[\\/]thumbs|\.meta$)/' , $rel_filetorenameafter )) { // If not a tmp file
2017-10-07 13:09:31 +02:00
$rel_filetorenamebefore = preg_replace ( '/^[\\/]/' , '' , $rel_filetorenamebefore );
$rel_filetorenameafter = preg_replace ( '/^[\\/]/' , '' , $rel_filetorenameafter );
2020-05-27 13:12:18 +02:00
//var_dump($rel_filetorenamebefore.' - '.$rel_filetorenameafter);exit;
2017-10-07 13:09:31 +02:00
dol_syslog ( " Try to rename also entries in database for full relative path before = " . $rel_filetorenamebefore . " after = " . $rel_filetorenameafter , LOG_DEBUG );
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
2019-11-13 19:37:08 +01:00
$ecmfiletarget = new EcmFiles ( $db );
2017-10-07 13:09:31 +02:00
$resultecmtarget = $ecmfiletarget -> fetch ( 0 , '' , $rel_filetorenameafter );
2021-02-23 22:03:23 +01:00
if ( $resultecmtarget > 0 ) { // An entry for target name already exists for target, we delete it, a new one will be created.
2017-10-07 13:09:31 +02:00
$ecmfiletarget -> delete ( $user );
}
2019-11-13 19:37:08 +01:00
$ecmfile = new EcmFiles ( $db );
2017-10-07 13:09:31 +02:00
$resultecm = $ecmfile -> fetch ( 0 , '' , $rel_filetorenamebefore );
2021-02-23 22:03:23 +01:00
if ( $resultecm > 0 ) { // If an entry was found for src file, we use it to move entry
2017-10-07 13:09:31 +02:00
$filename = basename ( $rel_filetorenameafter );
$rel_dir = dirname ( $rel_filetorenameafter );
$rel_dir = preg_replace ( '/[\\/]$/' , '' , $rel_dir );
$rel_dir = preg_replace ( '/^[\\/]/' , '' , $rel_dir );
$ecmfile -> filepath = $rel_dir ;
$ecmfile -> filename = $filename ;
2020-05-27 13:12:18 +02:00
2017-10-07 13:09:31 +02:00
$resultecm = $ecmfile -> update ( $user );
2021-02-23 22:03:23 +01:00
} elseif ( $resultecm == 0 ) { // If no entry were found for src files, create/update target file
2017-10-07 13:09:31 +02:00
$filename = basename ( $rel_filetorenameafter );
$rel_dir = dirname ( $rel_filetorenameafter );
$rel_dir = preg_replace ( '/[\\/]$/' , '' , $rel_dir );
$rel_dir = preg_replace ( '/^[\\/]/' , '' , $rel_dir );
$ecmfile -> filepath = $rel_dir ;
$ecmfile -> filename = $filename ;
2019-11-13 19:37:08 +01:00
$ecmfile -> label = md5_file ( dol_osencode ( $destfile )); // $destfile is a full path to file
2017-10-07 13:09:31 +02:00
$ecmfile -> fullpath_orig = $srcfile ;
$ecmfile -> gen_or_uploaded = 'unknown' ;
2019-11-13 19:37:08 +01:00
$ecmfile -> description = '' ; // indexed content
2021-06-29 19:49:26 +02:00
$ecmfile -> keywords = '' ; // keyword content
2017-10-07 13:09:31 +02:00
$resultecm = $ecmfile -> create ( $user );
2021-02-23 22:03:23 +01:00
if ( $resultecm < 0 ) {
2017-10-07 13:09:31 +02:00
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
}
2021-02-23 22:03:23 +01:00
} elseif ( $resultecm < 0 ) {
2017-10-07 13:09:31 +02:00
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
}
2021-02-23 22:03:23 +01:00
if ( $resultecm > 0 ) {
$result = true ;
} else {
$result = false ;
}
2017-10-07 13:09:31 +02:00
}
}
2010-10-03 18:59:24 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $newmask )) {
$newmask = empty ( $conf -> global -> MAIN_UMASK ) ? '0755' : $conf -> global -> MAIN_UMASK ;
}
2019-11-13 19:37:08 +01:00
$newmaskdec = octdec ( $newmask );
2017-10-07 13:09:31 +02:00
// Currently method is restricted to files (dol_delete_files previously used is for files, and mask usage if for files too)
// to allow mask usage for dir, we shoul introduce a new param "isdir" to 1 to complete newmask like this
// if ($isdir) $newmaskdec |= octdec('0111'); // Set x bit required for directories
@ chmod ( $newpathofdestfile , $newmaskdec );
}
return $result ;
2010-10-03 18:59:24 +02:00
}
2010-05-01 16:28:48 +02:00
2012-05-30 12:43:23 +02:00
/**
2012-06-10 13:22:28 +02:00
* Unescape a file submitted by upload .
* PHP escape char " (%22) or char ' (%27) into $FILES .
2012-05-30 12:43:23 +02:00
*
* @ param string $filename Filename
2012-06-10 13:22:28 +02:00
* @ return string Filename sanitized
2012-05-30 12:43:23 +02:00
*/
function dol_unescapefile ( $filename )
{
2012-07-02 19:30:37 +02:00
// Remove path information and dots around the filename, to prevent uploading
// into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename:
2012-06-01 10:16:42 +02:00
return trim ( basename ( $filename ), " . \x00 .. \x20 " );
2012-05-30 12:43:23 +02:00
}
2017-05-21 14:06:43 +02:00
/**
* Check virus into a file
2017-06-01 12:11:45 +02:00
*
2017-05-21 14:06:43 +02:00
* @ param string $src_file Source file to check
* @ return array Array of errors or empty array if not virus found
*/
function dolCheckVirus ( $src_file )
{
2022-02-25 09:33:08 +01:00
global $conf , $db ;
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_ANTIVIRUS_COMMAND )) {
2019-11-13 19:37:08 +01:00
if ( ! class_exists ( 'AntiVir' )) {
2017-10-07 13:09:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/antivir.class.php' ;
}
2020-09-07 10:18:17 +02:00
$antivir = new AntiVir ( $db );
2017-10-07 13:09:31 +02:00
$result = $antivir -> dol_avscan_file ( $src_file );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) { // If virus or error, we stop here
2020-09-07 10:18:17 +02:00
$reterrors = $antivir -> errors ;
2017-10-07 13:09:31 +02:00
return $reterrors ;
}
}
return array ();
2017-05-21 14:06:43 +02:00
}
2012-09-07 17:23:16 +02:00
/**
* Make control on an uploaded file from an GUI page and move it to final destination .
* If there is errors ( virus found , antivir in error , bad filename ), file is not moved .
2017-06-01 12:11:45 +02:00
* Note :
2017-05-21 14:06:43 +02:00
* - This function can be used only into a HTML page context . Use dol_move if you are outside .
* - Test on antivirus is always done ( if antivirus set ) .
2018-02-23 19:55:15 +01:00
* - Database of files is NOT updated ( this is done by dol_add_file_process () that calls this function ) .
2019-08-16 21:45:13 +02:00
* - Extension . noexe may be added if file is executable and MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED is not set .
2012-09-07 17:23:16 +02:00
*
* @ param string $src_file Source full path filename ( $_FILES [ 'field' ][ 'tmp_name' ])
* @ param string $dest_file Target full path filename ( $_FILES [ 'field' ][ 'name' ])
* @ param int $allowoverwrite 1 = Overwrite target file if it already exists
* @ param int $disablevirusscan 1 = Disable virus scan
2015-04-06 12:25:30 +02:00
* @ param integer $uploaderrorcode Value of PHP upload error code ( $_FILES [ 'field' ][ 'error' ])
2012-09-12 12:27:11 +02:00
* @ param int $nohook Disable all hooks
2012-09-07 17:23:16 +02:00
* @ param string $varfiles _FILES var name
2020-06-12 14:35:50 +02:00
* @ param string $upload_dir For information . Already included into $dest_file .
2020-05-27 13:12:18 +02:00
* @ return int | string 1 if OK , 2 if OK and . noexe appended , < 0 or string if KO
2019-03-11 01:01:15 +01:00
* @ see dol_move ()
2012-09-07 17:23:16 +02:00
*/
2020-06-12 14:35:50 +02:00
function dol_move_uploaded_file ( $src_file , $dest_file , $allowoverwrite , $disablevirusscan = 0 , $uploaderrorcode = 0 , $nohook = 0 , $varfiles = 'addedfile' , $upload_dir = '' )
2012-09-07 17:23:16 +02:00
{
2012-09-12 12:27:11 +02:00
global $conf , $db , $user , $langs ;
global $object , $hookmanager ;
2012-09-07 17:23:16 +02:00
2019-11-13 19:37:08 +01:00
$reshook = 0 ;
2012-09-07 17:23:16 +02:00
$file_name = $dest_file ;
2020-05-27 13:12:18 +02:00
$successcode = 1 ;
2012-09-07 17:23:16 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $nohook )) {
2019-11-13 19:37:08 +01:00
$reshook = $hookmanager -> initHooks ( array ( 'fileslib' ));
2010-05-01 16:28:48 +02:00
2019-11-13 19:37:08 +01:00
$parameters = array ( 'dest_file' => $dest_file , 'src_file' => $src_file , 'file_name' => $file_name , 'varfiles' => $varfiles , 'allowoverwrite' => $allowoverwrite );
$reshook = $hookmanager -> executeHooks ( 'moveUploadedFile' , $parameters , $object );
2010-05-01 16:28:48 +02:00
}
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook )) {
2017-10-07 13:09:31 +02:00
// If an upload error has been reported
2021-02-23 22:03:23 +01:00
if ( $uploaderrorcode ) {
switch ( $uploaderrorcode ) {
2017-10-07 13:09:31 +02:00
case UPLOAD_ERR_INI_SIZE : // 1
return 'ErrorFileSizeTooLarge' ;
case UPLOAD_ERR_FORM_SIZE : // 2
return 'ErrorFileSizeTooLarge' ;
case UPLOAD_ERR_PARTIAL : // 3
return 'ErrorPartialFile' ;
case UPLOAD_ERR_NO_TMP_DIR : //
return 'ErrorNoTmpDir' ;
case UPLOAD_ERR_CANT_WRITE :
return 'ErrorFailedToWriteInDir' ;
case UPLOAD_ERR_EXTENSION :
return 'ErrorUploadBlockedByAddon' ;
default :
break ;
}
}
// If we need to make a virus scan
2021-02-23 22:03:23 +01:00
if ( empty ( $disablevirusscan ) && file_exists ( $src_file )) {
2019-11-13 19:37:08 +01:00
$checkvirusarray = dolCheckVirus ( $src_file );
2021-02-23 22:03:23 +01:00
if ( count ( $checkvirusarray )) {
2020-01-20 14:29:02 +01:00
dol_syslog ( 'Files.lib::dol_move_uploaded_file File "' . $src_file . '" (target name "' . $dest_file . '") KO with antivirus: errors=' . join ( ',' , $checkvirusarray ), LOG_WARNING );
return 'ErrorFileIsInfectedWithAVirus: ' . join ( ',' , $checkvirusarray );
2017-10-07 13:09:31 +02:00
}
}
// Security:
// Disallow file with some extensions. We rename them.
// Because if we put the documents directory into a directory inside web root (very bad), this allows to execute on demand arbitrary code.
2021-02-23 22:03:23 +01:00
if ( isAFileWithExecutableContent ( $dest_file ) && empty ( $conf -> global -> MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED )) {
2020-06-12 14:35:50 +02:00
// $upload_dir ends with a slash, so be must be sure the medias dir to compare to ends with slash too.
$publicmediasdirwithslash = $conf -> medias -> multidir_output [ $conf -> entity ];
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/\/$/' , $publicmediasdirwithslash )) {
$publicmediasdirwithslash .= '/' ;
}
2020-06-12 14:35:50 +02:00
2020-06-12 15:20:28 +02:00
if ( strpos ( $upload_dir , $publicmediasdirwithslash ) !== 0 ) { // We never add .noexe on files into media directory
2020-06-12 14:35:50 +02:00
$file_name .= '.noexe' ;
$successcode = 2 ;
}
2017-10-07 13:09:31 +02:00
}
// Security:
// We refuse cache files/dirs, upload using .. and pipes into filenames.
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^\./' , basename ( $src_file )) || preg_match ( '/\.\./' , $src_file ) || preg_match ( '/[<>|]/' , $src_file )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " Refused to deliver file " . $src_file , LOG_WARNING );
return - 1 ;
}
// Security:
2019-08-16 21:45:13 +02:00
// We refuse cache files/dirs, upload using .. and pipes into filenames.
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^\./' , basename ( $dest_file )) || preg_match ( '/\.\./' , $dest_file ) || preg_match ( '/[<>|]/' , $dest_file )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " Refused to deliver file " . $dest_file , LOG_WARNING );
return - 2 ;
}
2017-05-21 14:06:43 +02:00
}
2017-06-01 12:11:45 +02:00
2020-10-05 05:00:59 +02:00
if ( $reshook < 0 ) { // At least one blocking error returned by one hook
2014-08-28 11:20:13 +02:00
$errmsg = join ( ',' , $hookmanager -> errors );
2021-02-23 22:03:23 +01:00
if ( empty ( $errmsg )) {
$errmsg = 'ErrorReturnedBySomeHooks' ; // Should not occurs. Added if hook is bugged and does not set ->errors when there is error.
}
2014-08-28 11:20:13 +02:00
return $errmsg ;
2020-10-05 05:00:59 +02:00
} elseif ( empty ( $reshook )) {
2012-09-12 12:41:44 +02:00
// The file functions must be in OS filesystem encoding.
2019-11-13 19:37:08 +01:00
$src_file_osencoded = dol_osencode ( $src_file );
$file_name_osencoded = dol_osencode ( $file_name );
2012-09-12 12:41:44 +02:00
// Check if destination dir is writable
2021-02-23 22:03:23 +01:00
if ( ! is_writable ( dirname ( $file_name_osencoded ))) {
2017-11-06 11:47:03 +01:00
dol_syslog ( " Files.lib::dol_move_uploaded_file Dir " . dirname ( $file_name_osencoded ) . " is not writable. Return 'ErrorDirNotWritable' " , LOG_WARNING );
return 'ErrorDirNotWritable' ;
}
2012-09-12 12:41:44 +02:00
// Check if destination file already exists
2021-02-23 22:03:23 +01:00
if ( ! $allowoverwrite ) {
if ( file_exists ( $file_name_osencoded )) {
2012-09-12 12:41:44 +02:00
dol_syslog ( " Files.lib::dol_move_uploaded_file File " . $file_name . " already exists. Return 'ErrorFileAlreadyExists' " , LOG_WARNING );
return 'ErrorFileAlreadyExists' ;
}
2020-05-29 03:20:37 +02:00
} else { // We are allowed to erase
if ( is_dir ( $file_name_osencoded )) { // If there is a directory with name of file to create
dol_syslog ( " Files.lib::dol_move_uploaded_file A directory with name " . $file_name . " already exists. Return 'ErrorDirWithFileNameAlreadyExists' " , LOG_WARNING );
return 'ErrorDirWithFileNameAlreadyExists' ;
}
2012-09-12 12:41:44 +02:00
}
// Move file
2019-11-13 19:37:08 +01:00
$return = move_uploaded_file ( $src_file_osencoded , $file_name_osencoded );
2021-02-23 22:03:23 +01:00
if ( $return ) {
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
@ chmod ( $file_name_osencoded , octdec ( $conf -> global -> MAIN_UMASK ));
}
2012-09-12 12:41:44 +02:00
dol_syslog ( " Files.lib::dol_move_uploaded_file Success to move " . $src_file . " to " . $file_name . " - Umask= " . $conf -> global -> MAIN_UMASK , LOG_DEBUG );
2020-05-27 13:12:18 +02:00
return $successcode ; // Success
2020-05-21 15:05:19 +02:00
} else {
2012-09-12 12:41:44 +02:00
dol_syslog ( " Files.lib::dol_move_uploaded_file Failed to move " . $src_file . " to " . $file_name , LOG_ERR );
2019-11-13 19:37:08 +01:00
return - 3 ; // Unknown error
2011-07-05 18:10:56 +02:00
}
2010-05-01 16:28:48 +02:00
}
2014-08-28 11:20:13 +02:00
2020-05-27 13:12:18 +02:00
return $successcode ; // Success
2010-05-01 16:28:48 +02:00
}
2011-03-09 16:48:25 +01:00
/**
2017-08-18 16:10:21 +02:00
* Remove a file or several files with a mask .
2017-08-21 00:40:32 +02:00
* This delete file physically but also database indexes .
2011-10-14 16:02:18 +02:00
*
2015-05-29 17:11:42 +02:00
* @ param string $file File to delete or mask of files to delete
* @ param int $disableglob Disable usage of glob like * so function is an exact delete function that will return error if no file found
2011-11-12 12:59:06 +01:00
* @ param int $nophperrors Disable all PHP output errors
2012-09-12 12:27:11 +02:00
* @ param int $nohook Disable all hooks
2012-09-11 17:01:54 +02:00
* @ param object $object Current object in use
2018-07-27 12:33:36 +02:00
* @ param boolean $allowdotdot Allow to delete file path with .. inside . Never use this , it is reserved for migration purpose .
2018-09-26 01:45:24 +02:00
* @ param int $indexdatabase Try to remove also index entries .
2021-08-23 11:32:25 +02:00
* @ param int $nolog Disable log file
2015-05-29 17:11:42 +02:00
* @ return boolean True if no error ( file is deleted or if glob is used and there ' s nothing to delete ), False if error
2019-03-11 01:01:15 +01:00
* @ see dol_delete_dir ()
2011-03-09 16:48:25 +01:00
*/
2021-08-23 11:32:25 +02:00
function dol_delete_file ( $file , $disableglob = 0 , $nophperrors = 0 , $nohook = 0 , $object = null , $allowdotdot = false , $indexdatabase = 1 , $nolog = 0 )
2011-03-09 16:48:25 +01:00
{
2012-09-12 12:27:11 +02:00
global $db , $conf , $user , $langs ;
global $hookmanager ;
2012-07-29 12:54:19 +02:00
2018-09-14 10:20:21 +02:00
// Load translation files required by the page
2020-01-20 14:29:02 +01:00
$langs -> loadLangs ( array ( 'other' , 'errors' ));
2012-09-12 12:27:11 +02:00
2021-08-23 11:32:25 +02:00
if ( empty ( $nolog )) {
dol_syslog ( " dol_delete_file file= " . $file . " disableglob= " . $disableglob . " nophperrors= " . $nophperrors . " nohook= " . $nohook );
}
2014-01-19 22:43:36 +01:00
2017-03-10 14:04:06 +01:00
// Security:
2017-03-10 14:08:53 +01:00
// We refuse transversal using .. and pipes into filenames.
2021-02-23 22:03:23 +01:00
if (( ! $allowdotdot && preg_match ( '/\.\./' , $file )) || preg_match ( '/[<>|]/' , $file )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " Refused to delete file " . $file , LOG_WARNING );
2018-04-22 20:57:43 +02:00
return false ;
2017-03-10 14:04:06 +01:00
}
2017-06-01 12:11:45 +02:00
2020-09-10 17:49:05 +02:00
$reshook = 0 ;
2021-02-23 22:03:23 +01:00
if ( empty ( $nohook )) {
2012-09-12 12:41:44 +02:00
$hookmanager -> initHooks ( array ( 'fileslib' ));
2019-11-13 19:37:08 +01:00
$parameters = array (
2020-01-20 14:29:02 +01:00
'GET' => $_GET ,
'file' => $file ,
'disableglob' => $disableglob ,
'nophperrors' => $nophperrors
2012-09-12 12:41:44 +02:00
);
2019-11-13 19:37:08 +01:00
$reshook = $hookmanager -> executeHooks ( 'deleteFile' , $parameters , $object );
2012-09-11 17:01:54 +02:00
}
2021-02-23 22:03:23 +01:00
if ( empty ( $nohook ) && $reshook != 0 ) { // reshook = 0 to do standard actions, 1 = ok and replace, -1 = ko
2020-09-10 17:08:51 +02:00
dol_syslog ( " reshook= " . $reshook );
2021-02-23 22:03:23 +01:00
if ( $reshook < 0 ) {
return false ;
}
2015-05-29 17:11:42 +02:00
return true ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$file_osencoded = dol_osencode ( $file ); // New filename encoded in OS filesystem encoding charset
2021-02-23 22:03:23 +01:00
if ( empty ( $disableglob ) && ! empty ( $file_osencoded )) {
2019-11-13 19:37:08 +01:00
$ok = true ;
$globencoded = str_replace ( '[' , '\[' , $file_osencoded );
$globencoded = str_replace ( ']' , '\]' , $globencoded );
$listofdir = glob ( $globencoded );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $listofdir ) && is_array ( $listofdir )) {
foreach ( $listofdir as $filename ) {
if ( $nophperrors ) {
$ok = @ unlink ( $filename );
} else {
$ok = unlink ( $filename );
}
2021-02-15 23:43:56 +01:00
// If it fails and it is because of the missing write permission on parent dir
if ( ! $ok && file_exists ( dirname ( $filename )) && ! ( fileperms ( dirname ( $filename )) & 0200 )) {
dol_syslog ( " Error in deletion, but parent directory exists with no permission to write, we try to change permission on parent directory and retry... " , LOG_DEBUG );
@ chmod ( dirname ( $filename ), fileperms ( dirname ( $filename )) | 0200 );
// Now we retry deletion
2021-02-23 22:03:23 +01:00
if ( $nophperrors ) {
$ok = @ unlink ( $filename );
} else {
$ok = unlink ( $filename );
}
2021-02-15 23:43:56 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $ok ) {
2021-08-23 11:58:14 +02:00
if ( empty ( $nolog )) {
dol_syslog ( " Removed file " . $filename , LOG_DEBUG );
}
2017-10-07 13:09:31 +02:00
// Delete entry into ecm database
2019-01-27 11:55:16 +01:00
$rel_filetodelete = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $filename );
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/(\/temp\/|\/thumbs\/|\.meta$)/' , $rel_filetodelete )) { // If not a tmp file
if ( is_object ( $db ) && $indexdatabase ) { // $db may not be defined when lib is in a context with define('NOREQUIREDB',1)
2020-05-27 13:12:18 +02:00
$rel_filetodelete = preg_replace ( '/^[\\/]/' , '' , $rel_filetodelete );
$rel_filetodelete = preg_replace ( '/\.noexe$/' , '' , $rel_filetodelete );
2018-06-05 18:05:33 +02:00
dol_syslog ( " Try to remove also entries in database for full relative path = " . $rel_filetodelete , LOG_DEBUG );
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
2019-11-13 19:37:08 +01:00
$ecmfile = new EcmFiles ( $db );
2018-06-05 18:05:33 +02:00
$result = $ecmfile -> fetch ( 0 , '' , $rel_filetodelete );
2021-02-23 22:03:23 +01:00
if ( $result >= 0 && $ecmfile -> id > 0 ) {
2018-06-05 18:05:33 +02:00
$result = $ecmfile -> delete ( $user );
}
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2018-06-05 18:05:33 +02:00
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
}
2017-10-07 13:09:31 +02:00
}
}
2020-05-21 15:05:19 +02:00
} else {
2019-09-03 22:39:24 +02:00
dol_syslog ( " Failed to remove file " . $filename , LOG_WARNING );
// TODO Failure to remove can be because file was already removed or because of permission
// If error because it does not exists, we should return true, and we should return false if this is a permission problem
}
2013-09-04 16:49:20 +02:00
}
2020-09-10 15:49:13 +02:00
} else {
dol_syslog ( " No files to delete found " , LOG_DEBUG );
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$ok = false ;
2021-02-23 22:03:23 +01:00
if ( $nophperrors ) {
$ok = @ unlink ( $file_osencoded );
} else {
$ok = unlink ( $file_osencoded );
}
if ( $ok ) {
2021-08-23 11:58:14 +02:00
if ( empty ( $nolog )) {
dol_syslog ( " Removed file " . $file_osencoded , LOG_DEBUG );
}
2021-02-23 22:03:23 +01:00
} else {
dol_syslog ( " Failed to remove file " . $file_osencoded , LOG_WARNING );
}
2012-09-12 12:41:44 +02:00
}
2012-09-12 12:27:11 +02:00
return $ok ;
}
2011-03-09 16:48:25 +01:00
}
/**
* Remove a directory ( not recursive , so content must be empty ) .
* If directory is not empty , return false
2011-10-14 16:02:18 +02:00
*
2011-11-12 12:59:06 +01:00
* @ param string $dir Directory to delete
* @ param int $nophperrors Disable all PHP output errors
* @ return boolean True if success , false if error
2019-03-11 01:01:15 +01:00
* @ see dol_delete_file () dolCopyDir ()
2011-03-09 16:48:25 +01:00
*/
2019-01-27 15:20:16 +01:00
function dol_delete_dir ( $dir , $nophperrors = 0 )
2011-03-09 16:48:25 +01:00
{
2017-03-10 14:08:53 +01:00
// Security:
// We refuse transversal using .. and pipes into filenames.
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/\.\./' , $dir ) || preg_match ( '/[<>|]/' , $dir )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " Refused to delete dir " . $dir , LOG_WARNING );
2018-04-22 20:57:43 +02:00
return false ;
2017-03-10 14:08:53 +01:00
}
2017-06-01 12:11:45 +02:00
2019-11-13 19:37:08 +01:00
$dir_osencoded = dol_osencode ( $dir );
return ( $nophperrors ? @ rmdir ( $dir_osencoded ) : rmdir ( $dir_osencoded ));
2011-03-09 16:48:25 +01:00
}
/**
2014-08-28 11:20:13 +02:00
* Remove a directory $dir and its subdirectories ( or only files and subdirectories )
2012-02-04 14:39:47 +01:00
*
* @ param string $dir Dir to delete
2017-07-13 00:18:58 +02:00
* @ param int $count Counter to count nb of elements found to delete
2012-02-04 14:39:47 +01:00
* @ param int $nophperrors Disable all PHP output errors
2014-08-28 11:20:13 +02:00
* @ param int $onlysub Delete only files and subdir , not main directory
2017-07-13 00:18:58 +02:00
* @ param int $countdeleted Counter to count nb of elements found really deleted
2021-08-23 11:32:25 +02:00
* @ param int $indexdatabase Try to remove also index entries .
* @ param int $nolog Disable log files ( too verbose when making recursive directories )
2018-09-08 14:10:29 +02:00
* @ return int Number of files and directory we try to remove . NB really removed is returned into var by reference $countdeleted .
2011-03-09 16:48:25 +01:00
*/
2021-08-23 11:32:25 +02:00
function dol_delete_dir_recursive ( $dir , $count = 0 , $nophperrors = 0 , $onlysub = 0 , & $countdeleted = 0 , $indexdatabase = 1 , $nolog = 0 )
2011-03-09 16:48:25 +01:00
{
2021-08-23 11:32:25 +02:00
if ( empty ( $nolog )) {
dol_syslog ( " functions.lib:dol_delete_dir_recursive " . $dir , LOG_DEBUG );
}
2021-02-23 22:03:23 +01:00
if ( dol_is_dir ( $dir )) {
2019-11-13 19:37:08 +01:00
$dir_osencoded = dol_osencode ( $dir );
2021-02-23 22:03:23 +01:00
if ( $handle = opendir ( " $dir_osencoded " )) {
while ( false !== ( $item = readdir ( $handle ))) {
if ( ! utf8_check ( $item )) {
$item = utf8_encode ( $item ); // should be useless
}
if ( $item != " . " && $item != " .. " ) {
if ( is_dir ( dol_osencode ( " $dir / $item " )) && ! is_link ( dol_osencode ( " $dir / $item " ))) {
2021-08-23 11:32:25 +02:00
$count = dol_delete_dir_recursive ( " $dir / $item " , $count , $nophperrors , 0 , $countdeleted , $indexdatabase , $nolog );
2020-05-21 15:05:19 +02:00
} else {
2021-08-23 11:32:25 +02:00
$result = dol_delete_file ( " $dir / $item " , 1 , $nophperrors , 0 , null , false , $indexdatabase , $nolog );
2017-10-07 13:09:31 +02:00
$count ++ ;
2021-02-23 22:03:23 +01:00
if ( $result ) {
$countdeleted ++ ;
}
2018-03-21 13:30:30 +01:00
//else print 'Error on '.$item."\n";
2017-10-07 13:09:31 +02:00
}
}
}
closedir ( $handle );
2011-03-09 16:48:25 +01:00
2021-08-23 11:32:25 +02:00
// Delete also the main directory
2021-02-23 22:03:23 +01:00
if ( empty ( $onlysub )) {
2019-11-13 19:37:08 +01:00
$result = dol_delete_dir ( $dir , $nophperrors );
2017-10-07 13:09:31 +02:00
$count ++ ;
2021-02-23 22:03:23 +01:00
if ( $result ) {
$countdeleted ++ ;
}
2018-03-21 13:30:30 +01:00
//else print 'Error on '.$dir."\n";
2017-10-07 13:09:31 +02:00
}
}
}
return $count ;
2011-03-09 16:48:25 +01:00
}
2011-10-14 16:02:18 +02:00
2011-09-17 02:04:44 +02:00
/**
2018-01-13 15:48:30 +01:00
* Delete all preview files linked to object instance .
* Note that preview image of PDF files is generated when required , by dol_banner_tab () for example .
2011-10-14 16:02:18 +02:00
*
2013-11-07 21:23:29 +01:00
* @ param object $object Object to clean
2011-09-17 02:04:44 +02:00
* @ return int 0 if error , 1 if OK
2019-03-11 01:01:15 +01:00
* @ see dol_convert_file ()
2011-09-17 02:04:44 +02:00
*/
function dol_delete_preview ( $object )
{
2019-11-13 19:37:08 +01:00
global $langs , $conf ;
2012-04-28 18:21:51 +02:00
2012-09-19 13:30:59 +02:00
// Define parent dir of elements
$element = $object -> element ;
2011-09-17 02:04:44 +02:00
2021-02-23 22:03:23 +01:00
if ( $object -> element == 'order_supplier' ) {
$dir = $conf -> fournisseur -> commande -> dir_output ;
} elseif ( $object -> element == 'invoice_supplier' ) {
$dir = $conf -> fournisseur -> facture -> dir_output ;
} elseif ( $object -> element == 'project' ) {
$dir = $conf -> projet -> dir_output ;
} elseif ( $object -> element == 'shipping' ) {
$dir = $conf -> expedition -> dir_output . '/sending' ;
} elseif ( $object -> element == 'delivery' ) {
$dir = $conf -> expedition -> dir_output . '/receipt' ;
} elseif ( $object -> element == 'fichinter' ) {
$dir = $conf -> ficheinter -> dir_output ;
} else {
$dir = empty ( $conf -> $element -> dir_output ) ? '' : $conf -> $element -> dir_output ;
}
2012-03-22 08:33:44 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $dir )) {
return 'ErrorObjectNoSupportedByFunction' ;
}
2011-10-14 16:02:18 +02:00
2011-09-17 02:04:44 +02:00
$refsan = dol_sanitizeFileName ( $object -> ref );
2019-11-13 19:37:08 +01:00
$dir = $dir . " / " . $refsan ;
$filepreviewnew = $dir . " / " . $refsan . " .pdf_preview.png " ;
$filepreviewnewbis = $dir . " / " . $refsan . " .pdf_preview-0.png " ;
$filepreviewold = $dir . " / " . $refsan . " .pdf.png " ;
2011-09-17 02:04:44 +02:00
2018-01-13 15:48:30 +01:00
// For new preview files
2021-02-23 22:03:23 +01:00
if ( file_exists ( $filepreviewnew ) && is_writable ( $filepreviewnew )) {
if ( ! dol_delete_file ( $filepreviewnew , 1 )) {
2019-11-13 19:37:08 +01:00
$object -> error = $langs -> trans ( " ErrorFailedToDeleteFile " , $filepreviewnew );
2018-01-13 15:48:30 +01:00
return 0 ;
}
}
2021-02-23 22:03:23 +01:00
if ( file_exists ( $filepreviewnewbis ) && is_writable ( $filepreviewnewbis )) {
if ( ! dol_delete_file ( $filepreviewnewbis , 1 )) {
2019-11-13 19:37:08 +01:00
$object -> error = $langs -> trans ( " ErrorFailedToDeleteFile " , $filepreviewnewbis );
2018-01-13 15:48:30 +01:00
return 0 ;
}
}
// For old preview files
2021-02-23 22:03:23 +01:00
if ( file_exists ( $filepreviewold ) && is_writable ( $filepreviewold )) {
if ( ! dol_delete_file ( $filepreviewold , 1 )) {
2019-11-13 19:37:08 +01:00
$object -> error = $langs -> trans ( " ErrorFailedToDeleteFile " , $filepreviewold );
2011-09-17 02:04:44 +02:00
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:37:08 +01:00
$multiple = $filepreviewold . " . " ;
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < 20 ; $i ++ ) {
2011-09-17 02:04:44 +02:00
$preview = $multiple . $i ;
2021-02-23 22:03:23 +01:00
if ( file_exists ( $preview ) && is_writable ( $preview )) {
if ( ! dol_delete_file ( $preview , 1 )) {
2019-11-13 19:37:08 +01:00
$object -> error = $langs -> trans ( " ErrorFailedToOpenFile " , $preview );
2011-09-17 02:04:44 +02:00
return 0 ;
}
}
}
}
return 1 ;
}
2011-11-04 00:27:30 +01:00
/**
2011-11-04 08:21:47 +01:00
* Create a meta file with document file into same directory .
2017-08-18 23:16:40 +02:00
* This make " grep " search possible .
* This feature to generate the meta file is enabled only if option MAIN_DOC_CREATE_METAFILE is set .
2011-11-04 08:21:47 +01:00
*
2015-04-06 12:25:30 +02:00
* @ param CommonObject $object Object
2017-08-18 23:16:40 +02:00
* @ return int 0 if do nothing , > 0 if we update meta file too , < 0 if KO
2011-11-04 08:21:47 +01:00
*/
2011-11-04 00:27:30 +01:00
function dol_meta_create ( $object )
{
2012-09-07 10:30:02 +02:00
global $conf ;
2011-12-17 01:16:33 +01:00
2017-08-21 00:40:32 +02:00
// Create meta file
2021-02-23 22:03:23 +01:00
if ( empty ( $conf -> global -> MAIN_DOC_CREATE_METAFILE )) {
return 0 ; // By default, no metafile.
}
2011-12-17 01:16:33 +01:00
2012-09-07 10:30:02 +02:00
// Define parent dir of elements
2019-11-13 19:37:08 +01:00
$element = $object -> element ;
2012-09-19 13:30:59 +02:00
2021-02-23 22:03:23 +01:00
if ( $object -> element == 'order_supplier' ) {
$dir = $conf -> fournisseur -> dir_output . '/commande' ;
} elseif ( $object -> element == 'invoice_supplier' ) {
$dir = $conf -> fournisseur -> dir_output . '/facture' ;
} elseif ( $object -> element == 'project' ) {
$dir = $conf -> projet -> dir_output ;
} elseif ( $object -> element == 'shipping' ) {
$dir = $conf -> expedition -> dir_output . '/sending' ;
} elseif ( $object -> element == 'delivery' ) {
$dir = $conf -> expedition -> dir_output . '/receipt' ;
} elseif ( $object -> element == 'fichinter' ) {
$dir = $conf -> ficheinter -> dir_output ;
} else {
$dir = empty ( $conf -> $element -> dir_output ) ? '' : $conf -> $element -> dir_output ;
}
2012-09-07 10:30:02 +02:00
2021-02-23 22:03:23 +01:00
if ( $dir ) {
2012-09-07 10:30:02 +02:00
$object -> fetch_thirdparty ();
2017-08-18 23:16:40 +02:00
$objectref = dol_sanitizeFileName ( $object -> ref );
2019-11-13 19:37:08 +01:00
$dir = $dir . " / " . $objectref ;
$file = $dir . " / " . $objectref . " .meta " ;
2011-12-17 01:16:33 +01:00
2021-02-23 22:03:23 +01:00
if ( ! is_dir ( $dir )) {
2012-02-19 18:34:22 +01:00
dol_mkdir ( $dir );
2011-11-04 08:29:10 +01:00
}
2011-12-17 01:16:33 +01:00
2021-02-23 22:03:23 +01:00
if ( is_dir ( $dir )) {
2019-06-29 16:29:32 +02:00
$nblines = count ( $object -> lines );
2020-01-30 01:48:28 +01:00
$client = $object -> thirdparty -> name . " " . $object -> thirdparty -> address . " " . $object -> thirdparty -> zip . " " . $object -> thirdparty -> town ;
$meta = " REFERENCE= \" " . $object -> ref . " \"
DATE = \ " " . dol_print_date ( $object -> date , '' ) . " \"
NB_ITEMS = \ " " . $nblines . " \"
CLIENT = \ " " . $client . " \"
AMOUNT_EXCL_TAX = \ " " . $object -> total_ht . " \"
AMOUNT = \ " " . $object -> total_ttc . " \" \n " ;
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $nblines ; $i ++ ) {
2011-11-04 08:29:10 +01:00
//Pour les articles
2020-01-30 01:48:28 +01:00
$meta .= " ITEM_ " . $i . " _QUANTITY= \" " . $object -> lines [ $i ] -> qty . " \"
ITEM_ " . $i . " _AMOUNT_WO_TAX = \ " " . $object -> lines [ $i ] -> total_ht . " \"
ITEM_ " . $i . " _VAT = \ " " . $object -> lines [ $i ] -> tva_tx . " \"
ITEM_ " . $i . " _DESCRIPTION = \ " " . str_replace ( " \r \n " , " " , nl2br ( $object -> lines [ $i ] -> desc )) . " \"
2011-11-04 08:29:10 +01:00
" ;
}
}
2011-12-17 01:16:33 +01:00
2019-01-27 11:55:16 +01:00
$fp = fopen ( $file , " w " );
fputs ( $fp , $meta );
2011-11-04 08:29:10 +01:00
fclose ( $fp );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
2020-01-20 14:29:02 +01:00
@ chmod ( $file , octdec ( $conf -> global -> MAIN_UMASK ));
2021-02-23 22:03:23 +01:00
}
2012-09-07 10:30:02 +02:00
2020-01-20 14:29:02 +01:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-08-18 23:16:40 +02:00
dol_syslog ( 'FailedToDetectDirInDolMetaCreateFor' . $object -> element , LOG_WARNING );
}
2012-09-07 10:30:02 +02:00
return 0 ;
2011-11-04 00:27:30 +01:00
}
2010-05-01 16:28:48 +02:00
2011-11-21 13:17:14 +01:00
/**
2016-06-24 16:10:52 +02:00
* Scan a directory and init $_SESSION to manage uploaded files with list of all found files .
* Note : Only email module seems to use this . Other feature initialize the $_SESSION doing $formmail -> clear_attached_files (); $formmail -> add_attached_files ()
2011-11-21 13:17:14 +01:00
*
* @ param string $pathtoscan Path to scan
2016-06-24 16:10:52 +02:00
* @ param string $trackid Track id ( used to prefix name of session vars to avoid conflict )
2011-11-21 13:17:14 +01:00
* @ return void
*/
2019-01-27 15:20:16 +01:00
function dol_init_file_process ( $pathtoscan = '' , $trackid = '' )
2011-11-21 13:17:14 +01:00
{
2019-11-13 19:37:08 +01:00
$listofpaths = array ();
$listofnames = array ();
$listofmimes = array ();
2011-11-21 13:17:14 +01:00
2021-02-23 22:03:23 +01:00
if ( $pathtoscan ) {
2019-11-13 19:37:08 +01:00
$listoffiles = dol_dir_list ( $pathtoscan , 'files' );
2021-02-23 22:03:23 +01:00
foreach ( $listoffiles as $key => $val ) {
2019-11-13 19:37:08 +01:00
$listofpaths [] = $val [ 'fullname' ];
$listofnames [] = $val [ 'name' ];
$listofmimes [] = dol_mimetype ( $val [ 'name' ]);
2011-11-21 13:17:14 +01:00
}
}
2019-11-13 19:37:08 +01:00
$keytoavoidconflict = empty ( $trackid ) ? '' : '-' . $trackid ;
$_SESSION [ " listofpaths " . $keytoavoidconflict ] = join ( ';' , $listofpaths );
$_SESSION [ " listofnames " . $keytoavoidconflict ] = join ( ';' , $listofnames );
$_SESSION [ " listofmimes " . $keytoavoidconflict ] = join ( ';' , $listofmimes );
2011-11-21 13:17:14 +01:00
}
2010-05-12 13:29:21 +02:00
/**
2017-08-18 23:16:40 +02:00
* Get and save an upload file ( for example after submitting a new file a mail form ) . Database index of file is also updated if donotupdatesession is set .
2010-05-12 13:29:21 +02:00
* All information used are in db , conf , langs , user and _FILES .
2012-07-29 14:15:29 +02:00
* Note : This function can be used only into a HTML page context .
2011-10-14 16:02:18 +02:00
*
2016-12-20 19:42:25 +01:00
* @ param string $upload_dir Directory where to store uploaded file ( note : used to forge $destpath = $upload_dir + filename )
2011-09-24 04:01:26 +02:00
* @ param int $allowoverwrite 1 = Allow overwrite existing file
2018-08-16 21:48:16 +02:00
* @ param int $donotupdatesession 1 = Do no edit _SESSION variable but update database index . 0 = Update _SESSION and not database index . - 1 = Do not update SESSION neither db .
2012-07-29 16:44:19 +02:00
* @ param string $varfiles _FILES var name
2013-10-29 17:50:39 +01:00
* @ param string $savingdocmask Mask to use to define output filename . For example 'XXXXX-__YYYYMMDD__-__file__'
2016-12-08 21:58:52 +01:00
* @ param string $link Link to add ( to add a link instead of a file )
2016-06-24 16:10:52 +02:00
* @ param string $trackid Track id ( used to prefix name of session vars to avoid conflict )
2017-08-01 18:32:21 +02:00
* @ param int $generatethumbs 1 = Generate also thumbs for uploaded image files
2020-10-14 16:59:05 +02:00
* @ param Object $object Object used to set 'src_object_*' fields
2017-02-22 20:35:42 +01:00
* @ return int <= 0 if KO , > 0 if OK
2021-02-04 17:13:00 +01:00
* @ see dol_remove_file_process ()
2010-05-12 13:29:21 +02:00
*/
2020-10-14 16:59:05 +02:00
function dol_add_file_process ( $upload_dir , $allowoverwrite = 0 , $donotupdatesession = 0 , $varfiles = 'addedfile' , $savingdocmask = '' , $link = null , $trackid = '' , $generatethumbs = 1 , $object = null )
2010-05-12 13:29:21 +02:00
{
2019-11-13 19:37:08 +01:00
global $db , $user , $conf , $langs ;
2010-05-12 13:29:21 +02:00
2017-02-22 20:35:42 +01:00
$res = 0 ;
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $_FILES [ $varfiles ])) { // For view $_FILES[$varfiles]['error']
2013-10-29 17:50:39 +01:00
dol_syslog ( 'dol_add_file_process upload_dir=' . $upload_dir . ' allowoverwrite=' . $allowoverwrite . ' donotupdatesession=' . $donotupdatesession . ' savingdocmask=' . $savingdocmask , LOG_DEBUG );
2020-06-17 14:45:15 +02:00
2021-04-04 17:05:01 +02:00
$result = dol_mkdir ( $upload_dir );
// var_dump($result);exit;
if ( $result >= 0 ) {
2015-12-10 13:48:37 +01:00
$TFile = $_FILES [ $varfiles ];
2021-02-23 22:03:23 +01:00
if ( ! is_array ( $TFile [ 'name' ])) {
foreach ( $TFile as $key => & $val ) {
2015-12-10 13:48:37 +01:00
$val = array ( $val );
2010-05-12 13:29:21 +02:00
}
}
2017-06-01 12:11:45 +02:00
2015-12-10 13:48:37 +01:00
$nbfile = count ( $TFile [ 'name' ]);
2017-11-05 01:06:34 +01:00
$nbok = 0 ;
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $nbfile ; $i ++ ) {
if ( empty ( $TFile [ 'name' ][ $i ])) {
continue ; // For example, when submitting a form with no file name
}
2020-10-05 05:00:59 +02:00
2016-12-31 03:43:22 +01:00
// Define $destfull (path to file including filename) and $destfile (only filename)
2020-01-30 01:48:28 +01:00
$destfull = $upload_dir . " / " . $TFile [ 'name' ][ $i ];
$destfile = $TFile [ 'name' ][ $i ];
2020-03-31 13:52:02 +02:00
$destfilewithoutext = preg_replace ( '/\.[^\.]+$/' , '' , $destfile );
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( $savingdocmask && strpos ( $savingdocmask , $destfilewithoutext ) !== 0 ) {
2020-01-30 01:48:28 +01:00
$destfull = $upload_dir . " / " . preg_replace ( '/__file__/' , $TFile [ 'name' ][ $i ], $savingdocmask );
$destfile = preg_replace ( '/__file__/' , $TFile [ 'name' ][ $i ], $savingdocmask );
2010-05-12 13:29:21 +02:00
}
2016-04-02 15:00:58 +02:00
2020-06-17 14:45:15 +02:00
$filenameto = basename ( $destfile );
if ( preg_match ( '/^\./' , $filenameto )) {
$langs -> load ( " errors " ); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
setEventMessages ( $langs -> trans ( " ErrorFilenameCantStartWithDot " , $filenameto ), null , 'errors' );
break ;
}
2017-10-03 09:35:45 +02:00
// dol_sanitizeFileName the file name and lowercase extension
2016-12-31 03:43:22 +01:00
$info = pathinfo ( $destfull );
2020-01-30 01:48:28 +01:00
$destfull = $info [ 'dirname' ] . '/' . dol_sanitizeFileName ( $info [ 'filename' ] . ( $info [ 'extension' ] != '' ? ( '.' . strtolower ( $info [ 'extension' ])) : '' ));
2016-04-02 15:54:51 +02:00
$info = pathinfo ( $destfile );
2019-05-13 12:22:14 +02:00
2020-01-30 01:48:28 +01:00
$destfile = dol_sanitizeFileName ( $info [ 'filename' ] . ( $info [ 'extension' ] != '' ? ( '.' . strtolower ( $info [ 'extension' ])) : '' ));
2016-12-08 21:58:52 +01:00
2019-05-13 12:00:13 +02:00
// We apply dol_string_nohtmltag also to clean file names (this remove duplicate spaces) because
2020-10-31 12:11:41 +01:00
// this function is also applied when we rename and when we make try to download file (by the GETPOST(filename, 'alphanohtml') call).
2019-05-07 14:33:17 +02:00
$destfile = dol_string_nohtmltag ( $destfile );
$destfull = dol_string_nohtmltag ( $destfull );
2019-05-13 12:22:14 +02:00
2020-05-27 13:12:18 +02:00
// Move file from temp directory to final directory. A .noexe may also be appended on file name.
2020-06-12 14:35:50 +02:00
$resupload = dol_move_uploaded_file ( $TFile [ 'tmp_name' ][ $i ], $destfull , $allowoverwrite , 0 , $TFile [ 'error' ][ $i ], 0 , $varfiles , $upload_dir );
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( is_numeric ( $resupload ) && $resupload > 0 ) { // $resupload can be 'ErrorFileAlreadyExists'
2016-01-23 11:51:56 +01:00
global $maxwidthsmall , $maxheightsmall , $maxwidthmini , $maxheightmini ;
2017-06-01 12:11:45 +02:00
2015-12-10 13:48:37 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ;
2017-06-01 12:11:45 +02:00
2016-12-20 19:42:25 +01:00
// Generate thumbs.
2021-02-23 22:03:23 +01:00
if ( $generatethumbs ) {
if ( image_format_supported ( $destfull ) == 1 ) {
2017-10-07 13:09:31 +02:00
// Create thumbs
// We can't use $object->addThumbs here because there is no $object known
// Used on logon for example
$imgThumbSmall = vignette ( $destfull , $maxwidthsmall , $maxheightsmall , '_small' , 50 , " thumbs " );
// Create mini thumbs for image (Ratio is near 16/9)
// Used on menu or for setup page for example
$imgThumbMini = vignette ( $destfull , $maxwidthmini , $maxheightmini , '_mini' , 50 , " thumbs " );
2017-08-01 18:32:21 +02:00
}
2016-12-20 19:42:25 +01:00
}
2017-06-01 12:11:45 +02:00
2016-12-20 19:42:25 +01:00
// Update session
2021-02-23 22:03:23 +01:00
if ( empty ( $donotupdatesession )) {
2015-12-10 13:48:37 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
$formmail = new FormMail ( $db );
2016-06-24 16:10:52 +02:00
$formmail -> trackid = $trackid ;
2016-12-31 03:43:22 +01:00
$formmail -> add_attached_files ( $destfull , $destfile , $TFile [ 'type' ][ $i ]);
2015-12-10 13:48:37 +01:00
}
2017-06-01 12:11:45 +02:00
2020-04-17 13:10:18 +02:00
// Update index table of files (llx_ecm_files)
2021-02-23 22:03:23 +01:00
if ( $donotupdatesession == 1 ) {
2020-10-14 16:59:05 +02:00
$result = addFileIntoDatabaseIndex ( $upload_dir , basename ( $destfile ) . ( $resupload == 2 ? '.noexe' : '' ), $TFile [ 'name' ][ $i ], 'uploaded' , 0 , $object );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2020-04-17 13:10:18 +02:00
if ( $allowoverwrite ) {
// Do not show error message. We can have an error due to DB_ERROR_RECORD_ALREADY_EXISTS
2020-05-21 15:05:19 +02:00
} else {
2020-05-27 14:24:03 +02:00
setEventMessages ( 'WarningFailedToAddFileIntoDatabaseIndex' , '' , 'warnings' );
2020-04-17 13:10:18 +02:00
}
2017-10-07 13:09:31 +02:00
}
2016-12-08 21:58:52 +01:00
}
2016-12-20 19:42:25 +01:00
2017-11-05 01:06:34 +01:00
$nbok ++ ;
2020-05-21 15:05:19 +02:00
} else {
2015-12-10 13:48:37 +01:00
$langs -> load ( " errors " );
2021-02-23 22:03:23 +01:00
if ( $resupload < 0 ) { // Unknown error
2015-12-10 13:48:37 +01:00
setEventMessages ( $langs -> trans ( " ErrorFileNotUploaded " ), null , 'errors' );
2021-02-23 22:03:23 +01:00
} elseif ( preg_match ( '/ErrorFileIsInfectedWithAVirus/' , $resupload )) { // Files infected by a virus
2015-12-10 13:48:37 +01:00
setEventMessages ( $langs -> trans ( " ErrorFileIsInfectedWithAVirus " ), null , 'errors' );
2020-05-21 15:05:19 +02:00
} else // Known error
2015-12-10 13:48:37 +01:00
{
setEventMessages ( $langs -> trans ( $resupload ), null , 'errors' );
}
2010-05-12 13:29:21 +02:00
}
}
2021-02-23 22:03:23 +01:00
if ( $nbok > 0 ) {
2017-11-05 01:06:34 +01:00
$res = 1 ;
setEventMessages ( $langs -> trans ( " FileTransferComplete " ), null , 'mesgs' );
}
2021-04-04 17:05:01 +02:00
} else {
setEventMessages ( $langs -> trans ( " ErrorFailedToCreateDir " , $upload_dir ), null , 'errors' );
2010-05-12 13:29:21 +02:00
}
2013-07-29 16:47:47 +02:00
} elseif ( $link ) {
2019-11-13 19:37:08 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php' ;
2017-01-20 10:27:37 +01:00
$linkObject = new Link ( $db );
$linkObject -> entity = $conf -> entity ;
$linkObject -> url = $link ;
$linkObject -> objecttype = GETPOST ( 'objecttype' , 'alpha' );
$linkObject -> objectid = GETPOST ( 'objectid' , 'int' );
$linkObject -> label = GETPOST ( 'label' , 'alpha' );
$res = $linkObject -> create ( $user );
$langs -> load ( 'link' );
if ( $res > 0 ) {
setEventMessages ( $langs -> trans ( " LinkComplete " ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " ErrorFileNotLinked " ), null , 'errors' );
2013-07-29 16:47:47 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2010-05-12 13:29:21 +02:00
$langs -> load ( " errors " );
2015-10-17 16:18:33 +02:00
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " File " )), null , 'errors' );
2010-05-12 13:29:21 +02:00
}
2017-06-01 12:11:45 +02:00
2017-02-22 20:35:42 +01:00
return $res ;
2010-05-12 13:29:21 +02:00
}
/**
* Remove an uploaded file ( for example after submitting a new file a mail form ) .
* All information used are in db , conf , langs , user and _FILES .
2011-12-17 01:16:33 +01:00
*
2011-11-12 12:59:06 +01:00
* @ param int $filenb File nb to delete
2018-08-16 21:48:16 +02:00
* @ param int $donotupdatesession - 1 or 1 = Do not update _SESSION variable
2011-11-12 12:59:06 +01:00
* @ param int $donotdeletefile 1 = Do not delete physically file
2016-06-24 16:10:52 +02:00
* @ param string $trackid Track id ( used to prefix name of session vars to avoid conflict )
2012-07-29 12:54:19 +02:00
* @ return void
2021-02-04 17:13:00 +01:00
* @ see dol_add_file_process ()
2010-05-12 13:29:21 +02:00
*/
2019-01-27 15:20:16 +01:00
function dol_remove_file_process ( $filenb , $donotupdatesession = 0 , $donotdeletefile = 1 , $trackid = '' )
2010-05-12 13:29:21 +02:00
{
2019-11-13 19:37:08 +01:00
global $db , $user , $conf , $langs , $_FILES ;
2010-05-12 13:29:21 +02:00
2019-11-13 19:37:08 +01:00
$keytodelete = $filenb ;
2010-05-12 13:29:21 +02:00
$keytodelete -- ;
2019-11-13 19:37:08 +01:00
$listofpaths = array ();
$listofnames = array ();
$listofmimes = array ();
$keytoavoidconflict = empty ( $trackid ) ? '' : '-' . $trackid ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $_SESSION [ " listofpaths " . $keytoavoidconflict ])) {
$listofpaths = explode ( ';' , $_SESSION [ " listofpaths " . $keytoavoidconflict ]);
}
if ( ! empty ( $_SESSION [ " listofnames " . $keytoavoidconflict ])) {
$listofnames = explode ( ';' , $_SESSION [ " listofnames " . $keytoavoidconflict ]);
}
if ( ! empty ( $_SESSION [ " listofmimes " . $keytoavoidconflict ])) {
$listofmimes = explode ( ';' , $_SESSION [ " listofmimes " . $keytoavoidconflict ]);
}
2010-05-12 13:29:21 +02:00
2021-02-23 22:03:23 +01:00
if ( $keytodelete >= 0 ) {
2019-11-13 19:37:08 +01:00
$pathtodelete = $listofpaths [ $keytodelete ];
$filetodelete = $listofnames [ $keytodelete ];
2021-02-23 22:03:23 +01:00
if ( empty ( $donotdeletefile )) {
$result = dol_delete_file ( $pathtodelete , 1 ); // The delete of ecm database is inside the function dol_delete_file
} else {
$result = 0 ;
}
if ( $result >= 0 ) {
if ( empty ( $donotdeletefile )) {
2012-07-29 15:47:24 +02:00
$langs -> load ( " other " );
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( " FileWasRemoved " , $filetodelete ), null , 'mesgs' );
2012-07-29 15:47:24 +02:00
}
2021-02-23 22:03:23 +01:00
if ( empty ( $donotupdatesession )) {
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
2010-05-12 13:29:21 +02:00
$formmail = new FormMail ( $db );
2016-06-24 16:10:52 +02:00
$formmail -> trackid = $trackid ;
2010-05-12 13:29:21 +02:00
$formmail -> remove_attached_files ( $keytodelete );
}
}
}
}
2018-02-23 19:55:15 +01:00
/**
* Add a file into database index .
* Called by dol_add_file_process when uploading a file and on other cases .
* See also commonGenerateDocument that also add / update database index when a file is generated .
*
* @ param string $dir Directory name ( full real path without ending / )
2020-05-27 13:12:18 +02:00
* @ param string $file File name ( May end with '.noexe' )
2018-02-23 19:55:15 +01:00
* @ param string $fullpathorig Full path of origin for file ( can be '' )
* @ param string $mode How file was created ( 'uploaded' , 'generated' , ... )
* @ param int $setsharekey Set also the share key
2020-10-14 16:59:05 +02:00
* @ param Object $object Object used to set 'src_object_*' fields
2018-02-23 19:55:15 +01:00
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK
*/
2020-10-14 16:59:05 +02:00
function addFileIntoDatabaseIndex ( $dir , $file , $fullpathorig = '' , $mode = 'uploaded' , $setsharekey = 0 , $object = null )
2018-02-23 19:55:15 +01:00
{
2021-05-07 22:39:14 +02:00
global $db , $user , $conf ;
2018-02-23 19:55:15 +01:00
$result = 0 ;
2019-01-27 11:55:16 +01:00
$rel_dir = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $dir );
2018-02-23 19:55:15 +01:00
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/' , $rel_dir )) { // If not a tmp dir
2020-05-27 13:12:18 +02:00
$filename = basename ( preg_replace ( '/\.noexe$/' , '' , $file ));
2018-02-23 19:55:15 +01:00
$rel_dir = preg_replace ( '/[\\/]$/' , '' , $rel_dir );
$rel_dir = preg_replace ( '/^[\\/]/' , '' , $rel_dir );
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
2019-11-13 19:37:08 +01:00
$ecmfile = new EcmFiles ( $db );
2018-02-23 19:55:15 +01:00
$ecmfile -> filepath = $rel_dir ;
$ecmfile -> filename = $filename ;
2019-11-13 19:37:08 +01:00
$ecmfile -> label = md5_file ( dol_osencode ( $dir . '/' . $file )); // MD5 of file content
2018-02-23 19:55:15 +01:00
$ecmfile -> fullpath_orig = $fullpathorig ;
$ecmfile -> gen_or_uploaded = $mode ;
2019-11-13 19:37:08 +01:00
$ecmfile -> description = '' ; // indexed content
2021-06-30 09:34:49 +02:00
$ecmfile -> keywords = '' ; // keyword content
2020-10-14 16:59:05 +02:00
2020-10-31 14:32:18 +01:00
if ( is_object ( $object ) && $object -> id > 0 ) {
$ecmfile -> src_object_id = $object -> id ;
2021-07-09 09:16:27 +02:00
if ( isset ( $object -> table_element )) {
$ecmfile -> src_object_type = $object -> table_element ;
} else {
dol_syslog ( 'Error: object ' . get_class ( $object ) . ' has no table_element attribute.' );
return - 1 ;
}
2021-07-11 20:16:47 +02:00
if ( isset ( $object -> src_object_description )) $ecmfile -> description = $object -> src_object_description ;
2021-06-30 09:34:49 +02:00
if ( isset ( $object -> src_object_keywords )) $ecmfile -> keywords = $object -> src_object_keywords ;
2020-10-31 14:32:18 +01:00
}
2020-10-14 16:59:05 +02:00
2021-08-17 20:10:47 +02:00
if ( ! empty ( $conf -> global -> MAIN_FORCE_SHARING_ON_ANY_UPLOADED_FILE )) {
2021-05-07 22:39:14 +02:00
$setsharekey = 1 ;
}
2021-08-17 20:11:12 +02:00
2021-02-23 22:03:23 +01:00
if ( $setsharekey ) {
2018-02-23 19:55:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
$ecmfile -> share = getRandomPassword ( true );
}
$result = $ecmfile -> create ( $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2018-02-23 19:55:15 +01:00
dol_syslog ( $ecmfile -> error );
}
}
return $result ;
}
/**
* Delete files into database index using search criterias .
*
* @ param string $dir Directory name ( full real path without ending / )
* @ param string $file File name
* @ param string $mode How file was created ( 'uploaded' , 'generated' , ... )
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK
*/
2019-01-27 15:20:16 +01:00
function deleteFilesIntoDatabaseIndex ( $dir , $file , $mode = 'uploaded' )
2018-02-23 19:55:15 +01:00
{
global $conf , $db , $user ;
$error = 0 ;
2021-02-23 22:03:23 +01:00
if ( empty ( $dir )) {
2018-02-23 19:55:15 +01:00
dol_syslog ( " deleteFilesIntoDatabaseIndex: dir parameter can't be empty " , LOG_ERR );
return - 1 ;
}
$db -> begin ();
2019-01-27 11:55:16 +01:00
$rel_dir = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $dir );
2018-02-23 19:55:15 +01:00
$filename = basename ( $file );
$rel_dir = preg_replace ( '/[\\/]$/' , '' , $rel_dir );
$rel_dir = preg_replace ( '/^[\\/]/' , '' , $rel_dir );
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2019-11-13 19:37:08 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'ecm_files' ;
$sql .= ' WHERE entity = ' . $conf -> entity ;
$sql .= " AND filepath = ' " . $db -> escape ( $rel_dir ) . " ' " ;
2021-02-23 22:03:23 +01:00
if ( $file ) {
$sql .= " AND filename = ' " . $db -> escape ( $file ) . " ' " ;
}
if ( $mode ) {
$sql .= " AND gen_or_uploaded = ' " . $db -> escape ( $mode ) . " ' " ;
}
2018-02-23 19:55:15 +01:00
$resql = $db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2018-02-23 19:55:15 +01:00
$error ++ ;
2019-11-13 19:37:08 +01:00
dol_syslog ( __METHOD__ . ' ' . $db -> lasterror (), LOG_ERR );
2018-02-23 19:55:15 +01:00
}
}
// Commit or rollback
if ( $error ) {
$db -> rollback ();
2019-11-13 19:37:08 +01:00
return - 1 * $error ;
2018-02-23 19:55:15 +01:00
} else {
$db -> commit ();
return 1 ;
}
}
2011-03-04 14:54:47 +01:00
/**
2021-04-04 01:12:25 +02:00
* Convert an image file or a PDF into another image format .
* This need Imagick php extension . You can use dol_imageResizeOrCrop () for a function that need GD .
2011-10-14 16:02:18 +02:00
*
2014-04-30 13:50:25 +02:00
* @ param string $fileinput Input file name
* @ param string $ext Format of target file ( It is also extension added to file if fileoutput is not provided ) .
* @ param string $fileoutput Output filename
2019-03-24 14:53:26 +01:00
* @ param string $page Page number if we convert a PDF into png
2017-08-29 13:41:44 +02:00
* @ return int < 0 if KO , 0 = Nothing done , > 0 if OK
2021-04-04 01:12:25 +02:00
* @ see dol_imageResizeOrCrop ()
2011-03-04 14:54:47 +01:00
*/
2019-03-24 16:11:12 +01:00
function dol_convert_file ( $fileinput , $ext = 'png' , $fileoutput = '' , $page = '' )
2011-03-04 14:54:47 +01:00
{
global $langs ;
2021-02-23 22:03:23 +01:00
if ( class_exists ( 'Imagick' )) {
2020-01-20 14:29:02 +01:00
$image = new Imagick ();
2017-09-18 16:06:58 +02:00
try {
2020-01-20 14:29:02 +01:00
$filetoconvert = $fileinput . (( $page != '' ) ? '[' . $page . ']' : '' );
//var_dump($filetoconvert);
$ret = $image -> readImage ( $filetoconvert );
2019-11-13 19:37:08 +01:00
} catch ( Exception $e ) {
2020-01-20 14:29:02 +01:00
$ext = pathinfo ( $fileinput , PATHINFO_EXTENSION );
dol_syslog ( " Failed to read image using Imagick (Try to install package 'apt-get install php-imagick ghostscript' and check there is no policy to disable " . $ext . " convertion in /etc/ImageMagick*/policy.xml): " . $e -> getMessage (), LOG_WARNING );
2017-09-18 16:06:58 +02:00
return 0 ;
}
2021-02-23 22:03:23 +01:00
if ( $ret ) {
2020-01-20 14:29:02 +01:00
$ret = $image -> setImageFormat ( $ext );
2021-02-23 22:03:23 +01:00
if ( $ret ) {
if ( empty ( $fileoutput )) {
$fileoutput = $fileinput . " . " . $ext ;
}
2014-04-30 13:50:25 +02:00
2017-08-29 13:41:44 +02:00
$count = $image -> getNumberImages ();
2019-09-03 22:39:24 +02:00
2021-02-23 22:03:23 +01:00
if ( ! dol_is_file ( $fileoutput ) || is_writeable ( $fileoutput )) {
2020-01-20 14:29:02 +01:00
try {
2019-10-31 21:16:41 +01:00
$ret = $image -> writeImages ( $fileoutput , true );
2021-02-23 22:03:23 +01:00
} catch ( Exception $e ) {
2020-01-20 14:29:02 +01:00
dol_syslog ( $e -> getMessage (), LOG_WARNING );
}
2020-05-21 15:05:19 +02:00
} else {
2018-02-02 10:33:41 +01:00
dol_syslog ( " Warning: Failed to write cache preview file '. $fileoutput .'. Check permission on file/dir " , LOG_ERR );
}
2021-02-23 22:03:23 +01:00
if ( $ret ) {
return $count ;
} else {
return - 3 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-08-29 13:41:44 +02:00
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-08-29 13:41:44 +02:00
return - 1 ;
2011-03-04 14:54:47 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-08-29 13:41:44 +02:00
return 0 ;
2011-03-04 14:54:47 +01:00
}
}
2011-10-14 16:02:18 +02:00
/**
2019-09-04 19:55:15 +02:00
* Compress a file .
* An error string may be returned into parameters .
2011-10-14 16:02:18 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $inputfile Source file name
* @ param string $outputfile Target file name
2012-04-28 18:21:51 +02:00
* @ param string $mode 'gz' or 'bz' or 'zip'
2019-09-04 19:55:15 +02:00
* @ param string $errorstring Error string
2012-02-04 14:39:47 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-10-14 16:02:18 +02:00
*/
2019-09-04 19:55:15 +02:00
function dol_compress_file ( $inputfile , $outputfile , $mode = " gz " , & $errorstring = null )
2011-10-14 16:02:18 +02:00
{
2019-08-14 03:43:15 +02:00
global $conf ;
2020-01-30 01:48:28 +01:00
$foundhandler = 0 ;
2011-10-14 16:02:18 +02:00
2020-05-21 01:41:27 +02:00
try {
2019-08-14 01:43:06 +02:00
dol_syslog ( " dol_compress_file mode= " . $mode . " inputfile= " . $inputfile . " outputfile= " . $outputfile );
2017-10-07 13:09:31 +02:00
$data = implode ( " " , file ( dol_osencode ( $inputfile )));
2021-02-23 22:03:23 +01:00
if ( $mode == 'gz' ) {
2021-03-01 20:37:16 +01:00
$foundhandler = 1 ;
$compressdata = gzencode ( $data , 9 );
2021-02-23 22:03:23 +01:00
} elseif ( $mode == 'bz' ) {
2021-03-01 20:37:16 +01:00
$foundhandler = 1 ;
$compressdata = bzcompress ( $data , 9 );
2021-10-17 11:59:25 +02:00
} elseif ( $mode == 'zstd' ) {
$foundhandler = 1 ;
$compressdata = zstd_compress ( $data , 9 );
2021-02-23 22:03:23 +01:00
} elseif ( $mode == 'zip' ) {
if ( class_exists ( 'ZipArchive' ) && ! empty ( $conf -> global -> MAIN_USE_ZIPARCHIVE_FOR_ZIP_COMPRESS )) {
2020-01-30 01:48:28 +01:00
$foundhandler = 1 ;
2019-08-14 03:43:15 +02:00
$rootPath = realpath ( $inputfile );
dol_syslog ( " Class ZipArchive is set so we zip using ZipArchive to zip into " . $outputfile . ' rootPath=' . $rootPath );
$zip = new ZipArchive ;
2019-08-16 17:35:12 +02:00
if ( $zip -> open ( $outputfile , ZipArchive :: CREATE ) !== true ) {
2020-01-30 01:48:28 +01:00
$errorstring = " dol_compress_file failure - Failed to open file " . $outputfile . " \n " ;
2019-09-04 19:55:15 +02:00
dol_syslog ( $errorstring , LOG_ERR );
global $errormsg ;
$errormsg = $errorstring ;
2019-08-14 03:43:15 +02:00
return - 6 ;
}
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator (
new RecursiveDirectoryIterator ( $rootPath ),
RecursiveIteratorIterator :: LEAVES_ONLY
2021-02-23 22:03:23 +01:00
);
2019-08-14 03:43:15 +02:00
2021-02-23 22:03:23 +01:00
foreach ( $files as $name => $file ) {
2019-08-14 03:43:15 +02:00
// Skip directories (they would be added automatically)
2021-02-23 22:03:23 +01:00
if ( ! $file -> isDir ()) {
2019-08-14 03:43:15 +02:00
// Get real and relative path for current file
2021-11-28 19:25:58 +01:00
$filePath = $file -> getPath (); // the full path with filename using the $inputdir root.
$fileName = $file -> getFilename ();
$fileFullRealPath = $file -> getRealPath (); // the full path with name and transformed to use real path directory.
//$relativePath = substr($fileFullRealPath, strlen($rootPath) + 1);
$relativePath = substr (( $filePath ? $filePath . '/' : '' ) . $fileName , strlen ( $rootPath ) + 1 );
2019-08-14 03:43:15 +02:00
// Add current file to archive
2021-11-28 19:25:58 +01:00
$zip -> addFile ( $fileFullRealPath , $relativePath );
2019-08-14 03:43:15 +02:00
}
}
// Zip archive will be created only after closing object
$zip -> close ();
dol_syslog ( " dol_compress_file success - " . count ( $zip -> numFiles ) . " files " );
return 1 ;
}
2021-02-23 22:03:23 +01:00
if ( defined ( 'ODTPHP_PATHTOPCLZIP' )) {
2020-01-30 01:48:28 +01:00
$foundhandler = 1 ;
2017-10-07 13:09:31 +02:00
include_once ODTPHP_PATHTOPCLZIP . '/pclzip.lib.php' ;
$archive = new PclZip ( $outputfile );
2019-08-14 01:43:06 +02:00
$result = $archive -> add ( $inputfile , PCLZIP_OPT_REMOVE_PATH , dirname ( $inputfile ));
2021-02-23 22:03:23 +01:00
if ( $result === 0 ) {
2019-08-14 01:43:06 +02:00
global $errormsg ;
2020-01-30 01:48:28 +01:00
$errormsg = $archive -> errorInfo ( true );
2019-09-04 19:55:15 +02:00
2021-02-23 22:03:23 +01:00
if ( $archive -> errorCode () == PCLZIP_ERR_WRITE_OPEN_FAIL ) {
2019-09-04 19:55:15 +02:00
$errorstring = " PCLZIP_ERR_WRITE_OPEN_FAIL " ;
dol_syslog ( " dol_compress_file error - archive->errorCode() = PCLZIP_ERR_WRITE_OPEN_FAIL " , LOG_ERR );
2019-08-14 01:43:06 +02:00
return - 4 ;
}
2019-09-04 19:55:15 +02:00
$errorstring = " dol_compress_file error archive->errorCode = " . $archive -> errorCode () . " errormsg= " . $errormsg ;
dol_syslog ( " dol_compress_file failure - " . $errormsg , LOG_ERR );
2019-08-14 01:43:06 +02:00
return - 3 ;
2020-05-21 15:05:19 +02:00
} else {
2019-08-14 01:43:06 +02:00
dol_syslog ( " dol_compress_file success - " . count ( $result ) . " files " );
return 1 ;
}
2017-10-07 13:09:31 +02:00
}
}
2021-02-23 22:03:23 +01:00
if ( $foundhandler ) {
2017-10-07 13:09:31 +02:00
$fp = fopen ( $outputfile , " w " );
fwrite ( $fp , $compressdata );
fclose ( $fp );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-09-04 19:55:15 +02:00
$errorstring = " Try to zip with format " . $mode . " with no handler for this format " ;
dol_syslog ( $errorstring , LOG_ERR );
global $errormsg ;
$errormsg = $errorstring ;
2017-10-07 13:09:31 +02:00
return - 2 ;
}
2021-02-23 22:03:23 +01:00
} catch ( Exception $e ) {
2017-10-07 13:09:31 +02:00
global $langs , $errormsg ;
$langs -> load ( " errors " );
2020-01-30 01:48:28 +01:00
$errormsg = $langs -> trans ( " ErrorFailedToWriteInDir " );
2019-09-04 19:55:15 +02:00
$errorstring = " Failed to open file " . $outputfile ;
dol_syslog ( $errorstring , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2011-10-14 16:02:18 +02:00
}
2012-04-28 18:21:51 +02:00
/**
* Uncompress a file
*
* @ param string $inputfile File to uncompress
* @ param string $outputdir Target dir name
* @ return array array ( 'error' => 'Error code' ) or array () if no error
*/
2019-01-27 15:20:16 +01:00
function dol_uncompress ( $inputfile , $outputdir )
2012-04-28 18:21:51 +02:00
{
2022-02-23 12:28:37 +01:00
global $conf , $langs , $db ;
2017-10-07 13:09:31 +02:00
2022-02-23 12:28:37 +01:00
$fileinfo = pathinfo ( $inputfile );
2022-04-05 15:18:52 +02:00
$fileinfo [ " extension " ] = strtolower ( $fileinfo [ " extension " ]);
2020-09-17 11:09:43 +02:00
2022-02-23 12:28:37 +01:00
if ( $fileinfo [ " extension " ] == " zip " ) {
if ( defined ( 'ODTPHP_PATHTOPCLZIP' ) && empty ( $conf -> global -> MAIN_USE_ZIPARCHIVE_FOR_ZIP_UNCOMPRESS )) {
dol_syslog ( " Constant ODTPHP_PATHTOPCLZIP for pclzip library is set to " . ODTPHP_PATHTOPCLZIP . " , so we use Pclzip to unzip into " . $outputdir );
include_once ODTPHP_PATHTOPCLZIP . '/pclzip.lib.php' ;
$archive = new PclZip ( $inputfile );
2020-09-17 11:09:43 +02:00
2022-04-05 17:19:12 +02:00
// We create output dir manually, so it uses the correct permission (When created by the archive->extract, dir is rwx for everybody).
dol_mkdir ( dol_sanitizePathName ( $outputdir ));
2022-02-23 12:28:37 +01:00
// Extract into outputdir, but only files that match the regex '/^((?!\.\.).)*$/' that means "does not include .."
$result = $archive -> extract ( PCLZIP_OPT_PATH , $outputdir , PCLZIP_OPT_BY_PREG , '/^((?!\.\.).)*$/' );
if ( ! is_array ( $result ) && $result <= 0 ) {
return array ( 'error' => $archive -> errorInfo ( true ));
} else {
$ok = 1 ;
$errmsg = '' ;
// Loop on each file to check result for unzipping file
foreach ( $result as $key => $val ) {
if ( $val [ 'status' ] == 'path_creation_fail' ) {
$langs -> load ( " errors " );
$ok = 0 ;
$errmsg = $langs -> trans ( " ErrorFailToCreateDir " , $val [ 'filename' ]);
break ;
}
}
2020-09-17 11:09:43 +02:00
2022-02-23 12:28:37 +01:00
if ( $ok ) {
return array ();
} else {
return array ( 'error' => $errmsg );
2015-04-03 18:42:07 +02:00
}
}
2022-02-23 12:28:37 +01:00
}
2015-04-03 18:42:07 +02:00
2022-02-23 12:28:37 +01:00
if ( class_exists ( 'ZipArchive' )) { // Must install php-zip to have it
dol_syslog ( " Class ZipArchive is set so we unzip using ZipArchive to unzip into " . $outputdir );
$zip = new ZipArchive ;
$res = $zip -> open ( $inputfile );
if ( $res === true ) {
//$zip->extractTo($outputdir.'/');
// We must extract one file at time so we can check that file name does not contains '..' to avoid transversal path of zip built for example using
// python3 path_traversal_archiver.py <Created_file_name> test.zip -l 10 -p tmp/
// with -l is the range of dot to go back in path.
// and path_traversal_archiver.py found at https://github.com/Alamot/code-snippets/blob/master/path_traversal/path_traversal_archiver.py
for ( $i = 0 ; $i < $zip -> numFiles ; $i ++ ) {
if ( preg_match ( '/\.\./' , $zip -> getNameIndex ( $i ))) {
dol_syslog ( " Warning: Try to unzip a file with a transversal path " . $zip -> getNameIndex ( $i ), LOG_WARNING );
continue ; // Discard the file
}
$zip -> extractTo ( $outputdir . '/' , array ( $zip -> getNameIndex ( $i )));
}
$zip -> close ();
2021-02-23 22:03:23 +01:00
return array ();
} else {
2022-02-23 12:28:37 +01:00
return array ( 'error' => 'ErrUnzipFails' );
2021-02-23 22:03:23 +01:00
}
2015-04-03 18:42:07 +02:00
}
2017-10-07 13:09:31 +02:00
2022-02-23 12:28:37 +01:00
return array ( 'error' => 'ErrNoZipEngine' );
2022-04-05 15:18:52 +02:00
} elseif ( in_array ( $fileinfo [ " extension " ], array ( 'gz' , 'bz2' , 'zst' ))) {
2022-04-05 15:19:55 +02:00
include_once DOL_DOCUMENT_ROOT . " /core/class/utils.class.php " ;
$utils = new Utils ( $db );
2020-09-17 11:09:43 +02:00
2022-04-05 17:19:12 +02:00
dol_mkdir ( dol_sanitizePathName ( $outputdir ));
$outputfilename = escapeshellcmd ( dol_sanitizePathName ( $outputdir ) . '/' . dol_sanitizeFileName ( $fileinfo [ " filename " ]));
dol_delete_file ( $outputfilename . '.tmp' );
dol_delete_file ( $outputfilename . '.err' );
2022-04-05 15:18:52 +02:00
$extension = strtolower ( pathinfo ( $fileinfo [ " filename " ], PATHINFO_EXTENSION ));
2022-02-23 12:28:37 +01:00
if ( $extension == " tar " ) {
2022-03-14 11:45:23 +01:00
$cmd = 'tar -C ' . escapeshellcmd ( dol_sanitizePathName ( $outputdir )) . ' -xvf ' . escapeshellcmd ( dol_sanitizePathName ( $fileinfo [ " dirname " ]) . '/' . dol_sanitizeFileName ( $fileinfo [ " basename " ]));
2022-04-05 17:19:12 +02:00
$resarray = $utils -> executeCLI ( $cmd , $outputfilename . '.tmp' , 0 , $outputfilename . '.err' , 0 );
if ( $resarray [ " result " ] != 0 ) {
$resarray [ " error " ] .= file_get_contents ( $outputfilename . '.err' );
}
2020-05-21 15:05:19 +02:00
} else {
2022-02-23 12:28:37 +01:00
$program = " " ;
if ( $fileinfo [ " extension " ] == " gz " ) {
2022-03-14 11:45:23 +01:00
$program = 'gzip' ;
2022-02-23 12:28:37 +01:00
} elseif ( $fileinfo [ " extension " ] == " bz2 " ) {
2022-03-14 11:45:23 +01:00
$program = 'bzip2' ;
} elseif ( $fileinfo [ " extension " ] == " zst " ) {
$program = 'zstd' ;
2022-02-23 12:28:37 +01:00
} else {
2022-04-05 17:19:12 +02:00
return array ( 'error' => 'ErrorBadFileExtension' );
2022-02-23 12:28:37 +01:00
}
2022-03-14 11:45:23 +01:00
$cmd = $program . ' -dc ' . escapeshellcmd ( dol_sanitizePathName ( $fileinfo [ " dirname " ]) . '/' . dol_sanitizeFileName ( $fileinfo [ " basename " ]));
2022-04-05 17:19:12 +02:00
$cmd .= ' > ' . $outputfilename ;
$resarray = $utils -> executeCLI ( $cmd , $outputfilename . '.tmp' , 0 , null , 1 , $outputfilename . '.err' );
if ( $resarray [ " result " ] != 0 ) {
$errfilecontent = @ file_get_contents ( $outputfilename . '.err' );
if ( $errfilecontent ) {
$resarray [ " error " ] .= " - " . $errfilecontent ;
}
2022-02-23 12:28:37 +01:00
}
2017-10-07 13:09:31 +02:00
}
2022-04-05 17:19:12 +02:00
return $resarray [ " result " ] != 0 ? array ( 'error' => $resarray [ " error " ]) : array ();
2017-10-07 13:09:31 +02:00
}
2022-04-05 17:19:12 +02:00
return array ( 'error' => 'ErrorBadFileExtension' );
2012-04-28 18:21:51 +02:00
}
2012-03-16 00:34:41 +01:00
2017-05-31 00:20:35 +02:00
/**
* Compress a directory and subdirectories into a package file .
*
* @ param string $inputdir Source dir name
2017-07-17 11:05:38 +02:00
* @ param string $outputfile Target file name ( output directory must exists and be writable )
2017-05-31 00:20:35 +02:00
* @ param string $mode 'zip'
2019-09-03 22:39:24 +02:00
* @ param string $excludefiles A regex pattern . For example : '/\.log$|\/temp\//'
2020-01-15 11:55:27 +01:00
* @ param string $rootdirinzip Add a root dir level in zip file
2017-05-31 00:20:35 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2020-01-15 11:55:27 +01:00
function dol_compress_dir ( $inputdir , $outputfile , $mode = " zip " , $excludefiles = '' , $rootdirinzip = '' )
2017-05-31 00:20:35 +02:00
{
2020-01-30 01:48:28 +01:00
$foundhandler = 0 ;
2017-05-31 00:20:35 +02:00
2019-09-05 13:42:58 +02:00
dol_syslog ( " Try to zip dir " . $inputdir . " into " . $outputfile . " mode= " . $mode );
2017-07-17 11:05:38 +02:00
2021-02-23 22:03:23 +01:00
if ( ! dol_is_dir ( dirname ( $outputfile )) || ! is_writable ( dirname ( $outputfile ))) {
2017-10-07 13:09:31 +02:00
global $langs , $errormsg ;
$langs -> load ( " errors " );
2020-01-30 01:48:28 +01:00
$errormsg = $langs -> trans ( " ErrorFailedToWriteInDir " , $outputfile );
2017-07-17 11:05:38 +02:00
return - 3 ;
2017-10-07 13:09:31 +02:00
}
2020-05-21 01:41:27 +02:00
try {
2021-02-23 22:03:23 +01:00
if ( $mode == 'gz' ) {
$foundhandler = 0 ;
} elseif ( $mode == 'bz' ) {
$foundhandler = 0 ;
} elseif ( $mode == 'zip' ) {
2017-10-07 13:09:31 +02:00
/* if ( defined ( 'ODTPHP_PATHTOPCLZIP' ))
2020-01-20 14:29:02 +01:00
{
$foundhandler = 0 ; // TODO implement this
include_once ODTPHP_PATHTOPCLZIP . '/pclzip.lib.php' ;
$archive = new PclZip ( $outputfile );
$archive -> add ( $inputfile , PCLZIP_OPT_REMOVE_PATH , dirname ( $inputfile ));
//$archive->add($inputfile);
return 1 ;
}
else */
2019-09-03 22:39:24 +02:00
//if (class_exists('ZipArchive') && ! empty($conf->global->MAIN_USE_ZIPARCHIVE_FOR_ZIP_COMPRESS))
2021-02-23 22:03:23 +01:00
if ( class_exists ( 'ZipArchive' )) {
2019-11-13 19:37:08 +01:00
$foundhandler = 1 ;
2017-06-01 12:11:45 +02:00
2017-10-07 13:09:31 +02:00
// Initialize archive object
$zip = new ZipArchive ();
$result = $zip -> open ( $outputfile , ZipArchive :: CREATE | ZipArchive :: OVERWRITE );
2021-02-23 22:03:23 +01:00
if ( ! $result ) {
2019-09-03 22:39:24 +02:00
global $langs , $errormsg ;
$langs -> load ( " errors " );
2020-01-30 01:48:28 +01:00
$errormsg = $langs -> trans ( " ErrorFailedToWriteInFile " , $outputfile );
2019-09-03 22:39:24 +02:00
return - 4 ;
}
2017-10-07 13:09:31 +02:00
// Create recursive directory iterator
2021-11-28 19:25:58 +01:00
// This does not return symbolic links
2017-10-07 13:09:31 +02:00
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator (
new RecursiveDirectoryIterator ( $inputdir ),
RecursiveIteratorIterator :: LEAVES_ONLY
2021-02-23 22:03:23 +01:00
);
2017-10-07 13:09:31 +02:00
2021-11-28 19:25:58 +01:00
//var_dump($inputdir);
2021-02-23 22:03:23 +01:00
foreach ( $files as $name => $file ) {
2017-10-07 13:09:31 +02:00
// Skip directories (they would be added automatically)
2021-02-23 22:03:23 +01:00
if ( ! $file -> isDir ()) {
2017-10-07 13:09:31 +02:00
// Get real and relative path for current file
2021-11-28 19:25:58 +01:00
$filePath = $file -> getPath (); // the full path with filename using the $inputdir root.
$fileName = $file -> getFilename ();
$fileFullRealPath = $file -> getRealPath (); // the full path with name and transformed to use real path directory.
//$relativePath = ($rootdirinzip ? $rootdirinzip.'/' : '').substr($fileFullRealPath, strlen($inputdir) + 1);
$relativePath = ( $rootdirinzip ? $rootdirinzip . '/' : '' ) . substr (( $filePath ? $filePath . '/' : '' ) . $fileName , strlen ( $inputdir ) + 1 );
2020-01-15 11:55:27 +01:00
2021-11-28 19:25:58 +01:00
//var_dump($filePath);var_dump($fileFullRealPath);var_dump($relativePath);
if ( empty ( $excludefiles ) || ! preg_match ( $excludefiles , $fileFullRealPath )) {
2019-09-03 22:39:24 +02:00
// Add current file to archive
2021-11-28 19:25:58 +01:00
$zip -> addFile ( $fileFullRealPath , $relativePath );
2019-09-03 22:39:24 +02:00
}
2017-10-07 13:09:31 +02:00
}
}
// Zip archive will be created only after closing object
$zip -> close ();
return 1 ;
}
}
2021-02-23 22:03:23 +01:00
if ( ! $foundhandler ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( " Try to zip with format " . $mode . " with no handler for this format " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 2 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return 0 ;
}
2021-02-23 22:03:23 +01:00
} catch ( Exception $e ) {
2017-10-07 13:09:31 +02:00
global $langs , $errormsg ;
$langs -> load ( " errors " );
dol_syslog ( " Failed to open file " . $outputfile , LOG_ERR );
dol_syslog ( $e -> getMessage (), LOG_ERR );
2019-11-13 19:37:08 +01:00
$errormsg = $langs -> trans ( " ErrorFailedToWriteInDir " , $outputfile );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2017-05-31 00:20:35 +02:00
}
2012-03-16 00:34:41 +01:00
/**
2013-03-30 14:10:15 +01:00
* Return file ( s ) into a directory ( by default most recent )
2012-03-16 00:34:41 +01:00
*
2014-04-23 15:53:45 +02:00
* @ param string $dir Directory to scan
* @ param string $regexfilter Regex filter to restrict list . This regex value must be escaped for '/' , since this char is used for preg_match function
2017-04-12 11:30:33 +02:00
* @ param array $excludefilter Array of Regex for exclude filter ( example : array ( '(\.meta|_preview.*\.png)$' , '^\.' )) . This regex value must be escaped for '/' , since this char is used for preg_match function
2014-04-23 15:53:45 +02:00
* @ param int $nohook Disable all hooks
2017-06-11 12:26:51 +02:00
* @ param int $mode 0 = Return array minimum keys loaded ( faster ), 1 = Force all keys like date and size to be loaded ( slower ), 2 = Force load of date only , 3 = Force load of size only
2014-04-23 15:53:45 +02:00
* @ return string Full path to most recent file
2012-03-16 00:34:41 +01:00
*/
2019-11-13 19:37:08 +01:00
function dol_most_recent_file ( $dir , $regexfilter = '' , $excludefilter = array ( '(\.meta|_preview.*\.png)$' , '^\.' ), $nohook = false , $mode = '' )
2012-03-16 00:34:41 +01:00
{
2019-11-13 19:37:08 +01:00
$tmparray = dol_dir_list ( $dir , 'files' , 0 , $regexfilter , $excludefilter , 'date' , SORT_DESC , $mode , $nohook );
2017-10-07 13:09:31 +02:00
return $tmparray [ 0 ];
2012-03-16 00:34:41 +01:00
}
2013-03-30 14:10:15 +01:00
2013-04-22 15:00:29 +02:00
/**
2021-05-21 18:53:09 +02:00
* Security check when accessing to a document ( used by document . php , viewimage . php and webservices to get documents ) .
2022-01-13 15:52:41 +01:00
* TODO Replace code that set $accessallowed by a call to restrictedArea ()
2013-04-25 10:06:54 +02:00
*
2021-08-06 12:18:51 +02:00
* @ param string $modulepart Module of document ( 'module' , 'module_user_temp' , 'module_user' or 'module_temp' ) . Exemple : 'medias' , 'invoice' , 'logs' , 'tax-vat' , ...
2016-12-13 00:15:21 +01:00
* @ param string $original_file Relative path with filename , relative to modulepart .
2017-05-20 11:31:03 +02:00
* @ param string $entity Restrict onto entity ( 0 = no restriction )
2013-06-25 23:22:00 +02:00
* @ param User $fuser User object ( forced )
2022-01-13 15:52:41 +01:00
* @ param string $refname Ref of object to check permission for external users ( autodetect if not provided ) or for hierarchy
2017-06-01 12:11:45 +02:00
* @ param string $mode Check permission for 'read' or 'write'
2016-12-13 00:15:21 +01:00
* @ return mixed Array with access information : 'accessallowed' & 'sqlprotectagainstexternals' & 'original_file' ( as a full path name )
2019-03-11 01:01:15 +01:00
* @ see restrictedArea ()
2013-04-22 15:00:29 +02:00
*/
2019-01-27 15:20:16 +01:00
function dol_check_secure_access_document ( $modulepart , $original_file , $entity , $fuser = '' , $refname = '' , $mode = 'read' )
2013-04-22 15:00:29 +02:00
{
2021-08-28 00:53:13 +02:00
global $conf , $db , $user , $hookmanager ;
2017-08-26 15:22:13 +02:00
global $dolibarr_main_data_root , $dolibarr_main_document_root_alt ;
2021-08-28 00:53:13 +02:00
global $object ;
2017-06-01 12:11:45 +02:00
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $fuser )) {
$fuser = $user ;
}
2013-06-25 15:09:51 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $modulepart )) {
return 'ErrorBadParameter' ;
}
if ( empty ( $entity )) {
if ( empty ( $conf -> multicompany -> enabled )) {
$entity = 1 ;
} else {
$entity = 0 ;
}
2017-12-27 15:48:20 +01:00
}
2021-08-06 12:18:51 +02:00
// Fix modulepart for backward compatibility
2021-02-23 22:03:23 +01:00
if ( $modulepart == 'users' ) {
$modulepart = 'user' ;
}
2021-08-06 12:18:51 +02:00
if ( $modulepart == 'tva' ) {
$modulepart = 'tax-vat' ;
}
2018-12-17 15:35:20 +01:00
2021-03-29 13:49:24 +02:00
//print 'dol_check_secure_access_document modulepart='.$modulepart.' original_file='.$original_file.' entity='.$entity;
2020-12-16 02:33:21 +01:00
dol_syslog ( 'dol_check_secure_access_document modulepart=' . $modulepart . ' original_file=' . $original_file . ' entity=' . $entity );
2019-03-11 01:01:15 +01:00
2013-04-25 10:06:54 +02:00
// We define $accessallowed and $sqlprotectagainstexternals
2020-01-30 01:48:28 +01:00
$accessallowed = 0 ;
$sqlprotectagainstexternals = '' ;
$ret = array ();
2013-04-25 10:06:54 +02:00
2021-09-09 19:21:32 +02:00
// Find the subdirectory name as the reference. For example original_file='10/myfile.pdf' -> refname='10'
2021-02-23 22:03:23 +01:00
if ( empty ( $refname )) {
$refname = basename ( dirname ( $original_file ) . " / " );
2021-09-09 19:21:32 +02:00
if ( $refname == 'thumbs' ) {
// If we get the thumbns directory, we must go one step higher. For example original_file='10/thumbs/myfile_small.jpg' -> refname='10'
$refname = basename ( dirname ( dirname ( $original_file )) . " / " );
}
2021-02-23 22:03:23 +01:00
}
2013-06-25 23:22:00 +02:00
2017-05-20 11:31:03 +02:00
// Define possible keys to use for permission check
2021-03-01 20:37:16 +01:00
$lire = 'lire' ;
$read = 'read' ;
$download = 'download' ;
2021-02-23 22:03:23 +01:00
if ( $mode == 'write' ) {
2021-03-01 20:37:16 +01:00
$lire = 'creer' ;
$read = 'write' ;
$download = 'upload' ;
2017-05-20 11:31:03 +02:00
}
2017-06-01 12:11:45 +02:00
2017-06-20 16:54:37 +02:00
// Wrapping for miscellaneous medias files
2021-02-23 22:03:23 +01:00
if ( $modulepart == 'medias' && ! empty ( $dolibarr_main_data_root )) {
if ( empty ( $entity ) || empty ( $conf -> medias -> multidir_output [ $entity ])) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> medias -> multidir_output [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'logs' && ! empty ( $dolibarr_main_data_root )) {
// Wrapping for *.log files, like when used with url http://.../document.php?modulepart=logs&file=dolibarr.log
2020-01-30 01:48:28 +01:00
$accessallowed = ( $user -> admin && basename ( $original_file ) == $original_file && preg_match ( '/^dolibarr.*\.log$/' , basename ( $original_file )));
$original_file = $dolibarr_main_data_root . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'doctemplates' && ! empty ( $dolibarr_main_data_root )) {
2022-02-22 21:24:34 +01:00
// Wrapping for doctemplates
2020-12-16 18:10:40 +01:00
$accessallowed = $user -> admin ;
$original_file = $dolibarr_main_data_root . '/doctemplates/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'doctemplateswebsite' && ! empty ( $dolibarr_main_data_root )) {
2022-02-22 21:24:34 +01:00
// Wrapping for doctemplates of websites
2020-01-30 01:48:28 +01:00
$accessallowed = ( $fuser -> rights -> website -> write && preg_match ( '/\.jpg$/i' , basename ( $original_file )));
$original_file = $dolibarr_main_data_root . '/doctemplates/websites/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'packages' && ! empty ( $dolibarr_main_data_root )) {
2022-02-22 21:24:34 +01:00
// Wrapping for *.zip package files, like when used with url http://.../document.php?modulepart=packages&file=module_myfile.zip
2017-08-26 15:22:13 +02:00
// Dir for custom dirs
2020-01-30 01:48:28 +01:00
$tmp = explode ( ',' , $dolibarr_main_document_root_alt );
2017-08-26 15:22:13 +02:00
$dirins = $tmp [ 0 ];
2020-01-30 01:48:28 +01:00
$accessallowed = ( $user -> admin && preg_match ( '/^module_.*\.zip$/' , basename ( $original_file )));
$original_file = $dirins . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'mycompany' && ! empty ( $conf -> mycompany -> dir_output )) {
// Wrapping for some images
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> mycompany -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'userphoto' && ! empty ( $conf -> user -> dir_output )) {
// Wrapping for users photos
2022-02-22 21:24:34 +01:00
$accessallowed = 0 ;
if ( preg_match ( '/^\d+\/photos\//' , $original_file )) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> user -> dir_output . '/' . $original_file ;
2022-02-25 09:33:08 +01:00
} elseif (( $modulepart == 'companylogo' ) && ! empty ( $conf -> mycompany -> dir_output )) {
// Wrapping for users logos
$accessallowed = 1 ;
$original_file = $conf -> mycompany -> dir_output . '/logos/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'memberphoto' && ! empty ( $conf -> adherent -> dir_output )) {
// Wrapping for members photos
2022-02-22 21:24:34 +01:00
$accessallowed = 0 ;
if ( preg_match ( '/^\d+\/photos\//' , $original_file )) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> adherent -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'apercufacture' && ! empty ( $conf -> facture -> multidir_output [ $entity ])) {
// Wrapping pour les apercu factures
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> facture -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> facture -> multidir_output [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'apercupropal' && ! empty ( $conf -> propal -> multidir_output [ $entity ])) {
// Wrapping pour les apercu propal
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> propale -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> propal -> multidir_output [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'apercucommande' && ! empty ( $conf -> commande -> multidir_output [ $entity ])) {
// Wrapping pour les apercu commande
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> commande -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> commande -> multidir_output [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'apercufichinter' || $modulepart == 'apercuficheinter' ) && ! empty ( $conf -> ficheinter -> dir_output )) {
// Wrapping pour les apercu intervention
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> ficheinter -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> ficheinter -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'apercucontract' ) && ! empty ( $conf -> contrat -> multidir_output [ $entity ])) {
// Wrapping pour les apercu contrat
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> contrat -> { $lire }) {
$accessallowed = 1 ;
}
2021-02-10 13:27:42 +01:00
$original_file = $conf -> contrat -> multidir_output [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'apercusupplier_proposal' || $modulepart == 'apercusupplier_proposal' ) && ! empty ( $conf -> supplier_proposal -> dir_output )) {
// Wrapping pour les apercu supplier proposal
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> supplier_proposal -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> supplier_proposal -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'apercusupplier_order' || $modulepart == 'apercusupplier_order' ) && ! empty ( $conf -> fournisseur -> commande -> dir_output )) {
// Wrapping pour les apercu supplier order
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> fournisseur -> commande -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> commande -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'apercusupplier_invoice' || $modulepart == 'apercusupplier_invoice' ) && ! empty ( $conf -> fournisseur -> facture -> dir_output )) {
// Wrapping pour les apercu supplier invoice
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> fournisseur -> facture -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> facture -> dir_output . '/' . $original_file ;
2022-01-13 15:52:41 +01:00
} elseif (( $modulepart == 'holiday' ) && ! empty ( $conf -> holiday -> dir_output )) {
2022-02-22 16:44:17 +01:00
if ( $fuser -> rights -> holiday -> { $read } || ! empty ( $fuser -> rights -> holiday -> readall ) || preg_match ( '/^specimen/i' , $original_file )) {
2022-01-13 15:52:41 +01:00
$accessallowed = 1 ;
// If we known $id of holiday, call checkUserAccessToObject to check permission on properties and hierarchy of leave request
2022-02-22 16:44:17 +01:00
if ( $refname && empty ( $fuser -> rights -> holiday -> readall ) && ! preg_match ( '/^specimen/i' , $original_file )) {
2022-01-13 15:52:41 +01:00
include_once DOL_DOCUMENT_ROOT . '/holiday/class/holiday.class.php' ;
$tmpholiday = new Holiday ( $db );
$tmpholiday -> fetch ( '' , $refname );
$accessallowed = checkUserAccessToObject ( $user , array ( 'holiday' ), $tmpholiday , 'holiday' , '' , '' , 'rowid' , '' );
}
}
$original_file = $conf -> holiday -> dir_output . '/' . $original_file ;
} elseif (( $modulepart == 'expensereport' ) && ! empty ( $conf -> expensereport -> dir_output )) {
2022-02-22 16:44:17 +01:00
if ( $fuser -> rights -> expensereport -> { $lire } || ! empty ( $fuser -> rights -> expensereport -> readall ) || preg_match ( '/^specimen/i' , $original_file )) {
2022-01-13 15:52:41 +01:00
$accessallowed = 1 ;
// If we known $id of expensereport, call checkUserAccessToObject to check permission on properties and hierarchy of expense report
2022-02-22 16:44:17 +01:00
if ( $refname && empty ( $fuser -> rights -> expensereport -> readall ) && ! preg_match ( '/^specimen/i' , $original_file )) {
2022-01-13 15:52:41 +01:00
include_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php' ;
$tmpexpensereport = new ExpenseReport ( $db );
$tmpexpensereport -> fetch ( '' , $refname );
$accessallowed = checkUserAccessToObject ( $user , array ( 'expensereport' ), $tmpexpensereport , 'expensereport' , '' , '' , 'rowid' , '' );
}
}
$original_file = $conf -> expensereport -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'apercuexpensereport' ) && ! empty ( $conf -> expensereport -> dir_output )) {
2022-02-22 21:24:34 +01:00
// Wrapping pour les apercu expense report
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> expensereport -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> expensereport -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'propalstats' && ! empty ( $conf -> propal -> multidir_temp [ $entity ])) {
// Wrapping pour les images des stats propales
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> propale -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> propal -> multidir_temp [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'orderstats' && ! empty ( $conf -> commande -> dir_temp )) {
// Wrapping pour les images des stats commandes
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> commande -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> commande -> dir_temp . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'orderstatssupplier' && ! empty ( $conf -> fournisseur -> dir_output )) {
if ( $fuser -> rights -> fournisseur -> commande -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> commande -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'billstats' && ! empty ( $conf -> facture -> dir_temp )) {
// Wrapping pour les images des stats factures
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> facture -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> facture -> dir_temp . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'billstatssupplier' && ! empty ( $conf -> fournisseur -> dir_output )) {
if ( $fuser -> rights -> fournisseur -> facture -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> facture -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'expeditionstats' && ! empty ( $conf -> expedition -> dir_temp )) {
// Wrapping pour les images des stats expeditions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> expedition -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> expedition -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'tripsexpensesstats' && ! empty ( $conf -> deplacement -> dir_temp )) {
// Wrapping pour les images des stats expeditions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> deplacement -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> deplacement -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'memberstats' && ! empty ( $conf -> adherent -> dir_temp )) {
// Wrapping pour les images des stats expeditions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> adherent -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> adherent -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( preg_match ( '/^productstats_/i' , $modulepart ) && ! empty ( $conf -> product -> dir_temp )) {
// Wrapping pour les images des stats produits
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> produit -> { $lire } || $fuser -> rights -> service -> { $lire }) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = ( ! empty ( $conf -> product -> multidir_temp [ $entity ]) ? $conf -> product -> multidir_temp [ $entity ] : $conf -> service -> multidir_temp [ $entity ]) . '/' . $original_file ;
2021-08-06 12:18:51 +02:00
} elseif ( in_array ( $modulepart , array ( 'tax' , 'tax-vat' , 'tva' )) && ! empty ( $conf -> tax -> dir_output )) {
2021-03-01 20:37:16 +01:00
// Wrapping for taxes
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> tax -> charges -> { $lire }) {
$accessallowed = 1 ;
}
2020-02-26 20:25:26 +01:00
$modulepartsuffix = str_replace ( 'tax-' , '' , $modulepart );
2020-02-27 18:35:32 +01:00
$original_file = $conf -> tax -> dir_output . '/' . ( $modulepartsuffix != 'tax' ? $modulepartsuffix . '/' : '' ) . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'actions' && ! empty ( $conf -> agenda -> dir_output )) {
// Wrapping for events
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> agenda -> myactions -> { $read }) {
$accessallowed = 1 ;
2021-05-24 20:04:23 +02:00
// If we known $id of project, call checkUserAccessToObject to check permission on the given agenda event on properties and assigned users
if ( $refname && ! preg_match ( '/^specimen/i' , $original_file )) {
include_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php' ;
$tmpobject = new ActionComm ( $db );
$tmpobject -> fetch (( int ) $refname );
$accessallowed = checkUserAccessToObject ( $user , array ( 'agenda' ), $tmpobject -> id , 'actioncomm&societe' , 'myactions|allactions' , 'fk_soc' , 'id' , '' );
if ( $user -> socid && $tmpobject -> socid ) {
$accessallowed = checkUserAccessToObject ( $user , array ( 'societe' ), $tmpobject -> socid );
}
}
2021-02-23 22:03:23 +01:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> agenda -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'category' && ! empty ( $conf -> categorie -> multidir_output [ $entity ])) {
// Wrapping for categories
2021-02-23 22:03:23 +01:00
if ( empty ( $entity ) || empty ( $conf -> categorie -> multidir_output [ $entity ])) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
2021-12-05 13:01:05 +01:00
if ( $fuser -> rights -> categorie -> { $lire } || $fuser -> rights -> takepos -> run ) {
2021-02-23 22:03:23 +01:00
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> categorie -> multidir_output [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'prelevement' && ! empty ( $conf -> prelevement -> dir_output )) {
// Wrapping pour les prelevements
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> prelevement -> bons -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
$accessallowed = 1 ;
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> prelevement -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'graph_stock' && ! empty ( $conf -> stock -> dir_temp )) {
// Wrapping pour les graph energie
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> stock -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'graph_fourn' && ! empty ( $conf -> fournisseur -> dir_temp )) {
// Wrapping pour les graph fournisseurs
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> fournisseur -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'graph_product' && ! empty ( $conf -> product -> dir_temp )) {
// Wrapping pour les graph des produits
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> product -> multidir_temp [ $entity ] . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'barcode' ) {
// Wrapping pour les code barre
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-03-10 13:39:11 +01:00
// If viewimage is called for barcode, we try to output an image on the fly, with no build of file on disk.
2013-06-05 16:24:32 +02:00
//$original_file=$conf->barcode->dir_temp.'/'.$original_file;
2020-01-30 01:48:28 +01:00
$original_file = '' ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'iconmailing' && ! empty ( $conf -> mailing -> dir_temp )) {
// Wrapping pour les icones de background des mailings
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> mailing -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'scanner_user_temp' && ! empty ( $conf -> scanner -> dir_temp )) {
// Wrapping pour le scanner
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> scanner -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'fckeditor' && ! empty ( $conf -> fckeditor -> dir_output )) {
// Wrapping pour les images fckeditor
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
$original_file = $conf -> fckeditor -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'user' && ! empty ( $conf -> user -> dir_output )) {
// Wrapping for users
2020-01-30 01:48:28 +01:00
$canreaduser = ( ! empty ( $fuser -> admin ) || $fuser -> rights -> user -> user -> { $lire });
2021-02-23 22:03:23 +01:00
if ( $fuser -> id == ( int ) $refname ) {
$canreaduser = 1 ;
} // A user can always read its own card
if ( $canreaduser || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> user -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'company' || $modulepart == 'societe' || $modulepart == 'thirdparty' ) && ! empty ( $conf -> societe -> multidir_output [ $entity ])) {
// Wrapping for third parties
2021-02-23 22:03:23 +01:00
if ( empty ( $entity ) || empty ( $conf -> societe -> multidir_output [ $entity ])) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
if ( $fuser -> rights -> societe -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> societe -> multidir_output [ $entity ] . '/' . $original_file ;
2017-05-30 18:50:54 +02:00
$sqlprotectagainstexternals = " SELECT rowid as fk_soc FROM " . MAIN_DB_PREFIX . " societe WHERE rowid=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'contact' && ! empty ( $conf -> societe -> multidir_output [ $entity ])) {
// Wrapping for contact
2021-02-23 22:03:23 +01:00
if ( empty ( $entity ) || empty ( $conf -> societe -> multidir_output [ $entity ])) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
if ( $fuser -> rights -> societe -> { $lire }) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2015-10-04 17:25:46 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> societe -> multidir_output [ $entity ] . '/contact/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'facture' || $modulepart == 'invoice' ) && ! empty ( $conf -> facture -> multidir_output [ $entity ])) {
// Wrapping for invoices
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> facture -> multidir_output [ $entity ] . '/' . $original_file ;
2020-04-27 19:15:17 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " facture WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'invoice' ) . " ) " ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'massfilesarea_proposals' && ! empty ( $conf -> propal -> multidir_output [ $entity ])) {
// Wrapping for mass actions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> propal -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> propal -> multidir_output [ $entity ] . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_orders' ) {
if ( $fuser -> rights -> commande -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> commande -> multidir_output [ $entity ] . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_sendings' ) {
if ( $fuser -> rights -> expedition -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2020-01-20 14:29:02 +01:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> expedition -> dir_output . '/sending/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_invoices' ) {
if ( $fuser -> rights -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> facture -> multidir_output [ $entity ] . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_expensereport' ) {
if ( $fuser -> rights -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> expensereport -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_interventions' ) {
if ( $fuser -> rights -> ficheinter -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> ficheinter -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_supplier_proposal' && ! empty ( $conf -> supplier_proposal -> dir_output )) {
if ( $fuser -> rights -> supplier_proposal -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> supplier_proposal -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_supplier_order' ) {
if ( $fuser -> rights -> fournisseur -> commande -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> commande -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_supplier_invoice' ) {
if ( $fuser -> rights -> fournisseur -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-10-07 13:09:31 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> facture -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'massfilesarea_contract' && ! empty ( $conf -> contrat -> dir_output )) {
if ( $fuser -> rights -> contrat -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2017-09-16 15:39:52 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> contrat -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'fichinter' || $modulepart == 'ficheinter' ) && ! empty ( $conf -> ficheinter -> dir_output )) {
// Wrapping for interventions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> ficheinter -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> ficheinter -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " fichinter WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'deplacement' && ! empty ( $conf -> deplacement -> dir_output )) {
// Wrapping pour les deplacements et notes de frais
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> deplacement -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> deplacement -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
//$sqlprotectagainstexternals = "SELECT fk_soc as fk_soc FROM ".MAIN_DB_PREFIX."fichinter WHERE ref='".$db->escape($refname)."' AND entity=".$conf->entity;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'propal' || $modulepart == 'propale' ) && ! empty ( $conf -> propal -> multidir_output [ $entity ])) {
// Wrapping pour les propales
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> propale -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> propal -> multidir_output [ $entity ] . '/' . $original_file ;
2020-04-27 19:15:17 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " propal WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'propal' ) . " ) " ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'commande' || $modulepart == 'order' ) && ! empty ( $conf -> commande -> multidir_output [ $entity ])) {
// Wrapping pour les commandes
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> commande -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> commande -> multidir_output [ $entity ] . '/' . $original_file ;
2020-04-27 19:15:17 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " commande WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'order' ) . " ) " ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'project' && ! empty ( $conf -> projet -> dir_output )) {
// Wrapping pour les projets
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> projet -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2021-05-21 18:53:09 +02:00
// If we known $id of project, call checkUserAccessToObject to check permission on properties and contact of project
if ( $refname && ! preg_match ( '/^specimen/i' , $original_file )) {
include_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
$tmpproject = new Project ( $db );
$tmpproject -> fetch ( '' , $refname );
$accessallowed = checkUserAccessToObject ( $user , array ( 'projet' ), $tmpproject -> id , 'projet&project' , '' , '' , 'rowid' , '' );
}
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> projet -> dir_output . '/' . $original_file ;
2017-05-30 18:50:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " projet WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'project' ) . " ) " ;
2021-02-23 22:03:23 +01:00
} elseif ( $modulepart == 'project_task' && ! empty ( $conf -> projet -> dir_output )) {
if ( $fuser -> rights -> projet -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2021-05-21 18:53:09 +02:00
// If we known $id of project, call checkUserAccessToObject to check permission on properties and contact of project
if ( $refname && ! preg_match ( '/^specimen/i' , $original_file )) {
include_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php' ;
$tmptask = new Task ( $db );
$tmptask -> fetch ( '' , $refname );
2022-01-10 19:23:46 +01:00
$accessallowed = checkUserAccessToObject ( $user , array ( 'projet_task' ), $tmptask -> id , 'projet_task&project' , '' , '' , 'rowid' , '' );
2021-05-21 18:53:09 +02:00
}
2014-02-08 02:02:01 +01:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> projet -> dir_output . '/' . $original_file ;
2017-05-30 18:50:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " projet WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'project' ) . " ) " ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'commande_fournisseur' || $modulepart == 'order_supplier' ) && ! empty ( $conf -> fournisseur -> commande -> dir_output )) {
// Wrapping pour les commandes fournisseurs
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> fournisseur -> commande -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> fournisseur -> commande -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " commande_fournisseur WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'facture_fournisseur' || $modulepart == 'invoice_supplier' ) && ! empty ( $conf -> fournisseur -> facture -> dir_output )) {
// Wrapping pour les factures fournisseurs
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> fournisseur -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2020-01-30 01:48:28 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2020-01-30 01:48:28 +01:00
$original_file = $conf -> fournisseur -> facture -> dir_output . '/' . $original_file ;
2019-07-04 16:02:04 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " facture_fourn WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'supplier_payment' ) {
// Wrapping pour les rapport de paiements
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> fournisseur -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2017-02-14 12:00:56 +01:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> fournisseur -> payment -> dir_output . '/' . $original_file ;
2017-02-14 12:00:56 +01:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " paiementfournisseur WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'facture_paiement' && ! empty ( $conf -> facture -> dir_output )) {
// Wrapping pour les rapport de paiements
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> facture -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $fuser -> societe_id > 0 ) {
$original_file = $conf -> facture -> dir_output . '/payments/private/' . $fuser -> id . '/' . $original_file ;
} else {
$original_file = $conf -> facture -> dir_output . '/payments/' . $original_file ;
}
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'export_compta' && ! empty ( $conf -> accounting -> dir_output )) {
// Wrapping for accounting exports
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> accounting -> bind -> write || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> accounting -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'expedition' || $modulepart == 'shipment' ) && ! empty ( $conf -> expedition -> dir_output )) {
// Wrapping pour les expedition
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> expedition -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2021-10-11 20:36:10 +02:00
$original_file = $conf -> expedition -> dir_output . " / " . ( strpos ( 'sending/' , $original_file ) === 0 ? '' : 'sending/' ) . $original_file ;
2021-10-11 20:04:45 +02:00
//$original_file = $conf->expedition->dir_output."/".$original_file;
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'livraison' || $modulepart == 'delivery' ) && ! empty ( $conf -> expedition -> dir_output )) {
// Delivery Note Wrapping
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> expedition -> delivery -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2021-10-11 20:36:10 +02:00
$original_file = $conf -> expedition -> dir_output . " / " . ( strpos ( 'receipt/' , $original_file ) === 0 ? '' : 'receipt/' ) . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'actions' && ! empty ( $conf -> agenda -> dir_output )) {
// Wrapping pour les actions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> agenda -> myactions -> { $read } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> agenda -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'actionsreport' && ! empty ( $conf -> agenda -> dir_temp )) {
// Wrapping pour les actions
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> agenda -> allactions -> { $read } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> agenda -> dir_temp . " / " . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'product' || $modulepart == 'produit' || $modulepart == 'service' || $modulepart == 'produit|service' ) {
// Wrapping pour les produits et services
2021-02-23 22:03:23 +01:00
if ( empty ( $entity ) || ( empty ( $conf -> product -> multidir_output [ $entity ]) && empty ( $conf -> service -> multidir_output [ $entity ]))) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
if (( $fuser -> rights -> produit -> { $lire } || $fuser -> rights -> service -> { $lire }) || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> product -> enabled )) {
$original_file = $conf -> product -> multidir_output [ $entity ] . '/' . $original_file ;
} elseif ( ! empty ( $conf -> service -> enabled )) {
$original_file = $conf -> service -> multidir_output [ $entity ] . '/' . $original_file ;
}
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'product_batch' || $modulepart == 'produitlot' ) {
// Wrapping pour les lots produits
2021-02-23 22:03:23 +01:00
if ( empty ( $entity ) || ( empty ( $conf -> productbatch -> multidir_output [ $entity ]))) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
if (( $fuser -> rights -> produit -> { $lire } ) || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2018-04-12 15:10:37 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> productbatch -> enabled )) {
$original_file = $conf -> productbatch -> multidir_output [ $entity ] . '/' . $original_file ;
}
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'movement' || $modulepart == 'mouvement' ) {
// Wrapping for stock movements
2021-02-23 22:03:23 +01:00
if ( empty ( $entity ) || empty ( $conf -> stock -> multidir_output [ $entity ])) {
return array ( 'accessallowed' => 0 , 'error' => 'Value entity must be provided' );
}
if (( $fuser -> rights -> stock -> { $lire } || $fuser -> rights -> stock -> movement -> { $lire } || $fuser -> rights -> stock -> mouvement -> { $lire }) || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2019-01-28 04:36:26 +01:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> stock -> enabled )) {
$original_file = $conf -> stock -> multidir_output [ $entity ] . '/movement/' . $original_file ;
}
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'contract' && ! empty ( $conf -> contrat -> multidir_output [ $entity ])) {
// Wrapping pour les contrats
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> contrat -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2021-02-10 13:02:13 +01:00
$original_file = $conf -> contrat -> multidir_output [ $entity ] . '/' . $original_file ;
2017-05-30 18:50:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " contrat WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'contract' ) . " ) " ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'donation' && ! empty ( $conf -> don -> dir_output )) {
// Wrapping pour les dons
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> don -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> don -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'dolresource' && ! empty ( $conf -> resource -> dir_output )) {
// Wrapping pour les dons
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> resource -> { $read } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2017-05-23 13:44:18 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> resource -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'remisecheque' && ! empty ( $conf -> bank -> dir_output )) {
// Wrapping pour les remises de cheques
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> banque -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
2019-11-13 19:37:08 +01:00
$original_file = $conf -> bank -> dir_output . '/checkdeposits/' . $original_file ; // original_file should contains relative path so include the get_exdir result
2021-03-01 20:37:16 +01:00
} elseif (( $modulepart == 'banque' || $modulepart == 'bank' ) && ! empty ( $conf -> bank -> dir_output )) {
// Wrapping for bank
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> banque -> { $lire }) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2015-10-02 16:29:54 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> bank -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'export' && ! empty ( $conf -> export -> dir_temp )) {
// Wrapping for export module
2021-03-23 17:27:43 +01:00
// Note that a test may not be required because we force the dir of download on the directory of the user that export
2021-03-29 13:49:24 +02:00
$accessallowed = $user -> rights -> export -> lire ;
2019-11-13 19:37:08 +01:00
$original_file = $conf -> export -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'import' && ! empty ( $conf -> import -> dir_temp )) {
// Wrapping for import module
2021-03-23 17:27:43 +01:00
$accessallowed = $user -> rights -> import -> run ;
2019-11-13 19:37:08 +01:00
$original_file = $conf -> import -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'editor' && ! empty ( $conf -> fckeditor -> dir_output )) {
2021-03-23 17:27:43 +01:00
// Wrapping for wysiwyg editor
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
$original_file = $conf -> fckeditor -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'systemtools' && ! empty ( $conf -> admin -> dir_output )) {
// Wrapping for backups
2021-02-23 22:03:23 +01:00
if ( $fuser -> admin ) {
$accessallowed = 1 ;
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> admin -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'admin_temp' && ! empty ( $conf -> admin -> dir_temp )) {
// Wrapping for upload file test
2021-02-23 22:03:23 +01:00
if ( $fuser -> admin ) {
$accessallowed = 1 ;
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> admin -> dir_temp . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'bittorrent' && ! empty ( $conf -> bittorrent -> dir_output )) {
// Wrapping pour BitTorrent
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
$dir = 'files' ;
2021-02-23 22:03:23 +01:00
if ( dol_mimetype ( $original_file ) == 'application/x-bittorrent' ) {
$dir = 'torrents' ;
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> bittorrent -> dir_output . '/' . $dir . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'member' && ! empty ( $conf -> adherent -> dir_output )) {
// Wrapping pour Foundation module
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> adherent -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> adherent -> dir_output . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
} elseif ( $modulepart == 'scanner_user_temp' && ! empty ( $conf -> scanner -> dir_temp )) {
// Wrapping for Scanner
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
$original_file = $conf -> scanner -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2021-03-01 20:37:16 +01:00
// If modulepart=module_user_temp Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/temp/iduser
// If modulepart=module_temp Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/temp
// If modulepart=module_user Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/iduser
// If modulepart=module Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart
// If modulepart=module-abc Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart
} else {
// GENERIC Wrapping
2020-08-23 15:15:47 +02:00
//var_dump($modulepart);
//var_dump($original_file);
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^specimen/i' , $original_file )) {
$accessallowed = 1 ; // If link to a file called specimen. Test must be done before changing $original_file int full path.
}
if ( $fuser -> admin ) {
$accessallowed = 1 ; // If user is admin
}
2021-10-21 16:02:47 +02:00
2020-08-23 15:15:47 +02:00
$tmpmodulepart = explode ( '-' , $modulepart );
2020-09-07 10:18:17 +02:00
if ( ! empty ( $tmpmodulepart [ 1 ])) {
2020-08-23 15:15:47 +02:00
$modulepart = $tmpmodulepart [ 0 ];
$original_file = $tmpmodulepart [ 1 ] . '/' . $original_file ;
}
2017-03-10 13:39:11 +01:00
2013-04-25 10:06:54 +02:00
// Define $accessallowed
2020-02-07 11:53:09 +01:00
$reg = array ();
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^([a-z]+)_user_temp$/i' , $modulepart , $reg )) {
if ( empty ( $conf -> { $reg [ 1 ]} -> dir_temp )) { // modulepart not supported
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , 'Error call dol_check_secure_access_document with not supported value for modulepart parameter (' . $modulepart . ')' );
2017-03-10 12:19:50 +01:00
exit ;
}
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> { $reg [ 1 ]} -> { $lire } || $fuser -> rights -> { $reg [ 1 ]} -> { $read } || ( $fuser -> rights -> { $reg [ 1 ]} -> { $download })) {
$accessallowed = 1 ;
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> { $reg [ 1 ]} -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( preg_match ( '/^([a-z]+)_temp$/i' , $modulepart , $reg )) {
if ( empty ( $conf -> { $reg [ 1 ]} -> dir_temp )) { // modulepart not supported
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , 'Error call dol_check_secure_access_document with not supported value for modulepart parameter (' . $modulepart . ')' );
2017-03-10 12:19:50 +01:00
exit ;
}
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> { $reg [ 1 ]} -> { $lire } || $fuser -> rights -> { $reg [ 1 ]} -> { $read } || ( $fuser -> rights -> { $reg [ 1 ]} -> { $download })) {
$accessallowed = 1 ;
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> { $reg [ 1 ]} -> dir_temp . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( preg_match ( '/^([a-z]+)_user$/i' , $modulepart , $reg )) {
if ( empty ( $conf -> { $reg [ 1 ]} -> dir_output )) { // modulepart not supported
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , 'Error call dol_check_secure_access_document with not supported value for modulepart parameter (' . $modulepart . ')' );
2017-03-10 12:19:50 +01:00
exit ;
}
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> { $reg [ 1 ]} -> { $lire } || $fuser -> rights -> { $reg [ 1 ]} -> { $read } || ( $fuser -> rights -> { $reg [ 1 ]} -> { $download })) {
$accessallowed = 1 ;
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> { $reg [ 1 ]} -> dir_output . '/' . $fuser -> id . '/' . $original_file ;
2021-02-23 22:03:23 +01:00
} elseif ( preg_match ( '/^massfilesarea_([a-z]+)$/i' , $modulepart , $reg )) {
if ( empty ( $conf -> { $reg [ 1 ]} -> dir_output )) { // modulepart not supported
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , 'Error call dol_check_secure_access_document with not supported value for modulepart parameter (' . $modulepart . ')' );
2018-12-01 14:44:34 +01:00
exit ;
}
2021-02-23 22:03:23 +01:00
if ( $fuser -> rights -> { $reg [ 1 ]} -> { $lire } || preg_match ( '/^specimen/i' , $original_file )) {
2019-11-13 19:37:08 +01:00
$accessallowed = 1 ;
2018-12-01 14:44:34 +01:00
}
2019-11-13 19:37:08 +01:00
$original_file = $conf -> { $reg [ 1 ]} -> dir_output . '/temp/massgeneration/' . $user -> id . '/' . $original_file ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( empty ( $conf -> $modulepart -> dir_output )) { // modulepart not supported
2019-08-16 05:14:20 +02:00
dol_print_error ( '' , 'Error call dol_check_secure_access_document with not supported value for modulepart parameter (' . $modulepart . '). The module for this modulepart value may not be activated.' );
2014-02-08 02:02:01 +01:00
exit ;
}
2020-01-20 14:29:02 +01:00
// Check fuser->rights->modulepart->myobject->read and fuser->rights->modulepart->read
$partsofdirinoriginalfile = explode ( '/' , $original_file );
2020-11-01 21:27:56 +01:00
if ( ! empty ( $partsofdirinoriginalfile [ 1 ])) { // If original_file is xxx/filename (xxx is a part we will use)
$partofdirinoriginalfile = $partsofdirinoriginalfile [ 0 ];
2021-02-23 22:03:23 +01:00
if ( $partofdirinoriginalfile && ! empty ( $fuser -> rights -> $modulepart -> $partofdirinoriginalfile ) && ( $fuser -> rights -> $modulepart -> $partofdirinoriginalfile -> { $lire } || $fuser -> rights -> $modulepart -> $partofdirinoriginalfile -> { $read })) {
$accessallowed = 1 ;
}
}
if ( ! empty ( $fuser -> rights -> $modulepart -> { $lire }) || ! empty ( $fuser -> rights -> $modulepart -> { $read })) {
$accessallowed = 1 ;
2020-11-01 21:27:56 +01:00
}
2020-09-29 13:19:03 +02:00
2020-09-27 18:55:09 +02:00
if ( is_array ( $conf -> $modulepart -> multidir_output ) && ! empty ( $conf -> $modulepart -> multidir_output [ $entity ])) {
2020-09-27 18:05:19 +02:00
$original_file = $conf -> $modulepart -> multidir_output [ $entity ] . '/' . $original_file ;
} else {
$original_file = $conf -> $modulepart -> dir_output . '/' . $original_file ;
}
2013-06-05 16:24:32 +02:00
}
2013-04-25 10:06:54 +02:00
2021-08-28 00:53:13 +02:00
$parameters = array (
'modulepart' => $modulepart ,
'original_file' => $original_file ,
'entity' => $entity ,
'fuser' => $fuser ,
'refname' => '' ,
'mode' => $mode
);
$reshook = $hookmanager -> executeHooks ( 'checkSecureAccess' , $parameters , $object );
if ( $reshook > 0 ) {
2021-10-21 16:02:47 +02:00
if ( ! empty ( $hookmanager -> resArray [ 'original_file' ])) {
$original_file = $hookmanager -> resArray [ 'original_file' ];
}
2021-08-28 00:53:13 +02:00
if ( ! empty ( $hookmanager -> resArray [ 'accessallowed' ])) {
$accessallowed = $hookmanager -> resArray [ 'accessallowed' ];
}
if ( ! empty ( $hookmanager -> resArray [ 'sqlprotectagainstexternals' ])) {
$sqlprotectagainstexternals = $hookmanager -> resArray [ 'sqlprotectagainstexternals' ];
}
}
2013-04-22 15:00:29 +02:00
}
2013-06-25 15:09:51 +02:00
2013-04-25 10:06:54 +02:00
$ret = array (
2022-01-13 15:52:41 +01:00
'accessallowed' => ( $accessallowed ? 1 : 0 ),
'sqlprotectagainstexternals' => $sqlprotectagainstexternals ,
'original_file' => $original_file
2013-04-25 10:06:54 +02:00
);
2013-04-22 15:00:29 +02:00
return $ret ;
}
2015-01-24 15:36:47 +01:00
/**
2017-03-10 13:42:55 +01:00
* Store object in file .
2015-01-24 15:36:47 +01:00
*
* @ param string $directory Directory of cache
* @ param string $filename Name of filecache
* @ param mixed $object Object to store in cachefile
* @ return void
*/
function dol_filecache ( $directory , $filename , $object )
{
2021-02-23 22:03:23 +01:00
if ( ! dol_is_dir ( $directory )) {
dol_mkdir ( $directory );
}
2019-11-13 19:37:08 +01:00
$cachefile = $directory . $filename ;
2017-10-07 13:09:31 +02:00
file_put_contents ( $cachefile , serialize ( $object ), LOCK_EX );
@ chmod ( $cachefile , 0644 );
2015-01-24 15:36:47 +01:00
}
/**
2017-03-10 13:42:55 +01:00
* Test if Refresh needed .
2015-01-24 15:36:47 +01:00
*
* @ param string $directory Directory of cache
* @ param string $filename Name of filecache
* @ param int $cachetime Cachetime delay
* @ return boolean 0 no refresh 1 if refresh needed
*/
function dol_cache_refresh ( $directory , $filename , $cachetime )
{
2017-10-07 13:09:31 +02:00
$now = dol_now ();
2019-11-13 19:37:08 +01:00
$cachefile = $directory . $filename ;
$refresh = ! file_exists ( $cachefile ) || ( $now - $cachetime ) > dol_filemtime ( $cachefile );
2017-10-07 13:09:31 +02:00
return $refresh ;
2015-01-24 15:36:47 +01:00
}
/**
2017-03-10 13:42:55 +01:00
* Read object from cachefile .
2015-01-24 15:36:47 +01:00
*
* @ param string $directory Directory of cache
* @ param string $filename Name of filecache
* @ return mixed Unserialise from file
*/
function dol_readcachefile ( $directory , $filename )
{
2019-11-13 19:37:08 +01:00
$cachefile = $directory . $filename ;
2017-10-07 13:09:31 +02:00
$object = unserialize ( file_get_contents ( $cachefile ));
return $object ;
2015-01-24 15:36:47 +01:00
}
2017-12-16 17:24:23 +01:00
/**
* Function to get list of updated or modified files .
* $file_list is used as global variable
*
* @ param array $file_list Array for response
* @ param SimpleXMLElement $dir SimpleXMLElement of files to test
* @ param string $path Path of files relative to $pathref . We start with '' . Used by recursive calls .
* @ param string $pathref Path ref ( DOL_DOCUMENT_ROOT )
* @ param array $checksumconcat Array of checksum
* @ return array Array of filenames
*/
function getFilesUpdated ( & $file_list , SimpleXMLElement $dir , $path = '' , $pathref = '' , & $checksumconcat = array ())
{
2018-02-16 22:00:34 +01:00
global $conffile ;
2017-12-16 17:24:23 +01:00
$exclude = 'install' ;
2021-02-23 22:03:23 +01:00
foreach ( $dir -> md5file as $file ) { // $file is a simpleXMLElement
2017-12-16 17:24:23 +01:00
$filename = $path . $file [ 'name' ];
$file_list [ 'insignature' ][] = $filename ;
2019-11-04 13:23:48 +01:00
$expectedsize = ( empty ( $file [ 'size' ]) ? '' : $file [ 'size' ]);
2018-02-16 22:00:34 +01:00
$expectedmd5 = ( string ) $file ;
2017-12-16 17:24:23 +01:00
//if (preg_match('#'.$exclude.'#', $filename)) continue;
2021-02-23 22:03:23 +01:00
if ( ! file_exists ( $pathref . '/' . $filename )) {
2019-11-04 13:23:48 +01:00
$file_list [ 'missing' ][] = array ( 'filename' => $filename , 'expectedmd5' => $expectedmd5 , 'expectedsize' => $expectedsize );
2020-05-21 15:05:19 +02:00
} else {
2017-12-16 17:24:23 +01:00
$md5_local = md5_file ( $pathref . '/' . $filename );
2018-02-16 22:00:34 +01:00
2021-02-23 22:03:23 +01:00
if ( $conffile == '/etc/dolibarr/conf.php' && $filename == '/filefunc.inc.php' ) { // For install with deb or rpm, we ignore test on filefunc.inc.php that was modified by package
2018-02-16 22:00:34 +01:00
$checksumconcat [] = $expectedmd5 ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $md5_local != $expectedmd5 ) {
$file_list [ 'updated' ][] = array ( 'filename' => $filename , 'expectedmd5' => $expectedmd5 , 'expectedsize' => $expectedsize , 'md5' => ( string ) $md5_local );
}
2018-02-16 22:00:34 +01:00
$checksumconcat [] = $md5_local ;
}
2017-12-16 17:24:23 +01:00
}
}
2021-02-23 22:03:23 +01:00
foreach ( $dir -> dir as $subdir ) { // $subdir['name'] is '' or '/accountancy/admin' for example
2018-02-16 22:00:34 +01:00
getFilesUpdated ( $file_list , $subdir , $path . $subdir [ 'name' ] . '/' , $pathref , $checksumconcat );
}
2017-12-16 17:24:23 +01:00
return $file_list ;
}