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; $istop(); } ); /** * @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; $istop(); } ); /** * @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(); } );