<?php

/* 
 *	W-AGORA 4.0
 *	-----------
 *	$Id: change_extension.php,v 1.4 2002/03/07 09:16:08 mdruilhe Exp $
 *	Usage:		Change extensions of PHP file and replace all occurrences of old w/new ext.
 *	Author:		Marc Druilhe <mdruilhe@w-agora.com>
 */

# Define globals
# --------------

	$agora_dir = "..";
	$ext = "php3";
	$newext = "php";

	function process_file ($file) {
		global $ext, $newext;
	

		$fext = strrchr ($file, ".");
		if ( ($fext != ".$ext") && ($fext != ".inc") ) {
			return FALSE;
		}
		$len = strlen ($file) - strlen ($fext);
		$new_file = substr ($file, 0, $len) . ".$newext";

		$fdin = fopen ("$file", "r");
		if ($fdin) {
			$content = fread ($fdin, filesize ("$file"));
			$content = ereg_replace ("([\.\"'])$ext", "\\1$newext", $content);
			fclose ($fdin);
		} else {
			printf ("could not open file: %s <br>", $file);
			return FALSE;
		}

		$file_out = ($fext == ".inc") ? $file : $new_file;

		$fdout = fopen ("$file_out", "w");
		if ($fdout) {
			fwrite($fdout, $content);
			fclose ($fdout);
		} else {
			printf ("could not open file: %s <br>", $file_out);
			return FALSE;
		}

		if ($file_out != $file) {
			unlink ($file);
		}

		echo "<li> <b>$file -> $file_out</b>";

	}

	function browse_dir ($path) {
		global $ext;

		echo "<ul>";
		$dir = dir($path);
		while ($entry = $dir->read() ) {
			if ( ($entry != ".") && ($entry != "..") ) {
				if ( is_dir("$path/$entry") ) {
					echo "<li> <b><font color='blue'>$entry</font></b>";
					browse_dir ("$path/$entry");
				} else {
					process_file ("$path/$entry");
				}
			}
		}
		$dir->close();
		echo "</ul>";
	}
?>

<html>
<head>
<style type="text/css">
<!--
 body { font-size: 9pt  font-family: Helvetica, Arial, Geneva; }
-->
</style>
</head>
<body>

<?
	echo "<H3>Changing extensions for PHP scripts from '.$ext' to '.$newext'</H3>";
	browse_dir ("$agora_dir");
?>