summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole/rest/common.php
blob: 8d192035ea72cd9ccc5f1466a11edca16e1813ec (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
 * @file    index.php
 * @brief   OpenGnsys REST API: common functions and routes
 * @warning All input and output messages are formatted in JSON.
 * @note    Some ideas are based on article "How to create REST API for Android app using PHP, Slim and MySQL" by Ravi Tamada, thanx.
 * @license GNU GPLv3+
 * @author  Ramón M. Gómez, ETSII Univ. Sevilla
 * @version 1.1.0 - First version
 * @date    2016-11-17
 */


// Common constants.
define('REST_LOGFILE', '/opt/opengnsys/log/rest.log');

// Set time zone.
if (function_exists("date_default_timezone_set")) {
    if (exec("timedatectl status | awk '/Time zone/ {print $3}'", $out, $err)) {
        date_default_timezone_set($out[0]);
    }
}

// Common functions.

/**
 * @brief   Function to write a line into log file.
 * @param   string message  Message to log.
 * warning  Line format: "Date: ClientIP: UserId: Status: Method Route: Message"
 */
function writeRestLog($message = "") {
	global $userid;
	if (is_writable(REST_LOGFILE)) {
		$app = \Slim\Slim::getInstance();
		file_put_contents(REST_LOGFILE, date(DATE_ISO8601) .": " .
						$_SERVER['REMOTE_ADDR'] . ": " .
						(isset($userid) ? $userid : "-") . ": " .
						$app->response->getStatus() . ": " .
						$app->request->getMethod() . " " .
						$app->request->getPathInfo() . ": $message\n",
			  	FILE_APPEND);
	}
}

/**
 * @brief   Compose JSON response.
 * @param   int status      Status code for HTTP response.
 * @param   array response  Response data.
 * @param   int opts        Options to encode JSON data.
 * @return  string          JSON response.
 */
function jsonResponse($status, $response, $opts=0) {
	$app = \Slim\Slim::getInstance();
	// HTTP status code.
	$app->status($status);
	// Content-type HTTP header.
	$app->contentType('application/json; charset=utf-8');
	// JSON response.
	echo json_encode($response, $opts);
}

/**
 * @brief   Print immediately JSON response to continue processing.
 * @param   int status      Status code for HTTP response.
 * @param   array response  Response data.
 * @param   int opts        Options to encode JSON data.
 * @return  string          JSON response.
 */
function jsonResponseNow($status, $response, $opts=0) {
	// Flush buffer.
	ob_end_clean();
	ob_end_flush();
	header("Connection: close");
	// Compose headers and content.
	http_response_code((int)$status);
	header('Content-type: application/json; charset=utf-8');
	ignore_user_abort();
	ob_start();
	echo json_encode($response, $opts);
	$size = ob_get_length();
	header("Content-Length: $size");
	// Print content.
	ob_end_flush();
	flush();
	session_write_close();
}

/**
 * @brief    Validate API key included in "Authorization" HTTP header.
 * @return   JSON response on error.
 */
function validateApiKey() {
	global $cmd;
	global $userid;
	$response = array();
	$app = \Slim\Slim::getInstance();
	// Read Authorization HTTP header.
	if (! empty($_SERVER['HTTP_AUTHORIZATION'])) {
		// Assign user id. that match this key to global variable.
		$apikey = htmlspecialchars($_SERVER['HTTP_AUTHORIZATION']);
		$cmd->texto = "SELECT idusuario
				 FROM usuarios
				WHERE apikey='$apikey' LIMIT 1";
		$rs=new Recordset;
		$rs->Comando=&$cmd;
		if ($rs->Abrir()) {
			$rs->Primero();
			if (!$rs->EOF){
				// Fetch user id.
				$userid = $rs->campos["idusuario"];
			} else {
                		// Credentials error.
                		$response['message'] = 'Login failed. Incorrect credentials';
				jsonResponse(401, $response);
				$app->stop();
			}
			$rs->Cerrar();
		} else {
			// Database error.
			$response['message'] = "An error occurred, please try again";
			jsonResponse(500, $response);
		}
	} else {
		// Error: missing API key.
		$response['message'] = 'Missing API key';
		jsonResponse(400, $response);
		$app->stop();
	}
}

/**
 * @brief    Check if parameter is set and print error messages if empty.
 * @param    string param    Parameter to check.
 * @return   boolean         "false" if parameter is null, otherwise "true".
 */
function checkParameter($param) {
	if (isset($param)) {
		return true;
	} else {
		// Print error message.
		$response['message'] = 'Parameter not found';
		jsonResponse(400, $response);
		return false;
	}
}

/**
 * @brief    Check if all parameters are positive integer numbers.
 * @param    int id ...      Identificators to check (variable number of parameters).
 * @return   boolean         "true" if all ids are int>0, otherwise "false".
 */
function checkIds() {
	$opts = Array('options' => Array('min_range' => 1));	// Check for int>0
	foreach (func_get_args() as $id) {
		if (filter_var($id, FILTER_VALIDATE_INT, $opts) === false) {
			return false;
		}
	}
	return true;
}

/**
 * @fn       sendCommand($serverip, $serverport, $reqframe, &$values)
 * @brief    Send a command to an OpenGnsys ogAdmServer and get request.
 * @param    string serverip    Server IP address.
 * @param    string serverport  Server port.
 * @param    string reqframe    Request frame (field's separator is "\r").
 * @param    array values       Response values (out parameter).
 * @return   boolean            "true" if success, otherwise "false".
 */
function sendCommand($serverip, $serverport, $reqframe, &$values) {
	global $LONCABECERA;
	global $LONHEXPRM;

	// Connect to server.
	$respvalues = "";
	$connect = new SockHidra($serverip, $serverport);
	if ($connect->conectar()) {
		// Send request frame to server.
		$result = $connect->envia_peticion($reqframe);
		if ($result) {
			// Parse request frame.
			$respframe = $connect->recibe_respuesta();
			$connect->desconectar();
			$paramlen = hexdec(substr($respframe, $LONCABECERA, $LONHEXPRM));
			$params = substr($respframe, $LONCABECERA+$LONHEXPRM, $paramlen);
			// Fetch values and return result.
			$values = extrae_parametros($params, "\r", '=');
			return ($values);
		} else {
			// Return with error.
			return (false);
		}
	} else {
		// Return with error.
		return (false);
	}
}

/**
 * @brief   Show custom message for "not found" error (404).
 */
$app->notFound(function() {
	echo "REST route not found.";
   }
);

/**
 * @brief   Hook to write a REST init log message, if debug is enabled.
 * @warning Message will be written in REST log file.
 */
$app->hook('slim.before', function() use ($app) {
	if ($app->settings['debug'])
		writeRestLog("Init.");
    }
);

/**
 * @brief   Hook to write an error log message and a REST exit log message if debug is enabled.
 * @warning Error message will be written in web server's error file.
 * @warning REST message will be written in REST log file.
 */
$app->hook('slim.after', function() use ($app) {
	if ($app->response->getStatus() != 200 ) {
		// Compose error message (truncating long lines). 
		$app->log->error(date(DATE_ISO8601) . ': ' .
				 $app->getName() . ': ' .
				 $_SERVER['REMOTE_ADDR'] . ": " .
				 (isset($userid) ? $userid : "-") . ": " .
				 $app->response->getStatus() . ': ' .
				 $app->request->getMethod() . ' ' .
				 $app->request->getPathInfo() . ': ' .
				 substr($app->response->getBody(), 0, 100));
	}
	if ($app->settings['debug'])
		writeRestLog("Exit.");
   }
);


// Common routes.

/**
 * @brief    Get general server information 
 * @note     Route: /info, Method: GET
 * @param    no
 * @return   JSON object with basic server information (version, services, etc.)
 */
$app->get('/info', function() {
      // Reading version file.
      @list($project, $version, $release) = explode(' ', file_get_contents('/opt/opengnsys/doc/VERSION.txt'));
      $response['project'] = trim($project);
      $response['version'] = trim($version);
      $response['release'] = trim($release);
      // Getting actived services.
      @$services = parse_ini_file('/etc/default/opengnsys');
      $response['services'] = Array();
      if (@$services["RUN_OGADMSERVER"] === "yes") {
          array_push($response['services'], "server");
          $hasOglive = true;
      }
      if (@$services["RUN_OGADMREPO"] === "yes")  array_push($response['services'], "repository");
      if (@$services["RUN_BTTRACKER"] === "yes")  array_push($response['services'], "tracker");
      // Reading installed ogLive information file.
      if ($hasOglive === true) {
          $data = json_decode(@file_get_contents('/opt/opengnsys/etc/ogliveinfo.json'));
          if (isset($data->oglive)) {
              $response['oglive'] = $data->oglive;
          }
      }
      jsonResponse(200, $response);
   }
);

/**
 * @brief    Get the server status
 * @note     Route: /status, Method: GET
 * @param    no
 * @return   JSON object with all data collected from server status (RAM, %CPU, etc.).
 */
$app->get('/status', function() {
      // Getting memory and CPU information.
      exec("awk '$1~/Mem/ {print $2}' /proc/meminfo",$memInfo);
      $memInfo = array("total" => $memInfo[0], "used" => $memInfo[1]);
      $cpuInfo = exec("awk '$1==\"cpu\" {printf \"%.2f\",($2+$4)*100/($2+$4+$5)}' /proc/stat");
      $cpuModel = exec("awk -F: '$1~/model name/ {print $2}' /proc/cpuinfo");
      $response["memInfo"] = $memInfo;
      $response["cpu"] = array("model" => trim($cpuModel), "usage" => $cpuInfo);
      jsonResponse(200, $response);
   } 
);
?>