Browse Source

Starting to create the classes

Phyks (Lucas Verney) 7 years ago
parent
commit
561af52a31
10 changed files with 228 additions and 15 deletions
  1. 2
    0
      .gitignore
  2. 16
    0
      humans.txt
  3. 0
    0
      inc/Invoices.class.php
  4. 0
    0
      inc/Payback.class.php
  5. 95
    0
      inc/Storage.class.php
  6. 41
    0
      inc/User.class.php
  7. 3
    1
      inc/config.php.sample
  8. 2
    0
      inc/header.php
  9. 21
    0
      index.php
  10. 48
    14
      install.php

+ 2
- 0
.gitignore View File

@@ -1 +1,3 @@
1 1
 *~
2
+.*.sw*
3
+old

+ 16
- 0
humans.txt View File

@@ -0,0 +1,16 @@
1
+/* TEAM */
2
+    Name : Phyks
3
+    Site : http://www.phyks.me
4
+    E-mail : phyks@phyks.me
5
+    Location : France.
6
+
7
+    Name : Cphyc
8
+    Site : http://www.cphyc.me
9
+    E-mail : contact@cphyc.me
10
+    Location : France.
11
+
12
+/* THANKS */
13
+    Baltazar for the algorithm to simplify the matrix of debts
14
+
15
+/* LICENSE */
16
+    BEER-WARE LICENSE (see LICENSE or README.md file for more infos)

+ 0
- 0
inc/Invoices.class.php View File


+ 0
- 0
inc/Payback.class.php View File


+ 95
- 0
inc/Storage.class.php View File

@@ -0,0 +1,95 @@
1
+<?php
2
+require_once('config.php');
3
+
4
+class Storage {
5
+    private $host, $login, $password, $db;
6
+    private $connection = null;
7
+
8
+    private function __construct($connection_params = null) {
9
+        if(is_array($connection_params) && !empty($connection_params)) {
10
+            $this->setHost($connection_params['host']);
11
+            $this->setLogin($connection_params['login']);
12
+            $this->setPassword($connection_params['password']);
13
+            $this->setDb($connection_params['db']);
14
+
15
+            $this->connect();
16
+        }
17
+    }
18
+
19
+    private function __destruct() {
20
+        $this->disconnect();
21
+    }
22
+
23
+    //Connect / Disconnect functions
24
+    public function connect() {
25
+        $this->connection = new PDO('mysql:host='.MYSQL_HOST.';dbname='.MYSQL_DB, MYSQL_LOGIN, MYSQL_PASSWORD);
26
+        $this->connection->query('SET NAMES utf8');
27
+    }
28
+
29
+    public function disconnect() {
30
+        $this->connection = null;
31
+    }
32
+
33
+    //Function to get and set vars
34
+    public function getHost() {
35
+        return $this->host;
36
+    }
37
+
38
+    public function getLogin() {
39
+        return $this->login;
40
+    }
41
+
42
+    public function getPassword() {
43
+        return $this->password;
44
+    }
45
+
46
+    public function getDb() {
47
+        return $this->db;
48
+    }
49
+
50
+    public function setHost($host) {
51
+        $this->host = host;
52
+    }
53
+
54
+    public function setLogin($login) {
55
+        $this->login = $login;
56
+    }
57
+
58
+    public function setPassword($password) {
59
+        $this->password = $password;
60
+    }
61
+
62
+    public function setDb($db) {
63
+        this->db = $db;
64
+    }
65
+
66
+    public function typeToSQL($type) {
67
+        $return = false;
68
+        switch($type) {
69
+            case 'key':
70
+                    $return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY_KEY'; 
71
+                break;
72
+
73
+            case 'string':
74
+                    $return = 'VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci';
75
+                break;
76
+
77
+            case 'bool':
78
+                    $return = 'TINYINT(1)';
79
+                break;
80
+
81
+            default:
82
+                    $return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
83
+                break;
84
+        }
85
+    }
86
+
87
+    public function createTable($table_name = null) {
88
+
89
+    }
90
+
91
+    public function initTables() {
92
+        $this->createTable('users');
93
+        $this->createTable('invoices');
94
+    } 
95
+}

+ 41
- 0
inc/User.class.php View File

@@ -0,0 +1,41 @@
1
+<?php
2
+require_once('config.php');
3
+
4
+class User extends Storage {
5
+    protected $id, $login, $password;
6
+    protected $TALE_NAME = "users";
7
+    protected $fields = array(
8
+        'id'=>'key',
9
+        'nom'=>'string',
10
+        'password'=>'string',
11
+        'admin'=>'bool'
12
+        );
13
+
14
+    private function __construct() {
15
+        parent::__construct();
16
+    }
17
+
18
+    public function getLogin() {
19
+        return $this->login;
20
+    }
21
+
22
+    public function getId() {
23
+        return $this->id;
24
+    }
25
+    
26
+    public function setLogin($login) {
27
+        $this->login = $login;
28
+    }
29
+
30
+    public function setPassword($password) {
31
+        $this->password = User::encrypt($password);
32
+    }
33
+
34
+    public function encrypt($text) {
35
+        return crypt($text, SALT);
36
+    }
37
+
38
+    public function check_password($password) {
39
+        return User::encrypt($password) == $this->password;
40
+    }
41
+}

+ 3
- 1
inc/config.php.sample View File

@@ -4,7 +4,9 @@
4 4
     define('MYSQL_HOST', 'localhost');
5 5
     define('MYSQL_LOGIN', '');
6 6
     define('MYSQL_PASSWORD', '');
7
-    define('MYSQL_BDD', '');
7
+    define('MYSQL_DB', '');
8 8
     define('MYSQL_PREFIX', '');
