Ignore:
Timestamp:
Sep 4, 2008, 3:28:34 AM (16 years ago)
Author:
rvelices
Message:
  • bug 854: better checks of directory creations ( local_data_dir, templates_c, tmp etc...)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r2484 r2497  
    186186
    187187/**
    188  * returns an array contening sub-directories, excluding "CVS"
     188 * returns an array contening sub-directories, excluding ".svn"
    189189 *
    190190 * @param string $dir
     
    194194{
    195195  $sub_dirs = array();
    196 
    197196  if ($opendir = opendir($directory))
    198197  {
     
    202201          and $file != '..'
    203202          and is_dir($directory.'/'.$file)
    204           and $file != 'CVS'
    205     and $file != '.svn')
     203          and $file != '.svn')
    206204      {
    207205        array_push($sub_dirs, $file);
    208206      }
    209207    }
     208    closedir($opendir);
    210209  }
    211210  return $sub_dirs;
     211}
     212
     213define('MKGETDIR_NONE', 0);
     214define('MKGETDIR_RECURSIVE', 1);
     215define('MKGETDIR_DIE_ON_ERROR', 2);
     216define('MKGETDIR_PROTECT_INDEX', 4);
     217define('MKGETDIR_PROTECT_HTACCESS', 8);
     218define('MKGETDIR_DEFAULT', 7);
     219/**
     220 * creates directory if not exists; ensures that directory is writable
     221 * @param:
     222 *  string $dir
     223 *  int $flags combination of MKGETDIR_xxx
     224 * @return bool false on error else true
     225 */
     226function mkgetdir($dir, $flags=MKGETDIR_DEFAULT)
     227{
     228  if ( !is_dir($dir) )
     229  {
     230    $umask = umask(0);
     231    $mkd = @mkdir($dir, 0755, ($flags&MKGETDIR_RECURSIVE) ? true:false );
     232    umask($umask);
     233    if ($mkd==false)
     234    {
     235      !($flags&MKGETDIR_DIE_ON_ERROR) or trigger_error( "$dir ".l10n('no_write_access'), E_USER_ERROR);
     236      return false;
     237    }
     238    if( $flags&MKGETDIR_PROTECT_HTACCESS )
     239    {
     240      $file = $dir.'/.htaccess';
     241      file_exists($file) or @file_put_contents( $file, 'deny from all' );
     242    }
     243    if( $flags&MKGETDIR_PROTECT_INDEX )
     244    {
     245      $file = $dir.'/index.htm';
     246      file_exists($file) or @file_put_contents( $file, 'Not allowed!' );
     247    }
     248  }
     249  if ( !is_writable($dir) )
     250  {
     251    if ( !is_writable($dir) )
     252    {
     253      !($flags&MKGETDIR_DIE_ON_ERROR) or trigger_error( "$dir ".l10n('no_write_access'), E_USER_ERROR);
     254      return false;
     255    }
     256  }
     257  return true;
    212258}
    213259
     
    225271{
    226272  $tndir = $dirname.'/thumbnail';
    227   if (!is_dir($tndir))
    228   {
    229     if (!is_writable($dirname))
    230     {
    231       array_push($errors,
    232                  '['.$dirname.'] : '.l10n('no_write_access'));
    233       return false;
    234     }
    235     umask(0000);
    236     mkdir($tndir, 0777);
    237   }
    238 
     273  if (! mkgetdir($tn_dir, MKGETDIR_NONE) )
     274  {
     275    array_push($errors,
     276          '['.$dirname.'] : '.l10n('no_write_access'));
     277    return false;
     278  }
    239279  return $tndir;
    240280}
Note: See TracChangeset for help on using the changeset viewer.