
XMLFUN
By: a guest on
May 2nd, 2012 | syntax:
PHP | size: 1.96 KB | hits: 22 | expires: Never
<?php
/* Write php protected XML config file to store database credentials */
function write_db_config($string, $user = "", $pass = "", $updir = false) {
/* Determine filename to be used */
$filename = 'db_config.xml.php';
if($updir)
$filename = "./../$filename";
else
$filename = "./$filename";
/* Create DOM object for config XML */
$dom = new DomDocument('1.0');
/* Add meta-root (Needed for php protection of XML files) */
$root = $dom->appendChild($dom->CreateElement('root'));
/* Add comment to prevent browsers reading XML */
$comment = $dom->createComment("<?php die('-->Permission denied.'); ?>");
$dom->appendChild($comment);
/* Add config root */
$dbconfig = $root->appendChild($dom->CreateElement('dbconfig'));
/* Add elements */
$connect_string = $dbconfig->appendChild($dom->createElement('connect_string'));
$username = $dbconfig->appendChild($dom->createElement('username'));
$password = $dbconfig->appendChild($dom->createElement('password'));
/* Add text to nodes */
$connect_string->appendChild($dom->createTextNode($string));
if($user != "" )
$username->appendChild($dom->createTextNode($user));
if($pass != "" )
$password->appendChild($dom->createTextNode($pass));
/* Generate XML */
$dom->formatOutput = true;
$output = $dom->saveXML($comment) . "\n" . $dom->saveXML($dbconfig);
/* Write out XML file */
$filehandle = fopen($filename, 'w');
$result = fwrite($filehandle, $output);
fclose($filehandle);
/* Final check to see if the file was written */
if($result) {
return true;
} else {
echo("Could not write config file!");
return false;
}
}
?>