1+ <?php
2+
3+ date_default_timezone_set ('UTC ' );
4+
5+ $ command = substr ($ _SERVER ['PATH_INFO ' ], 1 ); # dynamo.mit.edu/registry/invalidate/command -> /command
6+
7+ if ($ command == "" ) {
8+ # show webpage
9+ exit (0 );
10+ }
11+
12+ if ($ command == 'help ' ) {
13+ # echo file_get_contents(__DIR__ . '/html/invalidate_help.html');
14+ exit (0 );
15+ }
16+
17+ include_once (__DIR__ . '/../dynamo/common/db_conf.php ' );
18+ include_once (__DIR__ . '/common.php ' );
19+
20+ /* if ($_SERVER['SSL_CLIENT_VERIFY'] != 'SUCCESS') */
21+ /* send_response(401, 'AuthFailed', 'SSL authentication failed.'); */
22+
23+ if ($ command == 'invalidate ' || $ command == 'clear ' ) {
24+ if (!isset ($ _REQUEST ['item ' ]))
25+ send_response (400 , 'BadRequest ' , '\'item \' field is required ' , NULL , 'json ' );
26+
27+ $ item = $ _REQUEST ['item ' ];
28+
29+ $ db = new mysqli ($ db_conf ['host ' ], $ db_conf ['user ' ], $ db_conf ['password ' ], 'dynamoregister ' );
30+
31+ if ($ command == 'invalidate ' ) {
32+ $ stmt = $ db ->prepare ('INSERT INTO `invalidations` (`item`, `timestamp`) VALUES (?, NOW()) ' );
33+ $ message = 'Item added ' ;
34+ }
35+ else {
36+ $ stmt = $ db ->prepare ('DELETE FROM `invalidations` WHERE `item` = ? ' );
37+ $ message = 'Item removed ' ;
38+ }
39+
40+ $ return_data = array ();
41+
42+ if (is_array ($ item )) {
43+ foreach ($ item as $ t ) {
44+ $ stmt ->bind_param ('s ' , $ t );
45+ $ stmt ->execute ();
46+ $ return_data [] = array ('item ' => $ t );
47+ }
48+ }
49+ else {
50+ $ stmt ->bind_param ('s ' , $ item );
51+ $ stmt ->execute ();
52+ $ return_data [] = array ('item ' => $ item );
53+ }
54+ $ stmt ->close ();
55+
56+ send_response (200 , 'OK ' , $ message , $ return_data , 'json ' );
57+ }
58+ else {
59+ send_response (400 , 'BadRequest ' , 'Invalid command (possible values: help, invalidate) ' );
60+ }
61+
62+ ?>
0 commit comments