/** * Adding Middle Layer to authenticate every request * Checking if the request has valid api key in the 'Authorization' header */ function authenticate(\Slim\Route $route) { // Getting request headers $headers = apache_request_headers(); $response = array(); $app = \Slim\Slim::getInstance(); // Verifying Authorization Header if (isset($headers['Authorization'])) { $db = new DbHandlerAccount(); // get the api key $api_key = $headers['Authorization']; // validating api key if (!$db->isValidApiKey($api_key)) { // api key is not present in users table $response["error"] = true; $response["message"] = "Access Denied. Invalid Api key"; echoRespnse(401, $response); $app->stop(); } else { global $user_id; // get user primary key id $user = $db->getUserId($api_key); if ($user != NULL) { $user_id = $user["id"]; } } } else { // api key is missing in header $response["error"] = true; $response["message"] = "Api key is misssing"; echoRespnse(400, $response); $app->stop(); } }
/** * Verifying required params posted or not */ function verifyRequiredParams($required_fields) { $error = false; $error_fields = ""; $request_params = array(); $request_params = $_REQUEST; // Handling PUT request params if ($_SERVER['REQUEST_METHOD'] == 'PUT') { $app = \Slim\Slim::getInstance(); parse_str($app->request()->getBody(), $request_params); } foreach ($required_fields as $field) { if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) { $error = true; $error_fields .= $field . ', '; } } if ($error) { // Required field(s) are missing or empty // echo error json and stop the app $response = array(); $app = \Slim\Slim::getInstance(); $response["error"] = true; $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty'; echoRespnse(400, $response); $app->stop(); } }
/** * Validating email address */ function validateEmail($email) { $app = \Slim\Slim::getInstance(); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $response["error"] = true; $response["message"] = 'Email address is not valid'; echoRespnse(400, $response); $app->stop(); } }
/** Verify required Headers */ function verifyHeaders($required_fields) { $error = false; $error_fields = ""; $request_params = array(); $app = \Slim\Slim::getInstance(); $request_params = $app->request->headers->all(); foreach ($required_fields as $field) { if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) { $error = true; $error_fields .= $field . ', '; } } if ($error) { // Required field(s) are missing or empty // echo error json and stop the app $response = array(); $response["error"] = true; $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty'; echoRespnse(400, $response); $app->stop(); } }
//$temp = preg_replace('/[^A-Za-z0-9\-]/', '', $temp); // Removes special chars. //echo $temp; } //counting frequency of words in timeline $wordsInTimeline = str_word_count($temp, 1); $frequencyInText = array_count_values($wordsInTimeline); //counting frequency of words in tweet $wordsInTweet = str_word_count($tweet, 1); $frequencyInTweet = array_count_values($wordsInTweet); //counting probability of each words in given tweet, works foreach ($frequencyInTweet as $key => $value) { $proOfWordsInTweet[$key] = $value / count($frequencyInTweet); } foreach ($frequencyInTweet as $key => $value) { $tmp = $key; foreach ($frequencyInText as $key => $value) { if ($key == $tmp) { //if the word is in history, then calculate probability of that word in timeline history $proOfWordsInText[$key] = $frequencyInText[$key] / count($frequencyInText); //sum up that probability $probability = $probability + $proOfWordsInTweet[$tmp] * $proOfWordsInText[$key]; } } } //form the response $response["twitter_id"] = $screen_name; $response["tweet"] = $tweet; $response["probability"] = $probability; echoRespnse(200, $response); }); $app->run();
function exitApp($status, $message) { $response[STATUS] = $status; $response[ERROR] = $message; echoRespnse($response); $app = \Slim\Slim::getInstance(); $app->stop(); }
/** * Adding Middle Layer to authenticate every request * Checking if the request has valid api key in the 'Authorization' header */ function authenticate(\Slim\Route $route) { // Getting request headers $headers = apache_request_headers(); $response = array(); $app = \Slim\Slim::getInstance(); // Verifying Authorization Header if (isset($headers['Authorization']) && isset($headers['Token'])) { $db = new DbHandlerParse(); // get the api key $api_key = $headers['Authorization']; // get the session token $session_token = $headers['Token']; // validating api key if (!$db->isValidApiKey($api_key)) { // api key is not present in users table $response["result"] = "error"; $response["message"] = "Access Denied. Invalid Api key"; echoRespnse(401, $response); $app->stop(); } else { if (!$db->isValidSessionToken($session_token, $api_key)) { // session token does not match api key or is just invalid $response["result"] = "error"; $response["message"] = "Access Denied. Invalid Token"; echoRespnse(401, $response); $app->stop(); } else { global $user_id; // get user primary key id $userID = $db->getUserId($api_key); if (NULL != $userID) { $user_id = $userID; $_SESSION['userId'] = $user_id; } } } } else { if (!isset($headers['Authorization'])) { // api key is missing in header $response["result"] = "error"; $response["message"] = "Api key is misssing"; echoRespnse(400, $response); $app->stop(); } else { // token is missing in header $response["result"] = "error"; $response["message"] = "Token is misssing"; echoRespnse(400, $response); $app->stop(); } } }
/** * Adding Middle Layer to authenticate every request * Checking if the request has valid api key in the 'Authorization' header */ function authenticate(\Slim\Route $route) { // Getting request headers $headers = apache_request_headers(); $response = array(); $app = \Slim\Slim::getInstance(); // Verifying Authorization Header if (isset($headers['Authorization'])) { $db = new DBHandler(); // get the api key $apikey = $headers['Authorization']; // validating api key if (!$db->isValidApiKey($apikey)) { // api key is not present in users table $response["error"] = true; $response["message"] = "Zugriff verweigert! Falscher API-Key!"; echoRespnse(401, $response); $app->stop(); } else { global $userid; // get user primary key id $user = $db->getUserId($apikey); if ($user != NULL) { $userid = $user; } } } else { // api key is missing in header $response["error"] = true; $response["message"] = "Zugriff verweigert! API-Key fehlt!"; echoRespnse(400, $response); $app->stop(); } }
$tmp["status"] = $call["status"]; $tmp["createdAt"] = $call["created_at"]; array_push($response["calls"], $tmp); } echoRespnse(200, $response); }); /** * Listing single call of particual user * method GET * url /calls/:id * Will return 404 if the call doesn't belongs to user */ $app->get('/calls/:id', 'authenticate', function ($call_id) { global $user_id; $response = array(); $db = new Calls(); // fetch call $result = $db->getCall($call_id, $user_id); if ($result != NULL) { $response["error"] = false; $response["id"] = $result["id"]; $response["call"] = $result["call"]; $response["status"] = $result["status"]; $response["createdAt"] = $result["created_at"]; echoRespnse(200, $response); } else { $response["error"] = true; $response["message"] = "The requested resource doesn't exists"; echoRespnse(404, $response); } });
/** * * @param type $name Description * * @return type Description */ function calcularPerfilAntropometrico() { /** * Anamnese: Peso, altura, idade, sexo * Acima de 19 calcular IMC * Abaixo de 19 anos verificar percentil: IMC x Idade. */ $request = \Slim\Slim::getInstance()->request(); $body = $request->getBody(); $anamneseJson = json_decode($body); // Entrevistado $nascimento = $anamneseJson->entrevistado->nascimento; $sexo = strtoupper($anamneseJson->entrevistado->sexo); // Anamnese. $peso = $anamneseJson->peso; $altura = $anamneseJson->altura; $validacao = PerfilAntropometricoValidate::validate($peso, $altura, $sexo, $nascimento); if ($validacao == VALIDO) { $anamnese = new Anamnese(); $anamnese->setPeso($peso); $anamnese->setAltura($altura); // Entrevistado $entrevistado = new Entrevistado(); $entrevistado->setNascimento($nascimento); $entrevistado->setSexo($sexo); $anamnese->setEntrevistado($entrevistado); // Calcular IMC $imcValor = IMCController::calculaIMC($peso, $altura); $idadeMeses = DataUtil::calcularIdadeMeses($nascimento); $idadeAnos = DataUtil::calcularIdadeAnos($nascimento); $curva = new Curva(); // Acima de 19 calcular IMC. if ($idadeMeses > IDADE_PERCENTIL_19) { // Cálculo do IMC para entrevistado acima de 19 anos. $imc = new Imc(); $imc->setValor($imcValor); $curva->setImc($imc); } else { $percentilMediano = PercentilController::calcularPercentil($imcValor, $sexo, $nascimento); if (!empty($percentilMediano)) { $curva->setPercentilMediano($percentilMediano); } else { $curva = PercentilController::calcularPercentilMargens($imcValor, $sexo, $nascimento); } } // IMC padrão. $imc = new Imc(); $imc->setValor($imcValor); $curva->setImc($imc); $diagnostico = PercentilController::determinarDiagnosticoNutricional($curva); $curva->setDiagnostico($diagnostico); echoRespnse(HTTP_OK, $curva); } else { } }
$response["message"] = "population created successfully"; $response["population_id"] = $population_id; error_log(print_R("population created: {$population_id}\n", TRUE), 3, LOG); echoRespnse(201, $response); } else { error_log(print_R("after population result bad\n", TRUE), 3, LOG); error_log(print_R($population_id, TRUE), 3, LOG); $response["error"] = true; $response["message"] = "Failed to create population. Please try again"; echoRespnse(400, $response); } } else { error_log(print_R("after upload result bad\n", TRUE), 3, LOG); $response["error"] = true; $response["message"] = "Failed to upload. Please try again"; echoRespnse(400, $response); } }); function uploadAudio($dataJsonDecode) { $app = \Slim\Slim::getInstance(); $db = new BeeDbHandler(); $audio_cnt = 0; $maxfreq = 750; foreach ($dataJsonDecode["audio"] as $loop) { //reset the frequency $frequency = 0; error_log(print_R($loop["amplitude"], TRUE), 3, LOG); $freqcount = count($loop["amplitude"]); error_log(print_R($freqcount . "\n", TRUE), 3, LOG); $freqincrement = $maxfreq / $freqcount;
function removeFromFavourite($id) { $request = Slim\Slim::getInstance()->request(); $id = intval($id); $contact = array(CONTACTS::IS_FAVOURITE => false); $query = new QueryHandler(); $response = $query->updateContact($id, $contact); echoRespnse($response); }
/** * Validating email address */ function validateEmail($email) { $app = \Slim\Slim::getInstance(); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $response["error"] = true; $response['error_id'] = 400; $response['error_title'] = 'Invalid Email'; $response["error_message"] = 'Email address is not valid'; // echo json response echoRespnse(400, $response); $app->stop(); } }
/** * Adding Middle Layer to authenticate every request * Checking if the request has valid api key in the 'Authorization' header */ function authenticate(\Slim\Route $route) { // Getting request headers $headers = apache_request_headers(); $response = array(); $app = \Slim\Slim::getInstance(); return; $user = array(); $db = new DbHandler(); $userSession = $app->getCookie('BBC_recipes_session'); $userUID = $app->getCookie('BBC_recipes_user_id'); $header = array_change_key_case($headers, CASE_UPPER); $api_key = isset($header['BBC-API-KEY']) ? $header['BBC-API-KEY'] : ''; if (isset($api_key)) { // // get the api key // // validating api key if ($api_key !== '406cc6ed2c7471d7593461264c0db966') { // // api key is not present in users table $response['error'] = true; $response['msg'] = 'Access Denied. Invalid Api key.'; echoRespnse(401, $response); $app->stop(); } if (!$db->authenticate($userUID, $userSession)) { // authentication failed $response['error'] = true; $response['msg'] = 'Authentication failed.'; echoRespnse(401, $response); $app->stop(); } } else { // api key is missing in header $response['error'] = true; $response['msg'] = 'Api key is misssing.'; echoRespnse(401, $response); $app->stop(); } }
} else { $res["message"] = "The requested resource doesn't exists!"; echoRespnse(404, $res); } }); $app->get('/jadwaldosen', function () { $res = array(); $db = new DbHandler(); $result = $db->getLectureSchadule(); while ($schadule = $result->fetch_assoc()) { $tmp = array(); $tmp["lecturer_name"] = $schadule["lecturer_name"]; $tmp["hour_day"] = $schadule["hour_day"]; $tmp["room_name"] = $schadule["room_name"]; $tmp["from"] = $schadule["from"]; $tmp["to"] = $schadule["to"]; array_push($res, $tmp); } echoRespnse(200, $res); }); /** JSON Encode **/ function echoRespnse($status_code, $response) { $app = \Slim\Slim::getInstance(); // Http response code $app->status($status_code); // setting response content type to json $app->contentType('application/json'); echo json_encode($response); } $app->run();