Ignore:
Timestamp:
2013-05-29T12:46:27+12:00 (11 years ago)
Author:
jmt12
Message:

Adding a (hopefully) safe recursive delete, and removed some debugging comments from mkAllDirs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • documentation/trunk/php/common.php

    r25051 r27506  
    22
    33$base_path = getcwd();
     4if (strpos($base_path, 'greenstone') === false)
     5{
     6  printError('Failed to determine base path...');
     7}
    48
    59// The dokuwiki directory is always in the same place, relative to the PHP
     
    113117/** generateID($text) **/
    114118
    115 function mkAllDir($destination_dir, $mode)
    116 {
    117   echo '<p> * Make all directories: ' . $destination_dir . '</p>' . "\n";
     119function mkAllDir($destination_dir)
     120{
     121  ///cho '<p> * Make all directories: ' . $destination_dir . '</p>' . "\n";
    118122  $dirs_to_create = array();
    119123  $dir = $destination_dir;
    120124  while (!empty($dir) && !file_exists($dir))
    121125  {
    122     echo 'Does not exist - create...<br/>';
     126    ///cho 'Does not exist - create...<br/>';
    123127    array_unshift($dirs_to_create, $dir);
    124128    $dir = substr($dir, 0, strrpos($dir, '/'));
    125     echo 'Testing for the existance of: ' . $dir . '<br/>';
     129    ///cho 'Testing for the existance of: ' . $dir . '<br/>';
    126130  }
    127131  foreach ($dirs_to_create as $dir)
     
    129133    if (!file_exists($dir))
    130134    {
    131       mkdir($dir, 0755);
     135      mkdir($dir, 0775);
    132136    }
    133137  }
     
    151155        $_REQUEST[$matches[1]] = $_SERVER['argv'][$i];
    152156      }
     157      else if (preg_match('/^\-+l/i', $_SERVER['argv'][$i]))
     158      {
     159        $i++;
     160        $_REQUEST['l'] = $_SERVER['argv'][$i];
     161      }
    153162      else
    154163      {
     
    177186/** printError($message) **/
    178187
     188
     189/**
     190 */
     191function recursiveRemove($dir, $base_dir, $remove_this_dir=false)
     192{
     193  ///cho "<h2>recursiveRemove(\"$dir\", \"$base_dir\", $remove_this_dir)</h2>";
     194  // Ensure that base_dir is within_dir - safety first!
     195  if (strpos($dir, $base_dir) === false)
     196  {
     197    printError('Can\'t delete directory as is isn\'t within project');
     198  }
     199  if (is_dir($dir))
     200  {
     201    if ($dh = opendir($dir))
     202    {
     203      while (($file = readdir($dh)) !== false)
     204      {
     205    if (preg_match('/^\./', $file))
     206    {
     207      // Skip dot files
     208    }
     209    else
     210    {
     211      $path = fileCat(array($dir, $file));
     212      if (is_dir($path))
     213        {
     214          recursiveRemove($path, $base_dir, true);
     215        }
     216      else
     217        {
     218          unlink($path);
     219          if (file_exists($path))
     220        {
     221          printError('Failed to delete file: ' . $path);
     222        }
     223        }
     224    }
     225      }
     226      closedir($dh);
     227    }
     228    else
     229    {
     230      printError('Failed to open directory for reading: ' . $dir);
     231    }
     232    // Now that the directory is (hopefully) empty, we can delete it if
     233    // required to
     234    if ($remove_this_dir)
     235    {
     236      rmdir($dir);
     237      if (file_exists($dir))
     238      {
     239        printError('Failed to remove directory: ' . $dir);
     240      }
     241    }
     242  }
     243  else
     244  {
     245    printError('Can\'t recursive delete - not a directory: ' . $dir);
     246  }
     247}
     248/** recursiveRemove() **/
     249
    179250?>
Note: See TracChangeset for help on using the changeset viewer.