Overview
Comment:+ Garradin_DB étends SQLite3 + Singleton pour DB et Config + Mise à jour de Config + Débuts de Membres
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: aa141e2493453a9b202a1d12c816bdfb502d272e
User & Date: bohwaz on 2011-11-17 03:39:57
Other Links: manifest | tags
Context
2011-11-17
03:43
Ajout lib JS-AES pour chiffrement wiki check-in: 0f0a9d08f9 user: bohwaz tags: trunk
03:39
+ Garradin_DB étends SQLite3 + Singleton pour DB et Config + Mise à jour de Config + Débuts de Membres check-in: aa141e2493 user: bohwaz tags: trunk
2011-11-15
18:34
Base des premiers objets Schéma DB check-in: 4c36ea10e5 user: bohwaz tags: trunk
Changes

Modified DB_SCHEMA from [70e716c89c] to [f4ec867b9c].

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

-- Membres de l'asso
CREATE TABLE membres (
    id INTEGER PRIMARY KEY,
    id_categorie INTEGER,

    -- Connexion
    pseudo TEXT,
    passe TEXT,

    -- Données personnelles
    nom TEXT,
    email TEXT,

    adresse TEXT,







<







48
49
50
51
52
53
54

55
56
57
58
59
60
61

-- Membres de l'asso
CREATE TABLE membres (
    id INTEGER PRIMARY KEY,
    id_categorie INTEGER,

    -- Connexion

    passe TEXT,

    -- Données personnelles
    nom TEXT,
    email TEXT,

    adresse TEXT,

Modified include/class.config.php from [97fca59b70] to [d82b85b630].

1
2
3
4



5

6




7



8







9





10



























11


12
13

14





15






16
17
18
19

20




21

22


















23





24

25
26
27
28
<?php

class Garradin_Config extends Garradin_DB
{



    const TYPE_CATEGORIE_COMPTA = 'categorie_compta';

    const TYPE_CATEGORIE_MEMBRE = 'categorie_membre';




    const TYPE_TEXTE = 'texte';



    const TYPE_NUMERIQUE = 'numerique';







    const TYPE_BOOL = 'bool';





    const TYPE_CHOIX = 'choix';






























    protected function _initChamps()
    {

        $this->_ajoutChamp('asso_nom', self::TYPE_TEXTE, 'Mon asso');





        $this->_ajoutChamp('asso_adresse', self::TYPE_TEXTE, "42 rue des soupirs,\n21000 Dijon");






        $this->_ajoutChamp('asso_email', self::TYPE_TEXTE, "invalid@invalid.invalid");
        $this->_ajoutChamp('asso_site', self::TYPE_TEXTE, "http://example.tld/");

        $this->_ajoutChamp('email_expediteur', self::TYPE_TEXTE, "invalid@invalid.invalid");






        //$this->_ajoutChamp('membres_champs_obligatoires', self::TYPE_CHOIX_MULTIPLE, 'nom,




















        $this->_ajoutChamp('compta_categorie_cotisations', self::TYPE_CATEGORIE_COMPTA, 1);





        $this->_ajoutChamp('compta_categorie_dons', self::TYPE_CATEGORIE_COMPTA, 2);

    }
}

?>


|

>
>
>
|
>
|
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|

>
|
>
>
>
>
>
|
>
>
>
>
>
>
|
|
|
|
>
|
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
|
>




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

class Garradin_Config
{
    protected $fields_types = null;
    protected $config = null;
    protected $modified = false;

    static protected $_instance = null;

    static public function getInstance()
    {
        return self::$_instance ?: self::$_instance = new Garradin_Config;
    }

    private function __clone()
    {
    }

    protected function __construct()
    {
        $string = '';
        $int = 0;
        $float = 0.0;
        $array = array();
        $bool = false;

        $this->fields_types = array(
            'nom_asso'              =>  $string,
            'adresse_asso'          =>  $string,
            'email_asso'            =>  $string,
            'site_asso'             =>  $string,

            'email_envoi_automatique'=> $string,

            'champs_obligatoires'   =>  $array,

            'categorie_dons'        =>  $int,
            'categorie_cotisations' =>  $int,
        );

        $db = Garradin_DB::getInstance();

        $this->config = $db->queryAssociativeFetch('SELECT cle, valeur FROM config ORDER BY cle;');

        foreach ($this->config as $key=>&$value)
        {
            if (!array_key_exists($key, $this->fields_types))
            {
                throw new OutOfBoundsException('Le champ "'.$key.'" est inconnu.');
            }

            if (is_array($this->fields_types[$key]))
            {
                $value = json_decode($value, true);
            }
            else
            {
                settype($value, gettype($this->fields_types[$key]));
            }
        }
    }

    public function __destruct()
    {
    }

    public function save()
    {
        $values = $config;
        // serialization des valeurs (floatval, etc.) + SQL query
    }

    public function get($key)
    {
        if (!array_key_exists($key, $this->config))
        {
            throw new OutOfBoundsException('Ce champ est inconnu.');
        }

        return $this->config[$key];
    }

    public function set($key, $value)
    {
        if (!array_key_exists($key, $this->fields_types))
        {
            throw new OutOfBoundsException('Ce champ est inconnu.');
        }

        if (is_array($this->fields_types[$key]))
        {
            $value = (array) $value;
        }
        elseif (is_int($this->fields_types[$key]))
        {
            $value = (int) $value;
        }
        elseif (is_float($this->fields_types[$key]))
        {
            $value = (float) $value;
        }
        elseif (is_bool($this->fields_types[$key]))
        {
            $value = (bool) $value;
        }
        elseif (is_string($this->fields_types[$key]))
        {
            $value = (string) $value;
        }

        if ($value !== $this->config[$key])
        {
            $this->config[$key] = $value;
            $this->modified = true;
        }

        return true;
    }
}

?>

Modified include/class.db.php from [58e65c050e] to [daa789e03c].

1
2
3
4
5
6











7
8
9
10
11
12
13
14
15
16
17
18
19











































































20
21
22
23
24
25
26
27
<?php

class Garradin_DB
{
    protected $db = null;












    public function __construct()
    {
        $exists = file_exists(GARRADIN_DB_FILE) ? true : false;

        $this->db = new SQLite3(GARRADIN_DB_FILE);

        if (!$exists)
        {
            $this->db->exec('BEGIN;');
            $this->db->exec(file_get_contents(GARRADIN_DB_SCHEMA));
            $this->db->exec('END;');
        }
    }












































































    public function __destruct()
    {
        $this->db->close();
    }
}

?>


|



>
>
>
>
>
>
>
>
>
>
>
|



|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

class Garradin_DB extends SQLite3
{
    protected $db = null;

    static protected $_instance = null;

    static public function getInstance()
    {
        return self::$_instance ?: self::$_instance = new Garradin_DB;
    }

    private function __clone()
    {
    }

    protected function __construct()
    {
        $exists = file_exists(GARRADIN_DB_FILE) ? true : false;

        $this->db = parent::construct(GARRADIN_DB_FILE);

        if (!$exists)
        {
            $this->db->exec('BEGIN;');
            $this->db->exec(file_get_contents(GARRADIN_DB_SCHEMA));
            $this->db->exec('END;');
        }
    }

    public function escape($str)
    {
        return $this->escapeString($str);
    }

    public function e($str)
    {
        return $this->escapeString($str);
    }

    public function simpleStatement($query)
    {
        $statement = $this->prepare($query);

        for ($i = 2; $i <= func_num_args(); $i++)
        {
            $arg = func_get_arg($i - 1);

            if (is_float($arg)) $type = SQLITE3_FLOAT;
            elseif (is_numeric($arg)) $type = SQLITE3_INTEGER;
            elseif (is_bool($arg)) $type = SQLITE3_INTEGER;
            elseif (is_null($arg)) $type = SQLITE3_NULL;
            else $type = SQLITE3_TEXT;

            $statement->bindValue($i - 1, $arg, $type);
        }

        return $statement->execute();
    }

    public function simpleStatementFetch($query, $mode = SQLITE3_BOTH)
    {
        return $this->_fetchResult($this->simpleStatement($query), $mode);
    }

    public function queryFetch($query, $mode = SQLITE3_BOTH)
    {
        return $this->_fetchResult($this->query($query));
    }

    public function queryFetchAssoc($query)
    {
        return $this->_fetchResultAssoc($this->query($query));
    }

    protected function _fetchResult($result, $mode)
    {
        $out = array();

        while ($row = $result->fetchArray($mode))
        {
            $out[] = $row;
        }

        $res->finalize();
        unset($res, $row);

        return $out;
    }

    protected function _fetchResultAssoc($result)
    {
        $out = array();

        while ($row = $result->fetchArray(SQLITE3_NUM))
        {
            $out[$row[0]] = $row[1];
        }

        $res->finalize();
        unset($res, $row);

        return $out;
    }

    public function __destruct()
    {
        $this->db->close();
    }
}

?>

Modified include/class.membres.php from [5152366900] to [94deebcaa3].

34
35
36
37
38
39
40














































41
42



43














































44
45
46
    }

    protected function _checkPassword($password, $stored_hash)
    {
        return crypt($password, $stored_hash) == $stored_hash;
    }















































    public function connexion($pseudo, $passe)
    {



    }














































}

