Write config

This commit is contained in:
Jay 2021-11-12 17:01:04 +01:00
parent 3eda1f7b16
commit 38ace568a1
6 changed files with 96 additions and 5 deletions

View file

@ -25,6 +25,13 @@ form {
display: inline-block;
}
section {
max-width: 80ch;
padding: 2rem;
background-color: #fff;
}
/**
* Buttons
*/
@ -50,3 +57,21 @@ input[type="submit"] {
a.button {
text-decoration: none;
}
/**
* Inputs
*/
fieldset {
margin: 0 0 1em 0;
padding: 0;
border: none;
}
input[type="text"] {
width: 100%;
padding: 0.4em;
font-size: inherit;
border: 1px solid #f8f8f8;
}

View file

@ -0,0 +1,32 @@
<?php
/**
* database.php
*
* Establishes a database connection using its credentials.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class Database
{
public \PDO $pdo;
public function __construct(
public string $host,
public string $database,
public string $user,
public string $password,
) {
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->database . ';port=3306;charset=utf8';
$options = array();
try {
$this->pdo = new \PDO($dsn, $this->user, $this->password, $options);
} catch (\PDOException $PDOE) {
throw new \PDOException($PDOE->getMessage(), (int)$PDOE->getCode());
}
}
}

View file

@ -5,3 +5,8 @@
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
define('DATABASE_HOST', 'localhost');
define('DATABASE_NAME', 'wishthis');
define('DATABASE_USER', 'root');
define('DATABASE_PASSWORD', '');

View file

@ -12,7 +12,11 @@ $page = new page(__FILE__, 'Home');
$page->header();
?>
<h1>Hello</h1>
<main>
<section>
<h1>Welcome to wishthis</h1>
</section>
</main>
<?php
$page->footer();

View file

@ -6,7 +6,7 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\Page;
use wishthis\{Page, Database};
$page = new page(__FILE__, 'Home');
$page->header();
@ -17,6 +17,12 @@ if (isset($_POST['action']) && 'install' === $_POST['action']) {
$configSamplePath = $configDirectory . '/config-sample.php';
$configContents = file_get_contents($configSamplePath);
foreach ($_POST as $key => $value) {
if ('DATABASE' === substr($key, 0, 8)) {
$configContents = preg_replace('/(' . $key . '.+?\').*?(\')/', '$1' . $value . '$2', $configContents);
}
}
file_put_contents($configPath, $configContents);
?>
@ -35,12 +41,31 @@ if (isset($_POST['action']) && 'install' === $_POST['action']) {
<section>
<h1>Install</h1>
<p>Welcome to the wishthis installer.</p>
<p>Click Install to begin the installation.</p>
<p>wishthis needs a database to function properly. Please enter your credentials.</p>
<form method="post">
<input type="hidden" name="action" value="install" />
<fieldset>
<label>Host</label>
<input type="text" name="DATABASE_HOST" placeholder="localhost" value="localhost" />
</fieldset>
<fieldset>
<label>Name</label>
<input type="text" name="DATABASE_NAME" placeholder="withthis" value="withthis" />
</fieldset>
<fieldset>
<label>Username</label>
<input type="text" name="DATABASE_USER" placeholder="root" value="root" />
</fieldset>
<fieldset>
<label>Password</label>
<input type="text" name="DATABASE_PASSWORD" />
</fieldset>
<input type="submit" value="Install" />
</form>
</section>

View file

@ -20,8 +20,8 @@ autoInclude(__DIR__ . '/includes/functions');
$configPath = 'includes/config/config.php';
if (!file_exists($configPath)) {
$page = 'install';
}
$page = 'install';
/**
* Page