Version: 0.1

Type: Full Script

Category: Other

License: GNU General Public License

Description: After searching for a set of utilities that achieved something similar to reading and writing to .INI files in Windows, I ended up writing the attached little set of utilities that create a simple PHP file that can be include()d at the top of other files to share common 'master' settings. Obviously this would need to have some kind of user authentication added. Only a quick bodge, but does the job.



<?php

// masteradmin.php

// set global variables, etc.

$filename="config/masterconfig.php" ;

$itemsarray = array("companyname", "bgcolor") ;

if ($btnsubmit) {

	// already have all the values, so save them
	
	foreach ($itemsarray as $value) {
		
		${$value} = stripslashes(${$value}) ;
		writeconfig($value, ${$value}) ;
		
	}
	
} else {

	// need to read each variable from the config file

	foreach($itemsarray as $value) {
	
		${$value} = readconfig($value) ;
		
	}
	
}

function readconfig($item) {

global $filename ;

$fcontents=@file($filename) ;

if (!empty($fcontents)) {

	while (list($linenum, $line) = each($fcontents)) {
	
		if (leftside($line) == $item) {
		
			return rightside($line) ;
			break ;
			
		}
		
	}

}

return "" ;

}
		

function writeconfig($item, $value) {

global $filename ;

if (!file_exists($filename)) {
	$fp = fopen($filename, "w") ;
	fputs($fp, "<?php\n");
	fclose($fp) ;
}

$fcontents=file($filename) ;
$fp = fopen($filename, "w") ;

$found = false ;

while (list($linenum, $line) = each($fcontents)) {
	
	if ($line != "?>\n") {
	
		if (leftside($line) == $item) {
		
			fputs($fp, "\$".$item."=\"".$value."\";\n") ;
			$found = true ;
			
		} else {
		
			fputs($fp, $line) ;
			
		}
	}	
}

if (!$found) {

	fputs($fp, "\$".$item."=\"".$value."\";\n") ;
	
}

fputs($fp, "?>\n") ; 

fclose($fp) ;

}

function leftside($line) {

$pos = strpos($line, "=") ;

if ($pos === false) {

	return "" ;
	
} else {

	return substr($line, 1, $pos-1) ;
	
}

}

function rightside($line) {

$pos = strpos($line, "=") ;

if ($pos === false) {

	return "" ;
	
} else {

	return substr($line, ($pos+2), strlen($line)-5-strlen(leftside($line))) ;
	
}

}



echo "<html><body><form method=post action=\"".$PHP_SELF."\">" ;

foreach ($itemsarray as $value) {

	echo "$".$value." <input type=text name=".$value." size=20 value=\"".${$value}."\"><br>" ;
	
}

?>

<input type=submit name=btnsubmit value=Save>
</form>

</body>
</html>