<?php
/**
 * Retrieves and creates the wp-config.php file.
 *
 * The permissions for the base directory must allow for writing files in order
 * for the wp-config.php to be created using this page.
 *
 * @internal This file must be parsable by PHP4.
 *
 * @package WordPress
 * @subpackage Administration
 */

/**
 * We are installing.
 *
 * @package WordPress
 */
define('WP_INSTALLING', true);

/**
 * We are blissfully unaware of anything.
 */
define('WP_SETUP_CONFIG', true);

/**
 * Disable error reporting
 *
 * Set this to error_reporting( E_ALL ) or error_reporting( E_ALL | E_STRICT ) for debugging
 */
error_reporting(0);

/**#@+
 * These three defines are required to allow us to use require_wp_db() to load
 * the database class while being wp-content/db.php aware.
 * @ignore
 */
define('ABSPATH', dirname(dirname(__FILE__)).'/');
define('WPINC', 'wp-includes');
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
define('WP_DEBUG', false);
/**#@-*/

require_once(ABSPATH . WPINC . '/load.php');
require_once(ABSPATH . WPINC . '/version.php');
wp_check_php_mysql_versions();

require_once(ABSPATH . WPINC . '/compat.php');
require_once(ABSPATH . WPINC . '/functions.php');
require_once(ABSPATH . WPINC . '/class-wp-error.php');

if (!file_exists(ABSPATH . 'wp-config-sample.php'))
	wp_die('Duilich ach feumaidh mi faidhle wp-config-sample.php mus urrainn dhomh dad a dhèanamh. Feuch is luchdaich suas am faidhle seo on stàladh WordPress agad.');

$configFile = file(ABSPATH . 'wp-config-sample.php');

// Check if wp-config.php has been created
if (file_exists(ABSPATH . 'wp-config.php'))
	wp_die("<p>Tha am faidhle 'wp-config.php' ann mu thràth. Ma dh'fheumas tu gin dhen rèiteachadh seo a chur air gleus, sguab às e an toiseach. 'S urrainn dhut feuchainn ri <a href='install.php'>a stàladh an-dràsta</a>.</p>");

// Check if wp-config.php exists above the root directory but is not part of another install
if (file_exists(ABSPATH . '../wp-config.php') && ! file_exists(ABSPATH . '../wp-settings.php'))
	wp_die("<p>Tha am faidhle 'wp-config.php' ann mu thràth, aon ìre os cionn an stàladh WordPress agad. Ma dh'fheumas tu gin dhen rèiteachadh seo a chur air gleus, sguab às e an toiseach. 'S urrainn dhut feuchainn ri <a href='install.php'>a stàladh an-dràsta</a>.</p>");

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

/**
 * Display setup wp-config.php file header.
 *
 * @ignore
 * @since 2.3.0
 * @package WordPress
 * @subpackage Installer_WP_Config
 */
function display_header() {
	header( 'Content-Type: text/html; charset=utf-8' );
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WordPress &rsaquo; Suidheachadh an fhaidhle rèiteachaidh</title>
<link rel="stylesheet" href="css/install.css" type="text/css" />

</head>
<body>
<h1 id="logo"><img alt="WordPress" src="images/wordpress-logo.png" /></h1>
<?php
}//end function display_header();