9 9
 
10 10
     define('TITLE', 'Bouffe@Ulm');
11
+    define('BASE_URL', 'http://monsite.com/BouffeAtUlm/');
12
+    define('SALT', 'longandcomplicatedstring');

+ 2
- 0
inc/header.php View File

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

+ 21
- 0
index.php View File

@@ -1,2 +1,23 @@
1 1
 <?php
2 2
     require_once('inc/header.php');
3
+
4
+init(true,false) {
5
+    global $bdd;
6
+
7
+    $bdd = new PDO('mysql:host='.MYSQL_HOST.';dbname='.MYSQL_DB, MYSQL_LOGIN, MYSQL_PASSWORD);
8
+    $bdd->query("SET NAMES 'utf8'");
9
+
10
+    session_start();
11
+
12
+    date_default_timezone_set(TIMEZONE);
13
+
14
+    if($protect && empty($_SESSION['login'])) {
15
+        header('location: connexion.php');
16
+        exit();
17
+    }
18
+
19
+    if($admin && $_SESSION['admin']) {
20
+        header('location: message.php?id=7');
21
+        exit();
22
+    }
23
+}

+ 48
- 14
install.php View File

@@ -6,7 +6,7 @@
6 6
         $block_form = true;
7 7
     }
8 8
 
9
-    if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db'])) {
9
+    if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['admin_login']) && !empty($_POST['admin_pass'])) {
10 10
         $mysql_host = $_POST['mysql_host'];
11 11
         $mysql_login = $_POST['mysql_login'];
12 12
         $mysql_db = $_POST['mysql_login'];
@@ -15,24 +15,50 @@
15 15
         $instance_title = (!empty($_POST['instance_title'])) ? $_POST['instance_title'] : 'Bouffe@Ulm';
16 16
 
17 17
         try {
18
-            $db = new PDO("mysql:host=".$mysql_host.";dbname=".$mysql_db, $mysql_login, $mysql_password);
19
-        }
20
-        catch (PDOException $e) {
18
+            $db = new Storage(array('host'=>$mysql_host, 'login'=>$mysql_login, 'password'=>$mysql_password, 'db'=>$mysql_db);
19
+            //TODO : Create tables
20
+        } catch (PDOException $e) {
21 21
             $error = 'Unable to connect to database, check your credentials.';
22 22
         }
23 23
 
24 24
         if(empty($error)) {
25
-            $config = "
26
-                define('VERSION_NUMBER', '2.0');
25
+                if(function_exists('mcrypt_create_iv')) {
26
+                    $salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
27
+                }
28
+                else {
29
+                    mt_srand(microtime(true)*100000 + memory_get_usage(true));
30
+                    $salt = md5(uniqid(mt_rand(), true));
31
+                }
27 32
 
28
-                define('MYSQL_HOST', '".$mysql_host."');
29
-                define('MYSQL_LOGIN', '".$mysql_login."');
30
-                define('MYSQL_PASSWORD', '".$mysql_password."');
31
-                define('MYSQL_DB', '".$mysql_db."');
32
-                define('MYSQL_PREFIX', '".$mysql_prefix."');
33
+                define('SALT', $salt);
34
+                
35
+                $config = "
36
+                    define('VERSION_NUMBER', '2.0');
37
+                    define('MYSQL_HOST', '".$mysql_host."');
38
+                    define('MYSQL_LOGIN', '".$mysql_login."');
39
+                    define('MYSQL_PASSWORD', '".$mysql_password."');
40
+                    define('MYSQL_DB', '".$mysql_db."');
41
+                    define('MYSQL_PREFIX', '".$mysql_prefix."');
42
+                    define('INSTANCE_TITLE', '".$instance_title."');
43
+                    define('BASE_URL', '".$_POST['base_url']."');
44
+                    define('SALT', '".$salt."');";
33 45
 
34
-                define('INSTANCE_TITLE', '".$instance_title."');";
35
-            file_put_contents("inc/config.php", $config);
46
+                if(file_put_contents("inc/config.php", $config)) {
47
+                    try {
48
+                        $admin = new User();
49
+                        $admin->setLogin($_POST['admin_login']);
50
+                        $admin->setPassword($_POST['admin_password']);
51
+                        $admin->setAdmin(true);
52
+                        $admin->save();
53
+                        header('location: index.php');
54
+                        exit();
55
+                    } catch ($e) {
56
+                        //TODO
57
+                    }
58
+                }
59
+                else
60
+                    $error = 'Unable to write configuration to config file inc/config.php.';
61
+            }
36 62
         }
37 63
     }
38 64
 ?>
@@ -70,8 +96,16 @@
70 96
             <fieldset>
71 97
                 <legend>General options</legend>
72 98
                 <p><label for="instance_title">Title to display in pages : </label><input type="text" name="instance_title" id="instance_title" value="Bouffe@Ulm"/></p>
99
+                <p>
100
+                    <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/>
101
+                    <em>Note :</em> This is the base URL from which you access this website. You must keep the trailing "/" in the above address.
102
+                </p>
103
+            </fieldset>
104
+            <fieldset>
105
+                <legend>Administrator</legend>
106
+                <p><label for="admin_login">Username of the admin : </label><input type="text" name="admin_login" id="admin_login"/></p>
107
+                <p><label for="admin_mdp">Password for the admin : </label><input type="password" name="admin_pass" id="admin_pass"/></p>
73 108
             </fieldset>
74
-
75 109
             <p><input <?php echo (!empty($block_form)) ? 'disabled ' : '';?>type="submit" class="center"></p>
76 110
         </form>
77 111
     </body>