<commit_before><?php

if (isset($_SESSION['database_initialized']) && $_SESSION['database_initialized'] == true) {
	Redirect::to('?step=site_configuration');
	die();
}

if (!isset($_SESSION['hostname']) || !isset($_SESSION['install_path']) || !isset($_SESSION['friendly_urls'])) {
	Redirect::to('?step=general_configuration');
	die();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	$validate = new Validate();
	$validation = $validate->check($_POST, array(
		'db_address' => array(
			'required' => true,
		),
		'db_port' => array(
			'required' => true,
		),
		'db_username' => array(
			'required' => true,
		),
		'db_name' => array(
			'required' => true,
		),
	));

	if (!$validation->passed()) {

		$error = $language['database_error'];

	} else {

		$db_address = $_POST['db_address'];
		$db_port = $_POST['db_port'];
		$db_username = $_POST['db_username'];
		$db_password =  ((isset($_POST['db_password']) && !empty($_POST['db_password'])) ? str_replace('\'', '\\\'', $_POST['db_password']) : '');
		$db_name = $_POST['db_name'];

		$charset = ($_POST['charset'] == 'latin1') ? 'latin1' : 'utf8mb4';
		$engine = ($_POST['engine'] == 'MyISAM') ? 'MyISAM' : 'InnoDB';

		$mysqli = new mysqli($db_address, $db_username, $db_password, $db_name, $db_port);
		if ($mysqli->connect_errno) {

			$error = $mysqli->connect_errno . ' - ' . $mysqli->connect_error;

		} else {

			$mysqli->close();
			
			$conf = array(
				'mysql' => array(
					'host' => $db_address,
					'port' => $db_port,
					'username' => $db_username,
					'password' => $<PASSWORD>,
					'db' => $db_name,
					'prefix' => 'nl2_',
					'charset' => $charset,
					'engine' => $engine,
				),
				'remember' => array(
					'cookie_name' => 'nl2',
					'cookie_expiry' => 604800,
				),
				'session' => array(
					'session_name' => '2user',
					'admin_name' => '2admin',
					'token_name' => '2token',
				),
				'core' => array(
					'hostname' => $_SESSION['hostname'],
					'path' => $_SESSION['install_path'],
					'friendly' => $_SESSION['friendly_urls'],
				),
				'allowedProxies' => '',
			);

			try {

				if (!is_writable('core/config.php')) {

					$error = $language['config_not_writable'];

				} else {

					$config_content = '<?php' . PHP_EOL . '$conf = ' . var_export($conf, true) . ';';
					file_put_contents('core/config.php', $config_content);

					$_SESSION['charset'] = $charset;
					$_SESSION['engine'] = $engine;

					Redirect::to('?step=database_initialization');
					die();

				}

			} catch (Exception $e) {

				$error = $e->getMessage();
				
			}

		}

	}

}

?>

<?php if (isset($error)) { ?>
	<div class="ui error message">
		<?php echo $error; ?>
	</div>
<?php } ?>

<form action="" method="post">
	<div class="ui segments">
		<div class="ui secondary segment">
			<h4 class="ui header">
				<?php echo $language['database_configuration']; ?>
			</h4>
		</div>
		<div class="ui segment">
			<div class="ui centered grid">
				<div class="sixteen wide mobile twelve wide tablet ten wide computer column">
					<div class="ui form">
						<?php
							create_field('text', $language['database_address'], 'db_address', 'inputDBAddress', '127.0.0.1');
							create_field('text', $language['database_port'], 'db_port', 'inputDBPort', '3306');
							create_field('text', $language['database_username'], 'db_username', 'inputDBUsername');
							create_field('text', $language['database_password'], 'db_password', '<PASSWORD>');
							create_field('text', $language['database_name'], 'db_name', 'inputDBName');
							create_field('select', $language['character_set'], 'charset', 'inputCharset', 'utf8mb4', array(
								'utf8mb4' => 'Unicode (utf8mb4)',
								'latin1' => 'Latin (latin1)',
							));
							create_field('select', $language['database_engine'], 'engine', 'inputEngine', 'InnoDB', array(
								'InnoDB' => 'InnoDB',
								'MyISAM' => 'MyISAM',
							));
						?>
					</div>
				</div>
			</div>
		</div>
		<div class="ui secondary right aligned segment">
			<button type="submit" class="ui small primary button">
				<?php echo $language['proceed']; ?><commit_msg>Fix friendly urls in new installer + db password field<commit_after><?php

if (isset($_SESSION['database_initialized']) && $_SESSION['database_initialized'] == true) {
	Redirect::to('?step=site_configuration');
	die();
}

if (!isset($_SESSION['hostname']) || !isset($_SESSION['install_path']) || !isset($_SESSION['friendly_urls'])) {
	Redirect::to('?step=general_configuration');
	die();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	$validate = new Validate();
	$validation = $validate->check($_POST, array(
		'db_address' => array(
			'required' => true,
		),
		'db_port' => array(
			'required' => true,
		),
		'db_username' => array(
			'required' => true,
		),
		'db_name' => array(
			'required' => true,
		),
	));

	if (!$validation->passed()) {

		$error = $language['database_error'];

	} else {

		$db_address = $_POST['db_address'];
		$db_port = $_POST['db_port'];
		$db_username = $_POST['db_username'];
		$db_password =  ((isset($_POST['db_password']) && !empty($_POST['db_password'])) ? str_replace('\'', '\\\'', $_POST['db_password']) : '');
		$db_name = $_POST['db_name'];

		$charset = ($_POST['charset'] == 'latin1') ? 'latin1' : 'utf8mb4';
		$engine = ($_POST['engine'] == 'MyISAM') ? 'MyISAM' : 'InnoDB';

		$mysqli = new mysqli($db_address, $db_username, $db_password, $db_name, $db_port);
		if ($mysqli->connect_errno) {

			$error = $mysqli->connect_errno . ' - ' . $mysqli->connect_error;

		} else {

			$mysqli->close();
			
			$conf = array(
				'mysql' => array(
					'host' => $db_address,
					'port' => $db_port,
					'username' => $db_username,
					'password' => $<PASSWORD>,
					'db' => $db_name,
					'prefix' => 'nl2_',
					'charset' => $charset,
					'engine' => $engine,
				),
				'remember' => array(
					'cookie_name' => 'nl2',
					'cookie_expiry' => 604800,
				),
				'session' => array(
					'session_name' => '2user',
					'admin_name' => '2admin',
					'token_name' => '2<PASSWORD>',
				),
				'core' => array(
					'hostname' => $_SESSION['hostname'],
					'path' => $_SESSION['install_path'],
					'friendly' => $_SESSION['friendly_urls'] === true ? true : false,
				),
				'allowedProxies' => '',
			);

			try {

				if (!is_writable('core/config.php')) {

					$error = $language['config_not_writable'];

				} else {

					$config_content = '<?php' . PHP_EOL . '$conf = ' . var_export($conf, true) . ';';
					file_put_contents('core/config.php', $config_content);

					$_SESSION['charset'] = $charset;
					$_SESSION['engine'] = $engine;

					Redirect::to('?step=database_initialization');
					die();

				}

			} catch (Exception $e) {

				$error = $e->getMessage();
				
			}

		}

	}

}

?>

<?php if (isset($error)) { ?>
	<div class="ui error message">
		<?php echo $error; ?>
	</div>
<?php } ?>

<form action="" method="post">
	<div class="ui segments">
		<div class="ui secondary segment">
			<h4 class="ui header">
				<?php echo $language['database_configuration']; ?>
			</h4>
		</div>
		<div class="ui segment">
			<div class="ui centered grid">
				<div class="sixteen wide mobile twelve wide tablet ten wide computer column">
					<div class="ui form">
						<?php
							create_field('text', $language['database_address'], 'db_address', 'inputDBAddress', '127.0.0.1');
							create_field('text', $language['database_port'], 'db_port', 'inputDBPort', '3306');
							create_field('text', $language['database_username'], 'db_username', 'inputDBUsername');
							create_field('password', $language['database_password'], 'db_password', '<PASSWORD>');
							create_field('text', $language['database_name'], 'db_name', 'inputDBName');
							create_field('select', $language['character_set'], 'charset', 'inputCharset', 'utf8mb4', array(
								'utf8mb4' => 'Unicode (utf8mb4)',
								'latin1' => 'Latin (latin1)',
							));
							create_field('select', $language['database_engine'], 'engine', 'inputEngine', 'InnoDB', array(
								'InnoDB' => 'InnoDB',
								'MyISAM' => 'MyISAM',
							));
						?>
					</div>
				</div>
			</div>
		</div>
		<div class="ui secondary right aligned segment">
			<button type="submit" class="ui small primary button">
				<?php echo $language['proceed']; ?>
{'fraction_non_alphanumeric': 0.21666666666666667, 'fraction_numerical': 0.008454106280193236, 'mean_word_length': 3.7212086659064996, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 138, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '133668', 'n_tokens_mistral': 3525, 'n_tokens_neox': 2848, 'n_words': 537}