#!/usr/bin/php
<?php
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
function println($str) {
echo $str, "\n";
}
function main() {
global $argv;
if (count($argv) < 2 || in_array($argv[1], ['-h', '--help'])) {
println("Usage: php {$argv[0]} file [file...]");
return;
}
$db = new PDO('pgsql:', 'ubuntu', '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
foreach (array_slice($argv, 1) as $filename) {
$file = fopen($filename, 'r');
$db->beginTransaction();
while (($line = fgets($file)) !== false) {
[$sha1, $count] = explode(':', trim($line));
$sha1 = strtolower($sha1);
$stmt = $db->prepare('SELECT 1 FROM passwords WHERE sha1 = ?');
$stmt->execute([$sha1]);
if ($stmt->rowCount()) {
$db->prepare('UPDATE passwords SET frequency = ? '.
'WHERE sha1 = ?')
->execute([$count, $sha1]);
}
}
$db->commit();
fclose($file);
}
}
main();
To embed this program on your website, copy the following code and paste it into your website's HTML: