/
index.php
180 lines (167 loc) · 5.59 KB
/
index.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
<?php
// start the session
session_start();
$sms_message = "";
$binary_numbers = array(128,64,32,16,8,4,2,1);
// get the session varible if it exists
$counter = $_SESSION['counter'];
$which_binary = $_SESSION['which_binary'];
$number_1 = $_SESSION['number_1'];
$number_2 = $_SESSION['number_2'];
$number_1_temp = $_SESSION['number_1_temp'];
$number_2_temp = $_SESSION['number_2_temp'];
$addition = $_SESSION['addition'];
$num1_binary_array = $_SESSION['num1_binary_array'];
$num2_binary_array = $_SESSION['num2_binary_array'];
$result_array = $_SESSION['result_array'];
// if it doesnt, set the default.
if(!strlen($counter)) $counter = 0;
if(!isset($_SESSION['num1_binary_array'])) $num1_binary_array = array(0,0,0,0,0,0,0,0);
if(!isset($_SESSION['num2_binary_array'])) $num1_binary_array = array(0,0,0,0,0,0,0,0);
if(!isset($_SESSION['result_array'])) $result_array = array(0,0,0,0,0,0,0,0);
// If CALC then reset, else start the calc
if (strpos($_REQUEST['Body'],'CALC') !== false) {
$counter = 0;
$sms_message = "New calculation requested by user 51984806. I will need your help. Is that ok? (Y/N)";
//get the numbers
$pieces = explode(" ", $_REQUEST['Body']);
$_SESSION['number_1'] = $pieces[1];
$_SESSION['number_2'] = $pieces[3];
$_SESSION['number_1_temp'] = $pieces[1];
$_SESSION['number_2_temp'] = $pieces[3];
$addition = true;
if ($pieces[2] != '+') $addition = false;
$_SESSION['addition'] = $addition;
$which_binary = 0;
$num1_binary_array = array(0,0,0,0,0,0,0,0);
$num2_binary_array = array(0,0,0,0,0,0,0,0);
$result_array = array(0,0,0,0,0,0,0,0);
} else {
switch ($counter) {
case 1:
//Can the user help?
if (trim($_REQUEST['Body']) == 'N' || trim($_REQUEST['Body']) == 'n') {
$sms_message = "We will contact another processing user, and send the result once it has been calculated.";
} else {
$sms_message = "First we will convert $number_1 to binary. Is $binary_numbers[$which_binary] less than or equal to $number_1? (Y/N)";
$which_binary++;
}
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if (trim($_REQUEST['Body']) == 'Y' || trim($_REQUEST['Body']) == 'y') {
$num1_binary_array[$which_binary-1] = 1;
$number_1_temp -= $binary_numbers[$which_binary-1];
$_SESSION['number_1_temp'] = $number_1_temp;
}
if ((int)$number_1_temp < 1 || $counter == 9) {
$counter = 9;
$sms_message = "$number_1 is ";
for ($i=0; $i < 8; $i++) {
$sms_message .= $num1_binary_array[$i];
if ($i != 7) $sms_message .= ",";
}
$which_binary = 0;
$sms_message .= ". Now, $number_2. Is $binary_numbers[$which_binary] less than or equal to $number_2? (Y/N)";
$which_binary++;
} else {
// Binary of first number
$sms_message = "Is $binary_numbers[$which_binary] less than or equal to $number_1_temp? (Y/N)";
$which_binary++;
}
break;
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
if (trim($_REQUEST['Body']) == 'Y' || trim($_REQUEST['Body']) == 'y') {
$num2_binary_array[$which_binary-1] = 1;
$number_2_temp -= $binary_numbers[$which_binary-1];
$_SESSION['number_2_temp'] = $number_2_temp;
}
if ((int)$number_2_temp < 1 || $counter == 17) {
$counter = 17;
$which_binary = 7;
$sms_message = "$number_2 is: ";
for ($i=0; $i < 8; $i++) {
$sms_message .= $num2_binary_array[$i];
if ($i != 7) $sms_message .= ",";
}
if ($addition) {
$sms_message .= ". Now we will add the two numbers. What is $num1_binary_array[$which_binary] + $num2_binary_array[$which_binary]? (0/1/2)";
} else {
$sms_message .= ". Now we shall subtract the two numbers. How?";
}
$which_binary--;
} else {
// Binary of first number
$sms_message = "Is $binary_numbers[$which_binary] less than or equal to $number_2_temp? (Y/N)";
$which_binary++;
}
break;
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
$returned_result = (int)trim($_REQUEST['Body']);
$tempval = $result_array[$which_binary+1] + $returned_result;
//There's a problem with this
if ($result_array[$which_binary+1] > 1) {
$result_array[$which_binary+1] = 0;
$result_array[$which_binary] += 1;
} else {
$result_array[$which_binary+1] = $tempval;
}
// Process result
$sms_message .= "What is $num1_binary_array[$which_binary] + $num2_binary_array[$which_binary]? (0/1/2)";
$which_binary--;
break;
case 25:
$result_array[0] = (int)trim($_REQUEST['Body']);
$sms_message = "The result is: ";
for ($i=0; $i < 8; $i++) {
$sms_message .= $result_array[$i];
if ($i != 7) $sms_message .= ",";
}
$sms_message .= ". Or ";
for ($i=0; $i < 8; $i++) {
if ($result_array[$i] == 1) {
$sms_message .= $binary_numbers[$i]." + ";
}
}
$sms_message = rtrim($sms_message, '+');
break;
default:
// Other cases
$sms_message = "ERROR: counter is $counter";
break;
}
}
// increment it
$counter++;
// save it
$_SESSION['counter'] = $counter;
$_SESSION['which_binary'] = $which_binary;
$_SESSION['num1_binary_array'] = $num1_binary_array;
$_SESSION['num2_binary_array'] = $num2_binary_array;
$_SESSION['result_array'] = $result_array;
// output the counter response
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Sms><?php echo $sms_message ?></Sms>
</Response>