#!/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) {
$password = trim($line);
if (!$password || !mb_check_encoding($password, 'UTF-8')) {
continue;
}
$db->prepare('INSERT INTO passwords (password, sha1) '.
'VALUES (?, ?) ON CONFLICT DO NOTHING')
->execute([$password, sha1($password)]);
}
$db->commit();
fclose($file);
}
}
main();
To embed this program on your website, copy the following code and paste it into your website's HTML: