1 | <?php |
---|
2 | |
---|
3 | // OpenGnsys REST routes for OGAgent communications. |
---|
4 | // Author: Ramón M. Gómez |
---|
5 | // Date: 2015-09-04 |
---|
6 | // Warning: authentication/authorisation not included. |
---|
7 | |
---|
8 | |
---|
9 | // OGAgent sessions log file. |
---|
10 | define('LOG_FILE', '/opt/opengnsys/log/ogagent.log'); |
---|
11 | |
---|
12 | // OGAgent notifies that its service is started on client. |
---|
13 | $app->post('/ogagent/started', |
---|
14 | function() use ($app) { |
---|
15 | |
---|
16 | try { |
---|
17 | // Reading POST parameters in JSON format. |
---|
18 | $input = json_decode($app->request()->getBody()); |
---|
19 | $ip = htmlspecialchars($input->ip); |
---|
20 | $mac = htmlspecialchars($input->mac); |
---|
21 | // May check that client is included in the server database? |
---|
22 | // Default processing: log activity. |
---|
23 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent started: ip=$ip, mac=$mac.\n", FILE_APPEND); |
---|
24 | // Response. |
---|
25 | jsonResponse(200, $response); |
---|
26 | } catch (Exception $e) { |
---|
27 | // Comunication error. |
---|
28 | $response["message"] = $e->getMessage(); |
---|
29 | jsonResponse(400, $response); |
---|
30 | } |
---|
31 | } |
---|
32 | ); |
---|
33 | |
---|
34 | // OGAgent notifies that its service is stopped on client. |
---|
35 | $app->post('/ogagent/stopped', |
---|
36 | function() use ($app) { |
---|
37 | |
---|
38 | try { |
---|
39 | // Reading POST parameters in JSON format. |
---|
40 | $input = json_decode($app->request()->getBody()); |
---|
41 | $ip = htmlspecialchars($input->ip); |
---|
42 | $mac = htmlspecialchars($input->mac); |
---|
43 | // May check that client is included in the server database? |
---|
44 | // Default processing: log activity. |
---|
45 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent stopped: ip=$ip, mac=$mac.\n", FILE_APPEND); |
---|
46 | // Response. |
---|
47 | jsonResponse(200, $response); |
---|
48 | } catch (Exception $e) { |
---|
49 | // Comunication error. |
---|
50 | $response["message"] = $e->getMessage(); |
---|
51 | jsonResponse(400, $response); |
---|
52 | } |
---|
53 | } |
---|
54 | ); |
---|
55 | |
---|
56 | // OGAgent notifies that an user logs in. |
---|
57 | $app->post('/ogagent/loggedin', |
---|
58 | function() use ($app) { |
---|
59 | |
---|
60 | try { |
---|
61 | // Reading POST parameters in JSON format. |
---|
62 | $input = json_decode($app->request()->getBody()); |
---|
63 | $ip = htmlspecialchars($input->ip); |
---|
64 | $user = htmlspecialchars($input->user); |
---|
65 | // May check that client is included in the server database? |
---|
66 | // Default processing: log activity. |
---|
67 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged in: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
68 | // Response. |
---|
69 | jsonResponse(200, $response); |
---|
70 | } catch (Exception $e) { |
---|
71 | // Comunication error. |
---|
72 | $response["message"] = $e->getMessage(); |
---|
73 | jsonResponse(400, $response); |
---|
74 | } |
---|
75 | } |
---|
76 | ); |
---|
77 | |
---|
78 | // OGAgent notifies that an user logs out. |
---|
79 | $app->post('/ogagent/loggedout', |
---|
80 | function() use ($app) { |
---|
81 | |
---|
82 | try { |
---|
83 | // Reading POST parameters in JSON format. |
---|
84 | $input = json_decode($app->request()->getBody()); |
---|
85 | $ip = htmlspecialchars($input->ip); |
---|
86 | $user = htmlspecialchars($input->user); |
---|
87 | // May check that client is included in the server database? |
---|
88 | // Default processing: log activity. |
---|
89 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged out: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
90 | // Response. |
---|
91 | jsonResponse(200, $response); |
---|
92 | } catch (Exception $e) { |
---|
93 | // Comunication error. |
---|
94 | $response["message"] = $e->getMessage(); |
---|
95 | jsonResponse(400, $response); |
---|
96 | } |
---|
97 | } |
---|
98 | ); |
---|
99 | |
---|
100 | ?> |
---|
101 | |
---|