From 561af52a3172e9a0d055339b7a8ff94688f2bee8 Mon Sep 17 00:00:00 2001 From: Phyks Date: Wed, 7 Aug 2013 23:29:57 +0200 Subject: [PATCH] Starting to create the classes --- .gitignore | 2 + humans.txt | 16 +++++++ inc/Invoices.class.php | 0 inc/Payback.class.php | 0 inc/Storage.class.php | 95 ++++++++++++++++++++++++++++++++++++++++++ inc/User.class.php | 41 ++++++++++++++++++ inc/config.php.sample | 4 +- inc/header.php | 2 + index.php | 21 ++++++++++ install.php | 62 ++++++++++++++++++++------- 10 files changed, 228 insertions(+), 15 deletions(-) create mode 100644 inc/Invoices.class.php create mode 100644 inc/Payback.class.php create mode 100644 inc/Storage.class.php create mode 100644 inc/User.class.php diff --git a/.gitignore b/.gitignore index b25c15b..0bc0ff9 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *~ +.*.sw* +old diff --git a/humans.txt b/humans.txt index e69de29..aa8fde6 100644 --- a/humans.txt +++ b/humans.txt @@ -0,0 +1,16 @@ +/* TEAM */ + Name : Phyks + Site : http://www.phyks.me + E-mail : phyks@phyks.me + Location : France. + + Name : Cphyc + Site : http://www.cphyc.me + E-mail : contact@cphyc.me + Location : France. + +/* THANKS */ + Baltazar for the algorithm to simplify the matrix of debts + +/* LICENSE */ + BEER-WARE LICENSE (see LICENSE or README.md file for more infos) diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php new file mode 100644 index 0000000..e69de29 diff --git a/inc/Payback.class.php b/inc/Payback.class.php new file mode 100644 index 0000000..e69de29 diff --git a/inc/Storage.class.php b/inc/Storage.class.php new file mode 100644 index 0000000..9c9321b --- /dev/null +++ b/inc/Storage.class.php @@ -0,0 +1,95 @@ +setHost($connection_params['host']); + $this->setLogin($connection_params['login']); + $this->setPassword($connection_params['password']); + $this->setDb($connection_params['db']); + + $this->connect(); + } + } + + private function __destruct() { + $this->disconnect(); + } + + //Connect / Disconnect functions + public function connect() { + $this->connection = new PDO('mysql:host='.MYSQL_HOST.';dbname='.MYSQL_DB, MYSQL_LOGIN, MYSQL_PASSWORD); + $this->connection->query('SET NAMES utf8'); + } + + public function disconnect() { + $this->connection = null; + } + + //Function to get and set vars + public function getHost() { + return $this->host; + } + + public function getLogin() { + return $this->login; + } + + public function getPassword() { + return $this->password; + } + + public function getDb() { + return $this->db; + } + + public function setHost($host) { + $this->host = host; + } + + public function setLogin($login) { + $this->login = $login; + } + + public function setPassword($password) { + $this->password = $password; + } + + public function setDb($db) { + this->db = $db; + } + + public function typeToSQL($type) { + $return = false; + switch($type) { + case 'key': + $return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY_KEY'; + break; + + case 'string': + $return = 'VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci'; + break; + + case 'bool': + $return = 'TINYINT(1)'; + break; + + default: + $return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci'; + break; + } + } + + public function createTable($table_name = null) { + + } + + public function initTables() { + $this->createTable('users'); + $this->createTable('invoices'); + } +} diff --git a/inc/User.class.php b/inc/User.class.php new file mode 100644 index 0000000..6ec2466 --- /dev/null +++ b/inc/User.class.php @@ -0,0 +1,41 @@ +'key', + 'nom'=>'string', + 'password'=>'string', + 'admin'=>'bool' + ); + + private function __construct() { + parent::__construct(); + } + + public function getLogin() { + return $this->login; + } + + public function getId() { + return $this->id; + } + + public function setLogin($login) { + $this->login = $login; + } + + public function setPassword($password) { + $this->password = User::encrypt($password); + } + + public function encrypt($text) { + return crypt($text, SALT); + } + + public function check_password($password) { + return User::encrypt($password) == $this->password; + } +} diff --git a/inc/config.php.sample b/inc/config.php.sample index 23204c9..a06948a 100644 --- a/inc/config.php.sample +++ b/inc/config.php.sample @@ -4,7 +4,9 @@ define('MYSQL_HOST', 'localhost'); define('MYSQL_LOGIN', ''); define('MYSQL_PASSWORD', ''); - define('MYSQL_BDD', ''); + define('MYSQL_DB', ''); define('MYSQL_PREFIX', ''); define('TITLE', 'Bouffe@Ulm'); + define('BASE_URL', 'http://monsite.com/BouffeAtUlm/'); + define('SALT', 'longandcomplicatedstring'); diff --git a/inc/header.php b/inc/header.php index 9134832..0856396 100644 --- a/inc/header.php +++ b/inc/header.php @@ -1,4 +1,6 @@ query("SET NAMES 'utf8'"); + + session_start(); + + date_default_timezone_set(TIMEZONE); + + if($protect && empty($_SESSION['login'])) { + header('location: connexion.php'); + exit(); + } + + if($admin && $_SESSION['admin']) { + header('location: message.php?id=7'); + exit(); + } +} diff --git a/install.php b/install.php index 0c7b7d0..00a97c4 100644 --- a/install.php +++ b/install.php @@ -6,7 +6,7 @@ $block_form = true; } - if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db'])) { + if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['admin_login']) && !empty($_POST['admin_pass'])) { $mysql_host = $_POST['mysql_host']; $mysql_login = $_POST['mysql_login']; $mysql_db = $_POST['mysql_login']; @@ -15,24 +15,50 @@ $instance_title = (!empty($_POST['instance_title'])) ? $_POST['instance_title'] : 'Bouffe@Ulm'; try { - $db = new PDO("mysql:host=".$mysql_host.";dbname=".$mysql_db, $mysql_login, $mysql_password); - } - catch (PDOException $e) { + $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.'; } if(empty($error)) { - $config = " - define('VERSION_NUMBER', '2.0'); + if(function_exists('mcrypt_create_iv')) { + $salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); + } + else { + mt_srand(microtime(true)*100000 + memory_get_usage(true)); + $salt = md5(uniqid(mt_rand(), true)); + } - 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('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."');"; - define('INSTANCE_TITLE', '".$instance_title."');"; - file_put_contents("inc/config.php", $config); + if(file_put_contents("inc/config.php", $config)) { + try { + $admin = new User(); + $admin->setLogin($_POST['admin_login']); + $admin->setPassword($_POST['admin_password']); + $admin->setAdmin(true); + $admin->save(); + header('location: index.php'); + exit(); + } catch ($e) { + //TODO + } + } + else + $error = 'Unable to write configuration to config file inc/config.php.'; + } } } ?> @@ -70,8 +96,16 @@
General options

+

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

+
+
+ Administrator +

+

-

type="submit" class="center">