?>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    }

    protected function _checkPassword($password, $stored_hash)
    {
        return crypt($password, $stored_hash) == $stored_hash;
    }

    protected function _sessionStart($force = false)
    {
        if (!isset($_SESSION) && ($force || isset($_COOKIE[session_name()])))
            @session_start();

        return true;
    }

    protected function _login($user)
    {
        $this->_sessionStart(true);
        $_SESSION['logged_user'] = $user;
        return true;
    }

    public function login($email, $passe)
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            return false;

        $r = $this->db->querySingle('SELECT * FROM membres WHERE email=\''.$this->db->escapeString($email).'\' LIMIT 1;', true);

        if (empty($r))
            return false;

        if (!$this->_checkPassword($passe, $r['passe']))
            return false;

        return $this->_login($r);
    }

    public function isLogged()
    {
        $this->_sessionStart();

        return empty($_SESSION['logged_user']) ? false : true;
    }

    public function getLoggedUser()
    {
        if (!$this->isLogged())
            return false;

        return $_SESSION['logged_user'];
    }

    public function logout()
    {
        $_SESSION = array();
        setcookie(session_name(), '', 0, '/');
        return true;
    }

    public function _checkFields($data)
    {
        $mandatory = Config::getInstance()->get('champs_obligatoires');

        foreach ($mandatory as $field)
        {
            if (!array_key_exists($field, $data) || !trim($data[$field]))
            {
                throw new UserException('Le champ \''.$field.'\' ne peut rester vide.');
            }
        }

        if (isset($data['email']) && !filter_var($data['email'], FILTER_VALIDATE_EMAIL))
        {
            throw new UserException('Adresse e-mail \''.$field.'\' invalide.');
        }

        return true;
    }

    public function add($data = array())
    {
        $this->_checkFields($data);
        // INSERT SQL
    }

    public function edit($id, $data = array())
    {
        $this->_checkFields($data);
        // UPDATE SQL
    }

    public function remove($id)
    {
    }

    public function search($query)
    {
    }

    public function getList($page = 1)
    {
    }

    public function
}

