Mercurial > hg > fapweb
view keygen.php @ 14:e36c4d2b09c4
Fix settings to work, clean up the code, etc.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 06 Dec 2012 16:13:08 +0200 |
parents | 4c5f651aa107 |
children | 2b490ac5b128 |
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 = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789"; $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("voteKeyLength"); $n++) $key .= $keyChars[rand() % strlen($keyChars)]; // Check if it already exists, to avoid duplicates $sql = stPrepareSQL("SELECT * FROM voters WHERE key=%s", $key); if (($res = @$db->query($sql)) !== FALSE) { if ($res->fetchColumn() === FALSE) { // Nope, add into database $sql = stPrepareSQL( "INSERT INTO voters (key,name,enabled) 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 voters ".($all ? "" : "WHERE enabled=0 ")."ORDER BY id ASC"; if (($res = @$db->query($sql)) !== FALSE) { $rows = 0; $nitem = 0; $total = 0; foreach ($res as $item) { $total++; printf("%s%04d - %8s", $item["enabled"] ? "*" : " ", $item["id"], $item["key"]); if (++$nitem >= $maxItems) { $nitem = 0; echo "\n"; if (++$rows % $maxRows == 0) echo "\f\n"; } else echo " | "; } if ($nitem > 0) echo "\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; } ?>