From 94e5a8a8d04773f6c1e107c5a1d3e1a02324a47a Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Mon, 11 Mar 2013 16:55:19 +0100 Subject: [PATCH] Added ykval-gen-clients (fixes #7) --- NEWS | 2 + ykval-gen-clients | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100755 ykval-gen-clients diff --git a/NEWS b/NEWS index 2b3e17e..f0d8b8c 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,8 @@ * Added ykval-synchronize to easily call ykval-resync.php on a remote server. + * Added ykval-gen-clients to generate API clients. + * Version 2.21 (released 2013-02-05) * Fixed a problem that caused ykval-queue to terminate if the database diff --git a/ykval-gen-clients b/ykval-gen-clients new file mode 100755 index 0000000..447890c --- /dev/null +++ b/ykval-gen-clients @@ -0,0 +1,123 @@ +#!/usr/bin/php + +connect()) { + $myLog->log(LOG_WARNING, "Could not connect to database"); + error_log("Could not connect to database"); + exit(1); +} + +$count=1; +if($last_arg = intval(array_pop($argv))) { + $count = $last_arg; +} + +$result = $db->customQuery("SELECT id FROM clients ORDER BY id DESC LIMIT 1"); +$row = $db->fetchArray($result); +$db->closeCursor($result); +if($row) { + $next_id = $row['id']+1; +} else { + $next_id = 1; +} + +$random = array_key_exists('urandom', $options) ? "/dev/urandom" : "/dev/random"; +$fh = fopen($random, "r"); +if(!$fh) { + die("cannot open ".$random); +} + +for ($i=0; $i<$count; $i++) { + $client_id = $next_id++; + if (!($rnd = fread ($fh, 20))) { + die("cannot read from ".$random); + } + $secret = base64_encode($rnd); + + $params = array( + "id" => $client_id, + "active" => 1, + "created" => time(), + "secret" => $secret, + "email" => array_key_exists('email', $options) ? $options['email'] : '', + "notes" => array_key_exists('notes', $options) ? $options['notes'] : '', + "otp" => array_key_exists('otp', $options) ? $options['otp'] : '' + ); + + $query="INSERT INTO clients " . + "(id,active,created,secret,email,notes,otp) VALUES " . + "('" . $params["id"] . "', " . + "'" . $params["active"] . "', " . + "'" . $params['created'] . "'," . + "'" . $params['secret'] . "'," . + "'" . $params['email'] . "'," . + "'" . $params['notes'] . "'," . + "'" . $params['otp'] . "')"; + + if(!$db->customQuery($query)){ + $myLog->log(LOG_ERR, "Failed to insert new client with query " . $query); + error_log("Failed to insert new client with query " . $query); + exit(1); + } + + echo $client_id.", ".$secret."\n"; +} + +fclose($fh); + +$myLog->log(LOG_NOTICE, "Successfully inserted generated clients into database");