summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole/rest/repository.php
blob: 7a32b3885c0a92951ab12cc55ef3eb0ffe8c271f (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
<?php
/**
 * @file    repository.php
 * @brief   OpenGnsys Repository 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.1.0
 * @date    2016-04-06
 */


// Auxiliary functions.
/**
 * @brief    Validate API key included in "Authorization" HTTP header.
 * @return   JSON response on error.
 */
function validateRepositoryApiKey() {
	$response = [];
	$app = \Slim\Slim::getInstance();

	// Assign user id. that match this key to global variable.
	@$apikey = htmlspecialchars(function_exists('apache_request_headers') ? apache_request_headers()['Authorization'] : $_SERVER['HTTP_AUTHORIZATION']);
	if (isset($apikey)) {
		// fetch repository token from ogAdmRepo.cfg configuration file.
		@$confFile = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg', 'r');
		if (isset($confFile)) {
			if(@strcmp($apikey, $confFile['ApiToken']) == 0) {
				// Credentials OK.
				return true;
			} else {
				// Credentials error.
                		$response['message'] = 'Login failed. Incorrect credentials';
				jsonResponse(401, $response);
				$app->stop();
			}
		} else {
			// Cannot access configuration file.
			$response['message'] = "An error occurred, please try again";
			jsonResponse(500, $response);
			$app->stop();
		}
	} else {
		// Error: missing API key.
       		$response['message'] = 'Missing Repository API key';
		jsonResponse(400, $response);
		$app->stop();
	}
}

function commandExist($cmd) {
    $returnVal = shell_exec("which $cmd");
    return (empty($returnVal) ? false : true);
}


// REST routes.


/**
 * @brief    List all images in the repository
 * @note     Route: /repository/images, Method: GET
 * @return   string  JSON object with directory, images array, ous array and disk data.
 */
$app->get('/repository/images(/)', 'validateRepositoryApiKey', 
    function() use ($app) {
	$response = [];
	// Read repository information file.
	$cfgFile = '/opt/opengnsys/etc/repoinfo.json';
	$response = json_decode(@file_get_contents($cfgFile), true);
        // Check if directory exists.
	$imgPath = @$response['directory'];
	if (is_dir($imgPath)) {
		// Complete global image information.
		for ($i=0; $i<sizeof(@$response['images']); $i++) {
			$img = $response['images'][$i];
			$file = $imgPath."/".($img['type']==="dir" ? $img["name"] : $img["name"].".".$img["type"]);
			$response['images'][$i]['size'] = @stat($file)['size'];
			$response['images'][$i]['modified'] = date("Y-m-d H:i:s", @stat($file)['mtime']);
			$response['images'][$i]['mode'] = substr(decoct(@stat($file)['mode']), -4);
			$backupfile = "$file.ant";
			if (file_exists($backupfile)) {
				$response['images'][$i]['backedup'] = true;
				$response['images'][$i]['backupsize'] = @stat($backupfile)['size'];
			} else {
				$response['images'][$i]['backedup'] = false;
			}
			$lockfile = "$file.lock";
			$response['images'][$i]['locked'] = file_exists($lockfile);
		}
		// Complete image in OUs information.
		for ($j=0; $j<sizeof(@$response['ous']); $j++) {
			for ($i=0; $i<sizeof(@$response['ous'][$j]['images']); $i++) {
				$img = $response['ous'][$j]['images'][$i];
				$file = $imgPath."/".$response['ous'][$j]['subdir']."/".($img['type']==="dir" ? $img["name"] : $img["name"].".".$img["type"]);
				$response['ous'][$j]['images'][$i]['size'] = @stat($file)['size'];
				$response['ous'][$j]['images'][$i]['modified'] = date("Y-m-d H:i:s", @stat($file)['mtime']);
				$response['ous'][$j]['images'][$i]['mode'] = substr(decoct(@stat($file)['mode']), -4);
				$response['ous'][$j]['images'][$i]['backedup'] = false;
				$lockfile = "$file.lock";
				$response['ous'][$j]['images'][$i]['locked'] = file_exists($lockfile);
			}
		}
		// Retrieve disk information.
		$total = disk_total_space($imgPath);
		$free = disk_free_space($imgPath);
		$response['disk']['total'] = $total;
		$response['disk']['free'] = $free;
                // JSON response.
		jsonResponse(200, $response);
	} else {
		// Print error message.
		$response['message'] = 'Images directory not found';
		jsonResponse(404, $response);
	}
	$app->stop();
    }
);


/**
 * @brief    List image data
 * @note     Route: /repository/image/:imagename, Method: GET
 * @return   string  JSON object with image data.
 */
$app->get('/repository/image(/:ouname)/:imagename(/)', 'validateRepositoryApiKey', 
    function($ouname="/", $imagename) use ($app) {
	$images = [];
	$response = [];
	// Search image name in repository information file.
	$cfgFile = '/opt/opengnsys/etc/repoinfo.json';
	$json = json_decode(@file_get_contents($cfgFile), true);
	$imgPath = @$json['directory'];
	if (empty($ouname) or $ouname == "/") {
		// Search in global directory.
		$images = @$json['images'];
	} else {
		// Search in OU directory.
		for ($i=0; $i<sizeof(@$json['ous']); $i++) {
			if ($json['ous'][$i]['subdir'] == $ouname) {
				$images = $json['ous'][$i]['images'];
			}
		}
	}
	// Search image.
	foreach ($images as $img) {
		if ($img['name'] == $imagename) {
			$response = $img;
			$file = "$imgPath/$ouname/" . ($img['type']==="dir" ? $img["name"] : $img["name"].".".$img["type"]);
			$response['size'] = @stat($file)['size'];
			$response['modified'] = date("Y-m-d H:i:s", @stat($file)['mtime']);
			$response['mode'] = substr(decoct(@stat($file)['mode']), -4);
			$backupfile = "$file.ant";
			if (file_exists($backupfile)) {
				$response['backedup'] = true;
				$response['backupsize'] = @stat($backupfile)['size'];
			} else {
				$response['backedup'] = false;
			}
			$lockfile = "$file.lock";
			$response['locked'] = file_exists($lockfile);
		}
	}
	if (isset ($response)) {
                // JSON response.
		jsonResponse(200, $response);
	} else {
		// Print error message.
		$response['message'] = 'Image 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    array   Array of MAC addresses
 * @return   string  JSON string ok if the power on command was sent
 */
$app->post('/repository/poweron', 'validateRepositoryApiKey',
    function() use($app) {
		$response = [];
		// The macs parameter must come in the post (JSON object with array of MACs)
		$data = json_decode($app->request()->getBody());
		if (empty($data->macs)) {
			// Print error message.
			$response['message'] = 'Required param macs not found';
			jsonResponse(400, $response);
		} else {
			// Execute local wakeonlan command (may be installed)
			if(commandExist("wakeonlan")) {
				$strMacs = trim(implode(' ', $data->macs));
				if(stristr($strMacs, ':') === false) {
					$strMacs = implode(':', str_split($strMacs, 2));
				}
				$response["output"] = "Executing wakeonlan ".$strMacs."\n";
				$response["output"] .= shell_exec("wakeonlan ".$strMacs);
				jsonResponse(200, $response);
			} else {
				// Print error message.
				$response['message'] = 'Wakeonlan command not found in this repository';
				jsonResponse(404, $response);
			}
		}
		$app->stop();
	}
);