stop(); } } else { // Access error. $response['message'] = "An error occurred, please try again"; jsonResponse(500, $response); } } 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); } function humanSize($bytes) { $si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' ); $base = 1024; $class = min((int)log($bytes , $base) , count($si_prefix) - 1); return sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class]; } // 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() use ($app) { $response = array(); // 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 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 (objeto JSON con array de MACs) $data = $app->request()->post(); if(empty($data->macs)){ // Print error message. $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['message'] = 'Wakeonlan command not found in this repository'; jsonResponse(404, $response); } } } ); ?>