Currently Browsing: Home » Removing .svn Directories With PHP

Removing .svn Directories With PHP

A few days ago, Lateral Code decided to switch over to using SVN (Subversion) for WordPress. This not only is an easy way to install WordPress for new users, but it also makes it a breeze to update your version.

For those of you who aren’t familiar with Subversion, it’s a version control system. You can read more about it here.

Subversion

The Invasion of .svn

Upon reinstalling WordPress with a few simple SVN commands, we noticed many .svn directories. These were located in every single section of our site’s files. Not only that, but we had also read about the dangers of keeping .svn directories in our file system earlier through Smashing Magazine.

The Exterminator: PHP

To solve this problem, we decided to write a simple PHP script that recursively removes all .svn directories. I’ll first present the complete script and then explain the details. You may also download it below if you like:

Download the Files

// preconditon: $dir ends with a forward slash (/) and is a valid directory
// postcondition: $dir and all it's sub-directories are recursively
// searched through for .svn directories. If a .svn directory is found,
// it is deleted to remove any security holes.
function removeSVN( $dir ) {
	echo "Searching: $dir\n\t";

	$flag = false; // haven't found .svn directory
	$svn = $dir . '.svn';

	if( is_dir( $svn ) ) {
		if( !chmod( $svn, 0777 ) )
			echo "File permissions could not be changed (this may or may not be a problem--check the statement below).\n\t"; // if the permissions were already 777, this is not a problem

		delTree( $svn ); // remove the .svn directory with a helper function

		if( is_dir( $svn ) ) // deleting failed
			echo "Failed to delete $svn due to file permissions.";
		else
			echo "Successfully deleted $svn from the file system.";

		$flag = true; // found directory
	}

	if( !$flag ) // no .svn directory
		echo 'No .svn directory found.';
	echo "\n\n";

	$handle = opendir( $dir );
	while( false !== ( $file = readdir( $handle ) ) ) {
		if( $file == '.' || $file == '..' ) // don't get lost by recursively going through the current or top directory
			continue;

		if( is_dir( $dir . $file ) )
			removeSVN( $dir . $file . '/' ); // apply the SVN removal for sub directories
	}
}

This is a recursive function that requires a directory name as a parameter. It first checks if a .svn directory exists in the given directory. If so, it will attempt to modify its permissions and subsequently delete it. During this whole process, it will notify the user of any errors/successes.

After the deletion, the script searches through the directory for any sub-directories. If it finds some, it will recursively call itself to remove more .svn directories. Note that there is a helper function, delTree(), which is called here. It looks like this:

// precondition: $dir is a valid directory
// postcondition: $dir and all it's contents are removed
// simple function found at http://www.php.net/manual/en/function.rmdir.php#93836
function delTree( $dir ) {
	$files = glob( $dir . '*', GLOB_MARK ); // find all files in the directory

	foreach( $files as $file ) {
		if( substr( $file, -1 ) == '/' )
			delTree( $file ); // recursively apply this to sub directories
		else
			unlink( $file );
	}

	if ( is_dir( $dir ) )
		rmdir( $dir ); // remove the directory itself (rmdir only removes a directory once it is empty)
}

delTree is yet another recursive function. It deletes all files in a directory and recursively calls itself with sub-directories. Consequently, it will remove the directory itself with rmdir().

Now that the function is complete, you can easily call it using the following:

header( 'Content-type: text/plain' ); // plain text for easy display

// remove all .svn directories in the
// current directory and sub directories
// (recursively applied)
removeSVN( './' );

If this file is not in the root of your server, then you can easily just change the directory that is fed to removeSVN(). Good luck! You may download the script here.

Tags:

This entry was posted on Saturday, January 2nd, 2010 at 09:39:10. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Removing .svn Directories With PHP”

  1. Richard says:

    Thanks for this sample. Very helpful.

    To make it work in my case, I needed to add the leading zero in chmod:

    i.e. changed

    if( !chmod( $svn, 777 ) )

    to be

    if( !chmod( $svn, 0777 ) )

    I also needed to move the entire if (! chmod(… into the delTree() function to recursively set chmod.

    Regards
    Richard

  2. Alhvi says:

    I also had to change

    if( !chmod( $svn, 777 ) )

    to

    if( !chmod( $svn, 0777 ) )

    As Richard said. But Thank you!!!! you saved me a lot of time with this :D

  1. Removing .svn Directories With PHP | Coder Online

Leave a Reply

Want to be notified when someone replies? Subscribe to this post's comment RSS feed.
Any field marked with a * is required.