Mercurial > hg > fapweb
view keygen.php @ 0:8019b357cc03
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 04 Dec 2012 19:07:18 +0200 |
parents | |
children | 4c5f651aa107 |
line wrap: on
line source
#!/usr/bin/php <? require "mconfig.inc.php"; require "msite.inc.php"; $keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789"; $maxItems = 4; $maxRows = 25; function garg($index) { global $argc, $argv; if ($index < $argc) return strtolower($argv[$index]); else return FALSE; } function gSQLError($sql) { global $db; echo "Error executing SQL query: ".implode("; ", $db->errorInfo())." in statement \"".$sql."\"\n"; exit; } 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(garg(1), 0, 2)) { case "ge": // Check arguments for sanity if (($num = garg(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("votekeylen", 8); $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) gSQLError($sql); $i++; } } else { gSQLError($sql); } } echo "\nGenerated ".$i." new keys.\n"; break; case "pr": // Print keys $all = garg(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 { gSQLError($sql); } break; default: echo "Unknown operating mode '".garg(1)."'.\n"; break; } ?>