?>

Modified include/init.php from [cc6a9b1515] to [d630745c30].

11
12
13
14
15
16
17



18
19
20
21
22
23
24
25
26
27
28
29




30
31
32




33
34
35
36





37



38
39
40


































41
        =>  !defined('CRYPT_BLOWFISH') || !CRYPT_BLOWFISH,
    'Module de bases de données SQLite3 n\'est pas installé'
        =>  !class_exists('SQLite3'),
    'Dummy' => true,
);

$fail = false;




foreach ($tests as $desc=>$fail)
{
    if ($fail)
    {
        echo $desc . "\n";
    }
}

if ($fail)
{
    echo "Erreur fatale : Garradin a besoin que la condition mentionnée soit remplie pour s'exécuter.\n";




    exit;
}





define('GARRADIN_ROOT', __DIR__ . '/..');
define('GARRADIN_DB_FILE', GARRADIN_ROOT . '/garradin_asso.db');
define('GARRADIN_DB_SCHEMA', GARRADIN_ROOT . '/DB_SCHEMA');






class Garradin_Exception extends Exception {};



class Garradin_Internal_Exception extends Garradin_Exception {};
class Garradin_User_Exception extends Garradin_Exception {};



































?>







>
>
>












>
>
>
>



>
>
>
>
|



>
>
>
>
>
|
>
>
>
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        =>  !defined('CRYPT_BLOWFISH') || !CRYPT_BLOWFISH,
    'Module de bases de données SQLite3 n\'est pas installé'
        =>  !class_exists('SQLite3'),
    'Dummy' => true,
);

$fail = false;

if (PHP_SAPI != 'cli' && array_sum($tests) > 0)
    echo '<pre>';

foreach ($tests as $desc=>$fail)
{
    if ($fail)
    {
        echo $desc . "\n";
    }
}

if ($fail)
{
    echo "Erreur fatale : Garradin a besoin que la condition mentionnée soit remplie pour s'exécuter.\n";

    if (PHP_SAPI != 'cli')
        echo '</pre>';

    exit;
}

/*
 * Configuration globale
 */

define('GARRADIN_ROOT', dirname(__DIR__));
define('GARRADIN_DB_FILE', GARRADIN_ROOT . '/garradin_asso.db');
define('GARRADIN_DB_SCHEMA', GARRADIN_ROOT . '/DB_SCHEMA');

// Automagic URL discover
$path = substr(__DIR__ . '/www', strlen($_SERVER['DOCUMENT_ROOT']));
$path = (!empty($path[0]) && $path[0] != '/') ? '/' . $path : $path;
$path = (substr($path, -1) != '/') ? $path . '/' : $path;
define('LOCAL_URL', 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $path);

/*
 * Gestion des erreurs et exceptions
 */

class UserException extends Garradin_Exception {};

error_reporting(E_ALL);

function exception_error_handler($errno, $errstr, $errfile, $errline )
{
    // For @ ignored errors
    if (error_reporting() === 0) return;
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

function exception_handler($e)
{
    if ($e instanceOf UserException)
    {
        echo '<h3>'.$e->getMessage().'</h3>';
        exit;
    }

    $error = "Error happened !\n\n".
        $e->getCode()." - ".$e->getMessage()."\n\nIn: ".
        $e->getFile() . ":" . $e->getLine()."\n\n";

    if (!empty($_SERVER['HTTP_HOST']))
        $error .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n\n";

    $error .= $e->getTraceAsString();
    //$error .= print_r($_SERVER, true);

    echo $error;
    exit;
}

set_error_handler("exception_error_handler");
set_exception_handler("exception_handler");

?>