<?php
// tblprefixer.php
// v. 1.0.0
// developed by Paddy Gaunt http://www.eldwick.org.uk/ Oct 2005
// intended to allow webERP to be used in situations where shared
// hosting allows only one MySQL database but could be useful for other applications
//
$prefix = "wp_"  ;
//
// in theory this could be something like:
//  $prefix = "\".\$tbpx.\"";
// however it would be necessary to set the variable $tbpx to whatever the prefix was to be, presumably in confix.php
// also it would have to be hard coded in the .sql file but this could be done with Edit/Replace v. quickly
// 
$ftyp = array("php", "inc", "sql", "psql");
$sqlcmd = array("DROP", "CREATE", "DELETE", "INSERT", "LOCK", "SELECT", "UPDATE");
$table = array("accountgroups", "accountsection", "areas", "bankaccounts", "banktrans", "contractbom", "bom", "buckets", "chartdetails", "chartmaster",
  "cogsglpostings", "companies", "config", "contractreqts", "contracts", "currencies", "custallocns", "custbranch",
  "debtorsmaster", "debtortrans", "debtortranstaxes", "discountmatrix", "edi_orders_seg_groups", "edi_orders_segs", "ediitemmapping", "edimessageformat", "freightcosts", "gltrans", "grns", "holdreasons",
  "lastcostrollup", "locations", "locstock", "loctransfers", "orderdeliverydifferenceslog", "paymentmethods", "paymentterms", "periods", "prices", "purchdata", "purchorderdetails", "purchorders",
  "recurringsalesorders", "recurrsalesorderdetails", "reportcolumns", "reportheaders", "salesanalysis", "salescat", "salescatprod", "salesglpostings", "salesman", "salesorderdetails", "salesorders", "salestypes",
  "scripts", "securitygroups", "securityroles", "securitytokens", "shipmentcharges", "shipments", "shippers", "stockcategory", "stockcheckfreeze", "stockcounts", "stockmaster", "stockmoves",
  "stockmovestaxes", "stockserialitems", "stockserialmoves", "suppallocs", "suppliercontacts", "suppliers", "supptrans", "supptranstaxes", "systypes", "taxauthorities", "taxauthrates", "taxcategories", "taxgroups",
  "taxgrouptaxes", "taxprovinces", "unitsofmeasure", "workcentres", "worksorders", "www_users");
///////////////////////////////////////////////////////////
//
// call the function on the top directories
///////////////////////////////////////////////////////////
convert_dir("webERP", "webERPmod"); 
///////////////////////////////////////////////////////////
//
// function to do the conversion
///////////////////////////////////////////////////////////
function convert_dir($dir, $newdir){
	global $prefix, $ftyp, $sqlcmd, $table;
	if(file_exists($dir) && file_exists($newdir)){
//
// first part just reproduces file structure - create directories
//
		$convert_dir=opendir($dir);
		while (false !== ($file = readdir($convert_dir))) {
			if($file != "." && $file != ".."){
				if(file_exists($newdir."/".$file) && filetype($newdir."/".$file) != "dir"){
					unlink($newdir."/".$file);
				}
				if(filetype($dir."/".$file) == "dir"){
					if(!file_exists($newdir."/".$file)){
						mkdir($newdir."/".$file);
						convert_dir($dir."/".$file, $newdir."/".$file);
					}
				}
//
// file must be a file
//
				else {
					$fileflg = 0;
// check if the extension matches any on our list
// set $fileflg if it does
					foreach($ftyp as $value) {	if (strpos($file, $value) !== false) {$fileflg = 1; break; }}
					if ($fileflg==1) {
// overwrite or create the destination file
						$fh = fopen($newdir."/".$file, "w");
// read source file into array $lines[]
						$lines = file($dir."/".$file);
						$nlines = count($lines);
// $flg set to 1 after SQL command word until ;
						$flg = 0;
						for ($i=0; $i<$nlines; $i++) {
// check if one of the command words is on this line
							foreach ($sqlcmd as $value) { if (strpos($lines[$i], $value) !== false) {$flg=1; break;}}
							if ($flg==1) {
// substitue any table names with prefixed version
								foreach ($table as $value) { $lines[$i] = str_ireplace($value, $prefix.$value, $lines[$i]);}
								if (strpos($lines[$i], ";") !== false) $flg=0;
							}
// write the line to the destination file
							fwrite($fh, $lines[$i]);
						}
						fclose($fh);
					}
// if it wasn't that type of file just copy it
					else copy($dir."/".$file, $newdir."/".$file); // not a file that we need to check!
				}
			}
		}
	}
}
?>