-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathquery_functions.php
More file actions
404 lines (337 loc) · 9.89 KB
/
Copy pathquery_functions.php
File metadata and controls
404 lines (337 loc) · 9.89 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
function user_exists($email, $hash){
global $db;
if ($hash == 'NEW') {
$sql = "SELECT * FROM users WHERE email='$email'";
}
else {
$sql = "SELECT * FROM users WHERE email='$email' AND hash='$hash' AND active='0'";
}
$result = mysqli_query($db, $sql);
if (DEBUG_MODE == 'ONxx') {
echo 'DEBUG MODE: ' . dirname(__FILE__).'.user_exists()<br/>';
echo 'SQL: ' .$sql . '<br/>';
print_r($result);
echo '<br/>';
}
if($result) {
return $result;
} else {
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
function login_exists($email, $password){
global $db;
$sql = "SELECT * FROM users WHERE email='$email' AND hash='$password'";
$result = mysqli_query($db, $sql);
if (DEBUG_MODE == 'ONxx') {
echo 'DEBUG MODE: ' . dirname(__FILE__).'.user_exists()<br/>';
echo 'SQL: ' .$sql . '<br/>';
print_r($result);
echo '<br/>';
}
if($result) {
return $result;
} else {
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
function find_all_users() {
global $db;
$sql = "select id, first_name, last_name, email, role, active ";
$sql .= "from users ";
//$sql .= "ORDER BY position ASC";
//echo $sql;
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
}
function find_user_by_id($id) {
global $db;
$sql = "select id, first_name, last_name, email, role, active ";
$sql .= "from users ";
$sql .= "where id='" . $id . "' ";
//$sql .= "ORDER BY position ASC";
//echo $sql;
$result = mysqli_query($db, $sql);
confirm_result_set($result);
$user = mysqli_fetch_assoc($result);
mysqli_free_result($result);
return $user;
}
function validate_user($user) {
$errors = [];
// menu_name
if(is_blank($user['menu_name'])) {
$errors[] = "Name cannot be blank.";
} elseif(!has_length($user['menu_name'], ['min' => 2, 'max' => 255])) {
$errors[] = "Name must be between 2 and 255 characters.";
}
// position
// Make sure we are working with an integer
$postion_int = (int) $user['position'];
if($postion_int <= 0) {
$errors[] = "Position must be greater than zero.";
}
if($postion_int > 999) {
$errors[] = "Position must be less than 999.";
}
// visible
// Make sure we are working with a string
$visible_str = (string) $user['visible'];
if(!has_inclusion_of($visible_str, ["0","1"])) {
$errors[] = "Visible must be true or false.";
}
return $errors;
}
function insert_user($first_name, $last_name, $email, $password, $hash, $role='USER', $active=0) {
global $db;
$sql = "INSERT INTO users ";
$sql .= " (first_name, last_name, email, role, active, hash, CreatedTime) ";
$sql .= "VALUES (";
$sql .= "'" . $first_name . "',";
$sql .= "'" . $last_name . "',";
$sql .= "'" . $email . "',";
$sql .= "'" . $role . "',";
$sql .= "'" . $active . "',";
$sql .= "'" . $hash . "'";
$sql .= "'0000-00-00 00:00:00'";
$sql .= ")";
echo $sql;
$result = mysqli_query($db, $sql);
echo '$result is: ';
print_r($result);
// For INSERT statements, $result is true/false
if($result) {
return true;
} else {
// INSERT failed
echo 'Insert Error: ' . mysqli_error($db);
db_disconnect($db);
exit;
}
}
function update_user($user) {
global $db;
$sql = "UPDATE users SET ";
$sql .= "username='" . $user['username'] . "', ";
$sql .= "user_email='" . $user['user_email'] . "', ";
$sql .= "role='" . $user['role'] . "' ";
$sql .= "WHERE user_id='" . $user['id'] . "' ";
$sql .= "LIMIT 1";
$result = mysqli_query($db, $sql);
// For UPDATE statements, $result is true/false
if($result) {
return true;
} else {
// UPDATE failed
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
function activate_user($email) {
global $db;
$sql = "UPDATE users SET ";
$sql .= "active='1'";
$sql .= "WHERE email='" . $email . "' ";
$sql .= "LIMIT 1";
$result = mysqli_query($db, $sql);
// For UPDATE statements, $result is true/false
if($result) {
return true;
} else {
// UPDATE failed
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
function delete_user_old($id) {
global $db;
$sql = "DELETE FROM users ";
$sql .= " WHERE users.id='" . $id . "' ";
$sql .= "LIMIT 1";
$result = mysqli_query($db, $sql);
// For DELETE statements, $result is true/false
if($result) {
return true;
} else {
// DELETE failed
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
function delete_user($id) {
global $db;
if ($id < 0) {return true;} // Don't let the interface take out any special rows hidden with item_ids < 0
$sql = "DELETE FROM users";
$sql .= " WHERE id='" . $id . "'";
$sql .= " LIMIT 1";
$result = mysqli_query($db, $sql);
// For DELETE statements, $result is true/false
if($result) {
mysqli_commit($db);
return true;
} else {
// DELETE failed
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}
/**
* A function to get the given preference value from the database.
*
* @param $name The name of the preference.
* @return string|null The value of the preference from the database, or null if there is no such preference
* or something went wrong.
*/
function get_preference($name) {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("SELECT value FROM preferences WHERE NAME=:name");
$stmt->bindParam(':name', $name);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['value'];
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return null;
}
/**
* A function to get a given quote from the database.
*
* @param $quote_id The id of the quote.
* @return string|null The quote, if it exists, or null if there is no quote with the given id
* or something went wrong.
*/
function get_quote($quote_id) {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("SELECT quote FROM quote_table where id=:quote_id");
$stmt->bindParam(':quote_id', $quote_id);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['quote'];
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return null;
}
/**
* A function to create a quote record in the database.
*
* @param $author The author of the quote
* @param $topic The topic of the quote.
* @param $quote The quote.
* @return boolean True if the quote was successfully added to the database, false otherwise.
*/
function create_quote($author, $topic, $quote) {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("INSERT INTO quote_table (author, topic, quote)
VALUES (:author, :topic, :quote)");
$stmt->bindParam(':author', $author);
$stmt->bindParam(':topic', $topic);
$stmt->bindParam(':quote', $quote);
$stmt->execute();
if ($stmt->rowCount() > 0) {
return true;
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return false;
}
/**
* A function to get all preferences from the database.
*
* @return string|null The preference data from the database, or null if there are no preferences
* or something went wrong.
*/
function get_preferences() {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("SELECT * FROM preferences");
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return null;
}
function update_preference($name, $value) {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("UPDATE preferences SET value=:value WHERE name=:name");
$stmt->bindParam(':value', $value);
$stmt->bindParam(':name', $name);
$stmt->execute();
if ($stmt->rowCount() > 0) {
return true;
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return false;
}
/**
* A function to get the last several quotes from the datbase.
*
* @param $quotes_to_show The number of quotes to show
* @return array|null An associative array of the most recent quotes, or null if something went wrong.
*/
function get_recent_quotes($quotes_to_show) {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("SELECT id, author, topic FROM quote_table ORDER BY id DESC LIMIT " . $quotes_to_show);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return null;
}
/**
* A function to get the quotes from the datbase.
*
* @return array|null An associative array of the most recent quotes, or null if something went wrong.
*/
function get_quotes() {
try {
// connect to the database
$pdo = pdo_connect_to_db();
$stmt = $pdo->prepare("SELECT * FROM quote_table");
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
} catch(PDOException $e) {
error_log("Error: " . $e->getMessage(), 0);
}
return null;
}