Qual: Move function dol_delete_file and dol_delte_dir into files.lib.php

This commit is contained in:
Laurent Destailleur 2011-03-09 15:48:25 +00:00
parent e9f4879245
commit 7eb1e0b3d2
5 changed files with 127 additions and 129 deletions

View File

@ -100,6 +100,7 @@ class FormMail
function clear_attached_files() function clear_attached_files()
{ {
global $conf,$user; global $conf,$user;
require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
// Set tmp user directory // Set tmp user directory
$vardir=$conf->user->dir_output."/".$user->id; $vardir=$conf->user->dir_output."/".$user->id;

View File

@ -326,6 +326,7 @@ class EcmDirectory // extends CommonObject
function delete($user) function delete($user)
{ {
global $conf, $langs; global $conf, $langs;
require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
$relativepath=$this->getRelativePath(1); // Ex: dir1/dir2/dir3 $relativepath=$this->getRelativePath(1); // Ex: dir1/dir2/dir3

View File

@ -1,5 +1,5 @@
<?php <?php
/* Copyright (C) 2008-2010 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -128,7 +128,6 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil
/** /**
* Fast compare of 2 files identified by their properties ->name, ->date and ->size * Fast compare of 2 files identified by their properties ->name, ->date and ->size
*
* @param $a File 1 * @param $a File 1
* @param $b File 2 * @param $b File 2
* @return int 1, 0, 1 * @return int 1, 0, 1
@ -355,7 +354,6 @@ function dol_filesize($pathoffile)
/** /**
* Return time of a file * Return time of a file
*
* @param $pathoffile * @param $pathoffile
* @return timestamp Time of file * @return timestamp Time of file
*/ */
@ -423,14 +421,14 @@ function dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
} }
/** /**
* \brief Move an uploaded file after some controls. * Move an uploaded file after some controls.
* If there is errors (virus found, antivir in error, bad filename), file is not moved. * 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 src_file Source full path filename ($_FILES['field']['tmp_name'])
* \param dest_file Target full path filename * @param dest_file Target full path filename
* \param allowoverwrite 1=Overwrite target file if it already exists * @param allowoverwrite 1=Overwrite target file if it already exists
* \param disablevirusscan 1=Disable virus scan * @param disablevirusscan 1=Disable virus scan
* \param uploaderrorcode Value of upload error code ($_FILES['field']['error']) * @param uploaderrorcode Value of upload error code ($_FILES['field']['error'])
* \return int >0 if OK, <0 or string if KO * @return int >0 if OK, <0 or string if KO
*/ */
function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0) function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0)
{ {
@ -539,11 +537,91 @@ function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disable
return 1; return 1;
} }
/**
* Remove a file or several files with a mask
* @param file File to delete or mask of file to delete
* @param disableglob Disable usage of glob like *
* @param boolean True if file is deleted, False if error
*/
function dol_delete_file($file,$disableglob=0)
{
//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)
{
$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);
}
}
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);
}
return $ok;
}
/**
* Remove a directory (not recursive, so content must be empty).
* If directory is not empty, return false
* @param file Directory to delete
* @return boolean True if success, false if error
*/
function dol_delete_dir($dir)
{
$dir_osencoded=dol_osencode($dir);
return rmdir($dir_osencoded);
}
/**
* Remove a directory $dir and its subdirectories
* @param file Dir to delete
* @param count Counter to count nb of deleted elements
* @return int Number of files and directory removed
*/
function dol_delete_dir_recursive($dir,$count=0)
{
dol_syslog("functions.lib:dol_delete_dir_recursive ".$dir,LOG_DEBUG);
$dir_osencoded=dol_osencode($dir);
if ($handle = opendir("$dir_osencoded"))
{
while (false !== ($item = readdir($handle)))
{
if (! utf8_check($item)) $item=utf8_encode($item); // should be useless
if ($item != "." && $item != "..")
{
if (is_dir(dol_osencode("$dir/$item")))
{
$count=dol_delete_dir_recursive("$dir/$item",$count);
}
else
{
dol_delete_file("$dir/$item",1);
$count++;
//echo " removing $dir/$item<br>\n";
}
}
}
closedir($handle);
dol_delete_dir($dir);
$count++;
//echo "removing $dir<br>\n";
}
//echo "return=".$count;
return $count;
}
/** /**
* Get and save an upload file (for example after submitting a new file a mail form). * 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. * All information used are in db, conf, langs, user and _FILES.
*
* @param upload_dir Directory to store upload files * @param upload_dir Directory to store upload files
* @param allowoverwrite 1=Allow overwrite existing file * @param allowoverwrite 1=Allow overwrite existing file
* @param donotupdatesession 1=Do no edit _SESSION variable * @param donotupdatesession 1=Do no edit _SESSION variable
@ -602,7 +680,6 @@ function dol_add_file_process($upload_dir,$allowoverwrite=0,$donotupdatesession=
/** /**
* Remove an uploaded file (for example after submitting a new file a mail form). * 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. * All information used are in db, conf, langs, user and _FILES.
*
* @param filenb File nb to delete * @param filenb File nb to delete
* @param donotupdatesession 1=Do no edit _SESSION variable * @param donotupdatesession 1=Do no edit _SESSION variable
* @return string Message with result of upload and store. * @return string Message with result of upload and store.
@ -649,6 +726,8 @@ function dol_remove_file_process($filenb,$donotupdatesession=0)
/** /**
* Convert file to image * Convert file to image
* @param file Input file name
* @param ext Extension of target file
*/ */
function dol_convert_file($file,$ext='png') function dol_convert_file($file,$ext='png')
{ {
@ -662,7 +741,7 @@ function dol_convert_file($file,$ext='png')
if ($ret) if ($ret)
{ {
$count = $image->getNumberImages(); $count = $image->getNumberImages();
$ret = $image->writeImage( $file . "." . $ext, true ); $ret = $image->writeImages( $file . "." . $ext, true );
if ($ret) return $count; if ($ret) return $count;
else return -3; else return -3;
} }

View File

@ -37,6 +37,16 @@ if (! defined('DOL_DOCUMENT_ROOT')) define('DOL_DOCUMENT_ROOT', '..');
if (! defined('DOL_DOCUMENT_ROOT_ALT')) define('DOL_DOCUMENT_ROOT_ALT', ''); // If option not enabled, we keep it disabled but avoid warning if (! defined('DOL_DOCUMENT_ROOT_ALT')) define('DOL_DOCUMENT_ROOT_ALT', ''); // If option not enabled, we keep it disabled but avoid warning
if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/adodbtime/adodb-time.inc.php"); if (! defined('ADODB_DATE_VERSION')) include_once(DOL_DOCUMENT_ROOT."/includes/adodbtime/adodb-time.inc.php");
/**
* This function output memory used by PHP and exit everything. Used for debugging purpose.
*/
function dol_stopwithmem()
{
print memory_get_usage();
llxFooter();
exit;
}
/** /**
* Function called at end of web php process * Function called at end of web php process
*/ */
@ -2437,12 +2447,12 @@ function print_barre_liste($titre, $page, $file, $options='', $sortfield='', $so
} }
/** /**
* \brief Fonction servant a afficher les fleches de navigation dans les pages de listes * Fonction servant a afficher les fleches de navigation dans les pages de listes
* \param page Numero of page * @param page Numero of page
* \param file Lien * @param file Lien
* \param options Autres parametres d'url a propager dans les liens ("" par defaut) * @param options Autres parametres d'url a propager dans les liens ("" par defaut)
* \param nextpage Faut-il une page suivante * @param nextpage Faut-il une page suivante
* \param betweenarraows HTML Content to show between arrows * @param betweenarraows HTML Content to show between arrows
*/ */
function print_fleche_navigation($page,$file,$options='',$nextpage,$betweenarrows='') function print_fleche_navigation($page,$file,$options='',$nextpage,$betweenarrows='')
{ {
@ -2460,94 +2470,12 @@ function print_fleche_navigation($page,$file,$options='',$nextpage,$betweenarrow
/** /**
* Remove a file or several files with a mask * Fonction qui retourne un taux de tva formate pour visualisation
* @param file File to delete or mask of file to delete * Utilisee dans les pdf et les pages html
* @param disableglob Disable usage of glob like * * @param rate Rate value to format (19.6 19,6 19.6% 19,6%,...)
* @param boolean True if file is deleted, False if error * @param foundpercent Add a percent % sign in output
*/ * @param info_bits Miscellanous information on vat
function dol_delete_file($file,$disableglob=0) * @return string Chaine avec montant formate (19,6 ou 19,6% ou 8.5% *)
{
//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)
{
$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);
}
}
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);
}
return $ok;
}
/**
* Remove a directory (not recursive, so content must be empty).
* If directory is not empty, return false
* @param file Directory to delete
* @return boolean True if success, false if error
*/
function dol_delete_dir($dir)
{
$dir_osencoded=dol_osencode($dir);
return rmdir($dir_osencoded);
}
/**
* Remove a directory $dir and its subdirectories
* @param file Dir to delete
* @param count Counter to count nb of deleted elements
* @return int Number of files and directory removed
*/
function dol_delete_dir_recursive($dir,$count=0)
{
dol_syslog("functions.lib:dol_delete_dir_recursive ".$dir,LOG_DEBUG);
$dir_osencoded=dol_osencode($dir);
if ($handle = opendir("$dir_osencoded"))
{
while (false !== ($item = readdir($handle)))
{
if (! utf8_check($item)) $item=utf8_encode($item); // should be useless
if ($item != "." && $item != "..")
{
if (is_dir(dol_osencode("$dir/$item")))
{
$count=dol_delete_dir_recursive("$dir/$item",$count);
}
else
{
dol_delete_file("$dir/$item",1);
$count++;
//echo " removing $dir/$item<br>\n";
}
}
}
closedir($handle);
dol_delete_dir($dir);
$count++;
//echo "removing $dir<br>\n";
}
//echo "return=".$count;
return $count;
}
/**
* \brief Fonction qui retourne un taux de tva formate pour visualisation
* \remarks Fonction utilisee dans les pdf et les pages html
* \param rate Rate value to format (19.6 19,6 19.6% 19,6%,...)
* \param foundpercent Add a percent % sign in output
* \param info_bits Miscellanous information on vat
* \return string Chaine avec montant formate (19,6 ou 19,6% ou 8.5% *)
*/ */
function vatrate($rate,$addpercent=false,$info_bits=0) function vatrate($rate,$addpercent=false,$info_bits=0)
{ {
@ -3528,19 +3456,9 @@ function dol_htmloutput_errors($mesgstring='',$mesgarray='')
return dol_htmloutput_mesg($mesgstring, $mesgarray, 'error'); return dol_htmloutput_mesg($mesgstring, $mesgarray, 'error');
} }
/**
* This function output memory used by PHP and exit everything. Used for debugging purpose.
*/
function stopwithmem()
{
print memory_get_usage();
llxFooter();
exit;
}
/** /**
* \brief Advanced sort array by second index function, which produces * Advanced sort array by second index function, which produces
* ascending (default) or descending output and uses optionally * ascending (default) or descending output and uses optionally
* natural case insensitive sorting (which can be optionally case * natural case insensitive sorting (which can be optionally case
* sensitive as well). * sensitive as well).
@ -3654,7 +3572,6 @@ function dol_getIdFromCode($db,$key,$tablename,$fieldkey='code',$fieldid='id')
/** /**
* Verify if condition in string is ok or not * Verify if condition in string is ok or not
*
* @param string $strRights * @param string $strRights
* @return boolean true or false * @return boolean true or false
*/ */
@ -3686,7 +3603,6 @@ function verifCond($strRights)
/** /**
* Replace eval function to add more security. * Replace eval function to add more security.
* This function is called by verifCond(). * This function is called by verifCond().
*
* @param string $s * @param string $s
* @return int 1 * @return int 1
*/ */
@ -3710,7 +3626,7 @@ function dol_eval($s)
if (! function_exists('glob') && ! is_callable('glob')) if (! function_exists('glob') && ! is_callable('glob'))
{ {
/** /**
* \brief For replace glob() function * To define glob() function if not exists
*/ */
function glob($pattern) function glob($pattern)
{ {
@ -3747,7 +3663,7 @@ if (! function_exists('glob') && ! is_callable('glob'))
} }
/** /**
* \brief For dol_glob() function * For dol_glob() function
*/ */
function pattern_match($pattern,$string) function pattern_match($pattern,$string)
{ {

View File

@ -791,6 +791,7 @@ class Societe extends CommonObject
function delete($id) function delete($id)
{ {
global $user,$langs,$conf; global $user,$langs,$conf;
require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
dol_syslog("Societe::Delete", LOG_DEBUG); dol_syslog("Societe::Delete", LOG_DEBUG);
$sqr = 0; $sqr = 0;