<?php
define('WP_INSTALLING', true);
require_once('../wpj-wizard.php');

if (file_exists('../wp-config.php')) 
	die($wpj_head2 . _LANG_WA_CONFIG_GUIDE1 . $wpj_foot);

if (!file_exists('../wp-config-sample.php'))
    die($wpj_head2 . _LANG_WA_CONFIG_GUIDE2 . $wpj_foot);
$configFile = file('../wp-config-sample.php');

$my_dirname = dirname(__FILE__);
   $dir_list = explode("/", $my_dirname);
   $dummy = array_pop($dir_list);
   $inst_path = implode("/", $dir_list);
if (!is_writable('../')) die($wpj_head2 . sprintf(_LANG_WA_CONFIG_GUIDE3, $inst_path) . $wpj_foot);

if (isset($_GET['step']))
	$step = $_GET['step'];
else
	$step = 0;

header( 'Content-Type: text/html; charset=EUC-JP' );
echo $wpj_head2;
switch($step) {
	case 0:
?> 
<p><?php echo _LANG_WA_CONFIG_GUIDE4; ?></p>
<ol> 
  <li><?php echo _LANG_WA_CONFIG_DATABASE; ?></li>
  <li><?php echo _LANG_WA_CONFIG_USERNAME; ?></li>
  <li><?php echo _LANG_WA_CONFIG_PASSWORD; ?></li>
  <li><?php echo _LANG_WA_CONFIG_LOCALHOST; ?></li>
  <li><?php echo _LANG_WA_CONFIG_PREFIX; ?></li>
</ol> 
<p><?php echo _LANG_WA_CONFIG_GUIDE5; ?></p>
<?php
	break;

	case 1:
	?> 
</p> 
<form method="post" action="setup-config.php?step=2"> 
  <p><?php echo _LANG_WA_CONFIG_GUIDE6; ?></p>
  <table> 
    <tr> 
      <th scope="row"><?php echo _LANG_WA_CONFIG_DATABASE; ?></th>
      <td><input name="dbname" type="text" size="45" value="wordpress" /></td> 
    </tr> 
    <tr> 
      <td colspan="2"><?php echo _LANG_WA_CONFIG_GUIDE7; ?><br /><br /></td> 
       </tr> 
       <tr> 
         <th scope="row"><?php echo _LANG_WA_CONFIG_USERNAME; ?></th> 
      <td><input name="uname" type="text" size="45" value="username" /></td> 
    </tr> 
    <tr> 
      <td colspan="2"><?php echo _LANG_WA_CONFIG_GUIDE8; ?><br /><br /></td> 
       </tr> 
       <tr> 
         <th scope="row"><?php echo _LANG_WA_CONFIG_PASSWORD; ?></th> 
      <td><input name="pwd" type="text" size="45" value="password" /></td> 
    </tr> 
    <tr> 
      <td colspan="2"><?php echo _LANG_WA_CONFIG_GUIDE9; ?><br /><br /></td> 
       </tr> 
       <tr> 
         <th scope="row"><?php echo _LANG_WA_CONFIG_LOCALHOST; ?></th> 
      <td><input name="dbhost" type="text" size="45" value="localhost" /></td> 
    </tr>
    <tr>
      <td colspan="2"><?php echo _LANG_WA_CONFIG_GUIDE10; ?><br /><br /></td> 
       </tr> 
       <tr> 
         <th scope="row"><?php echo _LANG_WA_CONFIG_PREFIX; ?></th> 
      <td><input name="prefix" type="text" id="prefix" value="wp_" size="45" /></td>
      </tr> 
      <tr> 
         <td colspan="2"><?php echo _LANG_WA_CONFIG_GUIDE11; ?><br /><br /></td>
    </tr> 
    <tr>
      <th scope="row"><?php echo _LANG_WA_CONFIG_ENCODE; ?></th>
      <td>
<select name="encode">
        <option value="ja_UTF" selected="selected">UTF-8</option>
        <option value="ja_EUC" >EUC-JP</option>
        <option value="ja_SJIS">Shift_JIS</option>
</select>
      </td>
    </tr>
    <tr>
      <td colspan="2"><?php echo _LANG_WA_CONFIG_GUIDE13; ?>
      <ul>
        <li><?php echo _LANG_WA_CONFIG_GUIDE14; ?></li>
        <li><?php echo _LANG_WA_CONFIG_GUIDE15; ?></li>
        <li><?php echo _LANG_WA_CONFIG_GUIDE16; ?></li>
      </ul>
      <?php echo _LANG_WA_CONFIG_GUIDE17; ?></td>
    </tr>
  </table>
  <input name="submit" type="submit" value="Submit" /> 
</form>
<?php
	break;
	
	case 2:
	$dbname  = trim($_POST['dbname']);
    $uname   = trim($_POST['uname']);
    $passwrd = trim($_POST['pwd']);
    $dbhost  = trim($_POST['dbhost']);
	$prefix  = trim($_POST['prefix']);
	if (empty($prefix)) $prefix = 'wp_';
	$local_file = $_POST['encode'];

if($local_file == 'ja_EUC') {
	$mb_setting = 'EUC-JP';
	$dbcharset  = 'ujis';
} elseif($local_file == 'ja_SJIS') {
	$mb_setting = 'Shift_JIS';
	$dbcharset  = 'sjis';
} else {
	$local_file = 'ja_UTF';
	$dbcharset  = 'utf8';
	$mb_setting = 'UTF-8';
}
    // Test the db connection.
    define('DB_NAME', $dbname);
    define('DB_USER', $uname);
    define('DB_PASSWORD', $passwrd);
    define('DB_HOST', $dbhost);
    define('DB_CHARSET', $dbcharset);
    define('WPLANG', $local_file);

    // We'll fail here if the values are no good.
    require_once('../wp-includes/wp-db.php');
	$handle = fopen('../wp-config.php', 'w');

    foreach ($configFile as $line_num => $line) {
        switch (substr($line,0,16)) {
            case "define('DB_NAME'":
                fwrite($handle, str_replace("wordpress", $dbname, $line));
                break;
            case "define('DB_USER'":
                fwrite($handle, str_replace("'username'", "'$uname'", $line));
                break;
            case "define('DB_PASSW":
                fwrite($handle, str_replace("'password'", "'$passwrd'", $line));
                break;
            case "define('DB_HOST'":
                fwrite($handle, str_replace("localhost", $dbhost, $line));
                break;
             case "define('DB_CHARS":
                fwrite($handle, str_replace("utf8", $dbcharset, $line));
                break;
           case "define ('WPLANG'": 
                fwrite($handle, str_replace("ja_UTF", $local_file, $line)); 
                break; 
            case "mb_internal_enco": 
                fwrite($handle, str_replace("UTF-8", $mb_setting, $line)); 
                break; 
			case '$table_prefix  =':
				fwrite($handle, str_replace('wp_', $prefix, $line));
				break;
            default:
                fwrite($handle, $line);
        }
    }
    fclose($handle);
	chmod('../wp-config.php', 0606);
?> 
<p><?php echo _LANG_WA_CONFIG_GUIDE12; ?></p>
<?php
	break;

}
?> 
</body>
</html>
