Changeset 2497


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...)
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/feed.php

    r2339 r2497  
    196196
    197197$fileName= $conf['local_data_dir'].'/tmp';
    198 @mkdir($fileName); // just in case
     198mkgetdir($fileName); // just in case
    199199$fileName.='/feed.xml';
    200200// send XML feed
  • 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}
  • trunk/include/functions_mail.inc.php

    r2479 r2497  
    796796    global $conf, $user, $lang_info;
    797797    $dir = $conf['local_data_dir'].'/tmp';
    798     @mkdir( $dir );
    799     $filename = $dir.'/mail.'.$user['username'].'.'.$lang_info['code'].'.'.$args['template'].'.'.$args['theme'];
    800     if ($args['content_format'] == 'text/plain')
    801     {
    802       $filename .= '.txt';
    803     }
    804     else
    805     {
    806       $filename .= '.html';
    807     }
    808     $file = fopen($filename, 'w+');
    809     fwrite($file, $to ."\n");
    810     fwrite($file, $subject ."\n");
    811     fwrite($file, $headers);
    812     fwrite($file, $content);
    813     fclose($file);
     798    if ( mkgetdir( $dir,  MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) )
     799    {
     800      $filename = $dir.'/mail.'.$user['username'].'.'.$lang_info['code'].'.'.$args['template'].'.'.$args['theme'];
     801      if ($args['content_format'] == 'text/plain')
     802      {
     803        $filename .= '.txt';
     804      }
     805      else
     806      {
     807        $filename .= '.html';
     808      }
     809      $file = fopen($filename, 'w+');
     810      fwrite($file, $to ."\n");
     811      fwrite($file, $subject ."\n");
     812      fwrite($file, $headers);
     813      fwrite($file, $content);
     814      fclose($file);
     815    }
    814816    return $result;
    815817}
  • trunk/include/template.class.php

    r2489 r2497  
    5454    $this->smarty->debugging = $conf['debug_template'];
    5555
    56     if ( isset($conf['compiled_template_dir'] ) )
    57     {
    58       $compile_dir = $conf['compiled_template_dir'];
    59     }
    60     else
    61     {
    62       $compile_dir = $conf['local_data_dir'];
    63       if ( !is_dir($compile_dir) )
    64       {
    65         mkdir( $compile_dir, 0777);
    66         file_put_contents($compile_dir.'/index.htm', '');
    67       }
    68       $compile_dir .= '/templates_c';
    69     }
    70     if ( !is_dir($compile_dir) )
    71     {
    72       mkdir( $compile_dir, 0777 );
    73       file_put_contents($compile_dir.'/index.htm', '');
    74     }
     56    $compile_dir = $conf['local_data_dir'].'/templates_c';
     57    mkgetdir( $compile_dir );
    7558
    7659    $this->smarty->compile_dir = $compile_dir;
     
    124107      $this->smarty->clear_compiled_tpl();
    125108      $this->smarty->compile_id = $save_compile_id;
    126       file_put_contents($this->smarty->compile_dir.'/index.htm', '');
     109      file_put_contents($this->smarty->compile_dir.'/index.htm', 'Not allowed!');
    127110  }
    128111
  • trunk/plugins/event_tracer/main.inc.php

    r2342 r2497  
    3737  var $me_working;
    3838  var $my_config;
    39  
     39
    4040  function EventTracer()
    4141  {
     
    7575  {
    7676    $dir = $this->get_config_file_dir();
    77     @mkdir($dir);
     77    @mkgetdir($dir);
    7878    $file = fopen( $dir.$this->get_config_file_name(), 'w' );
    7979    fwrite($file, serialize($this->my_config) );
Note: See TracChangeset for help on using the changeset viewer.