-
Notifications
You must be signed in to change notification settings - Fork 1
/
booking.php.bak
113 lines (88 loc) · 3.33 KB
/
booking.php.bak
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
function rand_string( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
if( isset($_REQUEST['email']) && $_REQUEST['email'] != '' && isset($_REQUEST['nombre']) && $_REQUEST['nombre'] != '' && isset($_REQUEST['tel']) && $_REQUEST['tel'] != '' && isset($_REQUEST['id']) && $_REQUEST['id'] != '' && isset($_REQUEST['token']) && $_REQUEST['token'] != '' ) {
require_once('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpasswd) or die(mysql_error());
mysql_select_db($db, $conn) or die(mysql_error());
//echo $_REQUEST['id'] . " " . $_REQUEST['token'];
$cita_sql = "SELECT * FROM booking_cita WHERE status = '1' AND id = '" . $_REQUEST['id'] . "' AND cookie = '" . $_REQUEST['token'] . "';";
//echo $cita_sql;
$cita_result = mysql_query($cita_sql);
$id = '';
$cita = '';
$token = '';
while($cita_row = mysql_fetch_array($cita_result)) {
$id = $cita_row['id'];
$cita = $cita_row['fecha'];
$token = $cita_row['cookie'];
}
if($id == '') {
$id = -2;
} else {
$book_sql = "UPDATE booking_cita SET status = '2' WHERE id = '" . $id . "';";
$register_sql = "INSERT INTO booking_registro VALUES('','" . $_REQUEST['email'] . "','" . $_REQUEST['nombre'] . "','" . $_REQUEST['tel'] . "','" . $id . "');";
mysql_query($book_sql);
mysql_query($register_sql);
}
mysql_close();
$returnval = array("id" => $id, "cita" => $cita, "token" => $token);
echo json_encode($returnval);
} else {
require_once('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db, $conn) or die(mysql_error());
//Unbook
$unbook_sql = "UPDATE booking_cita SET status = 0, reservado = NULL, cookie = NULL WHERE status = 1 AND reservado < NOW() - INTERVAL 15 MINUTE";
mysql_query($unbook_sql);
//free
$free_sql = "SELECT COUNT(*) AS free FROM booking_cita WHERE status = 0;";
$free_result = mysql_query($free_sql);
$free = 0;
while($free_row = mysql_fetch_array($free_result)) {
$free = $free_row['free'];
}
//Pendientes
$pending_sql = "SELECT COUNT(*) AS pending FROM booking_cita WHERE status = 1;";
$pending_result = mysql_query($pending_sql);
$pending = 0;
while($pending_row = mysql_fetch_array($pending_result)) {
$pending = $pending_row['pending'];
}
$returnval = '';
//If all is booked
if( $free == 0 && $pending == 0 ) {
$returnval = array("id" => -1, "cita" => '');
//If all is booked or pending
} elseif( $free == 0 && $pending > 0 ) {
$returnval = array("id" => 0, "cita" => '');
//If there's free space
} else {
$free_sql = "SELECT * FROM booking_cita WHERE status = 0 LIMIT 1;";
$free_result = mysql_query($free_sql);
$id = 0;
$cita = '';
while($free_row = mysql_fetch_array($free_result)) {
$id = $free_row['id'];
$cita = $free_row['fecha'];
}
$str = rand_string(26);
$token = md5($id . $str . $cita);
mysql_query("UPDATE booking_cita SET status = 1, reservado = NOW(), cookie = '" . $token . "' WHERE id = " . $id . ";");
$returnval = array("id" => $id, "cita" => $cita, "token" => $token);
}
mysql_close($conn);
//echo "free: " . $free . ". pending: " . $pending . ". returnval: " . $returnval;
echo json_encode($returnval);
}
?>