[15acccd] | 1 | <?php |
---|
| 2 | /** |
---|
[fb2ee20] | 3 | * @file repository.php |
---|
| 4 | * @brief OpenGnsys Repository REST API manager. |
---|
[15acccd] | 5 | * @warning All input and output messages are formatted in JSON. |
---|
| 6 | * @note Some ideas are based on article "How to create REST API for Android app using PHP, Slim and MySQL" by Ravi Tamada, thanx. |
---|
| 7 | * @license GNU GPLv3+ |
---|
| 8 | * @author Juan Manuel Bardallo SIC Universidad de Huelva |
---|
| 9 | * @version 1.0 |
---|
| 10 | * @date 2016-04-06 |
---|
| 11 | */ |
---|
| 12 | |
---|
[2b00219] | 13 | |
---|
[15acccd] | 14 | // Auxiliar functions. |
---|
| 15 | /** |
---|
| 16 | * @brief Validate API key included in "Authorization" HTTP header. |
---|
| 17 | * @return JSON response on error. |
---|
| 18 | */ |
---|
| 19 | function validateRepositoryApiKey() { |
---|
| 20 | $response = array(); |
---|
[e3b5585] | 21 | $app = \Slim\Slim::getInstance(); |
---|
[15acccd] | 22 | |
---|
[41c430a] | 23 | // Assign user id. that match this key to global variable. |
---|
| 24 | @$apikey = htmlspecialchars(function_exists('apache_request_headers') ? apache_request_headers()['Authorization'] : $_SERVER['HTTP_AUTHORIZATION']); |
---|
| 25 | if (isset($apikey)) { |
---|
| 26 | // fetch repository token from ogAdmRepo.cfg configuration file. |
---|
[e57b608] | 27 | @$confFile = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg', 'r'); |
---|
| 28 | if (isset($confFile)) { |
---|
[41c430a] | 29 | if(@strcmp($apikey, $confFile['ApiToken']) == 0) { |
---|
| 30 | // Credentials OK. |
---|
| 31 | return true; |
---|
| 32 | } else { |
---|
[15acccd] | 33 | // Credentials error. |
---|
| 34 | $response['message'] = 'Login failed. Incorrect credentials'; |
---|
| 35 | jsonResponse(401, $response); |
---|
| 36 | $app->stop(); |
---|
| 37 | } |
---|
| 38 | } else { |
---|
[a38cb26] | 39 | // Cannot access configuration file. |
---|
[15acccd] | 40 | $response['message'] = "An error occurred, please try again"; |
---|
| 41 | jsonResponse(500, $response); |
---|
[41c430a] | 42 | $app->stop(); |
---|
[15acccd] | 43 | } |
---|
| 44 | } else { |
---|
| 45 | // Error: missing API key. |
---|
| 46 | $response['message'] = 'Missing Repository API key'; |
---|
| 47 | jsonResponse(400, $response); |
---|
[41c430a] | 48 | $app->stop(); |
---|
[15acccd] | 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | function commandExist($cmd) { |
---|
| 53 | $returnVal = shell_exec("which $cmd"); |
---|
| 54 | return (empty($returnVal) ? false : true); |
---|
| 55 | } |
---|
| 56 | |
---|
[338b30e] | 57 | |
---|
[15acccd] | 58 | // Define REST routes. |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * @brief List all images in the repository |
---|
[72bbcf8] | 63 | * @note Route: /repository/images, Method: GET |
---|
[15acccd] | 64 | * @param no |
---|
[72bbcf8] | 65 | * @return JSON object with directory, images array, ous array and disk data. |
---|
[15acccd] | 66 | */ |
---|
[72bbcf8] | 67 | $app->get('/repository/images(/)', 'validateRepositoryApiKey', |
---|
[338b30e] | 68 | function() use ($app) { |
---|
| 69 | $response = array(); |
---|
| 70 | // Read repository information file. |
---|
| 71 | $cfgFile = '/opt/opengnsys/etc/repoinfo.json'; |
---|
| 72 | $response = json_decode(@file_get_contents($cfgFile), true); |
---|
| 73 | // Check if directory exists. |
---|
| 74 | $imgPath = @$response['directory']; |
---|
| 75 | if (is_dir($imgPath)) { |
---|
[e3b5585] | 76 | // Complete global image information. |
---|
[338b30e] | 77 | for ($i=0; $i<sizeof(@$response['images']); $i++) { |
---|
[72bbcf8] | 78 | $img = $response['images'][$i]; |
---|
| 79 | $file = $imgPath."/".($img['type']==="dir" ? $img["name"] : $img["name"].".".$img["type"]); |
---|
[338b30e] | 80 | $response['images'][$i]['size'] = @stat($file)['size']; |
---|
[72bbcf8] | 81 | $response['images'][$i]['modified'] = date("Y-m-d H:i:s", @stat($file)['mtime']); |
---|
[338b30e] | 82 | $response['images'][$i]['mode'] = substr(decoct(@stat($file)['mode']), -4); |
---|
[3b8d1ea] | 83 | $backupfile = "$file.ant"; |
---|
[9d773c0] | 84 | if (file_exists($backupfile)) { |
---|
| 85 | $response['images'][$i]['backedup'] = true; |
---|
| 86 | $response['images'][$i]['backupsize'] = @stat($backupfile)['size']; |
---|
| 87 | } else { |
---|
| 88 | $response['images'][$i]['backedup'] = false; |
---|
| 89 | } |
---|
[3b8d1ea] | 90 | $lockfile = "$file.lock"; |
---|
| 91 | $response['images'][$i]['locked'] = file_exists($lockfile); |
---|
[15acccd] | 92 | } |
---|
[e3b5585] | 93 | // Complete image in OUs information. |
---|
| 94 | for ($j=0; $j<sizeof(@$response['ous']); $j++) { |
---|
| 95 | for ($i=0; $i<sizeof(@$response['ous'][$j]['images']); $i++) { |
---|
[72bbcf8] | 96 | $img = $response['ous'][$j]['images'][$i]; |
---|
| 97 | $file = $imgPath."/".$response['ous'][$j]['subdir']."/".($img['type']==="dir" ? $img["name"] : $img["name"].".".$img["type"]); |
---|
[e3b5585] | 98 | $response['ous'][$j]['images'][$i]['size'] = @stat($file)['size']; |
---|
[72bbcf8] | 99 | $response['ous'][$j]['images'][$i]['modified'] = date("Y-m-d H:i:s", @stat($file)['mtime']); |
---|
[e3b5585] | 100 | $response['ous'][$j]['images'][$i]['mode'] = substr(decoct(@stat($file)['mode']), -4); |
---|
[3b8d1ea] | 101 | $response['ous'][$j]['images'][$i]['backedup'] = false; |
---|
| 102 | $lockfile = "$file.lock"; |
---|
| 103 | $response['ous'][$j]['images'][$i]['locked'] = file_exists($lockfile); |
---|
[e3b5585] | 104 | } |
---|
| 105 | } |
---|
[338b30e] | 106 | // Retrieve disk information. |
---|
[72bbcf8] | 107 | $total = disk_total_space($imgPath); |
---|
| 108 | $free = disk_free_space($imgPath); |
---|
[d610135] | 109 | $response['disk']['total'] = $total; |
---|
| 110 | $response['disk']['free'] = $free; |
---|
[338b30e] | 111 | // JSON response. |
---|
| 112 | jsonResponse(200, $response); |
---|
| 113 | } else { |
---|
| 114 | // Print error message. |
---|
| 115 | $response['message'] = 'Images directory not found'; |
---|
| 116 | jsonResponse(404, $response); |
---|
[15acccd] | 117 | } |
---|
[338b30e] | 118 | $app->stop(); |
---|
| 119 | } |
---|
[15acccd] | 120 | ); |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | /** |
---|
[bdee878] | 124 | * @brief List image data |
---|
| 125 | * @note Route: /repository/image/:imagename, Method: GET |
---|
| 126 | * @param no |
---|
| 127 | * @return JSON object with image data. |
---|
| 128 | */ |
---|
[e368226] | 129 | $app->get('/repository/image(/:ouname)/:imagename(/)', 'validateRepositoryApiKey', |
---|
| 130 | function($ouname="/", $imagename) use ($app) { |
---|
| 131 | $images = array(); |
---|
[bdee878] | 132 | $response = array(); |
---|
| 133 | // Search image name in repository information file. |
---|
| 134 | $cfgFile = '/opt/opengnsys/etc/repoinfo.json'; |
---|
| 135 | $json = json_decode(@file_get_contents($cfgFile), true); |
---|
| 136 | $imgPath = @$json['directory']; |
---|
[e368226] | 137 | if (empty($ouname) or $ouname == "/") { |
---|
| 138 | // Search in global directory. |
---|
| 139 | $images = @$json['images']; |
---|
| 140 | } else { |
---|
| 141 | // Search in OU directory. |
---|
| 142 | for ($i=0; $i<sizeof(@$json['ous']); $i++) { |
---|
| 143 | if ($json['ous'][$i]['subdir'] == $ouname) { |
---|
| 144 | $images = $json['ous'][$i]['images']; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | // Search image. |
---|
| 149 | foreach ($images as $img) { |
---|
[bdee878] | 150 | if ($img['name'] == $imagename) { |
---|
[e368226] | 151 | $response = $img; |
---|
| 152 | $file = "$imgPath/$ouname/" . ($img['type']==="dir" ? $img["name"] : $img["name"].".".$img["type"]); |
---|
| 153 | $response['size'] = @stat($file)['size']; |
---|
| 154 | $response['modified'] = date("Y-m-d H:i:s", @stat($file)['mtime']); |
---|
| 155 | $response['mode'] = substr(decoct(@stat($file)['mode']), -4); |
---|
[3b8d1ea] | 156 | $backupfile = "$file.ant"; |
---|
[bdee878] | 157 | if (file_exists($backupfile)) { |
---|
[e368226] | 158 | $response['backedup'] = true; |
---|
| 159 | $response['backupsize'] = @stat($backupfile)['size']; |
---|
[bdee878] | 160 | } else { |
---|
[e368226] | 161 | $response['backedup'] = false; |
---|
[bdee878] | 162 | } |
---|
[3b8d1ea] | 163 | $lockfile = "$file.lock"; |
---|
| 164 | $response['locked'] = file_exists($lockfile); |
---|
[bdee878] | 165 | } |
---|
| 166 | } |
---|
[e368226] | 167 | if (isset ($response)) { |
---|
[bdee878] | 168 | // JSON response. |
---|
| 169 | jsonResponse(200, $response); |
---|
| 170 | } else { |
---|
| 171 | // Print error message. |
---|
| 172 | $response['message'] = 'Image not found'; |
---|
| 173 | jsonResponse(404, $response); |
---|
| 174 | } |
---|
| 175 | $app->stop(); |
---|
| 176 | } |
---|
| 177 | ); |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | /** |
---|
[15acccd] | 181 | * @brief Power on a pc or group of pcs with the MAC specified in POST parameters |
---|
| 182 | * @note Route: /poweron, Method: POST |
---|
| 183 | * @param macs OU id. |
---|
| 184 | * @return JSON string ok if the power on command was sent |
---|
| 185 | */ |
---|
| 186 | $app->post('/repository/poweron', 'validateRepositoryApiKey', |
---|
[fce0af3] | 187 | function() use($app) { |
---|
[e57b608] | 188 | // The macs parameter must come in the post (JSON object with array of MACs) |
---|
| 189 | $data = json_decode($app->request()->getBody()); |
---|
| 190 | if (empty($data->macs)) { |
---|
| 191 | // Print error message. |
---|
| 192 | $response['message'] = 'Required param macs not found'; |
---|
| 193 | jsonResponse(400, $response); |
---|
| 194 | } else { |
---|
| 195 | // Execute local wakeonlan command (may be installed) |
---|
| 196 | if(commandExist("wakeonlan")) { |
---|
| 197 | $strMacs = trim(implode(' ', $data->macs)); |
---|
| 198 | $response["output"] = "Executing wakeonlan ".$strMacs."\n"; |
---|
| 199 | $response["output"] .= shell_exec("wakeonlan ".$strMacs); |
---|
| 200 | jsonResponse(200, $response); |
---|
[a5f7c30] | 201 | } else { |
---|
[e57b608] | 202 | // Print error message. |
---|
| 203 | $response['message'] = 'Wakeonlan command not found in this repository'; |
---|
| 204 | jsonResponse(404, $response); |
---|
[15acccd] | 205 | } |
---|
| 206 | } |
---|
[e57b608] | 207 | $app->stop(); |
---|
[15acccd] | 208 | } |
---|
| 209 | ); |
---|
| 210 | |
---|