ComunicAPI/classes/APIClients.php

107 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2017-05-17 12:43:12 +00:00
<?php
/**
* API Clients checker class
2017-05-17 12:43:12 +00:00
*
* @author Pierre HUBERT
*/
class APIClients {
2017-05-17 12:43:12 +00:00
/**
2017-05-19 16:07:52 +00:00
* Check request client tokens
2017-05-17 12:43:12 +00:00
*
2018-01-02 16:43:00 +00:00
* @return bool Depends of the validity of the tokens
2017-05-17 12:43:12 +00:00
*/
public function checkClientRequestTokens() : bool {
2017-05-17 12:43:12 +00:00
if(!isset($_POST['serviceName']) OR !isset($_POST['serviceToken']))
return false; //No token specified
//Check tokens
2017-06-13 09:01:36 +00:00
if(!$serviceInfos = $this->validateClientTokens($_POST['serviceName'], $_POST['serviceToken']))
2017-05-17 12:43:12 +00:00
return false;
//Save service ID in a constant
2017-06-13 09:01:36 +00:00
define("APIServiceID", $serviceInfos["ID"]);
//Save service domain in a constant (if any)
2017-10-31 08:28:12 +00:00
if($serviceInfos["clientDomain"] != "")
2017-06-13 09:01:36 +00:00
define("APIServiceDomain", $serviceInfos["clientDomain"]);
2017-05-17 12:43:12 +00:00
//Else everything went good
return true;
}
/**
2017-05-19 16:07:52 +00:00
* Check client API credentials (tokens)
2017-05-17 12:43:12 +00:00
*
2018-01-02 16:43:00 +00:00
* @param string $serviceName The name of the service
* @param string $token The service's token
* @return bool / array False or Tokens ID / Depending of validity of credentials
2017-05-17 12:43:12 +00:00
*/
2018-01-02 16:43:00 +00:00
private function validateClientTokens(string $serviceName, string $token) {
2017-05-17 12:43:12 +00:00
//Prepare DataBase request
2017-06-07 12:53:58 +00:00
$tableName = CS::get()->config->get("dbprefix")."API_ServicesToken";
2017-05-17 12:43:12 +00:00
$conditions = "WHERE serviceName = ? AND token = ?";
$values = array(
$serviceName,
$token
);
//Make request
$requestResult = CS::get()->db->select($tableName, $conditions, $values);
//Analyse result
if(count($requestResult) == 0){
//There is no available entries
return false;
}
else {
//The API is correctly identified
2017-06-13 09:01:36 +00:00
//Generate client informations
$clientInformations = array(
"ID" => $requestResult[0]['ID'],
"clientDomain" => ($requestResult[0]["client_domain"] == "" ? false : $requestResult[0]["client_domain"])
);
//Return API informations
return $clientInformations;
2017-05-17 12:43:12 +00:00
}
}
2018-05-07 16:50:50 +00:00
/**
* Create new API client
*
* @param APIClient $client The client to create
* @return bool TRUE for a success / FALSE else
*/
public function create(APIClient $client) : bool {
//Get database entry
$entry = self::APIClientsToDb($client);
//Insert the entry in the database
$tableName = CS::get()->config->get("dbprefix")."API_ServicesToken";
return CS::get()->db->addLine($tableName, $entry);
}
/**
* Turn a APIClient object into database entry
*
* @param APIClientsToDb $client
* @return array Generated database entry
*/
private static function APIClientsToDb(APIClient $client) : array {
$data = array();
$data["time_insert"] = $client->get_time_insert();
$data["serviceName"] = $client->get_name();
$data["token"] = $client->get_token();
if($client->has_client_domain())
$data["client_domain"] = $client->get_client_domain();
return $data;
}
2017-05-17 12:43:12 +00:00
}