ComunicAPI/classes/APIClients.php

113 lines
2.8 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
2018-08-27 15:36:58 +00:00
/**
* Tables name
*/
const SERVICES_TOKENS_TABLE = DBprefix."api_services_tokens";
const USERS_TOKENS_TABLE = DBprefix."api_users_tokens";
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
2018-08-27 15:36:58 +00:00
define("APIServiceID", $serviceInfos["id"]);
2017-06-13 09:01:36 +00:00
//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
2018-08-27 15:36:58 +00:00
$tableName = self::SERVICES_TOKENS_TABLE;
$conditions = "WHERE service_name = ? AND token = ?";
2017-05-17 12:43:12 +00:00
$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(
2018-08-27 15:36:58 +00:00
"id" => $requestResult[0]['id'],
2017-06-13 09:01:36 +00:00
"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
2018-08-27 15:36:58 +00:00
$tableName = self::SERVICES_TOKENS_TABLE;
2018-05-07 16:50:50 +00:00
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();
2018-08-27 15:36:58 +00:00
$data["service_name"] = $client->get_name();
2018-05-07 16:50:50 +00:00
$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
}