switch($step) {
	case 0:
		display_header();
?>

<p>Fàilte gu WordPress. Mus urrainn dhuinn tòiseachadh, feumaidh sinn beagan fiosrachaidh mun stòr-dàta agad. Bidh feum agad air na leanas mus lean sinn air adhart:</p>
<ol>
	<li>Ainm an stòir-dhàta</li>
	<li>Ainm-cleachdaiche an stòir-dhàta</li>
	<li>Facal-faire an stòir-dhàta</li>
	<li>Òstair an stòir-dhàta</li>
	<li>Ro-leasachan a' chlàir ('s e sin beagan litrichean a nochdas aig toiseach ainm a' chlàir, rud a tha feumail ma tha thu airson barrachd air aon WordPress a chumail san aon stòr-dàta gus an cumail air falbh o chèile) </li>
</ol>
<p><strong>Feuchaidh sinn ris am faidhle a chruthachadh dhut ach mur obraich sin air adhbhar air choireigin, na gabh dragh. Tha sinn a' feuchainn ri fiosrachadh mun stòr-dàta a chur san fhaidhle rèiteachaidh, dìreach sin. 'S urrainn dhut cuideachd am faidhle <code>wp-config-sample.php</code> e fhèin fhosgladh ann an deasaiche teacsa, am fiosrachadh a chur ann a làimh agus a shàbhaladh mar <code>wp-config.php</code>. </strong></p>
<p>Bidh an t-òstair-lìn agad air an dàta seo a thoirt dhut mu thràth mar is trice. Mur eil am fiosrachadh seo agad, feumaidh tu bruidhinn riuthasan mus urrainn dhut leantainn air adhart. Ma tha thu deiseil is deònach&hellip;</p>

<p class="step"><a href="setup-config.php?step=1<?php if ( isset( $_GET['noapi'] ) ) echo '&amp;noapi'; ?>" class="button">Siuthad ma-thà!</a></p>
<?php
	break;

	case 1:
		display_header();
	?>
<form method="post" action="setup-config.php?step=2">
	<p>Cuir a-steach am fiosrachadh mun cheangal ris an stòr-dàta agad gu h-ìosal. Mur eil thu cinnteach mu dhèidhinn, cuir fios dhan òstair agad. </p>
	<table class="form-table">
		<tr>
			<th scope="row"><label for="dbname">Ainm an stòir-dhàta</label></th>
			<td><input name="dbname" id="dbname" type="text" size="25" value="wordpress" /></td>
			<td>Seo ainm an stòir-dhàta a bu mhath leat WP a ruith ann. </td>
		</tr>
		<tr>
			<th scope="row"><label for="uname">Ainm-cleachdaiche</label></th>
			<td><input name="uname" id="uname" type="text" size="25" value="ainm-cleachdaiche" /></td>
			<td>Seo an t-ainm-cleachdaiche MySQL agad</td>
		</tr>
		<tr>
			<th scope="row"><label for="pwd">Facal-faire</label></th>
			<td><input name="pwd" id="pwd" type="text" size="25" value="facal-faire" /></td>
			<td>...agus am facal-faire MySQL agad.</td>
		</tr>
		<tr>
			<th scope="row"><label for="dbhost">Òstair an stòir-dhàta</label></th>
			<td><input name="dbhost" id="dbhost" type="text" size="25" value="localhost" /></td>
			<td>Mur eil seo agad, gheibh thu seo on òstair-lìn agad mur eil <code>localhost</code> ag obair air a shon.</td>
		</tr>
		<tr>
			<th scope="row"><label for="prefix">Ro-leasachan a' chlàir</label></th>
			<td><input name="prefix" id="prefix" type="text" value="wp_" size="25" /></td>
			<td>Ma tha thu airson iomadh stàladh de WordPress a ruith san aon stòr-dàta, atharraich seo.</td>
		</tr>
	</table>
	<?php if ( isset( $_GET['noapi'] ) ) { ?><input name="noapi" type="hidden" value="true" /><?php } ?>
	<p class="step"><input name="submit" type="submit" value="Ceart ma-thà" class="button" /></p>
</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_';

	// Validate $prefix: it can only contain letters, numbers and underscores
	if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
		wp_die( /*WP_I18N_BAD_PREFIX*/'<strong>MEARACHD</strong>: Chan fhaod ach àireamhan, litrichean is fo-loidhnichean a bhith ann an ro-leasachan a\' chlàir.'/*/WP_I18N_BAD_PREFIX*/ );

	// Test the db connection.
	/**#@+
	 * @ignore
	 */
	define('DB_NAME', $dbname);
	define('DB_USER', $uname);
	define('DB_PASSWORD', $passwrd);
	define('DB_HOST', $dbhost);
	/**#@-*/

	// We'll fail here if the values are no good.
	require_wp_db();
	if ( ! empty( $wpdb->error ) ) {
		$back = '<p class="step"><a href="setup-config.php?step=1" onclick="javascript:history.go(-1);return false;" class="button">Feuch ris a-rithist</a></p>';
		wp_die( $wpdb->error->get_error_message() . $back );
	}

	// Fetch or generate keys and salts.
	$no_api = isset( $_POST['noapi'] );
	require_once( ABSPATH . WPINC . '/plugin.php' );
	require_once( ABSPATH . WPINC . '/l10n.php' );
	require_once( ABSPATH . WPINC . '/pomo/translations.php' );
	if ( ! $no_api ) {
		require_once( ABSPATH . WPINC . '/class-http.php' );
		require_once( ABSPATH . WPINC . '/http.php' );
		wp_fix_server_vars();
		/**#@+
		 * @ignore
		 */
		function get_bloginfo() {
			return ( ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . str_replace( $_SERVER['PHP_SELF'], '/wp-admin/setup-config.php', '' ) );
		}
		/**#@-*/
		$secret_keys = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
	}

	if ( $no_api || is_wp_error( $secret_keys ) ) {
		$secret_keys = array();
		require_once( ABSPATH . WPINC . '/pluggable.php' );
		for ( $i = 0; $i < 8; $i++ ) {
			$secret_keys[] = wp_generate_password( 64, true, true );
		}
	} else {
		$secret_keys = explode( "\n", wp_remote_retrieve_body( $secret_keys ) );
		foreach ( $secret_keys as $k => $v ) {
			$secret_keys[$k] = substr( $v, 28, 64 );
		}
	}
	$key = 0;

	foreach ($configFile as $line_num => $line) {
		switch (substr($line,0,16)) {
			case "define('DB_NAME'":
				$configFile[$line_num] = str_replace("database_name_here", $dbname, $line);
				break;
			case "define('DB_USER'":
				$configFile[$line_num] = str_replace("'username_here'", "'$uname'", $line);
				break;
			case "define('DB_PASSW":
				$configFile[$line_num] = str_replace("'password_here'", "'$passwrd'", $line);
				break;
			case "define('DB_HOST'":
				$configFile[$line_num] = str_replace("localhost", $dbhost, $line);
				break;
			case '$table_prefix  =':
				$configFile[$line_num] = str_replace('wp_', $prefix, $line);
				break;
			case "define('AUTH_KEY":
			case "define('SECURE_A":
			case "define('LOGGED_I":
			case "define('NONCE_KE":
			case "define('AUTH_SAL":
			case "define('SECURE_A":
			case "define('LOGGED_I":
			case "define('NONCE_SA":
				$configFile[$line_num] = str_replace('put your unique phrase here', $secret_keys[$key++], $line );
				break;
		}
	}
	if ( ! is_writable(ABSPATH) ) :
		display_header();
?>
<p>Tha mi duilich ach chan urrainn dhomh am faidhle <code>wp-config.php</code> a sgrìobhadh.</p>
<p>'S urrainn dhut am faidhle <code>wp-config.php</code> a chruthachadh a làimh agus an teacsa a leanas a chur a-steach ann.</p>
<textarea cols="98" rows="15" class="code"><?php
		foreach( $configFile as $line ) {
			echo htmlentities($line, ENT_COMPAT, 'UTF-8');
		}
?></textarea>
<p>Briog air "Ruith an stàladh" as dèidh dhut sin a dhèanamh.</p>
<p class="step"><a href="install.php" class="button">Ruith an stàladh</a></p>
<?php
	else :
		$handle = fopen(ABSPATH . 'wp-config.php', 'w');
		foreach( $configFile as $line ) {
			fwrite($handle, $line);
		}
		fclose($handle);
		chmod(ABSPATH . 'wp-config.php', 0666);
		display_header();
?>
<p>Nach tusa tha glic! Sin agad a' phàirt seo dhen stàladh, 's urrainn do WordPress conaltradh a dhèanamh ris an stòr-dàta agad a-nis. Ma tha thu deiseil, siuthad ort is&hellip;</p>

<p class="step"><a href="install.php" class="button">Ruith an stàladh</a></p>
<?php
	endif;
	break;
}
?>
</body>
</html>
