diff --git a/bin/add_client b/bin/add_client new file mode 100755 index 0000000..498e63c --- /dev/null +++ b/bin/add_client @@ -0,0 +1,46 @@ +#!/usr/bin/env php + +########################### +# ComunicAPI clients add # +# # +# @author Pierre HUBERT # +########################### + +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!"); \ No newline at end of file diff --git a/classes/APIClients.php b/classes/APIClients.php index 6d6680e..5b0f6af 100644 --- a/classes/APIClients.php +++ b/classes/APIClients.php @@ -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; + + } + } \ No newline at end of file diff --git a/classes/models/APIClient.php b/classes/models/APIClient.php index b639418..9521539 100644 --- a/classes/models/APIClient.php +++ b/classes/models/APIClient.php @@ -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"; } } \ No newline at end of file diff --git a/index.php b/index.php index 2b31f4a..789371e 100644 --- a/index.php +++ b/index.php @@ -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 diff --git a/init.php b/init.php index 31e080f..e237b6d 100644 --- a/init.php +++ b/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){