Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Import tool ported to PHP #25

Open
AnanasPfirsichSaft opened this issue Apr 3, 2019 · 0 comments
Open

Import tool ported to PHP #25

AnanasPfirsichSaft opened this issue Apr 3, 2019 · 0 comments

Comments

@AnanasPfirsichSaft
Copy link

Well, I have a small user base and use sqlite instead of a full-grown RDBMS. Because I only have a minimal perl installation I had to make a very basic script in PHP to import the keys (from stdin) exported by "ykksm-gen-keys".

#!/usr/bin/php
<?php
require_once 'ykksm-config.php';
require_once 'ykksm-utils.php';
$dbh = new PDO($db_dsn, $db_username, $db_password, $db_options);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$dbh->beginTransaction();
while($f = fgets(STDIN)){
$f = trim($f);
  if ( substr($f,0,1) === '#' )
  continue;
  if ( substr_count($f,',') >= 5 ){
  $e = explode(',',$f);
    if ( is_numeric($e[0]) ){
    $sql = $dbh->prepare('INSERT INTO yubikeys (serialnr,publicname,created,internalname,aeskey,lockcode,creator) VALUES (?,?,?,?,?,?,?)');
    $sql->bindValue(1,$e[0],PDO::PARAM_INT);
    $sql->bindValue(2,$e[1],PDO::PARAM_STR);
    $sql->bindValue(3,$e[5],PDO::PARAM_STR);
    $sql->bindValue(4,$e[2],PDO::PARAM_STR);
    $sql->bindValue(5,$e[3],PDO::PARAM_STR);
    $sql->bindValue(6,$e[4],PDO::PARAM_STR);
    $sql->bindValue(7,$e[6],PDO::PARAM_STR);
    $sql->execute();
      if ( $sql->rowCount() === 1 )
      echo "Imported key $e[1]\n";
      else
      $sql->rollBack();
    }
  }
}
$dbh->commit();
?>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant