diff --git a/TODO b/TODO index 1997329..b21a35f 100755 --- a/TODO +++ b/TODO @@ -1 +1,2 @@ * i18n +* Vérification des variables dans les classes + throw exception diff --git a/inc/Storage.class.php b/inc/Storage.class.php index 9c9321b..4f6bfc4 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -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; } + + $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); } - - public function initTables() { - $this->createTable('users'); - $this->createTable('invoices'); - } } diff --git a/inc/User.class.php b/inc/User.class.php index 6ec2466..ef0f10e 100644 --- a/inc/User.class.php +++ b/inc/User.class.php @@ -1,17 +1,18 @@ 'key', - 'nom'=>'string', - 'password'=>'string', + 'login'=>'string', + 'password'=>'password', 'admin'=>'bool' ); - private function __construct() { + public function __construct() { parent::__construct(); } @@ -22,6 +23,10 @@ class User extends Storage { public function getId() { 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); } diff --git a/inc/config.php b/inc/config.php new file mode 100644 index 0000000..a743d0c --- /dev/null +++ b/inc/config.php @@ -0,0 +1,10 @@ +inc/ folder (to write the inc/config.php 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 - } catch (PDOException $e) { - $error = 'Unable to connect to database, check your credentials.'; - } + $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 and config.
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 = "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.
Error message : '.$e->getMessage().'.'; } } else @@ -77,7 +87,7 @@ } ?> -

This small form will guide you through the installation of Bouffe@Ulm.

+

This small form will guide you through the installation of Bouffe@Ulm. You must fill in all the fields.

@@ -90,20 +100,21 @@
Note : You must create this database first.

-

+


+ Note : Leave the field blank to not use any.

General options -

+

- "/>
- Note : This is the base URL from which you access this website. You must keep the trailing "/" in the above address. + "/>
+ Note : This is the base URL from which you access this page. You must keep the trailing "/" in the above address.

Administrator -

-

+

/>

+

type="submit">

diff --git a/tpl/css/style.css b/tpl/css/style.css index 950c94b..462f547 100644 --- a/tpl/css/style.css +++ b/tpl/css/style.css @@ -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; +}