summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole/rest/repository.php
diff options
context:
space:
mode:
authorramon <ramongomez@us.es>2016-04-26 13:13:57 +0000
committerramon <ramongomez@us.es>2016-04-26 13:13:57 +0000
commit15acccdf950630f68cd77ed1e7abf6e4c34dc40e (patch)
tree4d584a525dd5d912c16238e1e446b013fd1151d2 /admin/WebConsole/rest/repository.php
parentdbbe6893690f7687e03b05d42e1d96ae88446f02 (diff)
#743: Integrar código del ticket:743 en rama de desarrollo.
git-svn-id: https://opengnsys.es/svn/branches/version1.1@4904 a21b9725-9963-47de-94b9-378ad31fedc9
Diffstat (limited to 'admin/WebConsole/rest/repository.php')
-rw-r--r--admin/WebConsole/rest/repository.php176
1 files changed, 176 insertions, 0 deletions
diff --git a/admin/WebConsole/rest/repository.php b/admin/WebConsole/rest/repository.php
new file mode 100644
index 00000000..0c18b6dd
--- /dev/null
+++ b/admin/WebConsole/rest/repository.php
@@ -0,0 +1,176 @@
+<?php
+/**
+ * @file index.php
+ * @brief OpenGnsys REST API manager.
+ * @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 Juan Manuel Bardallo SIC Universidad de Huelva
+ * @version 1.0
+ * @date 2016-04-06
+ */
+
+// Auxiliar functions.
+/**
+ * @brief Validate API key included in "Authorization" HTTP header.
+ * @return JSON response on error.
+ */
+function validateRepositoryApiKey() {
+ $response = array();
+
+ // Read Authorization HTTP header.
+ $headers = apache_request_headers();
+ if (! empty($headers['Authorization'])) {
+ // Assign user id. that match this key to global variable.
+ $apikey = htmlspecialchars($headers['Authorization']);
+ // El repositorio recupera el token desde el fichero de configuracion ogAdmRepo.cfg
+ $confFile = fopen("../../etc/ogAdmRepo.cfg", "r");
+
+ // Leemos cada linea hasta encontrar la clave "ApiToken"
+ if ($confFile) {
+ $found = false;
+ while(!feof($confFile)){
+ $line = fgets($confFile);
+ $key = strtok($line,"=");
+ if($key == "ApiToken"){
+ $token = trim(strtok("="));
+ if(strcmp($apikey,$token) == 0){
+ $found = true;
+ }
+ }
+ }
+ if (!$found){
+ // Credentials error.
+ $response['error'] = true;
+ $response['message'] = 'Login failed. Incorrect credentials';
+ jsonResponse(401, $response);
+ $app->stop();
+ }
+ } else {
+ // Access error.
+ $response['error'] = true;
+ $response['message'] = "An error occurred, please try again";
+ jsonResponse(500, $response);
+ }
+ } else {
+ // Error: missing API key.
+ $response['error'] = true;
+ $response['message'] = 'Missing Repository API key';
+ jsonResponse(400, $response);
+ $app = \Slim\Slim::getInstance();
+ $app->stop();
+ }
+}
+
+function commandExist($cmd) {
+ $returnVal = shell_exec("which $cmd");
+ return (empty($returnVal) ? false : true);
+}
+
+// Define REST routes.
+
+
+/**
+ * @brief List all images in the repository
+ * @note Route: /images, Method: GET
+ * @param no
+ * @return JSON array with imagename, file size
+ */
+$app->get('/repository/images', 'validateRepositoryApiKey',
+ function() {
+ $imgPath = '/opt/opengnsys/images';
+ $app = \Slim\Slim::getInstance();
+ // Comprobar si en la peticion se especificó un filtro por extensiones
+ $extensions = $app->request->get('extensions');
+
+ if ($manager = opendir($imgPath)) {
+ $repoInfo=exec("df -h ".$imgPath);
+ $repoInfo=split(" ",preg_replace('/\s+/', ' ', $repoInfo));
+
+ $response['disk']["total"]=$repoInfo[1];
+ $response['disk']["used"]=$repoInfo[2];
+ $response['disk']["free"]=$repoInfo[3];
+ $response['disk']["percent"]=$repoInfo[4];
+
+ $response['images'] = array();
+ while (false !== ($entry = readdir($manager))) {
+ $include = true;
+ if ($entry != "." && $entry != "..") {
+ // Si se especificó algun filtro por extension, comprobamos si el fichero la cumple
+ if($extensions){
+ $ext = pathinfo($imgPath."/".$entry, PATHINFO_EXTENSION);
+ // Puede ser una o varias dependiendo de si es array o no
+ if(is_array($extensions) && !in_array($ext, $extensions)){
+ $include = false;
+ }
+ else if(!is_array($extensions) && $extensions != $ext){
+ $include = false;
+ }
+
+ }
+ if($include == true){
+ $strFileName = $imgPath."/".$entry;
+ $fileInfo["file"]["name"] = $entry;
+ $fileInfo["file"]["size"] = filesize($strFileName);
+ $fileInfo["file"]["modified"] = date( "D d M Y g:i A", filemtime($strFileName));
+ $fileInfo["file"]["permissions"] = (is_readable($strFileName)?"r":"-").(is_writable($strFileName)?"w":"-").(is_executable($strFileName)?"x":"-");
+ array_push($response['images'], $fileInfo);
+ }
+ }
+ }
+ closedir($manager);
+ jsonResponse(200, $response);
+ }else{
+ // Print error message.
+ $response['error'] = true;
+ $response['message'] = 'Images directory not found';
+ jsonResponse(404, $response);
+ }
+ $app->stop();
+ }
+);
+
+
+/**
+ * @brief Power on a pc or group of pcs with the MAC specified in POST parameters
+ * @note Route: /poweron, Method: POST
+ * @param macs OU id.
+ * @return JSON string ok if the power on command was sent
+ */
+$app->post('/repository/poweron', 'validateRepositoryApiKey',
+ function() {
+ $app = \Slim\Slim::getInstance();
+ // Debe venir el parametro macs en el post
+ $data = $app->request()->post();
+ if(empty($data["macs"])){
+ // Print error message.
+ $response['error'] = true;
+ $response['message'] = 'Required param macs not found';
+ jsonResponse(400, $response);
+ }
+ else{
+ $macs = $data["macs"];
+ $strMacs = "";
+ foreach($macs as $mac){
+ $strMacs .= " ".$mac;
+ }
+ // Ejecutar comando wakeonlan, debe estar disponible en el sistema operativo
+ if(commandExist("wakeonlan")){
+ $response["output"] = "Executing wakeonlan ".trim($strMacs)."\n";
+ $response["output"] .= shell_exec("wakeonlan ".trim($strMacs));
+ // Comprobar si el comando se ejecutórrectamente
+ jsonResponse(200, $response);
+ }
+ else{
+ // Print error message.
+ $response['error'] = true;
+ $response['message'] = 'Wakeonlan command not found in this repository';
+ jsonResponse(404, $response);
+ }
+ }
+ }
+);
+
+?>
+
+