2008-04-28 22:49:59 +02:00
< ? php
2011-03-09 16:48:25 +01:00
/* Copyright ( C ) 2008 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
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
* the Free Software Foundation ; either version 2 of the License , or
* ( 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 /
*/
/**
2008-11-16 02:09:04 +01:00
* \file htdocs / lib / files . lib . php
* \brief Library for file managing functions
*/
2008-04-28 22:49:59 +02:00
2011-06-01 16:05:08 +02:00
/**
* Return user / group account of web server
* @ param $mode 'user' or 'group'
* @ return string
*/
function dol_getwebuser ( $mode )
{
$t = '?' ;
if ( $mode == 'user' ) $t = getenv ( 'APACHE_RUN_USER' ); // $_ENV['APACHE_RUN_USER'] is empty
if ( $mode == 'group' ) $t = getenv ( 'APACHE_RUN_GROUP' );
return $t ;
}
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 " / " .
2010-10-02 01:37:36 +02:00
* @ param $path Starting path from which to search
* @ param $types Can be " directories " , " files " , or " all "
* @ param $recursive Determines whether subdirectories are searched
* @ param $filter Regex for include filter
2011-05-22 22:37:38 +02:00
* @ param $excludefilter Array of Regex for exclude filter ( example : array ( '\.meta$' , '^\.' )
2010-10-03 17:42:01 +02:00
* @ param $sortcriteria Sort criteria ( " " , " name " , " date " , " size " )
2010-10-02 01:37:36 +02:00
* @ param $sortorder Sort order ( SORT_ASC , SORT_DESC )
2010-10-03 17:42:01 +02:00
* @ param $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
2010-10-02 01:37:36 +02: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
*/
function dol_dir_list ( $path , $types = " all " , $recursive = 0 , $filter = " " , $excludefilter = " " , $sortcriteria = " name " , $sortorder = SORT_ASC , $mode = 0 )
{
2009-10-12 20:31:45 +02:00
dol_syslog ( " files.lib.php::dol_dir_list path= " . $path . " types= " . $types . " recursive= " . $recursive . " filter= " . $filter . " excludefilter= " . $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
2009-10-12 20:31:45 +02:00
if ( ! is_dir ( $newpath )) return array ();
2008-04-29 23:13:49 +02:00
2009-10-12 20:31:45 +02:00
if ( $dir = opendir ( $newpath ))
2008-04-29 23:13:49 +02:00
{
$file_list = array ();
while ( false !== ( $file = readdir ( $dir )))
{
2009-12-15 12:02:01 +01:00
if ( ! utf8_check ( $file )) $file = utf8_encode ( $file ); // To be sure data is stored in utf8 in memory
2009-10-04 17:52:16 +02:00
2008-04-29 23:13:49 +02:00
$qualified = 1 ;
2009-02-24 23:52:55 +01:00
2011-05-22 22:37:38 +02:00
// 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 ; }
}
2009-02-24 23:52:55 +01:00
2008-04-29 23:13:49 +02:00
if ( $qualified )
{
2011-04-17 20:52:14 +02:00
$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 ))
2008-04-29 23:13:49 +02:00
{
// Add entry into file_list array
2011-04-17 20:52:14 +02:00
if (( $types == " directories " ) || ( $types == " all " ))
{
if ( $loaddate || $sortcriteria == 'date' ) $filedate = dol_filemtime ( $path . " / " . $file );
if ( $loadsize || $sortcriteria == 'size' ) $filesize = dol_filesize ( $path . " / " . $file );
2009-02-24 23:52:55 +01:00
2011-04-17 20:52:14 +02:00
if ( ! $filter || preg_match ( '/' . $filter . '/i' , $path . '/' . $file ))
{
$file_list [] = array (
" name " => $file ,
" fullname " => $path . '/' . $file ,
" date " => $filedate ,
" size " => $filesize ,
" type " => 'dir'
);
}
}
2009-02-24 23:52:55 +01:00
2008-04-29 23:13:49 +02:00
// if we're in a directory and we want recursive behavior, call this function again
if ( $recursive )
{
2011-04-17 20:52:14 +02:00
$file_list = array_merge ( $file_list , dol_dir_list ( $path . " / " . $file , $types , $recursive , $filter , $excludefilter , $sortcriteria , $sortorder , $mode ));
2008-04-29 23:13:49 +02:00
}
}
2011-04-17 20:52:14 +02:00
else if ( ! $isdir && (( $types == " files " ) || ( $types == " all " )))
2008-04-29 23:13:49 +02:00
{
2011-04-17 20:52:14 +02:00
// Add file into file_list array
2009-10-12 20:31:45 +02:00
if ( $loaddate || $sortcriteria == 'date' ) $filedate = dol_filemtime ( $path . " / " . $file );
if ( $loadsize || $sortcriteria == 'size' ) $filesize = dol_filesize ( $path . " / " . $file );
2010-10-03 17:50:34 +02:00
2009-10-23 12:44:19 +02:00
if ( ! $filter || preg_match ( '/' . $filter . '/i' , $path . '/' . $file ))
2008-04-29 23:13:49 +02:00
{
2011-04-17 20:52:14 +02:00
$file_list [] = array (
2008-04-29 23:13:49 +02:00
" name " => $file ,
" fullname " => $path . '/' . $file ,
" date " => $filedate ,
2009-06-30 03:06:10 +02:00
" size " => $filesize ,
" type " => 'file'
2008-04-29 23:13:49 +02:00
);
}
}
}
}
closedir ( $dir );
2009-02-24 23:52:55 +01:00
2008-04-29 23:13:49 +02:00
// Obtain a list of columns
$myarray = array ();
foreach ( $file_list as $key => $row )
{
$myarray [ $key ] = $row [ $sortcriteria ];
}
// Sort the data
2010-10-03 17:42:01 +02:00
if ( $sortorder ) array_multisort ( $myarray , $sortorder , $file_list );
2009-02-24 23:52:55 +01:00
2008-04-29 23:13:49 +02:00
return $file_list ;
}
else
{
2009-10-12 20:31:45 +02:00
return array ();
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
* @ param $a File 1
* @ param $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
*
2010-11-13 15:34:06 +01:00
* @ param file Filename we looking for MIME type
2010-08-18 16:48:17 +02:00
* @ param default Default mime type if extension not found in known list
2011-02-07 15:23:57 +01:00
* @ param mode 0 = Return full mime , 1 = otherwise short mime string , 2 = image for mime type , 3 = source language
2010-08-18 16:48:17 +02:00
* @ return string Return a mime type family
* ( text / xxx , application / xxx , image / xxx , audio , video , archive )
2011-07-06 11:25:05 +02:00
* @ 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' ; }
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 ( '/\.xls(b|m|x)?$/i' , $tmpfile )) { $mime = 'application/vnd.ms-excel' ; $imgmime = 'xls.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-09-14 00:19:57 +02:00
if ( preg_match ( '/\.xsl(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 );
return $tmp [ 1 ];
}
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
/**
2010-09-29 15:14:48 +02:00
* Test if filename is a directory
2011-08-26 19:59:14 +02:00
*
2010-09-29 15:14:48 +02:00
* @ param 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
*
2010-09-29 15:14:48 +02:00
* @ param $pathoffile
* @ return boolean True or false
*/
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
*
* @ param $url
* @ return boolean True or false
*/
function dol_is_url ( $url )
{
$tmpprot = array ( 'file' , 'http' , 'ftp' , 'zlib' , 'data' , 'ssh2' , 'ogg' , 'expect' );
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
*
2010-09-29 15:14:48 +02:00
* @ param 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 );
while (( gettype ( $name = readdir ( $handle )) != " boolean " ))
{
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
* @ param 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 );
$nb ++ ;
}
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
* @ param $pathoffile
* @ return string File size
*/
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
* @ param $pathoffile
* @ return timestamp Time of file
*/
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
/**
2010-02-10 18:44:25 +01:00
* Copy a file to another file
2010-04-28 17:52:32 +02:00
* @ param $srcfile Source file ( can ' t be a directory )
* @ param $destfile Destination file ( can ' t be a directory )
2010-03-21 21:29:29 +01:00
* @ param $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK )
* @ param $overwriteifexists Overwrite file if exists ( 1 by default )
* @ return boolean True if OK , false if KO
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 ;
2010-03-21 21:29:29 +01:00
$result = false ;
2010-02-13 22:26:52 +01:00
2010-10-03 18:59:24 +02:00
dol_syslog ( " files.lib.php::dol_copy srcfile= " . $srcfile . " destfile= " . $destfile . " newmask= " . $newmask . " overwritifexists= " . $overwriteifexists );
2010-03-21 21:29:29 +01:00
if ( $overwriteifexists || ! dol_is_file ( $destfile ))
{
2010-10-03 18:59:24 +02:00
$newpathofsrcfile = dol_osencode ( $srcfile );
$newpathofdestfile = dol_osencode ( $destfile );
$result =@ copy ( $newpathofsrcfile , $newpathofdestfile );
2010-04-28 17:52:32 +02:00
//$result=copy($srcfile, $destfile); // To see errors, remove @
if ( ! $result ) dol_syslog ( " files.lib.php::dol_copy failed " , LOG_WARNING );
2010-03-21 21:29:29 +01:00
if ( empty ( $newmask ) && ! empty ( $conf -> global -> MAIN_UMASK )) $newmask = $conf -> global -> MAIN_UMASK ;
2010-10-06 01:27:09 +02:00
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
2010-03-21 21:29:29 +01:00
}
2010-02-13 22:26:52 +01:00
2010-02-10 19:04:03 +01:00
return $result ;
2009-12-15 21:45:21 +01:00
}
2009-10-04 19:18:09 +02:00
2010-10-03 18:59:24 +02:00
/**
* Move a file into another name
* @ param $srcfile Source file ( can ' t be a directory )
* @ param $destfile Destination file ( can ' t be a directory )
* @ param $newmask Mask for new file ( 0 by default means $conf -> global -> MAIN_UMASK )
* @ param $overwriteifexists Overwrite file if exists ( 1 by default )
* @ return boolean True if OK , false if KO
*/
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
/**
2011-03-09 16:48:25 +01:00
* Move an uploaded file after some controls .
* If there is errors ( virus found , antivir in error , bad filename ), file is not moved .
* @ param src_file Source full path filename ( $_FILES [ 'field' ][ 'tmp_name' ])
* @ param dest_file Target full path filename
* @ param allowoverwrite 1 = Overwrite target file if it already exists
* @ param disablevirusscan 1 = Disable virus scan
* @ param uploaderrorcode Value of upload error code ( $_FILES [ 'field' ][ 'error' ])
2011-07-05 18:10:56 +02:00
* @ param notrigger Disable all triggers
2011-03-09 16:48:25 +01:00
* @ return int > 0 if OK , < 0 or string if KO
2010-05-01 16:28:48 +02:00
*/
2011-07-05 18:10:56 +02:00
function dol_move_uploaded_file ( $src_file , $dest_file , $allowoverwrite , $disablevirusscan = 0 , $uploaderrorcode = 0 , $notrigger = 0 )
2010-05-01 16:28:48 +02:00
{
2011-07-06 11:25:05 +02:00
global $conf , $user , $langs , $db ;
2011-07-05 18:10:56 +02:00
global $object ;
2010-05-01 16:28:48 +02:00
$file_name = $dest_file ;
// 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
2011-07-06 11:25:05 +02:00
if ( empty ( $disablevirusscan ) && file_exists ( $src_file ) && ! empty ( $conf -> global -> MAIN_ANTIVIRUS_COMMAND ))
2010-05-01 16:28:48 +02:00
{
require_once ( DOL_DOCUMENT_ROOT . '/lib/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 ;
2010-09-07 20:18:18 +02:00
dol_syslog ( 'Functions.lib::dol_move_uploaded_file File "' . $src_file . '" (target name "' . $file_name . '") KO with antivirus: result=' . $result . ' errors=' . join ( ',' , $antivir -> errors ), LOG_WARNING );
2010-05-01 16:28:48 +02:00
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' , $file_name ))
{
$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 ;
}
// 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 ( " Functions.lib::dol_move_uploaded_file File " . $file_name . " already exists " , 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 ( " Functions.lib::dol_move_uploaded_file Success to move " . $src_file . " to " . $file_name . " - Umask= " . $conf -> global -> MAIN_UMASK , LOG_DEBUG );
2011-07-06 11:25:05 +02:00
2011-07-11 08:23:22 +02:00
if ( ! $notrigger && is_object ( $object ))
2011-07-05 18:10:56 +02:00
{
$object -> src_file = $dest_file ;
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /core/class/interfaces.class.php " );
$interface = new Interfaces ( $db );
$result = $interface -> run_triggers ( 'FILE_UPLOAD' , $object , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $errors = $interface -> errors ; }
// Fin appel triggers
}
2011-07-06 11:25:05 +02:00
2010-05-01 16:28:48 +02:00
return 1 ; // Success
}
else
{
dol_syslog ( " Functions.lib::dol_move_uploaded_file Failed to move " . $src_file . " to " . $file_name , LOG_ERR );
return - 3 ; // Unknown error
}
return 1 ;
}
2011-03-09 16:48:25 +01:00
/**
* Remove a file or several files with a mask
2011-09-17 02:04:44 +02:00
*
2011-03-09 16:48:25 +01:00
* @ param file File to delete or mask of file to delete
* @ param disableglob Disable usage of glob like *
2011-06-15 12:35:29 +02:00
* @ param nophperrors Disable all PHP output errors
2011-07-05 18:10:56 +02:00
* @ param notrigger Disable all triggers
2011-07-14 07:21:52 +02:00
* @ param triggercode Code of trigger TODO ? ? ? ? why ?
2011-07-13 23:33:38 +02:00
* @ param object Object for trigger
2011-05-25 15:27:07 +02:00
* @ return boolean True if file is deleted , False if error
2011-03-09 16:48:25 +01:00
*/
2011-07-13 23:33:38 +02:00
function dol_delete_file ( $file , $disableglob = 0 , $nophperrors = 0 , $notrigger = 0 , $triggercode = 'FILE_DELETE' , $object = null )
2011-03-09 16:48:25 +01:00
{
2011-07-13 23:33:38 +02:00
global $db , $conf , $user , $langs ;
2011-07-06 11:25:05 +02:00
2011-03-09 16:48:25 +01:00
//print "x".$file." ".$disableglob;
$ok = true ;
$file_osencoded = dol_osencode ( $file ); // New filename encoded in OS filesystem encoding charset
if ( empty ( $disableglob ))
{
foreach ( glob ( $file_osencoded ) as $filename )
{
2011-06-15 12:35:29 +02:00
if ( $nophperrors ) $ok =@ unlink ( $filename ); // The unlink encapsulated by dolibarr
else $ok = unlink ( $filename ); // The unlink encapsulated by dolibarr
2011-07-05 18:10:56 +02:00
if ( $ok )
{
dol_syslog ( " Removed file " . $filename , LOG_DEBUG );
if ( ! $notrigger )
{
2011-07-13 23:33:38 +02:00
if ( ! is_object ( $object )) $object = ( object ) 'dummy' ;
2011-07-05 18:10:56 +02:00
$object -> src_file = $file ;
2011-07-06 11:25:05 +02:00
2011-07-05 18:10:56 +02:00
// Appel des triggers
include_once ( DOL_DOCUMENT_ROOT . " /core/class/interfaces.class.php " );
$interface = new Interfaces ( $db );
2011-07-13 23:33:38 +02:00
$result = $interface -> run_triggers ( $triggercode , $object , $user , $langs , $conf );
2011-07-05 18:10:56 +02:00
if ( $result < 0 ) { $error ++ ; $errors = $interface -> errors ; }
// Fin appel triggers
}
}
2011-03-09 16:48:25 +01:00
else dol_syslog ( " Failed to remove file " . $filename , LOG_WARNING );
}
}
else
{
2011-06-15 12:35:29 +02:00
if ( $nophperrors ) $ok =@ unlink ( $file_osencoded ); // The unlink encapsulated by dolibarr
else $ok = unlink ( $file_osencoded ); // The unlink encapsulated by dolibarr
2011-03-09 16:48:25 +01:00
if ( $ok ) dol_syslog ( " Removed file " . $file_osencoded , LOG_DEBUG );
else dol_syslog ( " Failed to remove file " . $file_osencoded , LOG_WARNING );
}
return $ok ;
}
/**
* Remove a directory ( not recursive , so content must be empty ) .
* If directory is not empty , return false
2011-09-17 02:04:44 +02:00
*
2011-05-25 15:27:07 +02:00
* @ param dir Directory to delete
2011-06-15 12:35:29 +02:00
* @ param nophperrors Disable all PHP output errors
2011-03-09 16:48:25 +01:00
* @ return boolean True if success , false if error
*/
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
2011-05-25 15:27:07 +02:00
* @ param dir Dir to delete
2011-03-09 16:48:25 +01:00
* @ param count Counter to count nb of deleted elements
2011-06-15 12:35:29 +02:00
* @ param nophperrors Disable all PHP output errors
2011-03-09 16:48:25 +01:00
* @ return int Number of files and directory removed
*/
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-09-17 02:04:44 +02:00
/**
* Delete all preview files linked to object instance
*
* @ param Object $object Object to clean
* @ return int 0 if error , 1 if OK
*/
function dol_delete_preview ( $object )
{
global $langs , $conf ;
require_once ( DOL_DOCUMENT_ROOT . " /lib/files.lib.php " );
if ( $object -> element == 'commande' ) $dir = $conf -> commande -> dir_output ;
if ( empty ( $dir )) return 'ErrorObjectNoSupportedByFunction' ;
$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 ;
}
2010-05-01 16:28:48 +02:00
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 .
* @ param upload_dir Directory to store upload files
* @ param allowoverwrite 1 = Allow overwrite existing file
* @ param donotupdatesession 1 = Do no edit _SESSION variable
* @ return string Message with result of upload and store .
*/
function dol_add_file_process ( $upload_dir , $allowoverwrite = 0 , $donotupdatesession = 0 )
{
global $db , $user , $conf , $langs , $_FILES ;
$mesg = '' ;
if ( ! empty ( $_FILES [ 'addedfile' ][ 'tmp_name' ]))
{
2011-07-10 22:03:38 +02:00
if ( dol_mkdir ( $upload_dir ) >= 0 )
2010-05-12 13:29:21 +02:00
{
$resupload = dol_move_uploaded_file ( $_FILES [ 'addedfile' ][ 'tmp_name' ], $upload_dir . " / " . $_FILES [ 'addedfile' ][ 'name' ], $allowoverwrite , 0 , $_FILES [ 'addedfile' ][ 'error' ]);
if ( is_numeric ( $resupload ) && $resupload > 0 )
{
$mesg = '<div class="ok">' . $langs -> trans ( " FileTransferComplete " ) . '</div>' ;
if ( empty ( $donotupdatesession ))
{
include_once ( DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' );
$formmail = new FormMail ( $db );
$formmail -> add_attached_files ( $upload_dir . " / " . $_FILES [ 'addedfile' ][ 'name' ], $_FILES [ 'addedfile' ][ 'name' ], $_FILES [ 'addedfile' ][ 'type' ]);
}
}
else
{
$langs -> load ( " errors " );
if ( $resupload < 0 ) // Unknown error
{
$mesg = '<div class="error">' . $langs -> trans ( " ErrorFileNotUploaded " ) . '</div>' ;
}
else if ( preg_match ( '/ErrorFileIsInfectedWithAVirus/' , $resupload )) // Files infected by a virus
{
$mesg = '<div class="error">' . $langs -> trans ( " ErrorFileIsInfectedWithAVirus " ) . '</div>' ;
}
else // Known error
{
$mesg = '<div class="error">' . $langs -> trans ( $resupload ) . '</div>' ;
}
}
}
}
else
{
$langs -> load ( " errors " );
$mesg = '<div class="warning">' . $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " File " )) . '</div>' ;
}
return $mesg ;
}
/**
* 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 .
* @ param filenb File nb to delete
2011-07-10 22:03:38 +02:00
* @ param donotupdatesession 1 = Do not edit _SESSION variable
* @ param donotdeletefile 1 = Do not delete physically file
2010-05-12 13:29:21 +02:00
* @ return string Message with result of upload and store .
*/
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 ;
$mesg = '' ;
$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 )
{
2011-07-10 22:03:38 +02:00
if ( empty ( $donotdeletefile ))
{
$langs -> load ( " other " );
$mesg = '<div class="ok">' . $langs -> trans ( " FileWasRemoved " , $filetodelete ) . '</div>' ;
//print_r($_FILES);
}
2010-05-12 13:29:21 +02:00
if ( empty ( $donotupdatesession ))
{
include_once ( DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' );
$formmail = new FormMail ( $db );
$formmail -> remove_attached_files ( $keytodelete );
}
}
}
return $mesg ;
}
2011-03-04 14:54:47 +01:00
/**
* Convert file to image
2011-03-09 16:48:25 +01:00
* @ param file Input file name
* @ param ext Extension of target file
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-03-09 16:48:25 +01:00
2011-03-04 14:54:47 +01:00
return 1 ;
}
2008-04-28 22:49:59 +02:00
?>