mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-23 13:59:29 +00:00
Can create clients from shell
This commit is contained in:
parent
97ef188167
commit
0b5019d487
46
bin/add_client
Executable file
46
bin/add_client
Executable file
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env php
|
||||
|
||||
###########################
|
||||
# ComunicAPI clients add #
|
||||
# #
|
||||
# @author Pierre HUBERT #
|
||||
###########################
|
||||
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Display a message
|
||||
*/
|
||||
function msg(string $message){
|
||||
echo $message."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Display help
|
||||
*/
|
||||
function help(){
|
||||
msg("Usage: ./add_client [name] [token]");
|
||||
}
|
||||
|
||||
//Check for arguments
|
||||
if(count($_SERVER['argv']) < 3){
|
||||
exit(help());
|
||||
}
|
||||
|
||||
//Initialize page
|
||||
require __DIR__."/../init.php";
|
||||
|
||||
//Extract the arguments
|
||||
$client = new APIClient();
|
||||
$client->set_time_insert(time());
|
||||
$client->set_name($_SERVER['argv'][1]);
|
||||
$client->set_token($_SERVER['argv'][2]);
|
||||
|
||||
//Try to create the client
|
||||
if(!cs()->clients->create($client)){
|
||||
msg("Err! Could not create client tokens!");
|
||||
exit(10);
|
||||
}
|
||||
|
||||
//Success
|
||||
msg("The client has been created!");
|
@ -68,4 +68,40 @@ class APIClients {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -11,9 +11,19 @@ require_once __DIR__."/BaseUniqueObject.php";
|
||||
class APIClient extends BaseUniqueObject {
|
||||
|
||||
//Private fields
|
||||
private $time_insert;
|
||||
private $name;
|
||||
private $token;
|
||||
private $url;
|
||||
private $client_domain;
|
||||
|
||||
//Set and get creation time
|
||||
public function set_time_insert(int $time_insert){
|
||||
$this->time_insert = $time_insert;
|
||||
}
|
||||
|
||||
public function get_time_insert() : int {
|
||||
return $this->time_insert;
|
||||
}
|
||||
|
||||
//Get and set client name
|
||||
public function set_name(string $name){
|
||||
@ -43,17 +53,17 @@ class APIClient extends BaseUniqueObject {
|
||||
}
|
||||
|
||||
|
||||
//Get and set client URL
|
||||
public function set_url(string $url){
|
||||
$this->url = $url == "" ? null : $url;
|
||||
//Get and set client domain
|
||||
public function set_client_domain(string $client_domain){
|
||||
$this->client_domain = $client_domain == "" ? null : $client_domain;
|
||||
}
|
||||
|
||||
public function has_url() : bool {
|
||||
return $this->url != null;
|
||||
public function has_client_domain() : bool {
|
||||
return $this->client_domain != null;
|
||||
}
|
||||
|
||||
public function get_url() : string {
|
||||
return $this->url != null ? $this->url : "null";
|
||||
public function get_client_domain() : string {
|
||||
return $this->client_domain != null ? $this->client_domain : "null";
|
||||
}
|
||||
|
||||
}
|
@ -28,7 +28,7 @@ if(!isset($_GET["format"]))
|
||||
header("Technology: Official Comunic API Server");
|
||||
|
||||
//Check client tokens
|
||||
if(!$cs->tokens->checkClientRequestTokens())
|
||||
if(!$cs->clients->checkClientRequestTokens())
|
||||
Rest_fatal_error(401, "Please check your client tokens!");
|
||||
|
||||
//Check for remote requests limit
|
||||
|
6
init.php
6
init.php
@ -47,9 +47,9 @@ define("DBprefix", $cs->config->get("dbprefix"));
|
||||
unset($db);
|
||||
|
||||
//Add token object
|
||||
$tokens = new APIClients();
|
||||
$cs->register("tokens", $tokens);
|
||||
unset($tokens);
|
||||
$clients = new APIClients();
|
||||
$cs->register("clients", $clients);
|
||||
unset($clients);
|
||||
|
||||
//Include models
|
||||
foreach(glob(PROJECT_PATH."classes/models/*.php") as $classFile){
|
||||
|
Loading…
Reference in New Issue
Block a user