forked from commando/commando
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLQueries.php
More file actions
600 lines (513 loc) · 26.7 KB
/
MySQLQueries.php
File metadata and controls
600 lines (513 loc) · 26.7 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/*
# Copyright 2012 NodeSocket, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/
class MySQLQueries {
public static function add_server($server_label, $server_group, $server_tags, $server_address, $server_ssh_username, $server_ssh_port) {
$SQL = "INSERT INTO servers
(`id`,
label,
`group`,
tags,
address,
ssh_username,
ssh_port,
added,
modified)
VALUES (" . MySQLConnection::smart_quote(Functions::generate_id('srv')) . ",
" . MySQLConnection::smart_quote($server_label) . ",
" . MySQLConnection::smart_quote($server_group) . ",
" . MySQLConnection::smart_quote($server_tags) . ",
AES_ENCRYPT(" . MySQLConnection::smart_quote($server_address) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
AES_ENCRYPT(" . MySQLConnection::smart_quote($server_ssh_username) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
AES_ENCRYPT(" . MySQLConnection::smart_quote($server_ssh_port) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
UTC_TIMESTAMP(),
UTC_TIMESTAMP())";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function is_server_label_unique($server_label) {
//Fast way to check
$SQL = "SELECT label
FROM servers
WHERE label = " . MySQLConnection::smart_quote($server_label) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$numb_rows = MySQLConnection::numb_rows($result);
if($numb_rows > 0) {
return false;
} else {
return true;
}
}
public static function get_servers() {
$SQL = "SELECT s.`id`,
s.label,
s.`group`,
g.name AS group_name,
s.tags,
AES_DECRYPT(s.address, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS address,
AES_DECRYPT(s.ssh_username, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_username,
AES_DECRYPT(s.ssh_port, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_port,
CONVERT_TZ(s.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(s.modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified
FROM servers s
LEFT OUTER JOIN groups g
ON s.group = g.`id`
ORDER BY s.added ASC";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function get_server($server_id) {
$SQL = "SELECT s.`id`,
s.label,
s.`group`,
g.name AS group_name,
s.tags,
AES_DECRYPT(s.address, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS address,
AES_DECRYPT(s.ssh_username, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_username,
AES_DECRYPT(s.ssh_port, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_port,
CONVERT_TZ(s.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(s.modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified
FROM servers s
LEFT OUTER JOIN groups g
ON s.group = g.`id`
WHERE s.`id` = " . MySQLConnection::smart_quote($server_id) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function get_servers_by_groups(array $groups) {
//Special case for the 'default' group
if(count($groups) === 0) {
$SQL = "SELECT `id`,
label,
`group`,
NULL as group_name,
tags,
AES_DECRYPT(address, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS address,
AES_DECRYPT(ssh_username, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_username,
AES_DECRYPT(ssh_port, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_port,
CONVERT_TZ(added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified
FROM servers
WHERE `group` IS NULL
ORDER BY added ASC";
} else {
//Smart quote each group
$groups = array_map(function($element) {
if(empty($element)) {
if(!defined('INCLUDE_DEFAULT_GROUP_SERVERS')) {
define('INCLUDE_DEFAULT_GROUP_SERVERS', true);
}
return;
} else {
return MySQLConnection::smart_quote($element);
}
}, $groups);
//Removes empty elements (including 0) from the array completey
$groups = array_filter($groups);
$SQL = "SELECT s.`id`,
s.label,
s.`group`,
g.name AS group_name,
s.tags,
AES_DECRYPT(s.address, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS address,
AES_DECRYPT(s.ssh_username, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_username,
AES_DECRYPT(s.ssh_port, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS ssh_port,
CONVERT_TZ(s.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(s.modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified
FROM servers s
LEFT OUTER JOIN groups g
ON s.group = g.`id`
WHERE g.`id` IN (" . implode(',', $groups) . ")";
if(defined('INCLUDE_DEFAULT_GROUP_SERVERS')) {
$SQL .= " OR g.`id` IS NULL";
}
$SQL .= " ORDER BY s.added ASC";
}
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function delete_server($server_id) {
$SQL = "DELETE
FROM servers
WHERE `id` = " . MySQLConnection::smart_quote($server_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function edit_server($server_id, $server_label, $server_group, $server_tags, $server_address, $server_ssh_username, $server_ssh_port) {
$SQL = "UPDATE servers
SET label = " . MySQLConnection::smart_quote($server_label) . ",
`group` = " . MySQLConnection::smart_quote($server_group) . ",
tags = " . MySQLConnection::smart_quote($server_tags) . ",
address = AES_ENCRYPT(" . MySQLConnection::smart_quote($server_address) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
ssh_username = AES_ENCRYPT(" . MySQLConnection::smart_quote($server_ssh_username) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
ssh_port = AES_ENCRYPT(" . MySQLConnection::smart_quote($server_ssh_port) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
modified = UTC_TIMESTAMP()
WHERE `id` = " . MySQLConnection::smart_quote($server_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function add_group($group_name) {
$SQL = "INSERT INTO groups
(`id`,
name,
added,
modified)
VALUES (" . MySQLConnection::smart_quote(Functions::generate_id('grp')) . ",
" . MySQLConnection::smart_quote($group_name) . ",
UTC_TIMESTAMP(),
UTC_TIMESTAMP())";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function is_group_name_unique($group_name) {
//Fast way to check
$SQL = "SELECT name
FROM groups
WHERE name = " . MySQLConnection::smart_quote($group_name) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$numb_rows = MySQLConnection::numb_rows($result);
if($numb_rows > 0) {
return false;
} else {
return true;
}
}
public static function get_groups() {
$SQL = "SELECT g.`id`,
g.name,
CONVERT_TZ(g.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(g.modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified,
GROUP_CONCAT(s.label ORDER BY s.added ASC SEPARATOR ', ') AS servers,
COUNT(s.label) AS servers_count
FROM groups g
LEFT OUTER JOIN servers s
ON g.`id` = s.`group`
GROUP BY g.`id`
ORDER BY g.added ASC";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function get_group($group_id) {
$SQL = "SELECT `id`,
name,
CONVERT_TZ(added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified
FROM groups
WHERE `id` = " . MySQLConnection::smart_quote($group_id) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function delete_group($group_id) {
$SQL = "DELETE
FROM groups
WHERE `id` = " . MySQLConnection::smart_quote($group_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function edit_group($group_id, $group_name) {
$SQL = "UPDATE groups
SET name = " . MySQLConnection::smart_quote($group_name) . ",
modified = UTC_TIMESTAMP()
WHERE `id` = " . MySQLConnection::smart_quote($group_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function get_settings($include_date_modified = true) {
if($include_date_modified) {
$SQL = "SELECT AES_DECRYPT(data, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS data,
CONVERT_TZ(modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()) . ") AS modified
FROM settings
WHERE `id` = 1";
} else {
$SQL = "SELECT AES_DECRYPT(data, " . MySQLConnection::smart_quote(CRYPTO_SEED) . ") AS data
FROM settings
WHERE `id` = 1";
}
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function edit_settings($data) {
//Will always update if a row exists, otherwise will insert
$SQL = "REPLACE INTO settings
(id,
data,
modified)
VALUES (1,
AES_ENCRYPT(" . MySQLConnection::smart_quote($data) . ", " . MySQLConnection::smart_quote(CRYPTO_SEED) . "),
UTC_TIMESTAMP())";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
public static function add_recipe($recipe_name, $recipe_interpreter, $recipe_notes, $recipe_content) {
////
// Turn MySQL commiting off, run as a transaction
////
MySQLConnection::autocommit(false);
$recipe_id = Functions::generate_id('rec');
$SQL = "INSERT INTO recipes
(`id`,
name,
added,
modified)
VALUES (" . MySQLConnection::smart_quote($recipe_id) . ",
" . MySQLConnection::smart_quote($recipe_name) . ",
UTC_TIMESTAMP(),
UTC_TIMESTAMP())";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$recipe_version_id = Functions::generate_id('ver');
$SQL = "INSERT INTO recipe_versions
(`id`,
recipe,
version,
interpreter,
notes,
content,
added)
VALUES (" . MySQLConnection::smart_quote($recipe_version_id) . ",
" . MySQLConnection::smart_quote($recipe_id) . ",
SHA1(" . MySQLConnection::smart_quote($recipe_id . $recipe_interpreter . $recipe_notes . $recipe_content) . "),
" . MySQLConnection::smart_quote($recipe_interpreter) . ",
" . MySQLConnection::smart_quote($recipe_notes) . ",
" . MySQLConnection::smart_quote($recipe_content) . ",
UTC_TIMESTAMP())";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$SQL = "INSERT INTO recipe_heads
(recipe,
recipe_version)
VALUES (" . MySQLConnection::smart_quote($recipe_id) . ",
" . MySQLConnection::smart_quote($recipe_version_id) . ")";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
////
// Commit the MySQL transaction
////
MySQLConnection::commit();
}
public static function edit_recipe($recipe_id, $recipe_name, $recipe_interpreter, $recipe_notes, $recipe_content) {
$SQL = "UPDATE recipes
SET name = " . MySQLConnection::smart_quote($recipe_name) . ",
modified = UTC_TIMESTAMP()
WHERE `id` = " . MySQLConnection::smart_quote($recipe_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
////
// Generate SHA1
////
$sha1 = sha1($recipe_id . $recipe_interpreter . $recipe_notes . $recipe_content);
////
// Get if head version matches generated sha1 above
////
$SQL = "SELECT rv.version
FROM recipe_heads rh,
recipe_versions rv
WHERE rh.recipe = rv.recipe
AND rh.recipe_version = rv.id
AND rh.recipe = " . MySQLConnection::smart_quote($recipe_id) . "
AND rv.version = " . MySQLConnection::smart_quote($sha1) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$numb_rows = MySQLConnection::numb_rows($result);
////
// This update is the same as head, don't store another version, simply return
////
if($numb_rows > 0) {
return;
}
////
// Turn MySQL commiting off, run as a transaction
////
MySQLConnection::autocommit(false);
$recipe_version_id = Functions::generate_id('ver');
$SQL = "INSERT INTO recipe_versions
(`id`,
recipe,
version,
interpreter,
notes,
content,
added)
VALUES (" . MySQLConnection::smart_quote($recipe_version_id) . ",
" . MySQLConnection::smart_quote($recipe_id) . ",
SHA1(" . MySQLConnection::smart_quote($recipe_id . $recipe_interpreter . $recipe_notes . $recipe_content) . "),
" . MySQLConnection::smart_quote($recipe_interpreter) . ",
" . MySQLConnection::smart_quote($recipe_notes) . ",
" . MySQLConnection::smart_quote($recipe_content) . ",
UTC_TIMESTAMP())";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$SQL = "UPDATE recipe_heads
SET recipe_version = " . MySQLConnection::smart_quote($recipe_version_id) . "
WHERE recipe = " . MySQLConnection::smart_quote($recipe_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
////
// Commit the MySQL transaction
////
MySQLConnection::commit();
}
public static function edit_recipe_head($recipe_id, $recipe_version_id) {
////
// Turn MySQL commiting off, run as a transaction
////
MySQLConnection::autocommit(false);
$SQL = "UPDATE recipes
SET modified = UTC_TIMESTAMP()
WHERE `id` = " . MySQLConnection::smart_quote($recipe_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$SQL = "UPDATE recipe_heads
SET recipe_version = " . MySQLConnection::smart_quote($recipe_version_id) . "
WHERE recipe = " . MySQLConnection::smart_quote($recipe_id) . "";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
////
// Commit the MySQL transaction
////
MySQLConnection::commit();
}
public static function is_recipe_name_unique($recipe_name) {
//Fast way to check
$SQL = "SELECT name
FROM recipes
WHERE name = " . MySQLConnection::smart_quote($recipe_name) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
$numb_rows = MySQLConnection::numb_rows($result);
if($numb_rows > 0) {
return false;
} else {
return true;
}
}
////
// Select the latest (head) version of recipes
////
public static function get_recipes(array $recipe_ids = array()) {
if(empty($recipe_ids)) {
$SQL = "SELECT r.`id`,
r.name,
rv.version,
rv.interpreter,
rv.notes,
rv.content,
CONVERT_TZ(r.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(r.modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS modified
FROM recipe_heads rh,
recipes r,
recipe_versions rv
WHERE rh.recipe = r.`id`
AND rh.recipe_version = rv.`id`
AND r.`id` = rv.recipe
ORDER BY r.added DESC";
} else {
//Smart quote each recipe id
$recipe_ids = array_map(function($element) {
return MySQLConnection::smart_quote($element);
}, $recipe_ids);
$SQL = "SELECT r.`id`,
r.name,
rv.version,
rv.interpreter,
rv.notes,
rv.content,
CONVERT_TZ(r.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added,
CONVERT_TZ(r.modified, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS modified
FROM recipe_heads rh,
recipes r,
recipe_versions rv
WHERE rh.recipe = r.`id`
AND rh.recipe_version = rv.`id`
AND r.`id` = rv.recipe
AND r.`id` IN (" . implode(',', $recipe_ids) . ")
ORDER BY r.added DESC";
}
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
////
// Select the latest (head) version of a specific recipe
////
public static function get_recipe($recipe_id) {
$SQL = "SELECT r.`id`,
r.name,
rv.`id` AS recipe_version,
rv.version,
rv.interpreter,
rv.notes,
rv.content,
CONVERT_TZ(rv.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added
FROM recipe_heads rh,
recipes r,
recipe_versions rv
WHERE rh.recipe = r.`id`
AND rh.recipe_version = rv.`id`
AND r.`id` = rv.recipe
AND r.`id` = " . MySQLConnection::smart_quote($recipe_id) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function get_recipe_head_version($recipe_id) {
$SQL = "SELECT recipe_version
FROM recipe_heads
WHERE recipe = " . MySQLConnection::smart_quote($recipe_id) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function get_recipe_by_version($recipe_id, $recipe_version_id) {
$SQL = "SELECT r.`id`,
r.name,
rv.`id` AS recipe_version,
rv.version,
rv.interpreter,
rv.notes,
rv.content,
CONVERT_TZ(rv.added, '+00:00', " . MySQLConnection::smart_quote(Functions::get_timezone_offset()). ") AS added
FROM recipes r,
recipe_versions rv
WHERE r.`id` = rv.recipe
AND r.`id` = " . MySQLConnection::smart_quote($recipe_id) . "
AND rv.`id` = " . MySQLConnection::smart_quote($recipe_version_id) . "";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function get_recipe_versions($recipe_id) {
$SQL = "SELECT `id`,
version,
added
FROM recipe_versions
WHERE recipe = " . MySQLConnection::smart_quote($recipe_id) . "
ORDER BY added DESC";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
////
// Select the number of versions for all recipes
////
public static function get_number_of_recipe_versions() {
$SQL = "SELECT r.`id`,
COUNT(rv.id) AS `count`
FROM recipes r,
recipe_versions rv
WHERE r.`id` = rv.recipe
GROUP BY rv.recipe
ORDER BY r.added DESC";
$result = MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
return $result;
}
public static function delete_recipes(array $recipe_ids) {
//Smart quote each recipe id
$recipe_ids = array_map(function($element) {
return MySQLConnection::smart_quote($element);
}, $recipe_ids);
$SQL = "DELETE FROM recipes
WHERE `id` IN (" . implode(',', $recipe_ids) . ")";
MySQLConnection::query($SQL) or Error::db_halt(500, 'internal server error', 'Unable to execute request, SQL query error.', __FUNCTION__, MySQLConnection::error(), $SQL);
}
////
// Special case query, does not require the error condition check
////
public static function get_db_version() {
$SQL = "SELECT `current`
FROM db_version";
return MySQLConnection::query($SQL);
}
}
?>