view keygen.php @ 63:922b5192b2ff

Cosmetics.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 05 Oct 2013 12:35:33 +0300
parents 2a2ec7112df1
children
line wrap: on
line source

#!/usr/bin/php
<?
require "mconfig.inc.php";
require "msite.inc.php";

// We don't want to be run from anywhere else than commandline
stCheckCLIOrDie();


// Settings
$keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
$maxItems = 4;
$maxRows  = 25;


//
// Check for commandline arguments
//
if ($argc < 2)
{
  echo "Usage: ".$argv[0]." <mode> [args]\n".
  "Where mode is one of following:\n".
  "\n".
  "  generate <#>    Generate # MORE vote keys. If previously\n".
  "                  generated keys exist, # keys will be added.\n".
  "\n".
  "  print [all]     Print list of list of UNactive keys\n".
  "                  (unless 'all' option is specified)\n".
  "\n";
  exit;
}

if (!stConnectSQLDB())
  die("Could not connect to SQL database.\n");

switch (substr(stCArgLC(1), 0, 2))
{
  case "ge":
    // Check arguments for sanity
    if (($num = stCArgLC(2)) === FALSE)
    {
      echo "No number of keys specified.\n";
      exit;
    }

    if ($num < 1 || $num > 1000)
    {
      echo "Invalid number of keys.\n";
      exit;
    }

    echo "Generating keys ..";
    for ($i = 0; $i < $num; )
    {
      echo ".";
      
      // Generate one keycode
      $key = "";
      for ($n = 0; $n < stGetSetting("userKeyLength"); $n++)
        $key .= $keyChars[rand() % strlen($keyChars)];

      // Check if it already exists, to avoid duplicates
      $sql = stPrepareSQL("SELECT * FROM users WHERE key=%s", $key);
      if (($res = @$db->query($sql)) !== FALSE)
      {
        if ($res->fetchColumn() === FALSE)
        {
          // Nope, add into database
          $sql = stPrepareSQL(
            "INSERT INTO users (key,active) VALUES (%s,0)",
            $key);

          if (($res = $db->query($sql)) === FALSE)
            stCSQLError($sql);

          $i++;
        }
      }
      else
      {
        stCSQLError($sql);
      }
    }
    echo "\nGenerated ".$i." new keys.\n";
    break;
  
  case "pr":
    // Print keys
    $all = stCArgLC(2) == "all";
    $sql = "SELECT * FROM users ".($all ? "" : "WHERE enabled=0 ")."ORDER BY id ASC";
    if (($res = @$db->query($sql)) !== FALSE)
    {
      $rows = 0;
      $nitem = 0;
      $total = 0;
      $str = "";

      foreach ($res as $item)
      {
        $total++;
        $str .= sprintf("%s%04d  -  %8s",
          $item["enabled"] ? "*" : " ",
          $item["id"], $item["key"]);

        if (++$nitem >= $maxItems)
        {
          echo $str."\n".str_repeat("-", strlen($str))."\n";
          $str = "";
          $nitem = 0;
          if (++$rows % $maxRows == 0)
            echo "\f\n";
        }
        else
          $str .= " |";
      }
      
      if ($nitem > 0)
        echo $str."\n";

      echo "Total of ".$total." ".($all ? "keys (all printed)" : "unactive keys")."\n";
    }
    else
    {
      stCSQLError($sql);
    }
    break;
  
  default:
    echo "Unknown operating mode '".stCArg(1)."'.\n";
    break;
}

?>