-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
200 lines (153 loc) · 4.43 KB
/
db.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
ini_set('session.save_path', 'sessions');
session_start();
function getPropertyValue($str){
preg_match('~=(.*?);~', $str, $output);
return $output[1];
}
function getProperties(){
$file = fopen("Restricted/connexion.properties", "r");
$properties["host"] = getPropertyValue(fgets($file));
$properties["user"] = getPropertyValue(fgets($file));
$properties["password"] = getPropertyValue(fgets($file));
$properties["bdname"] = getPropertyValue(fgets($file));
return $properties;
}
function Connect_db(){
if(($properties = getProperties()) != null){
$host=$properties["host"];
$user=$properties["user"];
$password=$properties["password"];
$bdname=$properties["bdname"];
}
else{
$host="localhost";
$user="root";
$password="";
$bdname="ptut";
}
try{
$bdd = new PDO('mysql:host='.$host.';dbname='.$bdname.
';charset=utf8',$user,$password);
return $bdd;
}
catch(Exception $e)
{
die('Erreur: ' .$e->getMessage());
}
}
function getIdDepartements(){
$bdd = Connect_db();
$SQL_Query = 'select idDepartement as id from Departement';
$dpts = array();
$i = 0;
$query = $bdd -> prepare($SQL_Query);
$query -> execute();
while($line = $query -> fetch()){
$dpts[$i] = $line['id'];
$i++;
}
return $dpts;
}
function getInfos($idDepartement){
$bdd = Connect_db();
$SQL_Query = 'select nomDepartement as titre, description as description, lat, lng from Departement where idDepartement = '. $idDepartement;
$query = $bdd -> prepare($SQL_Query);
$query -> execute();
return str_replace("'", "\'", ($query -> fetch(PDO::FETCH_ASSOC)));
}
function getImages($idDepartement){
$bdd = Connect_db();
$SQL_Query = 'select nomImage as nom, lienImage as link from Images where idDepartement = '. $idDepartement;
$query = $bdd -> prepare($SQL_Query);
$query -> execute();
return str_replace("'", "\'", ($query -> fetchAll(PDO::FETCH_ASSOC)));
}
function addDept($nomDept, $desc, $lat, $lng){
$bdd = Connect_db();
$SQL_Query = "insert into Departement values (null, ?, ?, ?, ?)";
$query = $bdd -> prepare($SQL_Query);
$query->bindParam(1, $nomDept);
$query->bindParam(2, $desc);
$query->bindParam(3, $lat);
$query->bindParam(4, $lng);
$query->execute();
$id = $bdd->lastInsertId();
mkdir("dptImg/" . $id);
return $id;
}
function getCookieId(){
if(isset($_COOKIE["dptId"])){
return $_COOKIE['dptId'];
}
else return -1;
}
function addImg($path, $dpt){
$bdd = Connect_db();
$SQL_Query = "insert into Images values (null, ?, ?, ?)";
$query = $bdd -> prepare($SQL_Query);
$name = pathinfo($path, PATHINFO_FILENAME);
echo($name);
$query->bindParam(1, $name);
$query->bindParam(2, $path);
$query->bindParam(3, $dpt);
$query->execute();
return $bdd->lastInsertId();
}
function suppImg($path, $dpt){
$bdd = Connect_db();
$SQL_Query = "delete from Images where idDepartement = " . $dpt . " and lienImage = '" . $path . "'";
$query = $bdd -> prepare($SQL_Query);
$query->execute();
}
function updateDpt($nomDept, $desc, $lat, $lng, $idDepartement){
$bdd = Connect_db();
$SQL_Query = "update Departement set nomDepartement = ?,
description = ?,
lat = ?,
lng = ?
where idDepartement = ". $idDepartement;
$query = $bdd -> prepare($SQL_Query);
$query->bindParam(1, $nomDept);
$query->bindParam(2, $desc);
$query->bindParam(3, $lat);
$query->bindParam(4, $lng);
$query->execute();
}
function deleteDpt($idDepartement){
$dir = "dptImg/" . $idDepartement;
$bdd = Connect_db();
$SQL_Query = "delete from Departement where idDepartement = ". $idDepartement;
$query = $bdd -> prepare($SQL_Query);
$query->execute();
$SQL_Query = "delete from Images where idDepartement = ". $idDepartement;
$query = $bdd -> prepare($SQL_Query);
$query->execute();
array_map('unlink', glob("$dir/*.*"));
rmdir($dir);
}
function isConnected(){
if(isset($_SESSION["id"]) && isset($_SESSION["pwd"])) return true;
else return false;
}
function connectAdmin($id, $pwd){
$bdd = Connect_db();
$SQL_Query = "select * from Admin where id = ? and pwd = ?";
$query = $bdd -> prepare($SQL_Query);
$query->bindParam(1, $id);
$query->bindParam(2, $pwd);
$i = 0;
$query->execute();
if($query->fetch()){
session_start();
$_SESSION['id'] = $id;
$_SESSION['pwd'] = $pwd;
return true;
}
else return false;
}
function disconnect(){
session_unset();
session_destroy();
}
?>