2008-04-28 22:49:59 +02:00
< ? php
2013-04-12 10:26:52 +02:00
/* Copyright ( C ) 2008 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2012 - 2013 Regis Houssin < regis . houssin @ capnetworks . com >
* Copyright ( C ) 2012 Juanjo Menent < jmenent @ 2 byte . es >
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
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2008-04-28 22:49:59 +02:00
* or see http :// www . gnu . org /
*/
/**
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 )
{
return preg_replace ( '/^.*\/([^\/]+)$/' , '$1' , rtrim ( $pathfile , '/' ));
}
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
*
2011-11-12 12:59:06 +01:00
* @ param string $path Starting path from which to search
* @ param string $types Can be " directories " , " files " , or " all "
* @ param int $recursive Determines whether subdirectories are searched
2013-03-30 14:04:09 +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
2012-03-16 00:34:41 +01:00
* @ param string $excludefilter Array of Regex for exclude filter ( example : array ( '\.meta$' , '^\.' ))
2012-02-06 14:34:58 +01:00
* @ param string $sortcriteria Sort criteria ( " " , " fullname " , " name " , " date " , " size " )
2011-11-12 12:59:06 +01:00
* @ param string $sortorder Sort order ( SORT_ASC , SORT_DESC )
* @ 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
2012-09-17 19:32:45 +02:00
* @ param int $nohook Disable all hooks
2011-11-12 12:59:06 +01:00
* @ return array Array of array ( 'name' => 'xxx' , 'fullname' => '/abc/xxx' , 'date' => 'yyy' , 'size' => 99 , 'type' => 'dir|file' )
2008-04-29 23:13:49 +02:00
*/
2012-09-17 19:32:45 +02:00
function dol_dir_list ( $path , $types = " all " , $recursive = 0 , $filter = " " , $excludefilter = " " , $sortcriteria = " name " , $sortorder = SORT_ASC , $mode = 0 , $nohook = false )
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
2012-07-13 10:15:47 +02:00
dol_syslog ( " files.lib.php::dol_dir_list path= " . $path . " types= " . $types . " recursive= " . $recursive . " filter= " . $filter . " excludefilter= " . json_encode ( $excludefilter ));
2012-10-03 20:10:29 +02:00
//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
2010-10-03 17:42:01 +02:00
$loaddate = ( $mode == 1 || $mode == 2 ) ? true : false ;
$loadsize = ( $mode == 1 || $mode == 3 ) ? true : false ;
2009-02-24 23:52:55 +01:00
2008-04-29 23:13:49 +02:00
// Clean parameters
2009-10-21 18:50:15 +02:00
$path = preg_replace ( '/([\\/]+)$/i' , '' , $path );
2009-12-15 12:02:01 +01:00
$newpath = dol_osencode ( $path );
2009-02-24 23:52:55 +01:00
2013-03-30 14:10:15 +01:00
if ( ! $nohook )
2013-01-25 19:12:54 +01:00
{
2012-09-17 19:32:45 +02:00
$hookmanager -> initHooks ( array ( 'fileslib' ));
$parameters = array (
'path' => $newpath ,
'types' => $types ,
'recursive' => $recursive ,
'filter' => $filter ,
'excludefilter' => $excludefilter ,
'sortcriteria' => $sortcriteria ,
'sortorder' => $sortorder ,
'loaddate' => $loaddate ,
'loadsize' => $loadsize
);
$reshook = $hookmanager -> executeHooks ( 'getNodesList' , $parameters , $object );
}
2012-09-05 10:17:02 +02:00
2012-09-05 14:30:12 +02:00
// $reshook may contain returns stacked by other modules
// $reshook is always empty with an array for can not lose returns stacked with other modules
// $hookmanager->resArray may contain array stacked by other modules
2012-09-17 19:32:45 +02:00
if ( ! $nohook && ! empty ( $hookmanager -> resArray )) // forced to use $hookmanager->resArray even if $hookmanager->resArray['nodes'] is empty
2008-04-29 23:13:49 +02:00
{
2012-09-05 14:30:12 +02:00
return $hookmanager -> resArray [ 'nodes' ];
2008-04-29 23:13:49 +02:00
}
else
{
2012-09-07 17:23:16 +02:00
if ( ! is_dir ( $newpath )) return array ();
if ( $dir = opendir ( $newpath ))
{
$filedate = '' ;
$filesize = '' ;
$file_list = array ();
while ( false !== ( $file = readdir ( $dir )))
{
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // To be sure data is stored in utf8 in memory
$qualified = 1 ;
// Define excludefilterarray
$excludefilterarray = array ( '^\.' );
if ( is_array ( $excludefilter ))
{
$excludefilterarray = array_merge ( $excludefilterarray , $excludefilter );
}
else if ( $excludefilter ) $excludefilterarray [] = $excludefilter ;
// Check if file is qualified
foreach ( $excludefilterarray as $filt )
{
if ( preg_match ( '/' . $filt . '/i' , $file )) {
$qualified = 0 ; break ;
}
}
if ( $qualified )
{
$isdir = is_dir ( dol_osencode ( $path . " / " . $file ));
// Check whether this is a file or directory and whether we're interested in that type
if ( $isdir && (( $types == " directories " ) || ( $types == " all " ) || $recursive ))
{
// Add entry into file_list array
if (( $types == " directories " ) || ( $types == " all " ))
{
if ( $loaddate || $sortcriteria == 'date' ) $filedate = dol_filemtime ( $path . " / " . $file );
if ( $loadsize || $sortcriteria == 'size' ) $filesize = dol_filesize ( $path . " / " . $file );
2013-01-07 00:40:18 +01:00
if ( ! $filter || preg_match ( '/' . $filter . '/i' , $file )) // We do not search key $filter into $path, only into $file
2012-09-07 17:23:16 +02:00
{
2013-07-14 00:40:22 +02:00
preg_match ( '/([^\/]+)\/[^\/]+$/' , $path . '/' . $file , $reg );
$level1name = ( isset ( $reg [ 1 ]) ? $reg [ 1 ] : '' );
2012-09-07 17:23:16 +02:00
$file_list [] = array (
" name " => $file ,
2013-07-14 00:40:22 +02:00
" level1name " => $level1name ,
2012-09-07 17:23:16 +02:00
" fullname " => $path . '/' . $file ,
" date " => $filedate ,
" size " => $filesize ,
" type " => 'dir'
);
}
}
// if we're in a directory and we want recursive behavior, call this function again
if ( $recursive )
{
$file_list = array_merge ( $file_list , dol_dir_list ( $path . " / " . $file , $types , $recursive , $filter , $excludefilter , $sortcriteria , $sortorder , $mode ));
}
}
else if ( ! $isdir && (( $types == " files " ) || ( $types == " all " )))
{
// Add file into file_list array
if ( $loaddate || $sortcriteria == 'date' ) $filedate = dol_filemtime ( $path . " / " . $file );
if ( $loadsize || $sortcriteria == 'size' ) $filesize = dol_filesize ( $path . " / " . $file );
2013-01-07 00:40:18 +01:00
if ( ! $filter || preg_match ( '/' . $filter . '/i' , $file )) // We do not search key $filter into $path, only into $file
2012-09-07 17:23:16 +02:00
{
2013-07-14 00:40:22 +02:00
preg_match ( '/([^\/]+)\/[^\/]+$/' , $path . '/' . $file , $reg );
$level1name = ( isset ( $reg [ 1 ]) ? $reg [ 1 ] : '' );
2012-09-07 17:23:16 +02:00
$file_list [] = array (
" name " => $file ,
2013-07-14 00:40:22 +02:00
" level1name " => $level1name ,
2012-09-07 17:23:16 +02:00
" fullname " => $path . '/' . $file ,
" date " => $filedate ,
" size " => $filesize ,
" type " => 'file'
);
}
}
}
}
closedir ( $dir );
// Obtain a list of columns
if ( ! empty ( $sortcriteria ))
{
$myarray = array ();
foreach ( $file_list as $key => $row )
{
$myarray [ $key ] = ( isset ( $row [ $sortcriteria ]) ? $row [ $sortcriteria ] : '' );
}
// Sort the data
if ( $sortorder ) array_multisort ( $myarray , $sortorder , $file_list );
}
return $file_list ;
}
else
{
return array ();
2012-09-05 10:17:02 +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
2008-04-28 22:49:59 +02:00
$sortorder = strtoupper ( $sortorder );
2009-02-24 23:52:55 +01:00
2008-04-28 22:49:59 +02:00
if ( $sortorder == 'ASC' ) { $retup =- 1 ; $retdown = 1 ; }
else { $retup = 1 ; $retdown =- 1 ; }
2009-02-24 23:52:55 +01:00
2008-04-28 22:49:59 +02:00
if ( $sortfield == 'name' )
{
if ( $a -> name == $b -> name ) return 0 ;
return ( $a -> name < $b -> name ) ? $retup : $retdown ;
}
if ( $sortfield == 'date' )
{
if ( $a -> date == $b -> date ) return 0 ;
return ( $a -> date < $b -> date ) ? $retup : $retdown ;
}
if ( $sortfield == 'size' )
{
if ( $a -> size == $b -> size ) return 0 ;
return ( $a -> size < $b -> size ) ? $retup : $retdown ;
}
}
2008-08-04 00:50:32 +02:00
/**
2010-08-18 16:48:17 +02:00
* Return mime type of a file
2011-08-26 19:59:14 +02:00
*
2011-11-12 12:59:06 +01:00
* @ param string $file Filename we looking for MIME type
* @ param string $default Default mime type if extension not found in known list
* @ param int $mode 0 = Return full mime , 1 = otherwise short mime string , 2 = image for mime type , 3 = source language
* @ return string Return a mime type family ( text / xxx , application / xxx , image / xxx , audio , video , archive )
* @ see image_format_supported ( images . lib . php )
2008-08-04 00:50:32 +02:00
*/
2010-08-21 18:38:19 +02:00
function dol_mimetype ( $file , $default = 'application/octet-stream' , $mode = 0 )
2008-08-04 00:50:32 +02:00
{
2010-08-18 16:48:17 +02:00
$mime = $default ;
2010-08-21 18:38:19 +02:00
$imgmime = 'other.png' ;
2010-08-22 14:44:14 +02:00
$srclang = '' ;
2010-08-20 18:25:31 +02:00
2010-11-13 15:34:06 +01:00
$tmpfile = preg_replace ( '/\.noexe$/' , '' , $file );
2009-10-15 04:27:20 +02:00
// Text files
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.txt$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; }
if ( preg_match ( '/\.rtx$/i' , $tmpfile )) { $mime = 'text/richtext' ; $imgmime = 'text.png' ; }
if ( preg_match ( '/\.csv$/i' , $tmpfile )) { $mime = 'text/csv' ; $imgmime = 'text.png' ; }
if ( preg_match ( '/\.tsv$/i' , $tmpfile )) { $mime = 'text/tab-separated-values' ; $imgmime = 'text.png' ; }
if ( preg_match ( '/\.(cf|conf|log)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; }
if ( preg_match ( '/\.ini$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'ini' ; }
if ( preg_match ( '/\.css$/i' , $tmpfile )) { $mime = 'text/css' ; $imgmime = 'css.png' ; $srclang = 'css' ; }
2010-08-20 18:26:55 +02:00
// Certificate files
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.(crt|cer|key|pub)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; }
2010-08-22 14:44:14 +02:00
// HTML/XML
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.(html|htm|shtml)$/i' , $tmpfile )) { $mime = 'text/html' ; $imgmime = 'html.png' ; $srclang = 'html' ; }
if ( preg_match ( '/\.(xml|xhtml)$/i' , $tmpfile )) { $mime = 'text/xml' ; $imgmime = 'other.png' ; $srclang = 'xml' ; }
2010-08-20 18:26:55 +02:00
// Languages
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.bas$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'bas' ; }
if ( preg_match ( '/\.(c)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'c' ; }
if ( preg_match ( '/\.(cpp)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'cpp' ; }
if ( preg_match ( '/\.(h)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'h' ; }
if ( preg_match ( '/\.(java|jsp)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'java' ; }
if ( preg_match ( '/\.php([0-9]{1})?$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'php.png' ; $srclang = 'php' ; }
2013-01-30 16:52:33 +01:00
if ( preg_match ( '/\.phtml$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'php.png' ; $srclang = 'php' ; }
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.(pl|pm)$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'pl.png' ; $srclang = 'perl' ; }
if ( preg_match ( '/\.sql$/i' , $tmpfile )) { $mime = 'text/plain' ; $imgmime = 'text.png' ; $srclang = 'sql' ; }
if ( preg_match ( '/\.js$/i' , $tmpfile )) { $mime = 'text/x-javascript' ; $imgmime = 'jscript.png' ; $srclang = 'js' ; }
2010-08-20 18:26:55 +02:00
// Open office
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.odp$/i' , $tmpfile )) { $mime = 'application/vnd.oasis.opendocument.presentation' ; $imgmime = 'ooffice.png' ; }
if ( preg_match ( '/\.ods$/i' , $tmpfile )) { $mime = 'application/vnd.oasis.opendocument.spreadsheet' ; $imgmime = 'ooffice.png' ; }
if ( preg_match ( '/\.odt$/i' , $tmpfile )) { $mime = 'application/vnd.oasis.opendocument.text' ; $imgmime = 'ooffice.png' ; }
2010-08-20 18:26:55 +02:00
// MS Office
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.mdb$/i' , $tmpfile )) { $mime = 'application/msaccess' ; $imgmime = 'mdb.png' ; }
if ( preg_match ( '/\.doc(x|m)?$/i' , $tmpfile )) { $mime = 'application/msword' ; $imgmime = 'doc.png' ; }
if ( preg_match ( '/\.dot(x|m)?$/i' , $tmpfile )) { $mime = 'application/msword' ; $imgmime = 'doc.png' ; }
if ( preg_match ( '/\.xlt(x)?$/i' , $tmpfile )) { $mime = 'application/vnd.ms-excel' ; $imgmime = 'xls.png' ; }
if ( preg_match ( '/\.xla(m)?$/i' , $tmpfile )) { $mime = 'application/vnd.ms-excel' ; $imgmime = 'xls.png' ; }
2011-12-17 01:16:33 +01:00
if ( preg_match ( '/\.xls$/i' , $tmpfile )) { $mime = 'application/vnd.ms-excel' ; $imgmime = 'xls.png' ; }
2011-12-31 02:30:20 +01:00
if ( preg_match ( '/\.xls(b|m|x)$/i' , $tmpfile )) { $mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ; $imgmime = 'xls.png' ; }
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.pps(m|x)?$/i' , $tmpfile )) { $mime = 'application/vnd.ms-powerpoint' ; $imgmime = 'ppt.png' ; }
if ( preg_match ( '/\.ppt(m|x)?$/i' , $tmpfile )) { $mime = 'application/x-mspowerpoint' ; $imgmime = 'ppt.png' ; }
2010-08-20 18:26:55 +02:00
// Other
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.pdf$/i' , $tmpfile )) { $mime = 'application/pdf' ; $imgmime = 'pdf.png' ; }
2010-08-18 16:48:17 +02:00
// Scripts
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.bat$/i' , $tmpfile )) { $mime = 'text/x-bat' ; $imgmime = 'script.png' ; $srclang = 'dos' ; }
if ( preg_match ( '/\.sh$/i' , $tmpfile )) { $mime = 'text/x-sh' ; $imgmime = 'script.png' ; $srclang = 'bash' ; }
if ( preg_match ( '/\.ksh$/i' , $tmpfile )) { $mime = 'text/x-ksh' ; $imgmime = 'script.png' ; $srclang = 'bash' ; }
if ( preg_match ( '/\.bash$/i' , $tmpfile )) { $mime = 'text/x-bash' ; $imgmime = 'script.png' ; $srclang = 'bash' ; }
2009-10-15 04:27:20 +02:00
// Images
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.ico$/i' , $tmpfile )) { $mime = 'image/x-icon' ; $imgmime = 'image.png' ; }
if ( preg_match ( '/\.(jpg|jpeg)$/i' , $tmpfile )) { $mime = 'image/jpeg' ; $imgmime = 'image.png' ; }
if ( preg_match ( '/\.png$/i' , $tmpfile )) { $mime = 'image/png' ; $imgmime = 'image.png' ; }
if ( preg_match ( '/\.gif$/i' , $tmpfile )) { $mime = 'image/gif' ; $imgmime = 'image.png' ; }
if ( preg_match ( '/\.bmp$/i' , $tmpfile )) { $mime = 'image/bmp' ; $imgmime = 'image.png' ; }
2010-11-13 19:00:35 +01:00
if ( preg_match ( '/\.(tif|tiff)$/i' , $tmpfile )) { $mime = 'image/tiff' ; $imgmime = 'image.png' ; }
2009-10-15 04:27:20 +02:00
// Calendar
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.vcs$/i' , $tmpfile )) { $mime = 'text/calendar' ; $imgmime = 'other.png' ; }
if ( preg_match ( '/\.ics$/i' , $tmpfile )) { $mime = 'text/calendar' ; $imgmime = 'other.png' ; }
2009-10-15 04:27:20 +02:00
// Other
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.torrent$/i' , $tmpfile )) { $mime = 'application/x-bittorrent' ; $imgmime = 'other.png' ; }
2009-10-15 04:27:20 +02:00
// Audio
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.(mp3|ogg|au|wav|wma|mid)$/i' , $tmpfile )) { $mime = 'audio' ; $imgmime = 'audio.png' ; }
2009-10-15 04:27:20 +02:00
// Video
2011-07-06 18:56:01 +02:00
if ( preg_match ( '/\.ogv$/i' , $tmpfile )) { $mime = 'video/ogg' ; $imgmime = 'video.png' ; }
if ( preg_match ( '/\.webm$/i' , $tmpfile )) { $mime = 'video/webm' ; $imgmime = 'video.png' ; }
if ( preg_match ( '/\.avi$/i' , $tmpfile )) { $mime = 'video/x-msvideo' ; $imgmime = 'video.png' ; }
if ( preg_match ( '/\.divx$/i' , $tmpfile )) { $mime = 'video/divx' ; $imgmime = 'video.png' ; }
if ( preg_match ( '/\.xvid$/i' , $tmpfile )) { $mime = 'video/xvid' ; $imgmime = 'video.png' ; }
if ( preg_match ( '/\.(wmv|mpg|mpeg)$/i' , $tmpfile )) { $mime = 'video' ; $imgmime = 'video.png' ; }
2009-10-15 04:27:20 +02:00
// Archive
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.(zip|rar|gz|tgz|z|cab|bz2|7z|tar|lzh)$/i' , $tmpfile )) { $mime = 'archive' ; $imgmime = 'archive.png' ; } // application/xxx where zzz is zip, ...
2010-08-20 18:25:31 +02:00
// Exe
2010-11-13 15:34:06 +01:00
if ( preg_match ( '/\.(exe|com)$/i' , $tmpfile )) { $mime = 'application/octet-stream' ; $imgmime = 'other.png' ; }
// Lib
if ( preg_match ( '/\.(dll|lib|o|so|a)$/i' , $tmpfile )) { $mime = 'library' ; $imgmime = 'library.png' ; }
// Err
if ( preg_match ( '/\.err$/i' , $tmpfile )) { $mime = 'error' ; $imgmime = 'error.png' ; }
2010-08-18 16:48:17 +02:00
2010-08-21 18:38:19 +02:00
// Return string
if ( $mode == 1 )
2010-08-20 18:25:31 +02:00
{
$tmp = explode ( '/' , $mime );
2012-07-30 22:38:21 +02:00
return ( ! empty ( $tmp [ 1 ]) ? $tmp [ 1 ] : $tmp [ 0 ]);
2010-08-20 18:25:31 +02:00
}
2010-08-21 18:38:19 +02:00
if ( $mode == 2 )
2010-08-20 18:25:31 +02:00
{
2010-08-21 18:38:19 +02:00
return $imgmime ;
2010-08-20 18:25:31 +02:00
}
2010-08-22 14:44:14 +02:00
if ( $mode == 3 )
{
return $srclang ;
}
2010-08-21 18:38:19 +02:00
return $mime ;
2008-08-04 00:50:32 +02:00
}
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 )
{
$newfolder = dol_osencode ( $folder );
if ( is_dir ( $newfolder )) return true ;
else return false ;
}
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 )
{
$newpathoffile = dol_osencode ( $pathoffile );
return is_file ( $newpathoffile );
}
2010-08-21 17:30:17 +02:00
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 )
{
2012-01-21 18:24:21 +01:00
$tmpprot = array ( 'file' , 'http' , 'https' , 'ftp' , 'zlib' , 'data' , 'ssh' , 'ssh2' , 'ogg' , 'expect' );
2011-08-26 19:59:14 +02:00
foreach ( $tmpprot as $prot )
{
if ( preg_match ( '/^' . $prot . ':/i' , $url )) return true ;
}
return false ;
}
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 )
{
2009-12-15 12:02:01 +01:00
$newfolder = dol_osencode ( $folder );
2009-10-04 17:52:16 +02:00
if ( is_dir ( $newfolder ))
2008-11-16 02:09:04 +01:00
{
2009-10-04 17:52:16 +02:00
$handle = opendir ( $newfolder );
2012-02-04 14:39:47 +01:00
while (( gettype ( $name = readdir ( $handle )) != " boolean " ))
2009-10-04 17:52:16 +02:00
{
2008-11-16 02:09:04 +01:00
$name_array [] = $name ;
}
2009-10-04 17:52:16 +02:00
foreach ( $name_array as $temp ) $folder_content .= $temp ;
2008-11-16 02:09:04 +01:00
2009-10-04 17:52:16 +02:00
if ( $folder_content == " ... " ) return true ;
else return false ;
2009-02-24 23:52:55 +01:00
2008-11-16 02:09:04 +01:00
closedir ( $handle );
}
else
2009-10-04 14:16:45 +02:00
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
2009-09-30 18:02:54 +02:00
*/
function dol_count_nb_of_line ( $file )
{
$nb = 0 ;
2009-10-04 17:52:16 +02:00
2009-12-15 12:02:01 +01:00
$newfile = dol_osencode ( $file );
2009-09-30 18:02:54 +02:00
//print 'x'.$file;
2009-10-04 17:52:16 +02:00
$fp = fopen ( $newfile , 'r' );
2009-09-30 18:02:54 +02:00
if ( $fp )
{
2009-10-04 17:52:16 +02:00
while ( ! feof ( $fp ))
{
$line = fgets ( $fp );
2012-01-20 10:09:19 +01: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.
if ( ! $line === false ) $nb ++ ;
2009-10-04 17:52:16 +02:00
}
fclose ( $fp );
2009-09-30 18:02:54 +02:00
}
else
{
$nb =- 1 ;
}
return $nb ;
}
2009-10-04 19:18:09 +02:00
/**
* Return size of a file
2011-12-17 01:16:33 +01:00
*
2011-11-12 12:59:06 +01:00
* @ param tring $pathoffile Path of file
* @ return string File size
2009-10-04 19:18:09 +02:00
*/
function dol_filesize ( $pathoffile )
{
2009-12-15 12:02:01 +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
* @ return timestamp 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
{
2009-12-15 12:02:01 +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
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 )
* @ param int $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK )
* @ 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
2009-12-15 21:45:21 +01:00
*/
2010-03-21 21:29:29 +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 );
2012-02-05 19:37:52 +01:00
$destexists = dol_is_file ( $destfile );
if ( ! $overwriteifexists && $destexists ) return 0 ;
$newpathofsrcfile = dol_osencode ( $srcfile );
$newpathofdestfile = dol_osencode ( $destfile );
$newdirdestfile = dirname ( $newpathofdestfile );
if ( $destexists && ! is_writable ( $newpathofdestfile ))
{
dol_syslog ( " files.lib.php::dol_copy failed Permission denied to overwrite target file " , LOG_WARNING );
return - 1 ;
}
if ( ! is_writable ( $newdirdestfile ))
{
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
$result =@ copy ( $newpathofsrcfile , $newpathofdestfile );
//$result=copy($newpathofsrcfile, $newpathofdestfile); // To see errors, remove @
if ( ! $result )
2010-03-21 21:29:29 +01:00
{
2012-02-05 19:37:52 +01:00
dol_syslog ( " files.lib.php::dol_copy failed to copy " , LOG_WARNING );
return - 3 ;
2010-03-21 21:29:29 +01:00
}
2012-02-05 19:37:52 +01:00
if ( empty ( $newmask ) && ! empty ( $conf -> global -> MAIN_UMASK )) $newmask = $conf -> global -> MAIN_UMASK ;
@ 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
2010-10-03 18:59:24 +02:00
/**
2012-09-07 10:30:02 +02:00
* Move a file into another name .
2012-09-19 14:11:01 +02:00
* This function differs from dol_move_uploaded_file , because it can be called in any context .
2011-12-17 01:16:33 +01:00
*
2011-11-12 12:59:06 +01:00
* @ param string $srcfile Source file ( can ' t be a directory )
* @ param string $destfile Destination file ( can ' t be a directory )
* @ param string $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK )
* @ param int $overwriteifexists Overwrite file if exists ( 1 by default )
* @ return boolean True if OK , false if KO
2010-10-03 18:59:24 +02:00
*/
function dol_move ( $srcfile , $destfile , $newmask = 0 , $overwriteifexists = 1 )
{
global $conf ;
$result = false ;
2010-05-01 16:28:48 +02:00
2010-10-03 18:59:24 +02:00
dol_syslog ( " files.lib.php::dol_move srcfile= " . $srcfile . " destfile= " . $destfile . " newmask= " . $newmask . " overwritifexists= " . $overwriteifexists );
if ( $overwriteifexists || ! dol_is_file ( $destfile ))
{
$newpathofsrcfile = dol_osencode ( $srcfile );
$newpathofdestfile = dol_osencode ( $destfile );
$result =@ rename ( $newpathofsrcfile , $newpathofdestfile ); // To see errors, remove @
if ( ! $result ) dol_syslog ( " files.lib.php::dol_move failed " , LOG_WARNING );
if ( empty ( $newmask ) && ! empty ( $conf -> global -> MAIN_UMASK )) $newmask = $conf -> global -> MAIN_UMASK ;
@ chmod ( $newpathofsrcfile , octdec ( $newmask ));
}
return $result ;
}
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
}
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 .
2012-09-07 10:30:02 +02:00
* Note : This function can be used only into a HTML page context . Use dol_move if you are outside .
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
* @ param string $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
2012-09-07 10:30:02 +02:00
* @ return int > 0 if OK , < 0 or string if KO
2012-09-12 12:41:44 +02:00
* @ see dol_move
2012-09-07 17:23:16 +02:00
*/
2012-09-12 12:27:11 +02:00
function dol_move_uploaded_file ( $src_file , $dest_file , $allowoverwrite , $disablevirusscan = 0 , $uploaderrorcode = 0 , $nohook = 0 , $varfiles = 'addedfile' )
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
$error = 0 ;
$file_name = $dest_file ;
2012-09-12 12:27:11 +02:00
if ( empty ( $nohook ))
2012-09-12 12:41:44 +02:00
{
// If an upload error has been reported
if ( $uploaderrorcode )
{
switch ( $uploaderrorcode )
{
case UPLOAD_ERR_INI_SIZE : // 1
return 'ErrorFileSizeTooLarge' ;
break ;
case UPLOAD_ERR_FORM_SIZE : // 2
return 'ErrorFileSizeTooLarge' ;
break ;
case UPLOAD_ERR_PARTIAL : // 3
return 'ErrorPartialFile' ;
break ;
case UPLOAD_ERR_NO_TMP_DIR : //
return 'ErrorNoTmpDir' ;
break ;
case UPLOAD_ERR_CANT_WRITE :
return 'ErrorFailedToWriteInDir' ;
break ;
case UPLOAD_ERR_EXTENSION :
return 'ErrorUploadBlockedByAddon' ;
break ;
default :
break ;
}
}
// If we need to make a virus scan
if ( empty ( $disablevirusscan ) && file_exists ( $src_file ) && ! empty ( $conf -> global -> MAIN_ANTIVIRUS_COMMAND ))
{
if ( ! class_exists ( 'AntiVir' )) {
require DOL_DOCUMENT_ROOT . '/core/class/antivir.class.php' ;
}
$antivir = new AntiVir ( $db );
$result = $antivir -> dol_avscan_file ( $src_file );
if ( $result < 0 ) // If virus or error, we stop here
{
$reterrors = $antivir -> errors ;
dol_syslog ( 'Files.lib::dol_move_uploaded_file File "' . $src_file . '" (target name "' . $dest_file . '") KO with antivirus: result=' . $result . ' errors=' . join ( ',' , $antivir -> errors ), LOG_WARNING );
return 'ErrorFileIsInfectedWithAVirus: ' . join ( ',' , $reterrors );
}
}
// Security:
// Disallow file with some extensions. We renamed them.
// Car si on a mis le rep documents dans un rep de la racine web (pas bien), cela permet d'executer du code a la demande.
if ( preg_match ( '/\.htm|\.html|\.php|\.pl|\.cgi$/i' , $dest_file ))
{
$file_name .= '.noexe' ;
}
// Security:
// On interdit fichiers caches, remontees de repertoire ainsi que les pipes dans les noms de fichiers.
if ( preg_match ( '/^\./' , $src_file ) || preg_match ( '/\.\./' , $src_file ) || preg_match ( '/[<>|]/' , $src_file ))
{
dol_syslog ( " Refused to deliver file " . $src_file , LOG_WARNING );
return - 1 ;
}
// Security:
// On interdit fichiers caches, remontees de repertoire ainsi que les pipe dans
// les noms de fichiers.
if ( preg_match ( '/^\./' , $dest_file ) || preg_match ( '/\.\./' , $dest_file ) || preg_match ( '/[<>|]/' , $dest_file ))
{
dol_syslog ( " Refused to deliver file " . $dest_file , LOG_WARNING );
return - 2 ;
}
2013-04-12 13:18:24 +02:00
$reshook = $hookmanager -> initHooks ( array ( 'fileslib' ));
2010-05-01 16:28:48 +02:00
2013-03-26 17:47:37 +01:00
$parameters = array ( 'dest_file' => $dest_file , 'src_file' => $src_file , 'file_name' => $file_name , 'varfiles' => $varfiles , 'allowoverwrite' => $allowoverwrite );
2013-04-12 13:18:24 +02:00
$reshook = $hookmanager -> executeHooks ( 'moveUploadedFile' , $parameters , $object );
2010-05-01 16:28:48 +02:00
}
2013-04-12 13:18:24 +02:00
if ( empty ( $reshook ))
2010-05-01 16:28:48 +02:00
{
2012-09-12 12:41:44 +02:00
// The file functions must be in OS filesystem encoding.
$src_file_osencoded = dol_osencode ( $src_file );
$file_name_osencoded = dol_osencode ( $file_name );
// Check if destination dir is writable
// TODO
// Check if destination file already exists
if ( ! $allowoverwrite )
{
if ( file_exists ( $file_name_osencoded ))
{
dol_syslog ( " Files.lib::dol_move_uploaded_file File " . $file_name . " already exists. Return 'ErrorFileAlreadyExists' " , LOG_WARNING );
return 'ErrorFileAlreadyExists' ;
}
}
// Move file
$return = move_uploaded_file ( $src_file_osencoded , $file_name_osencoded );
if ( $return )
{
if ( ! empty ( $conf -> global -> MAIN_UMASK )) @ chmod ( $file_name_osencoded , octdec ( $conf -> global -> MAIN_UMASK ));
dol_syslog ( " Files.lib::dol_move_uploaded_file Success to move " . $src_file . " to " . $file_name . " - Umask= " . $conf -> global -> MAIN_UMASK , LOG_DEBUG );
return 1 ; // Success
}
else
{
dol_syslog ( " Files.lib::dol_move_uploaded_file Failed to move " . $src_file . " to " . $file_name , LOG_ERR );
return - 3 ; // Unknown error
2011-07-05 18:10:56 +02:00
}
2010-05-01 16:28:48 +02:00
}
2013-04-12 10:00:20 +02:00
else
2013-04-12 13:18:24 +02:00
return $reshook ;
2010-05-01 16:28:48 +02:00
}
2011-03-09 16:48:25 +01:00
/**
* Remove a file or several files with a mask
2011-10-14 16:02:18 +02:00
*
2011-11-12 12:59:06 +01:00
* @ param string $file File to delete or mask of file to delete
* @ param int $disableglob Disable usage of glob like *
* @ 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
2011-11-12 12:59:06 +01:00
* @ return boolean True if file is deleted , False if error
2011-03-09 16:48:25 +01:00
*/
2012-09-12 12:27:11 +02:00
function dol_delete_file ( $file , $disableglob = 0 , $nophperrors = 0 , $nohook = 0 , $object = null )
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
2012-09-12 12:41:44 +02:00
$langs -> load ( " other " );
2012-09-12 12:27:11 +02:00
$langs -> load ( " errors " );
if ( empty ( $nohook ))
2012-09-11 17:01:54 +02:00
{
2012-09-12 12:41:44 +02:00
$hookmanager -> initHooks ( array ( 'fileslib' ));
$parameters = array (
2012-09-12 14:49:36 +02:00
'GET' => $_GET ,
2012-09-12 12:41:44 +02:00
'file' => $file ,
'disableglob' => $disableglob ,
'nophperrors' => $nophperrors
);
2012-09-12 12:27:11 +02:00
$reshook = $hookmanager -> executeHooks ( 'deleteFile' , $parameters , $object );
2012-09-11 17:01:54 +02:00
}
2012-09-12 12:27:11 +02:00
if ( empty ( $nohook ) && isset ( $reshook ) && $reshook != '' ) // 0:not deleted, 1:deleted, null or '' for bypass
2012-09-11 17:01:54 +02:00
{
return $reshook ;
}
else
{
2012-09-12 12:41:44 +02:00
$error = 0 ;
2013-04-30 17:24:11 +02:00
//print "x".$file." ".$disableglob;exit;
2012-09-12 12:41:44 +02:00
$ok = true ;
$file_osencoded = dol_osencode ( $file ); // New filename encoded in OS filesystem encoding charset
if ( empty ( $disableglob ) && ! empty ( $file_osencoded ))
{
2013-04-30 17:24:11 +02:00
$globencoded = str_replace ( '[' , '\[' , $file_osencoded );
$globencoded = str_replace ( ']' , '\]' , $globencoded );
2013-09-04 16:49:20 +02:00
$listofdir = glob ( $globencoded );
if ( ! empty ( $listofdir ) && is_array ( $listofdir ))
2012-09-12 12:41:44 +02:00
{
2013-09-04 16:49:20 +02:00
foreach ( $listofdir as $filename )
{
if ( $nophperrors ) $ok =@ unlink ( $filename ); // The unlink encapsulated by dolibarr
else $ok = unlink ( $filename ); // The unlink encapsulated by dolibarr
if ( $ok ) dol_syslog ( " Removed file " . $filename , LOG_DEBUG );
else dol_syslog ( " Failed to remove file " . $filename , LOG_WARNING );
}
2012-09-12 12:41:44 +02:00
}
2013-09-04 16:49:20 +02:00
else dol_syslog ( " No files to delete found " , LOG_WARNING );
2012-09-12 12:41:44 +02:00
}
else
{
if ( $nophperrors ) $ok =@ unlink ( $file_osencoded ); // The unlink encapsulated by dolibarr
else $ok = unlink ( $file_osencoded ); // The unlink encapsulated by dolibarr
if ( $ok ) dol_syslog ( " Removed file " . $file_osencoded , LOG_DEBUG );
else dol_syslog ( " Failed to remove file " . $file_osencoded , LOG_WARNING );
}
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
2011-03-09 16:48:25 +01:00
*/
2011-06-15 12:35:29 +02:00
function dol_delete_dir ( $dir , $nophperrors = 0 )
2011-03-09 16:48:25 +01:00
{
$dir_osencoded = dol_osencode ( $dir );
2011-06-15 12:35:29 +02:00
return ( $nophperrors ? @ rmdir ( $dir_osencoded ) : rmdir ( $dir_osencoded ));
2011-03-09 16:48:25 +01:00
}
/**
* Remove a directory $dir and its subdirectories
2012-02-04 14:39:47 +01:00
*
* @ param string $dir Dir to delete
* @ param int $count Counter to count nb of deleted elements
* @ param int $nophperrors Disable all PHP output errors
* @ return int Number of files and directory removed
2011-03-09 16:48:25 +01:00
*/
2011-06-15 12:35:29 +02:00
function dol_delete_dir_recursive ( $dir , $count = 0 , $nophperrors = 0 )
2011-03-09 16:48:25 +01:00
{
dol_syslog ( " functions.lib:dol_delete_dir_recursive " . $dir , LOG_DEBUG );
2011-06-22 11:11:00 +02:00
if ( dol_is_dir ( $dir ))
2011-03-09 16:48:25 +01:00
{
2011-06-22 11:11:00 +02:00
$dir_osencoded = dol_osencode ( $dir );
if ( $handle = opendir ( " $dir_osencoded " ))
2011-03-09 16:48:25 +01:00
{
2011-06-22 11:11:00 +02:00
while ( false !== ( $item = readdir ( $handle )))
2011-03-09 16:48:25 +01:00
{
2011-06-22 11:11:00 +02:00
if ( ! utf8_check ( $item )) $item = utf8_encode ( $item ); // should be useless
if ( $item != " . " && $item != " .. " )
2011-03-09 16:48:25 +01:00
{
2011-06-22 11:11:00 +02:00
if ( is_dir ( dol_osencode ( " $dir / $item " )))
{
$count = dol_delete_dir_recursive ( " $dir / $item " , $count , $nophperrors );
}
else
{
dol_delete_file ( " $dir / $item " , 1 , $nophperrors );
$count ++ ;
//echo " removing $dir/$item<br>\n";
}
2011-03-09 16:48:25 +01:00
}
}
2011-06-22 11:11:00 +02:00
closedir ( $handle );
dol_delete_dir ( $dir , $nophperrors );
$count ++ ;
//echo "removing $dir<br>\n";
2011-03-09 16:48:25 +01:00
}
}
//echo "return=".$count;
return $count ;
}
2011-10-14 16:02:18 +02:00
2011-09-17 02:04:44 +02:00
/**
* Delete all preview files linked to object instance
2011-10-14 16:02:18 +02:00
*
2011-09-17 02:04:44 +02:00
* @ param Object $object Object to clean
* @ return int 0 if error , 1 if OK
*/
function dol_delete_preview ( $object )
{
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
2012-08-19 09:05:50 +02: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 ;
2012-09-19 13:30:59 +02:00
else $dir = empty ( $conf -> $element -> dir_output ) ? '' : $conf -> $element -> dir_output ;
2012-03-22 08:33:44 +01:00
2011-09-17 02:04:44 +02: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 );
$dir = $dir . " / " . $refsan ;
$file = $dir . " / " . $refsan . " .pdf.png " ;
$multiple = $file . " . " ;
if ( file_exists ( $file ) && is_writable ( $file ))
{
if ( ! dol_delete_file ( $file , 1 ) )
{
$this -> error = $langs -> trans ( " ErrorFailedToOpenFile " , $file );
return 0 ;
}
}
else
{
for ( $i = 0 ; $i < 20 ; $i ++ )
{
$preview = $multiple . $i ;
if ( file_exists ( $preview ) && is_writable ( $preview ))
{
if ( ! dol_delete_file ( $preview , 1 ) )
{
$this -> error = $langs -> trans ( " ErrorFailedToOpenFile " , $preview );
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 .
2012-09-07 10:30:02 +02:00
* This should allow " grep " search .
* This feature is enabled only if option MAIN_DOC_CREATE_METAFILE is set .
2011-11-04 08:21:47 +01:00
*
* @ param Object $object Object
2012-09-07 10:30:02 +02:00
* @ return int 0 if we did nothing , > 0 success , < 0 error
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
2012-09-19 13:30:59 +02: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
$element = $object -> element ;
2012-09-19 13:30:59 +02:00
2012-09-19 17:53:09 +02: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 ;
2012-09-19 13:30:59 +02:00
else $dir = empty ( $conf -> $element -> dir_output ) ? '' : $conf -> $element -> dir_output ;
2012-09-07 10:30:02 +02:00
if ( $dir )
2011-11-04 08:29:10 +01:00
{
2012-09-07 10:30:02 +02:00
$object -> fetch_thirdparty ();
2011-11-04 08:29:10 +01:00
$facref = dol_sanitizeFileName ( $object -> ref );
2012-09-07 10:30:02 +02:00
$dir = $dir . " / " . $facref ;
2011-11-04 08:29:10 +01:00
$file = $dir . " / " . $facref . " .meta " ;
2011-12-17 01:16:33 +01:00
2011-11-04 08:29:10 +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
2011-11-04 08:29:10 +01:00
if ( is_dir ( $dir ))
{
$nblignes = count ( $object -> lines );
2013-02-24 17:08:52 +01:00
$client = $object -> client -> nom . " " . $object -> client -> address . " " . $object -> client -> zip . " " . $object -> client -> town ;
2011-11-04 08:29:10 +01:00
$meta = " REFERENCE= \" " . $object -> ref . " \"
2011-11-04 00:27:30 +01:00
DATE = \ " " . dol_print_date ( $object -> date , '' ) . " \"
NB_ITEMS = \ " " . $nblignes . " \"
CLIENT = \ " " . $client . " \"
TOTAL_HT = \ " " . $object -> total_ht . " \"
TOTAL_TTC = \ " " . $object -> total_ttc . " \" \n " ;
2011-12-17 01:16:33 +01:00
2011-11-04 08:29:10 +01:00
for ( $i = 0 ; $i < $nblignes ; $i ++ )
{
//Pour les articles
$meta .= " ITEM_ " . $i . " _QUANTITY= \" " . $object -> lines [ $i ] -> qty . " \"
2012-07-29 14:15:29 +02:00
ITEM_ " . $i . " _TOTAL_HT = \ " " . $object -> lines [ $i ] -> total_ht . " \"
2011-11-04 08:29:10 +01:00
ITEM_ " . $i . " _TVA = \ " " . $object -> lines [ $i ] -> tva_tx . " \"
ITEM_ " . $i . " _DESCRIPTION = \ " " . str_replace ( " \r \n " , " " , nl2br ( $object -> lines [ $i ] -> desc )) . " \"
" ;
}
}
2011-12-17 01:16:33 +01:00
2011-11-04 08:29:10 +01:00
$fp = fopen ( $file , " w " );
fputs ( $fp , $meta );
fclose ( $fp );
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
@ chmod ( $file , octdec ( $conf -> global -> MAIN_UMASK ));
2012-09-07 10:30:02 +02:00
return 1 ;
2011-11-04 08:29:10 +01:00
}
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
/**
* Init $_SESSION with uploaded files
*
* @ param string $pathtoscan Path to scan
* @ return void
*/
function dol_init_file_process ( $pathtoscan = '' )
{
$listofpaths = array ();
$listofnames = array ();
$listofmimes = array ();
if ( $pathtoscan )
{
$listoffiles = dol_dir_list ( $pathtoscan , 'files' );
foreach ( $listoffiles as $key => $val )
{
2011-12-17 01:16:33 +01:00
$listofpaths [] = $val [ 'fullname' ];
2011-11-21 13:17:14 +01:00
$listofnames [] = $val [ 'name' ];
$listofmimes [] = dol_mimetype ( $val [ 'name' ]);
}
}
$_SESSION [ " listofpaths " ] = join ( ';' , $listofpaths );
$_SESSION [ " listofnames " ] = join ( ';' , $listofnames );
$_SESSION [ " listofmimes " ] = join ( ';' , $listofmimes );
}
2010-05-12 13:29:21 +02:00
/**
* Get and save an upload file ( for example after submitting a new file a mail form ) .
* 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
*
2012-09-07 10:30:02 +02:00
* @ param string $upload_dir Directory where to store uploaded file ( note : also find in first part of dest_file )
2011-09-24 04:01:26 +02:00
* @ param int $allowoverwrite 1 = Allow overwrite existing file
* @ param int $donotupdatesession 1 = Do no edit _SESSION variable
2012-07-29 16:44:19 +02:00
* @ param string $varfiles _FILES var name
2012-07-29 12:54:19 +02:00
* @ return void
2010-05-12 13:29:21 +02:00
*/
2012-07-29 16:44:19 +02:00
function dol_add_file_process ( $upload_dir , $allowoverwrite = 0 , $donotupdatesession = 0 , $varfiles = 'addedfile' )
2010-05-12 13:29:21 +02:00
{
2012-09-12 14:55:39 +02:00
global $db , $user , $conf , $langs ;
2010-05-12 13:29:21 +02:00
2012-09-06 21:12:02 +02:00
if ( ! empty ( $_FILES [ $varfiles ])) // For view $_FILES[$varfiles]['error']
2010-05-12 13:29:21 +02:00
{
2013-07-24 11:26:09 +02:00
dol_syslog ( 'dol_add_file_process upload_dir=' . $upload_dir . ' allowoverwrite=' . $allowoverwrite . ' donotupdatesession=' . $donotupdatesession , LOG_DEBUG );
2011-07-10 22:03:38 +02:00
if ( dol_mkdir ( $upload_dir ) >= 0 )
2010-05-12 13:29:21 +02:00
{
2012-09-07 10:58:29 +02:00
$resupload = dol_move_uploaded_file ( $_FILES [ $varfiles ][ 'tmp_name' ], $upload_dir . " / " . $_FILES [ $varfiles ][ 'name' ], $allowoverwrite , 0 , $_FILES [ $varfiles ][ 'error' ], 0 , $varfiles );
2010-05-12 13:29:21 +02:00
if ( is_numeric ( $resupload ) && $resupload > 0 )
{
2013-07-24 11:26:09 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ;
2010-05-12 13:29:21 +02: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 );
2012-07-29 16:44:19 +02:00
$formmail -> add_attached_files ( $upload_dir . " / " . $_FILES [ $varfiles ][ 'name' ], $_FILES [ $varfiles ][ 'name' ], $_FILES [ $varfiles ][ 'type' ]);
}
2013-07-24 11:26:09 +02:00
if ( image_format_supported ( $upload_dir . " / " . $_FILES [ $varfiles ][ 'name' ]) == 1 )
2012-07-29 16:44:19 +02:00
{
// Create small thumbs for image (Ratio is near 16/9)
// Used on logon for example
$imgThumbSmall = vignette ( $upload_dir . " / " . $_FILES [ $varfiles ][ 'name' ], 160 , 120 , '_small' , 50 , " thumbs " );
// Create mini thumbs for image (Ratio is near 16/9)
// Used on menu or for setup page for example
$imgThumbMini = vignette ( $upload_dir . " / " . $_FILES [ $varfiles ][ 'name' ], 160 , 120 , '_mini' , 50 , " thumbs " );
2010-05-12 13:29:21 +02:00
}
2012-07-29 16:44:19 +02:00
setEventMessage ( $langs -> trans ( " FileTransferComplete " ));
2010-05-12 13:29:21 +02:00
}
else
{
$langs -> load ( " errors " );
if ( $resupload < 0 ) // Unknown error
{
2012-07-29 12:54:19 +02:00
setEventMessage ( $langs -> trans ( " ErrorFileNotUploaded " ), 'errors' );
2010-05-12 13:29:21 +02:00
}
else if ( preg_match ( '/ErrorFileIsInfectedWithAVirus/' , $resupload )) // Files infected by a virus
{
2012-07-29 12:54:19 +02:00
setEventMessage ( $langs -> trans ( " ErrorFileIsInfectedWithAVirus " ), 'errors' );
2010-05-12 13:29:21 +02:00
}
else // Known error
{
2012-07-29 12:54:19 +02:00
setEventMessage ( $langs -> trans ( $resupload ), 'errors' );
2010-05-12 13:29:21 +02:00
}
}
}
}
else
{
$langs -> load ( " errors " );
2012-07-29 12:54:19 +02:00
setEventMessage ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " File " )), 'warnings' );
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
* @ param int $donotupdatesession 1 = Do not edit _SESSION variable
* @ param int $donotdeletefile 1 = Do not delete physically file
2012-07-29 12:54:19 +02:00
* @ return void
2010-05-12 13:29:21 +02:00
*/
2011-07-10 22:03:38 +02:00
function dol_remove_file_process ( $filenb , $donotupdatesession = 0 , $donotdeletefile = 0 )
2010-05-12 13:29:21 +02:00
{
global $db , $user , $conf , $langs , $_FILES ;
$keytodelete = $filenb ;
$keytodelete -- ;
$listofpaths = array ();
$listofnames = array ();
$listofmimes = array ();
if ( ! empty ( $_SESSION [ " listofpaths " ])) $listofpaths = explode ( ';' , $_SESSION [ " listofpaths " ]);
if ( ! empty ( $_SESSION [ " listofnames " ])) $listofnames = explode ( ';' , $_SESSION [ " listofnames " ]);
if ( ! empty ( $_SESSION [ " listofmimes " ])) $listofmimes = explode ( ';' , $_SESSION [ " listofmimes " ]);
if ( $keytodelete >= 0 )
{
$pathtodelete = $listofpaths [ $keytodelete ];
$filetodelete = $listofnames [ $keytodelete ];
2011-07-10 22:03:38 +02:00
if ( empty ( $donotdeletefile )) $result = dol_delete_file ( $pathtodelete , 1 );
else $result = 0 ;
2010-05-12 13:29:21 +02:00
if ( $result >= 0 )
{
2012-07-29 16:44:19 +02:00
if ( empty ( $donotdeletefile ))
{
2012-07-29 15:47:24 +02:00
$langs -> load ( " other " );
2012-07-29 16:44:19 +02:00
setEventMessage ( $langs -> trans ( " FileWasRemoved " , $filetodelete ));
2012-07-29 15:47:24 +02:00
}
2010-05-12 13:29:21 +02: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 );
$formmail -> remove_attached_files ( $keytodelete );
}
}
}
}
2011-03-04 14:54:47 +01:00
/**
2011-10-14 16:02:18 +02:00
* Convert an image file into antoher format .
* This need Imagick php extension .
*
2012-02-04 14:39:47 +01:00
* @ param string $file Input file name
* @ param string $ext Extension of target file
* @ return int < 0 if KO , > 0 if OK
2011-03-04 14:54:47 +01:00
*/
function dol_convert_file ( $file , $ext = 'png' )
{
global $langs ;
2011-03-09 16:48:25 +01:00
2011-03-04 14:54:47 +01:00
$image = new Imagick ();
$ret = $image -> readImage ( $file );
if ( $ret )
{
$ret = $image -> setImageFormat ( $ext );
if ( $ret )
{
2011-03-04 19:50:54 +01:00
$count = $image -> getNumberImages ();
2011-09-20 11:40:27 +02:00
$ret = $image -> writeImages ( $file . " . " . $ext , true );
2011-03-04 19:50:54 +01:00
if ( $ret ) return $count ;
else return - 3 ;
2011-03-04 14:54:47 +01:00
}
else
{
return - 2 ;
}
}
else
{
return - 1 ;
}
}
2011-10-14 16:02:18 +02:00
/**
* Compress a file
*
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'
2012-02-04 14:39:47 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-10-14 16:02:18 +02:00
*/
function dol_compress_file ( $inputfile , $outputfile , $mode = " gz " )
{
2012-04-28 18:21:51 +02:00
$foundhandler = 0 ;
2011-10-14 16:02:18 +02:00
try
{
2012-04-28 18:21:51 +02:00
$data = implode ( " " , file ( dol_osencode ( $inputfile )));
if ( $mode == 'gz' ) { $foundhandler = 1 ; $compressdata = gzencode ( $data , 9 ); }
elseif ( $mode == 'bz' ) { $foundhandler = 1 ; $compressdata = bzcompress ( $data , 9 ); }
elseif ( $mode == 'zip' )
{
if ( defined ( 'ODTPHP_PATHTOPCLZIP' ))
{
$foundhandler = 1 ;
2011-10-14 16:02:18 +02:00
2012-08-23 02:04:35 +02:00
include_once ODTPHP_PATHTOPCLZIP . '/pclzip.lib.php' ;
2012-04-28 18:21:51 +02:00
$archive = new PclZip ( $outputfile );
$archive -> add ( $inputfile , PCLZIP_OPT_REMOVE_PATH , dirname ( $inputfile ));
//$archive->add($inputfile);
return 1 ;
}
}
if ( $foundhandler )
{
$fp = fopen ( $outputfile , " w " );
fwrite ( $fp , $compressdata );
fclose ( $fp );
return 1 ;
}
else
{
dol_syslog ( " Try to zip with format " . $mode . " with no handler for this format " , LOG_ERR );
return - 2 ;
}
2011-10-14 16:02:18 +02:00
}
catch ( Exception $e )
{
global $langs , $errormsg ;
$langs -> load ( " errors " );
dol_syslog ( " Failed to open file " . $outputfile , LOG_ERR );
$errormsg = $langs -> trans ( " ErrorFailedToWriteInDir " );
return - 1 ;
}
}
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
*/
function dol_uncompress ( $inputfile , $outputdir )
{
global $conf ;
if ( defined ( 'ODTPHP_PATHTOPCLZIP' ))
{
2012-08-23 02:04:35 +02:00
include_once ODTPHP_PATHTOPCLZIP . '/pclzip.lib.php' ;
2012-04-28 18:21:51 +02:00
$archive = new PclZip ( $inputfile );
if ( $archive -> extract ( PCLZIP_OPT_PATH , $outputdir ) == 0 ) return array ( 'error' => $archive -> errorInfo ( true ));
else return array ();
}
if ( class_exists ( 'ZipArchive' ))
{
$zip = new ZipArchive ;
$res = $zip -> open ( $inputfile );
if ( $res === TRUE )
{
$zip -> extractTo ( $outputdir . '/' );
$zip -> close ();
return array ();
}
else
{
2012-04-30 12:19:11 +02:00
return array ( 'error' => 'ErrUnzipFails' );
2012-04-28 18:21:51 +02:00
}
}
2012-04-30 12:19:11 +02:00
return array ( 'error' => 'ErrNoZipEngine' );
2012-04-28 18:21:51 +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
*
* @ param string $dir Directory to scan
2013-03-30 14:04:09 +01:00
* @ param string $regexfilter Regex filter to restrict list . This regex value must be escaped for '/' , since this char is used for preg_match function
* @ param string $excludefilter Array of Regex for exclude filter ( example : array ( '\.meta$' , '^\.' )) . This regex value must be escaped for '/' , since this char is used for preg_match function
2013-05-16 15:46:54 +02:00
* @ param int $nohook Disable all hooks
2013-02-14 13:15:23 +01:00
* @ return string Full path to most recent file
2012-03-16 00:34:41 +01:00
*/
2013-02-14 13:15:23 +01:00
function dol_most_recent_file ( $dir , $regexfilter = '' , $excludefilter = array ( '\.meta$' , '^\.' ), $nohook = false )
2012-03-16 00:34:41 +01:00
{
2013-02-14 13:15:23 +01:00
$tmparray = dol_dir_list ( $dir , 'files' , 0 , $regexfilter , $excludefilter , 'date' , SORT_DESC , '' , $nohook );
2012-03-16 00:34:41 +01:00
return $tmparray [ 0 ];
}
2013-03-30 14:10:15 +01:00
2013-04-22 15:00:29 +02:00
/**
2013-04-25 10:06:54 +02:00
* Security check when accessing to a document ( used by document . php , viewimage . php and webservices )
*
2013-07-12 15:10:32 +02:00
* @ param string $modulepart Module of document ( module , module_user_temp , module_user or module_temp )
2013-04-25 10:06:54 +02:00
* @ param string $original_file Relative path with filename
2013-04-25 10:22:08 +02:00
* @ param string $entity Restrict onto entity
2013-06-25 23:22:00 +02:00
* @ param User $fuser User object ( forced )
2013-05-16 15:46:54 +02:00
* @ param string $refname Ref of object to check permission for external users ( autodetect if not provided )
2013-04-25 10:06:54 +02:00
* @ return mixed Array with access information : accessallowed & sqlprotectagainstexternals & original_file ( as full path name )
2013-04-22 15:00:29 +02:00
*/
2013-06-25 23:22:00 +02:00
function dol_check_secure_access_document ( $modulepart , $original_file , $entity , $fuser = '' , $refname = '' )
2013-04-22 15:00:29 +02:00
{
2013-05-16 15:46:54 +02:00
global $user , $conf , $db ;
2013-04-25 10:06:54 +02:00
2013-06-25 15:09:51 +02:00
if ( ! is_object ( $fuser )) $fuser = $user ;
2013-04-25 10:06:54 +02:00
if ( empty ( $modulepart )) return 'ErrorBadParameter' ;
2013-04-25 10:22:08 +02:00
if ( empty ( $entity )) $entity = 0 ;
2013-06-26 02:05:18 +02:00
dol_syslog ( 'modulepart=' . $modulepart . ' original_file= ' . $original_file );
2013-04-25 10:06:54 +02:00
// We define $accessallowed and $sqlprotectagainstexternals
2013-04-22 15:00:29 +02:00
$accessallowed = 0 ;
$sqlprotectagainstexternals = '' ;
$ret = array ();
2013-04-25 10:06:54 +02:00
2013-06-05 16:24:32 +02:00
// find the subdirectory name as the reference
2013-05-16 15:46:54 +02:00
if ( empty ( $refname )) $refname = basename ( dirname ( $original_file ) . " / " );
2013-06-25 23:22:00 +02:00
2013-04-25 10:06:54 +02:00
// Wrapping for some images
2013-06-05 16:24:32 +02:00
if ( $modulepart == 'companylogo' )
{
$accessallowed = 1 ;
$original_file = $conf -> mycompany -> dir_output . '/logos/' . $original_file ;
}
// Wrapping for users photos
elseif ( $modulepart == 'userphoto' )
{
$accessallowed = 1 ;
$original_file = $conf -> user -> dir_output . '/' . $original_file ;
}
// Wrapping for members photos
elseif ( $modulepart == 'memberphoto' )
{
$accessallowed = 1 ;
$original_file = $conf -> adherent -> dir_output . '/' . $original_file ;
}
// Wrapping pour les apercu factures
elseif ( $modulepart == 'apercufacture' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> facture -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> facture -> dir_output . '/' . $original_file ;
}
// Wrapping pour les apercu propal
elseif ( $modulepart == 'apercupropal' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> propale -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> propal -> dir_output . '/' . $original_file ;
}
// Wrapping pour les apercu commande
elseif ( $modulepart == 'apercucommande' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> commande -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> commande -> dir_output . '/' . $original_file ;
}
// Wrapping pour les apercu intervention
elseif ( $modulepart == 'apercufichinter' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> ficheinter -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> ficheinter -> dir_output . '/' . $original_file ;
}
// Wrapping pour les images des stats propales
elseif ( $modulepart == 'propalstats' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> propale -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> propal -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les images des stats commandes
elseif ( $modulepart == 'orderstats' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> commande -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> commande -> dir_temp . '/' . $original_file ;
}
elseif ( $modulepart == 'orderstatssupplier' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> fournisseur -> commande -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> fournisseur -> dir_output . '/commande/temp/' . $original_file ;
}
// Wrapping pour les images des stats factures
elseif ( $modulepart == 'billstats' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> facture -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> facture -> dir_temp . '/' . $original_file ;
}
elseif ( $modulepart == 'billstatssupplier' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> fournisseur -> facture -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> fournisseur -> dir_output . '/facture/temp/' . $original_file ;
}
// Wrapping pour les images des stats expeditions
elseif ( $modulepart == 'expeditionstats' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> expedition -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> expedition -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les images des stats expeditions
elseif ( $modulepart == 'tripsexpensesstats' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> deplacement -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> deplacement -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les images des stats expeditions
elseif ( $modulepart == 'memberstats' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> adherent -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> adherent -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les images des stats produits
elseif ( preg_match ( '/^productstats_/i' , $modulepart ))
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> produit -> lire || $fuser -> rights -> service -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = ( ! empty ( $conf -> product -> multidir_temp [ $entity ]) ? $conf -> product -> multidir_temp [ $entity ] : $conf -> service -> multidir_temp [ $entity ]) . '/' . $original_file ;
}
// Wrapping for products or services
elseif ( $modulepart == 'tax' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> tax -> charges -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> tax -> dir_output . '/' . $original_file ;
}
// Wrapping for products or services
elseif ( $modulepart == 'actions' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> agenda -> myactions -> read ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> agenda -> dir_output . '/' . $original_file ;
}
// Wrapping for categories
elseif ( $modulepart == 'category' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> categorie -> lire ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> categorie -> multidir_output [ $entity ] . '/' . $original_file ;
}
// Wrapping pour les prelevements
elseif ( $modulepart == 'prelevement' )
2013-05-07 16:50:27 +02:00
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> prelevement -> bons -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-05-07 16:50:27 +02:00
{
$accessallowed = 1 ;
}
2013-06-05 16:24:32 +02:00
$original_file = $conf -> prelevement -> dir_output . '/' . $original_file ;
}
// Wrapping pour les graph energie
elseif ( $modulepart == 'graph_stock' )
{
$accessallowed = 1 ;
$original_file = $conf -> stock -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les graph fournisseurs
elseif ( $modulepart == 'graph_fourn' )
{
$accessallowed = 1 ;
$original_file = $conf -> fournisseur -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les graph des produits
elseif ( $modulepart == 'graph_product' )
{
$accessallowed = 1 ;
$original_file = $conf -> product -> multidir_temp [ $entity ] . '/' . $original_file ;
}
// Wrapping pour les code barre
elseif ( $modulepart == 'barcode' )
{
$accessallowed = 1 ;
// If viewimage is called for barcode, we try to output an image on the fly,
// with not build of file on disk.
//$original_file=$conf->barcode->dir_temp.'/'.$original_file;
$original_file = '' ;
}
// Wrapping pour les icones de background des mailings
elseif ( $modulepart == 'iconmailing' )
{
$accessallowed = 1 ;
$original_file = $conf -> mailing -> dir_temp . '/' . $original_file ;
}
// Wrapping pour les icones de background des mailings
elseif ( $modulepart == 'scanner_user_temp' )
{
$accessallowed = 1 ;
2013-06-25 15:09:51 +02:00
$original_file = $conf -> scanner -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2013-06-05 16:24:32 +02:00
}
// Wrapping pour les images fckeditor
elseif ( $modulepart == 'fckeditor' )
{
$accessallowed = 1 ;
$original_file = $conf -> fckeditor -> dir_output . '/' . $original_file ;
}
2013-04-25 10:06:54 +02:00
// Wrapping for third parties
else if ( $modulepart == 'company' || $modulepart == 'societe' )
2013-04-22 15:00:29 +02:00
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> societe -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> societe -> multidir_output [ $entity ] . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT rowid as fk_soc FROM " . MAIN_DB_PREFIX . " societe WHERE rowid=' " . $db -> escape ( $refname ) . " ' AND entity IN ( " . getEntity ( 'societe' , 1 ) . " ) " ;
2013-04-25 10:06:54 +02:00
}
// Wrapping for invoices
else if ( $modulepart == 'facture' || $modulepart == 'invoice' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> facture -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> facture -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " facture WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2013-04-25 10:06:54 +02:00
}
else if ( $modulepart == 'unpaid' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> facture -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> facture -> dir_output . '/unpaid/temp/' . $original_file ;
}
// Wrapping pour les fiches intervention
else if ( $modulepart == 'ficheinter' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> ficheinter -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02: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 ;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les deplacements et notes de frais
else if ( $modulepart == 'deplacement' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> deplacement -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02: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;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les propales
else if ( $modulepart == 'propal' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> propale -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> propal -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " propal WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les commandes
else if ( $modulepart == 'commande' || $modulepart == 'order' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> commande -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> 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 WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les projets
else if ( $modulepart == 'project' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> projet -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> projet -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " projet WHERE ref=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les commandes fournisseurs
else if ( $modulepart == 'commande_fournisseur' || $modulepart == 'order_supplier' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> fournisseur -> commande -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02: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 ;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les factures fournisseurs
else if ( $modulepart == 'facture_fournisseur' || $modulepart == 'invoice_supplier' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> fournisseur -> facture -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> fournisseur -> facture -> dir_output . '/' . $original_file ;
2013-05-16 15:46:54 +02:00
$sqlprotectagainstexternals = " SELECT fk_soc as fk_soc FROM " . MAIN_DB_PREFIX . " facture_fourn WHERE facnumber=' " . $db -> escape ( $refname ) . " ' AND entity= " . $conf -> entity ;
2013-04-25 10:06:54 +02:00
}
// Wrapping pour les rapport de paiements
else if ( $modulepart == 'facture_paiement' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> facture -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-06-25 15:09:51 +02:00
if ( $fuser -> societe_id > 0 ) $original_file = $conf -> facture -> dir_output . '/payments/private/' . $fuser -> id . '/' . $original_file ;
2013-04-25 10:06:54 +02:00
else $original_file = $conf -> facture -> dir_output . '/payments/' . $original_file ;
}
// Wrapping pour les exports de compta
else if ( $modulepart == 'export_compta' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> compta -> ventilation -> creer || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> compta -> dir_output . '/' . $original_file ;
}
// Wrapping pour les expedition
else if ( $modulepart == 'expedition' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> expedition -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> expedition -> dir_output . " /sending/ " . $original_file ;
}
// Wrapping pour les bons de livraison
else if ( $modulepart == 'livraison' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> expedition -> livraison -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> expedition -> dir_output . " /receipt/ " . $original_file ;
}
// Wrapping pour les actions
else if ( $modulepart == 'actions' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> agenda -> myactions -> read || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> agenda -> dir_output . '/' . $original_file ;
}
// Wrapping pour les actions
else if ( $modulepart == 'actionsreport' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> agenda -> allactions -> read || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02: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 ;
}
// Wrapping pour les produits et services
else if ( $modulepart == 'product' || $modulepart == 'produit' || $modulepart == 'service' )
{
2013-06-25 15:09:51 +02:00
if (( $fuser -> rights -> produit -> lire || $fuser -> rights -> service -> lire ) || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
$accessallowed = 1 ;
}
2013-04-25 10:06:54 +02: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 ;
}
// Wrapping pour les contrats
else if ( $modulepart == 'contract' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> contrat -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
$accessallowed = 1 ;
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> contrat -> dir_output . '/' . $original_file ;
}
// Wrapping pour les dons
else if ( $modulepart == 'donation' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> don -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
$accessallowed = 1 ;
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> don -> dir_output . '/' . $original_file ;
}
// Wrapping pour les remises de cheques
else if ( $modulepart == 'remisecheque' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> banque -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> banque -> dir_output . '/bordereau/' . get_exdir ( basename ( $original_file , " .pdf " ), 2 , 1 ) . $original_file ;
}
// Wrapping for export module
else if ( $modulepart == 'export' )
{
// Aucun test necessaire car on force le rep de download sur
// le rep export qui est propre a l'utilisateur
$accessallowed = 1 ;
2013-06-25 15:09:51 +02:00
$original_file = $conf -> export -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2013-04-25 10:06:54 +02:00
}
// Wrapping for import module
else if ( $modulepart == 'import' )
{
// Aucun test necessaire car on force le rep de download sur
// le rep export qui est propre a l'utilisateur
$accessallowed = 1 ;
$original_file = $conf -> import -> dir_temp . '/' . $original_file ;
}
// Wrapping pour l'editeur wysiwyg
else if ( $modulepart == 'editor' )
{
// Aucun test necessaire car on force le rep de download sur
// le rep export qui est propre a l'utilisateur
$accessallowed = 1 ;
$original_file = $conf -> fckeditor -> dir_output . '/' . $original_file ;
}
// Wrapping pour les backups
else if ( $modulepart == 'systemtools' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> admin )
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
2013-04-22 15:00:29 +02:00
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> admin -> dir_output . '/' . $original_file ;
}
// Wrapping for upload file test
else if ( $modulepart == 'admin_temp' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> admin )
2013-04-25 10:06:54 +02:00
$accessallowed = 1 ;
$original_file = $conf -> admin -> dir_temp . '/' . $original_file ;
}
// Wrapping pour BitTorrent
else if ( $modulepart == 'bittorrent' )
{
$accessallowed = 1 ;
$dir = 'files' ;
if ( $type == 'application/x-bittorrent' ) $dir = 'torrents' ;
$original_file = $conf -> bittorrent -> dir_output . '/' . $dir . '/' . $original_file ;
}
// Wrapping pour Foundation module
else if ( $modulepart == 'member' )
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> adherent -> lire || preg_match ( '/^specimen/i' , $original_file ))
2013-04-22 15:00:29 +02:00
{
$accessallowed = 1 ;
}
2013-04-25 10:06:54 +02:00
$original_file = $conf -> adherent -> dir_output . '/' . $original_file ;
}
// Wrapping for Scanner
else if ( $modulepart == 'scanner_user_temp' )
{
$accessallowed = 1 ;
2013-06-25 15:09:51 +02:00
$original_file = $conf -> scanner -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2013-04-25 10:06:54 +02:00
}
// GENERIC Wrapping
// 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
else
{
// Define $accessallowed
2013-06-05 16:24:32 +02:00
if ( preg_match ( '/^([a-z]+)_user_temp$/i' , $modulepart , $reg ))
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> $reg [ 1 ] -> lire || $fuser -> rights -> $reg [ 1 ] -> read || ( $fuser -> rights -> $reg [ 1 ] -> download )) $accessallowed = 1 ;
$original_file = $conf -> $reg [ 1 ] -> dir_temp . '/' . $fuser -> id . '/' . $original_file ;
2013-06-05 16:24:32 +02:00
}
else if ( preg_match ( '/^([a-z]+)_temp$/i' , $modulepart , $reg ))
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> $reg [ 1 ] -> lire || $fuser -> rights -> $reg [ 1 ] -> read || ( $fuser -> rights -> $reg [ 1 ] -> download )) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> $reg [ 1 ] -> dir_temp . '/' . $original_file ;
}
else if ( preg_match ( '/^([a-z]+)_user$/i' , $modulepart , $reg ))
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> $reg [ 1 ] -> lire || $fuser -> rights -> $reg [ 1 ] -> read || ( $fuser -> rights -> $reg [ 1 ] -> download )) $accessallowed = 1 ;
$original_file = $conf -> $reg [ 1 ] -> dir_output . '/' . $fuser -> id . '/' . $original_file ;
2013-06-05 16:24:32 +02:00
}
else
{
$perm = GETPOST ( 'perm' );
$subperm = GETPOST ( 'subperm' );
if ( $perm || $subperm )
{
2013-06-25 15:09:51 +02:00
if (( $perm && ! $subperm && $fuser -> rights -> $modulepart -> $perm ) || ( $perm && $subperm && $fuser -> rights -> $modulepart -> $perm -> $subperm )) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> $modulepart -> dir_output . '/' . $original_file ;
}
else
{
2013-06-25 15:09:51 +02:00
if ( $fuser -> rights -> $modulepart -> lire || $fuser -> rights -> $modulepart -> read ) $accessallowed = 1 ;
2013-06-05 16:24:32 +02:00
$original_file = $conf -> $modulepart -> dir_output . '/' . $original_file ;
}
}
2013-04-25 10:06:54 +02:00
if ( preg_match ( '/^specimen/i' , $original_file )) $accessallowed = 1 ; // If link to a specimen
2013-06-25 15:09:51 +02:00
if ( $fuser -> admin ) $accessallowed = 1 ; // If user is admin
2013-04-25 10:06:54 +02:00
// For modules who wants to manage different levels of permissions for documents
$subPermCategoryConstName = strtoupper ( $modulepart ) . '_SUBPERMCATEGORY_FOR_DOCUMENTS' ;
if ( ! empty ( $conf -> global -> $subPermCategoryConstName ))
2013-04-22 15:00:29 +02:00
{
2013-04-25 10:06:54 +02:00
$subPermCategory = $conf -> global -> $subPermCategoryConstName ;
2013-06-25 15:09:51 +02:00
if ( ! empty ( $subPermCategory ) && (( $fuser -> rights -> $modulepart -> $subPermCategory -> lire ) || ( $fuser -> rights -> $modulepart -> $subPermCategory -> read ) || ( $fuser -> rights -> $modulepart -> $subPermCategory -> download )))
2013-04-22 15:00:29 +02:00
{
$accessallowed = 1 ;
}
}
2013-04-25 10:06:54 +02:00
// Define $sqlprotectagainstexternals for modules who want to protect access using a SQL query.
$sqlProtectConstName = strtoupper ( $modulepart ) . '_SQLPROTECTAGAINSTEXTERNALS_FOR_DOCUMENTS' ;
if ( ! empty ( $conf -> global -> $sqlProtectConstName )) // If module want to define its own $sqlprotectagainstexternals
2013-04-22 15:00:29 +02:00
{
2013-05-16 15:46:54 +02:00
// Example: mymodule__SQLPROTECTAGAINSTEXTERNALS_FOR_DOCUMENTS = "SELECT fk_soc FROM ".MAIN_DB_PREFIX.$modulepart." WHERE ref='".$db->escape($refname)."' AND entity=".$conf->entity;
2013-04-25 10:06:54 +02:00
eval ( '$sqlprotectagainstexternals = "' . $conf -> global -> $sqlProtectConstName . '";' );
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 (
'accessallowed' => $accessallowed ,
'sqlprotectagainstexternals' => $sqlprotectagainstexternals ,
'original_file' => $original_file
);
2013-04-22 15:00:29 +02:00
return $ret ;
}
2008-04-28 22:49:59 +02:00
?>