www-thermferm/profiles.php

changeset 137
e4518fd9b626
parent 136
264e5ee5abfc
child 139
ffcabb9166bf
equal deleted inserted replaced
136:264e5ee5abfc 137:e4518fd9b626
21 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *****************************************************************************/ 22 *****************************************************************************/
23 23
24 require_once('utilities.php'); 24 require_once('utilities.php');
25 25
26 profile_admin(); 26 /*
27 * Each time this page is loaded, get the profiles from the server.
28 */
29 $sock = open_socket();
30 if ($sock == false) {
31 load('index.php');
32 }
33
34 socket_write($sock, "LIST PROFILES", 4096);
35 $answer = "";
36 while (1) {
37 $line = socket_read($sock, 4096);
38 if ($line === '')
39 break;
40 $answer .= $line;
41 }
42 socket_close($sock);
43 /*
44 * $arr contains the complete reply of he LIST PROFILES command.
45 */
46 $arr = explode("\r\n", $answer);
47
48
49 if (isset($_GET['action'])) {
50 switch ($_GET['action']) {
51 case 'edit': profile_edit();
52 break;
53 default: break;
54 }
55 } elseif (isset($_POST['action'])) {
56 switch ($_POST['action']) {
57 case 'testdata': testdata();
58 break;
59 default: break;
60 }
61 } else {
62 profile_list();
63 }
64
27 exit; 65 exit;
28 66
29 67
30 function profile_admin() { 68
31 if (isset($_GET['action'])) { 69
32 switch ($_GET['action']) { 70 /*
33 default: break; 71 * Profile add
34 } 72 *
35 } elseif (isset($_POST['action'])) { 73 * @param string $_POST['Name'] The rpofile name
36 switch ($_POST['action']) { 74 */
37 default: break;
38 }
39 } else {
40 profile_list();
41 }
42 }
43
44
45
46 function profile_add() { 75 function profile_add() {
47 $cmd = "ADD PROFILE ".$_GET['Name']; 76
77 $cmd = "ADD PROFILE ".$_POST['Name'];
48 78
49 $sock = open_socket(); 79 $sock = open_socket();
50 if ($sock != false) { 80 if ($sock != false) {
51 /* 81 /*
52 * Send command and absorb the result. 82 * Send command and absorb the result.
53 */ 83 */
54 socket_write($sock, $update_cmd, 4096); 84 socket_write($sock, $cmd, 4096);
55 while (1) { 85 while (1) {
56 $line = socket_read($sock, 4096); 86 $line = socket_read($sock, 4096);
57 if ($line === '') 87 if ($line === '')
58 break; 88 break;
59 } 89 }
60 socket_close($sock); 90 socket_close($sock);
61 } 91 }
62 } 92
63 93 unset($_POST['UUID']);
64 94 unset($_POST['Name']);
65 95 unset($_POST['key']);
96 unset($_POST['command']);
97 load('profiles.php');
98 }
99
100
101
102 /*
103 * Profile update
104 *
105 * @param string $_POST['UUID'] The profile UUID
106 * @param string $_POST['Name'] The profile name
107 * @param string $_POST['key'] The button pressed.
108 */
66 function profile_update() { 109 function profile_update() {
67 /* 110 /*
68 * Build the update command 111 * Build the update command
69 */ 112 */
70 $update_cmd = "PROFILE ".$_GET['UUID'].",".$_GET['Name']; 113 if ($_POST['key'] == 'Delete')
114 $update_cmd = "DEL PROFILE ".$_POST['UUID'];
115 else
116 $update_cmd = "PROFILE ".$_POST['UUID'].",".$_POST['Name'];
71 117
72 $sock = open_socket(); 118 $sock = open_socket();
73 if ($sock != false) { 119 if ($sock != false) {
74 /* 120 /*
75 * Send command and absorb the result. 121 * Send command and absorb the result.
80 if ($line === '') 126 if ($line === '')
81 break; 127 break;
82 } 128 }
83 socket_close($sock); 129 socket_close($sock);
84 } 130 }
131
132 unset($_POST['UUID']);
133 unset($_POST['Name']);
134 unset($_POST['key']);
135 unset($_POST['command']);
136 load('profiles.php');
137 }
138
139
140
141 /*
142 * Test input of a modified or new profile.
143 *
144 * @param string $_POST['UUID'] Unique record UUID
145 * @param string $_POST['Name'] Profile name
146 * @param string $_POST['key'] Key choice, Save or Cancel
147 * @param string $_POST['command'] Command used, 'add' or 'update'
148 *
149 * Return: 0 = Ok
150 * 1 = Missing data
151 * 2 = Name field too short
152 * 3 = Name already in use
153 * 99 = Cancel key
154 */
155 function test_thedata() {
156
157 global $arr;
158
159 if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['key']) && isset($_POST['command'])) {
160
161 if ($_POST['key'] == 'Cancel')
162 return 99;
163
164 if (strlen($_POST['Name']) < 2)
165 return 2;
166
167 if (startsWith($arr[0], "212")) {
168 $j = 1;
169 while (1) {
170 if (strcmp($arr[$j], ".") == 0)
171 break;
172 $f = explode(",", $arr[$j]);
173 if (strcmp($f[0], $_POST['UUID']) && (strcmp($f[1], $_POST['Name']) == 0)) {
174 return 3;
175 }
176 $j++;
177 }
178 }
179
180 } else {
181 return 1;
182 }
183
184 return 0;
185 }
186
187
188
189 /*
190 * Test result from edit screen and do next action
191 */
192 function testdata() {
193
194 $result = test_thedata();
195 $error = '';
196
197 switch ($result) {
198 case 0: if ($_POST['command'] == 'add') {
199 profile_add();
200 return;
201 } else if ($_POST['command'] == 'update') {
202 profile_update();
203 return;
204 }
205 break;
206 case 1: $error = 'Missing data';
207 break;
208 case 2: $error = 'The name is too short';
209 break;
210 case 3: $error = 'The name is already in use, choose another one';
211 break;
212 case 99:
213 load('profiles.php');
214 break;
215 }
216
217 if ($_POST['command'] == 'add') {
218 $heading = 'ThermFerm - Add Profile';
219 } else {
220 $heading = 'ThermFerm - Edit Profile';
221 }
222
223 edit_screen($_POST['UUID'], $_POST['Name'], $_POST['command'], $heading, $error);
224 }
225
226
227
228 /*
229 * Profiles edit screen. Used by profile_edit(), profile_add() and testdata()
230 *
231 * @param string $UUID The record UUID (fixed).
232 * @param string $Name The Profile Name.
233 * @param string $command 'add' or 'update'
234 * @param string $heading Pagina heading title.
235 * @Param string $error_message Blank or previous error.
236 */
237 function edit_screen($UUID, $Name, $command, $heading, $error_message) {
238
239 $outstr = build_header($heading);
240 $outstr .= ' <div id="errors">'.PHP_EOL;
241 $outstr .= ' '.$error_message.PHP_EOL;
242 $outstr .= ' </div> <!-- errors -->'.PHP_EOL;
243 $outstr .= ' <div id="etable">'.PHP_EOL;
244 $outstr .= ' <form method="POST" action="profiles.php">'.PHP_EOL;
245 $outstr .= ' <table class="editor">'.PHP_EOL;
246 $outstr .= ' <tr class="editor">'.PHP_EOL;
247 $outstr .= ' <td class="editname">UUID</td>'.PHP_EOL;
248 $outstr .= ' <td class="editfield">'.$UUID.'</td>'.PHP_EOL;
249 $outstr .= ' </tr>'.PHP_EOL;
250 $outstr .= ' <tr class="editor">'.PHP_EOL;
251 $outstr .= ' <td class="editname">Name</td>'.PHP_EOL;
252 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$Name.'"></td>'.PHP_EOL;
253 $outstr .= ' </tr>'.PHP_EOL;
254 $outstr .= ' <tr class="editor">'.PHP_EOL;
255 $outstr .= ' <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL;
256 $outstr .= ' <td class="editfield"><input type="submit" value="Cancel" name="key">';
257 $outstr .= '<input type="submit" value="Delete" name="key" style="margin-left: 100px;">';
258 $outstr .= '<input type="hidden" value="testdata" name="action">';
259 $outstr .= '<input type="hidden" value="'.$command.'" name="command">';
260 $outstr .= '<input type="hidden" value="'.$UUID.'" name="UUID"></td>'.PHP_EOL;
261 $outstr .= ' </tr>'.PHP_EOL;
262 $outstr .= ' </table>'.PHP_EOL;
263 $outstr .= ' </form>'.PHP_EOL;
264 $outstr .= ' </div> <!-- etable -->'.PHP_EOL;
265 $outstr .= build_footer();
266 echo $outstr;
267 }
268
269
270
271 /*
272 * Edit a Profile. Fetches the record data and shows the edit screen.
273 *
274 * @param string $_GET['action'] Must be 'edit'.
275 * @param string $_GET['UUID'] The UUID of the Profile.
276 */
277 function profile_edit() {
278
279 global $arr;
280
281 if ($_GET['action'] == 'edit') {
282
283 if (startsWith($arr[0], "212")) {
284 $j = 1;
285 while (1) {
286 if (strcmp($arr[$j], ".") == 0)
287 break;
288 $f = explode(",", $arr[$j]);
289 if (strcmp($f[0], $_GET['UUID']) == 0) {
290 edit_screen($f[0], $f[1], 'update', 'ThermFerm - Edit Profile', '');
291 return;
292 }
293 $j++;
294 }
295 } else {
296 load('profiles.php');
297 }
298
299 } else {
300 load('profiles.php');
301 }
85 } 302 }
86 303
87 304
88 305
89 /* 306 /*
90 * @link Edit profile 307 * @link Edit profile
91 * @link Add profile 308 * @link Add profile
92 */ 309 */
93 function profile_list() { 310 function profile_list() {
94 311
95 $sock = open_socket(); 312 global $arr;
96 if ($sock == false) { 313
97 echo ""; 314 $outstr = build_header("ThermFerm - Profiles");
98 return;
99 }
100
101 socket_write($sock, "LIST PROFILES", 4096);
102 $answer = "";
103 while (1) {
104 $line = socket_read($sock, 4096);
105 if ($line === '')
106 break;
107 $answer .= $line;
108 }
109 socket_close($sock);
110 $arr = explode("\r\n", $answer);
111
112 $outstr = build_header();
113 315
114 $outstr .= ' <div id="errors">'.PHP_EOL; 316 $outstr .= ' <div id="errors">'.PHP_EOL;
115 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; 317 $outstr .= ' </div> <!-- errors -->'.PHP_EOL;
116
117 $outstr .= ' <div id="etable">'.PHP_EOL; 318 $outstr .= ' <div id="etable">'.PHP_EOL;
118 $outstr .= ' <table class="setup">'.PHP_EOL; 319 $outstr .= ' <table class="setup">'.PHP_EOL;
119 $outstr .= ' <tr style="background-color: #FFCC01;">'.PHP_EOL; 320 $outstr .= ' <tr style="background-color: #FFCC01;">'.PHP_EOL;
120 $outstr .= ' <td class="setup">UUID</td>'.PHP_EOL; 321 $outstr .= ' <td class="setup">UUID</td>'.PHP_EOL;
121 $outstr .= ' <td class="setup">Name</td>'.PHP_EOL; 322 $outstr .= ' <td class="setup">Name</td>'.PHP_EOL;
127 $j = 1; 328 $j = 1;
128 while (1) { 329 while (1) {
129 if (strcmp($arr[$j], ".") == 0) 330 if (strcmp($arr[$j], ".") == 0)
130 break; 331 break;
131 $f = explode(",", $arr[$j]); 332 $f = explode(",", $arr[$j]);
132 $busy = 0;
133 $outstr .= ' <tr class="setup">'.PHP_EOL; 333 $outstr .= ' <tr class="setup">'.PHP_EOL;
134 $outstr .= ' <td class="setup">'.$f[0].'</td>'.PHP_EOL; 334 $outstr .= ' <td class="setup">'.$f[0].'</td>'.PHP_EOL;
135 $outstr .= ' <td class="setup">'.$f[1].'</td>'.PHP_EOL; 335 $outstr .= ' <td class="setup">'.$f[1].'</td>'.PHP_EOL;
136 $outstr .= ' <td class="setup">'.$f[2].'</td>'.PHP_EOL; 336 $outstr .= ' <td class="setup">'.$f[2].'</td>'.PHP_EOL;
137 if ($busy == 1) 337 if ($f[3] == 1)
138 $outstr .= ' <td class="setup">Busy</td>'.PHP_EOL; 338 $outstr .= ' <td class="setup">Busy</td>'.PHP_EOL;
139 else 339 else
140 $outstr .= ' <td class="setup"><a href="profiles.php?action=edit&amp;UUID='.$f[0].'">Edit</a></td>'.PHP_EOL; 340 $outstr .= ' <td class="setup"><a href="profiles.php?action=edit&amp;UUID='.$f[0].'">Edit</a></td>'.PHP_EOL;
141 $outstr .= ' </tr>'.PHP_EOL; 341 $outstr .= ' </tr>'.PHP_EOL;
142 $j++; 342 $j++;
143 } 343 }
144 } 344 }
145 345
146 $outstr .= ' </table>'.PHP_EOL; 346 $outstr .= ' </table>'.PHP_EOL;
147 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; 347 $outstr .= ' </div> <!-- etable -->'.PHP_EOL;
348 $outstr .= ' <div id="atable">'.PHP_EOL;
349
350 $outstr .= ' <form method="POST" action="profiles.php">'.PHP_EOL;
351 $outstr .= ' <table class="editor">'.PHP_EOL;
352 $outstr .= ' <tr class="editor">'.PHP_EOL;
353 $outstr .= ' <td class="editname">Name</td>'.PHP_EOL;
354 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$Name.'"></td>'.PHP_EOL;
355 $outstr .= ' </tr>'.PHP_EOL;
356 $outstr .= ' <tr class="editor">'.PHP_EOL;
357 $outstr .= ' <td class="editname"><input type="submit" value="Add" name="key"></td>'.PHP_EOL;
358 $outstr .= '<input type="hidden" value="testdata" name="action">';
359 $outstr .= '<input type="hidden" value="add" name="command">';
360 $outstr .= '<input type="hidden" value="00000000-0000-0000-0000-000000000000" name="UUID">';
361 $outstr .= ' </tr>'.PHP_EOL;
362 $outstr .= ' </table>'.PHP_EOL;
363 $outstr .= ' </form>'.PHP_EOL;
364
365 $outstr .= ' </div> <!-- atable -->'.PHP_EOL;
148 $outstr .= build_footer(); 366 $outstr .= build_footer();
149 echo $outstr; 367 echo $outstr;
150 } 368 }
151 369

mercurial