<?PHP
// File: $Id: modify_config.php,v 1.7 2001/12/02 16:59:42 niceguyeddie Exp $ $Name: $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 by the Post-Nuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Scott Kirkwood (hide@address.com)
// Purpose of file: Routines to modify the config.php file.
// General routine modify_file() is useful in it's own right.
// ----------------------------------------------------------------------
// This is the last update to this script before the new version is finished.
// mod_file is general, give it a source file a destination.
// an array of search patterns (Perl style) and replacement patterns
// Returns a string which starts with "Err" if there's an error
// TODO: internationalize the error messages?
function modify_file($src, $dest, $reg_src, $reg_rep)
{
$in = @fopen($src, "r");
if (! $in)
{
return "Error: unable to open $src for read";
}
$out = @fopen($dest, "w");
if (! $out)
{
return "Error: unable to open $dest for write";
}
$lines = 0; // Keep track of the number of lines changed
while (!feof($in))
{
$buffer = fgets($in, 4096);
$new = preg_replace($reg_src, $reg_rep, $buffer);
if ($new != $buffer)
{
$lines++;
}
fputs($out, $new);
}
fclose($in);
fclose($out);
if ($lines == 0)
{
// Skip the rest - no lines changed
return "0 lines changed, did nothing";
}
$swap = 'tmp-' . $src;
// Rename the current file to the swap filename, permissions kept.
if (! @rename($src, $swap))
{
return "Error: unable to rename $src to $swap. Please overwrite $dest as $src manually left in folder.";
}
// Rename the newly created file to the final filename ($src). Permissions are probably 666?
if (! @rename($dest, $src))
{
return "Error: unable to rename $dest to $src. Please do manually.";
}
// Rename $swap to $dest
if (! @rename($swap, $dest))
{
return "Error: unable to rename $swap to $dest. File named $swap left behind by operation.";
}
// Success!
return "$src updated with $lines lines of changes, backup is called $backup";
}
// Two global arrays
$reg_src = array();
$reg_rep = array();
// Setup various searches and replaces
// Scott Kirkwood
function add_src_rep($key, $rep)
{
global $reg_src, $reg_rep;
// Note: /x is to permit spaces in regular expressions
// Great for making the reg expressions easier to read
// Ex: $pnconfig['sitename'] = stripslashes("Your Site Name");
$reg_src[] = "/ \['$key'\] \s* = \s* stripslashes\( (\' | \") (.*) \\1 \); /x";
$reg_rep[] = "['$key'] = stripslashes(\\1$rep\\1);";
// Ex. $pnconfig['site_logo'] = "logo.gif";
$reg_src[] = "/ \['$key'\] \s* = \s* (\' | \") (.*) \\1 ; /x";
$reg_rep[] = "['$key'] = '$rep';";
// Ex. $pnconfig['pollcomm'] = 1;
$reg_src[] = "/ \['$key'\] \s* = \s* (\d*\.?\d*) ; /x";
$reg_rep[] = "['$key'] = $rep;";
}
function show_error_info()
{
global $dbhost, $dbuname, $dbpass, $dbname, $prefix, $dbtype;
echo <<< EOT
<br/><br/>
<b>Write error</b> unable to update your "config.php" file<br/>
You will have to modify this file yourself using a text editor.<br/>
Here are the changes required:<br/>
<tt>
\$pnconfig['dbtype'] = '$dbtype';<br/>
\$pnconfig['dbhost'] = '$dbhost';<br/>
\$pnconfig['dbuname'] = '$dbuname';<br/>
\$pnconfig['dbpass'] = '$dbpass';<br/>
\$pnconfig['dbname'] = '$dbname';<br/>
\$pnconfig['prefix'] = '$prefix';<br/>
</tt>
EOT;
}
// Update the config.php file with the database information.
function update_config_php($db_prefs = false)
{
global $reg_src, $reg_rep;
global $dbhost, $dbuname, $dbpass, $dbname, $prefix, $dbtype;
global $email, $url, $HTTP_ENV_VARS;
add_src_rep("dbhost", $dbhost);
add_src_rep("dbuname", base64_encode($dbuname));
add_src_rep("dbpass", base64_encode($dbpass));
add_src_rep("dbname", $dbname);
add_src_rep("prefix", $prefix);
add_src_rep("dbtype", $dbtype);
if (strstr($HTTP_ENV_VARS["OS"],"Win")) {
add_src_rep("system" , '1');
} else {
add_src_rep("system", '0');
}
add_src_rep("encoded", '1');
if ($email)
{
add_src_rep("adminmail", $email);
}
if ($url)
{
add_src_rep("nukeurl", $url);
}
$ret = modify_file("config.php", "config-old.php", $reg_src, $reg_rep);
if (preg_match("/Error/", $ret))
{
show_error_info();
}
}
?>