MOD Passage de MySQL à un vrai SGBDR : PostgreSQL
[cl7.git] / php / connexion.php
1 <?php # coding: utf-8
2
3 include_once("participant.php");
4
5 /*
6 * Connexion à la base de données et création du participant courant.
7 */
8 class Connexion
9 {
10 # Message à destination de l'utilisateur, par exemple pour lui indiquer une erreur.
11 public $message_utilisateur = NULL;
12
13 public $participant;
14
15 function Connexion()
16 {
17 if (!file_exists("php/config_bd.php"))
18 {
19 echo "Le fichier 'php/config_bd.php' n'existe pas, création en cours...\n";
20 if (!is_writable("."))
21 {
22 echo "Le dossier 'php' n'est pas accessible en écriture, veuillez changer les droits et recommencer.";
23 exit();
24 }
25 $f = fopen("php/config_bd.php", "w");
26 fwrite($f, '<?php # encoding:utf-8
27 # Parametres de connexion à la BD
28 $SQL_HOTE = "localhost";
29 $SQL_LOGIN = "";
30 $SQL_PASS = "";
31 $NOM_BASE = "corcelles_lan_7";?>'
32 );
33 fclose($f);
34 echo "Le fichier a été créé, veuillez le compléter et recommencer.";
35 exit();
36 }
37
38 include_once("config_bd.php");
39
40 $conn_bd = pg_connect(sprintf("host=%s dbname=%s user=%s password=%s", $SQL_HOTE, $NOM_BASE, $SQL_LOGIN, $SQL_PASS));
41 if (!$conn_bd)
42 {
43 echo "Connexion à la base de données impossible. Voir le fichier 'php/config_bd.php'";
44 exit();
45 }
46
47 //mysql_set_charset("UTF8");
48 //mysql_query('SET AUTOCOMMIT=0');
49
50 if (isset($_POST['effacer_cookie'])) # le membre se délogue
51 {
52 setcookie("COOKIE_INFO_PARTICIPANT", "", time() - 100); # 'efface' le cookie membre
53 unset($_COOKIE["COOKIE_INFO_PARTICIPANT"]);
54 }
55
56 if (isset($_POST['log'])) # le membre se logue
57 {
58 $this->participant = new Participant($_POST['pseudo'], $_POST['password']);
59 if ($this->participant->valide)
60 {
61 setcookie("COOKIE_INFO_PARTICIPANT", $this->participant->info->id, time() + 31104000);
62 $this->message_utilisateur = "Loggation ok";
63 }
64 else
65 $this->message_utilisateur = "Erreur de loggation";
66 }
67 else if (isset($_COOKIE["COOKIE_INFO_PARTICIPANT"])) # le cookie existe deja chez le participant
68 {
69 $this->participant = new Participant($_COOKIE["COOKIE_INFO_PARTICIPANT"]);
70 }
71 else
72 {
73 $this->participant = new Participant();
74 }
75 }
76 }
77
78 ?>