Install.php working, Storage and User class on their way

This commit is contained in:
Phyks 2013-08-08 22:55:12 +02:00
parent bb664e34a4
commit c72585c95b
8 changed files with 129 additions and 63 deletions

1
TODO
View File

@ -1 +1,2 @@
* i18n
* Vérification des variables dans les classes + throw exception

View File

@ -5,18 +5,11 @@ class Storage {
private $host, $login, $password, $db;
private $connection = null;
private function __construct($connection_params = null) {
if(is_array($connection_params) && !empty($connection_params)) {
$this->setHost($connection_params['host']);
$this->setLogin($connection_params['login']);
$this->setPassword($connection_params['password']);
$this->setDb($connection_params['db']);
$this->connect();
}
public function __construct() {
$this->connect();
}
private function __destruct() {
public function __destruct() {
$this->disconnect();
}
@ -60,36 +53,77 @@ class Storage {
}
public function setDb($db) {
this->db = $db;
$this->db = $db;
}
public function typeToSQL($type) {
$return = false;
switch($type) {
case 'key':
$return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY_KEY';
$return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY';
break;
case 'string':
$return = 'VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci';
$return = 'VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci';
break;
case 'bool':
$return = 'TINYINT(1)';
$return = 'TINYINT(1)';
break;
case 'password':
$return = 'VARCHAR(130)';
break;
default:
$return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
$return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
break;
}
}
public function createTable($table_name = null) {
public function save() {
if(!empty($this->id)) {
$query = 'UPDATE `'.MYSQL_PREFIX.$this->TABLE_NAME.'` SET ';
}
$i = false;
foreach($this->fields as $field=>$type) {
if($i) { $query .= ','; } else { $i = true; }
public function initTables() {
$this->createTable('users');
$this->createTable('invoices');
$id = $this->$field;
$query .= '`'.$field.'` = "'.$this($id).'"';
}
$query .= 'WHERE `id`="'.$this->id.'"';
}
else {
$query = 'INSERT INTO '.MYSQL_PREFIX.$this->TABLE_NAME.'(';
$i = false;
foreach($this->fields as $field=>$type) {
if($i) { $query .= ','; } else { $i = true; }
$query .= $field;
}
$query .= ') VALUES(';
$i = false;
foreach($this->fields as $field=>$type) {
if($i) { $query .= ','; } else { $i = true; }
$query .= ':'.$field;
}
$query .= ')';
}
$query = $this->connection->prepare($query);
foreach($this->fields as $field=>$type) {
$query->bindParam(':'.$field, $this->$field);
}
$query->execute();
$this->id = (!isset($this->id) ? $this->connection->lastInsertId() : $this->id);
}
}

View File

@ -1,17 +1,18 @@
<?php
require_once('config.php');
require_once('Storage.class.php');
class User extends Storage {
protected $id, $login, $password;
protected $TALE_NAME = "users";
protected $id, $login, $password, $admin;
protected $TABLE_NAME = "Users";
protected $fields = array(
'id'=>'key',
'nom'=>'string',
'password'=>'string',
'login'=>'string',
'password'=>'password',
'admin'=>'bool'
);
private function __construct() {
public function __construct() {
parent::__construct();
}
@ -23,6 +24,10 @@ class User extends Storage {
return $this->id;
}
public function getAdmin() {
return $this->admin;
}
public function setLogin($login) {
$this->login = $login;
}
@ -31,6 +36,10 @@ class User extends Storage {
$this->password = User::encrypt($password);
}
public function setAdmin($admin) {
$this->admin = $admin;
}
public function encrypt($text) {
return crypt($text, SALT);
}

10
inc/config.php Normal file
View File

@ -0,0 +1,10 @@
<?php
define('VERSION_NUMBER', '2.0');
define('MYSQL_HOST', 'localhost');
define('MYSQL_LOGIN', 'root');
define('MYSQL_PASSWORD', 'lv:05/02/93-mariadb');
define('MYSQL_DB', 'Bouffe@Ulm');
define('MYSQL_PREFIX', 'bouffeatulm_');
define('INSTANCE_TITLE', 'Bouffe@Ulm');
define('BASE_URL', 'http://localhost/Bouffe@Ulm/');
define('SALT', '$2a$10$AXnaxClN4pYlcXGfafGZCA==');

View File

@ -1,6 +1,6 @@
<?php
if(!file_exists('config.php')) header('location: install.php');
if(!file_exists('inc/config.php')) header('location: install.php');
session_start();
require_once('config.php');
require_once('inc/config.php');

0
inc/rain.tpl.class.php Executable file → Normal file
View File

View File

@ -6,45 +6,55 @@
$block_form = true;
}
if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['admin_login']) && !empty($_POST['admin_pass'])) {
if(!is_writable('inc/')) {
$error = "The script seems to be unable to write to <em>inc/</em> folder (to write the <em>inc/config.php</em> configuration file). You should give write access during install and disable them after (chmod 777 -R inc/ to install and chmod 755 -R inc/ after installation for example).";
$block_form = true;
}
if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['admin_login']) && !empty($_POST['admin_password'])) {
$mysql_host = $_POST['mysql_host'];
$mysql_login = $_POST['mysql_login'];
$mysql_db = $_POST['mysql_login'];
$mysql_db = $_POST['mysql_db'];
$mysql_password = $_POST['mysql_password'];
$mysql_prefix = $_POST['mysql_prefix'];
$mysql_prefix = (!empty($_POST['mysql_prefix'])) ? $_POST['mysql_prefix'] : '';
$instance_title = (!empty($_POST['instance_title'])) ? $_POST['instance_title'] : 'Bouffe@Ulm';
try {
$db = new Storage(array('host'=>$mysql_host, 'login'=>$mysql_login, 'password'=>$mysql_password, 'db'=>$mysql_db));
//TODO : Create tables
$db = new PDO('mysql:host='.$mysql_host.';dbname='.$mysql_db, $mysql_login, $mysql_password);
//Create table "Users"
$dump = $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(255), password VARCHAR(130), admin TINYINT(1)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
//Create table "Invoices" - TODO
//Create table "Payback" - TODO
} catch (PDOException $e) {
$error = 'Unable to connect to database, check your credentials.';
$error = 'Unable to connect to database, check your credentials and config.<br/>Error message : '.$e->getMessage().'.';
}
if(empty($error)) {
if(function_exists('mcrypt_create_iv')) {
$salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
}
else {
mt_srand(microtime(true)*100000 + memory_get_usage(true));
$salt = md5(uniqid(mt_rand(), true));
}
$salt = sprintf("$2a$%02d$", 10) . $salt; //prefix for blowfish
define('SALT', $salt);
$config = "
define('VERSION_NUMBER', '2.0');
define('MYSQL_HOST', '".$mysql_host."');
define('MYSQL_LOGIN', '".$mysql_login."');
define('MYSQL_PASSWORD', '".$mysql_password."');
define('MYSQL_DB', '".$mysql_db."');
define('MYSQL_PREFIX', '".$mysql_prefix."');
define('INSTANCE_TITLE', '".$instance_title."');
define('BASE_URL', '".$_POST['base_url']."');
define('SALT', '".$salt."');";
$config = "<?php
define('VERSION_NUMBER', '2.0');
define('MYSQL_HOST', '".$mysql_host."');
define('MYSQL_LOGIN', '".$mysql_login."');
define('MYSQL_PASSWORD', '".$mysql_password."');
define('MYSQL_DB', '".$mysql_db."');
define('MYSQL_PREFIX', '".$mysql_prefix."');
define('INSTANCE_TITLE', '".$instance_title."');
define('BASE_URL', '".$_POST['base_url']."');
define('SALT', '".$salt."');";
if(file_put_contents("inc/config.php", $config)) {
try {
require_once('inc/User.class.php');
$admin = new User();
$admin->setLogin($_POST['admin_login']);
$admin->setPassword($_POST['admin_password']);
@ -53,7 +63,7 @@
header('location: index.php');
exit();
} catch (Exception $e) {
//TODO
$erreur = 'An error occurred when inserting user in the database.<br/> Error message : '.$e->getMessage().'.';
}
}
else
@ -77,7 +87,7 @@
}
?>
<p class="center">This small form will guide you through the installation of Bouffe@Ulm.</p>
<p class="center">This small form will guide you through the installation of Bouffe@Ulm. You must fill in all the fields.</p>
<form action="install.php" method="post">
<fieldset>
@ -90,20 +100,21 @@
<label for="mysql_db">Name of the MySQL database to use : </label><input type="text" name="mysql_db" id="mysql_db" value="<?php echo (!empty($_POST['mysql_db'])) ? htmlspecialchars($_POST['mysql_db']) : 'Bouffe@Ulm';?>"/><br/>
<em>Note :</em> You <em>must</em> create this database first.
</p>
<p><label for="mysql_prefix">Prefix for the created tables : </label><input type="text" name="mysql_prefix" id="mysql_prefix" value="<?php echo (!empty($_POST['mysql_prefix'])) ? htmlspecialchars($_POST['mysql_prefix']) : 'bouffeatulm_';?>"/></p>
<p><label for="mysql_prefix">Prefix for the created tables : </label><input type="text" name="mysql_prefix" id="mysql_prefix" value="<?php echo (!empty($_POST['mysql_prefix'])) ? htmlspecialchars($_POST['mysql_prefix']) : 'bouffeatulm_';?>"/><br/>
<em>Note :</em> Leave the field blank to not use any.</p>
</fieldset>
<fieldset>
<legend>General options</legend>
<p><label for="instance_title">Title to display in pages : </label><input type="text" name="instance_title" id="instance_title" value="Bouffe@Ulm"/></p>
<p><label for="instance_title">Title to display in pages : </label><input type="text" name="instance_title" id="instance_title" value="<?php echo (!empty($_POST['instance_title'])) ? htmlspecialchars($_POST['instance_title']) : 'Bouffe@Ulm';?>"/></p>
<p>
<label for="base_url">Base URL : </label><input type="text" size="30" name="base_url" id="base_url" value="<?php echo 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].str_replace("install.php", "", $_SERVER['REQUEST_URI']); ?>"/><br/>
<em>Note :</em> This is the base URL from which you access this website. You must keep the trailing "/" in the above address.
<label for="base_url">Base URL : </label><input type="text" size="30" name="base_url" id="base_url" value="<?php echo (!empty($_POST['base_url'])) ? htmlspecialchars($_POST['base_url']) : 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].str_replace("install.php", "", $_SERVER['REQUEST_URI']); ?>"/><br/>
<em>Note :</em> This is the base URL from which you access this page. You must keep the trailing "/" in the above address.
</p>
</fieldset>
<fieldset>
<legend>Administrator</legend>
<p><label for="admin_login">Username of the admin : </label><input type="text" name="admin_login" id="admin_login"/></p>
<p><label for="admin_mdp">Password for the admin : </label><input type="password" name="admin_pass" id="admin_pass"/></p>
<p><label for="admin_login">Username of the admin : </label><input type="text" name="admin_login" id="admin_login" <?php echo (!empty($_POST['admin_login'])) ? 'value="'.htmlspecialchars($_POST['admin_login']).'"' : '';?>/></p>
<p><label for="admin_password">Password for the admin : </label><input type="password" name="admin_password" id="admin_password"/></p>
</fieldset>
<p class="center"><input <?php echo (!empty($block_form)) ? 'disabled ' : '';?>type="submit"></p>
</form>

View File

@ -11,12 +11,6 @@ fieldset {
text-align: center;
}
.error {
font-size: 1.5em;
color: red;
font-weight: bold;
}
input[type=submit] {
background-color: green;
color: white;
@ -39,3 +33,10 @@ input[type=submit] {
padding: 0.5em;
margin: 0;
}
#install .error {
font-size: 1.5em;
color: red;
font-weight: bold;
text-align: center;
}