Oihana PHP

assertRemovableDirectory.php

Table of Contents

Functions

assertRemovableDirectory()  : void
Asserts that a path is safe to hand to a recursive deletion.

Functions

assertRemovableDirectory()

Asserts that a path is safe to hand to a recursive deletion.

assertRemovableDirectory(string $path[, int $minDepth = 3 ]) : void

A path read from a configuration file ends up, sooner or later, in deleteDirectory() or an rm -rf. A truncated, relative or mistyped value there turns a routine clean-up into the loss of a system directory, so the path is checked as text before anything acts on it.

A path is accepted when it is:

  • not empty ;
  • absolute — a relative path depends on the current directory, which nobody checks ;
  • free of . and .. segments — /var/www/site/.. is /var/www ;
  • deep enough — at least $minDepth segments, so /, /var and /var/www are refused with the default of 3.

Nothing is read on disk: the directory need not exist, and a link is judged by its own path. This is a guard against a wrong value, not against a hostile one.

Parameters
$path : string

The directory about to be deleted.

$minDepth : int = 3

The number of segments the path must have, at least.

Tags
throws
DirectoryException

When the path is not safe to delete.

example
use function oihana\files\assertRemovableDirectory;
use function oihana\files\deleteDirectory;

assertRemovableDirectory( '/var/www/example.com/htdocs' ) ; // passes
assertRemovableDirectory( '/var/www' ) ;                    // throws: 2 segments, 3 required
assertRemovableDirectory( 'www/example.com' ) ;             // throws: relative
assertRemovableDirectory( '/var/www/site/..' ) ;            // throws: relative segment

assertRemovableDirectory( $path ) ;
deleteDirectory( $path ) ;
author

Marc Alcaraz (ekameleon)

since
1.7.0
On this page

Search results