
* New .gitignore Added violinist.io configuration Start with CI Started moving CSS/JS to NPM * Fix & improvements Security improves (SHA256 instead of MD5) and new PHP7 random function Security fix admin check from database instead of session Security fix user active for every page that require login UX fix admin cannot demote himself Added Gruntfile.js Updated composer.json dependency Addeded PHP >=7.1 to composer.json Moved static file to src * Results of .gitignore "static/" * Fix migration for admin user_code * Travis test for grunt (JS) * Changed user_code generation method Updated Travis test
89 lines
2.3 KiB
PHP
89 lines
2.3 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
use App\Database\DB;
|
|
|
|
require 'vendor/autoload.php';
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
die();
|
|
}
|
|
|
|
$config = include 'config.php';
|
|
|
|
if (!$config) {
|
|
die('config.php not found. Please create a new one.');
|
|
}
|
|
|
|
DB::setDsn($config['db']['connection'] . ':' . $config['db']['dsn'], $config['db']['username'], $config['db']['password']);
|
|
|
|
$firstMigrate = false;
|
|
if (!file_exists($config['db']['dsn']) && DB::driver() === 'sqlite') {
|
|
touch($config['db']['dsn']);
|
|
$firstMigrate = true;
|
|
}
|
|
|
|
try {
|
|
DB::query('SELECT 1 FROM `migrations` LIMIT 1');
|
|
} catch (PDOException $exception) {
|
|
$firstMigrate = true;
|
|
}
|
|
|
|
echo 'Connected.' . PHP_EOL;
|
|
|
|
if ($firstMigrate) {
|
|
echo 'Creating migrations table...' . PHP_EOL;
|
|
DB::raw()->exec(file_get_contents('resources/schemas/migrations.sql'));
|
|
}
|
|
|
|
$files = glob('resources/schemas/' . DB::driver() . '/*.sql');
|
|
|
|
$names = array_map(function ($path) {
|
|
return basename($path);
|
|
}, $files);
|
|
|
|
$in = str_repeat('?, ', count($names) - 1) . '?';
|
|
|
|
$inMigrationsTable = DB::query("SELECT * FROM `migrations` WHERE `name` IN ($in)", $names)->fetchAll();
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$continue = false;
|
|
$exists = false;
|
|
|
|
foreach ($inMigrationsTable as $migration) {
|
|
if (basename($file) === $migration->name && $migration->migrated) {
|
|
$continue = true;
|
|
break;
|
|
} elseif (basename($file) === $migration->name && !$migration->migrated) {
|
|
$exists = true;
|
|
break;
|
|
}
|
|
}
|
|
if ($continue) continue;
|
|
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
DB::raw()->exec($sql);
|
|
if (!$exists) {
|
|
DB::query('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 1]);
|
|
} else {
|
|
DB::query('UPDATE `migrations` SET `migrated`=? WHERE `name`=?', [1, basename($file)]);
|
|
}
|
|
echo "Migrated '$file'" . PHP_EOL;
|
|
} catch (PDOException $exception) {
|
|
if (!$exists) {
|
|
DB::query('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 0]);
|
|
}
|
|
echo "Error migrating '$file' (" . $exception->getMessage() . ')' . PHP_EOL;
|
|
echo $exception->getTraceAsString() . PHP_EOL;
|
|
|
|
}
|
|
}
|
|
|
|
if (isset($argv[1]) && $argv[1] === '--install') {
|
|
DB::query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES ('admin@example.com', 'admin', ?, 1, ?)", [password_hash('admin', PASSWORD_DEFAULT),substr(bin2hex(random_bytes(3)), 5)]);
|
|
}
|
|
|
|
echo 'Done.' . PHP_EOL;
|