2014-04-06 02:24:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Model\Database;
|
|
|
|
|
2015-01-18 00:53:40 +01:00
|
|
|
use Schema;
|
|
|
|
use DirectoryIterator;
|
|
|
|
use Model\Config;
|
2014-04-06 02:24:13 +02:00
|
|
|
use SimpleValidator\Validator;
|
|
|
|
use SimpleValidator\Validators;
|
|
|
|
|
|
|
|
// Create a new database for a new user
|
|
|
|
function create($filename, $username, $password)
|
|
|
|
{
|
|
|
|
$filename = DATA_DIRECTORY.DIRECTORY_SEPARATOR.$filename;
|
|
|
|
|
2014-04-16 00:31:59 +02:00
|
|
|
if (ENABLE_MULTIPLE_DB && ! file_exists($filename)) {
|
2014-04-06 02:24:13 +02:00
|
|
|
|
|
|
|
$db = new \PicoDb\Database(array(
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
'filename' => $filename,
|
|
|
|
));
|
|
|
|
|
2015-01-18 00:53:40 +01:00
|
|
|
if ($db->schema()->check(Schema\VERSION)) {
|
2015-01-22 23:12:06 +01:00
|
|
|
$credentials = array(
|
2014-04-06 02:24:13 +02:00
|
|
|
'username' => $username,
|
2015-01-18 00:53:40 +01:00
|
|
|
'password' => password_hash($password, PASSWORD_BCRYPT)
|
2015-01-22 23:12:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$db->table('config')->update($credentials);
|
2014-04-06 02:24:13 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get or set the current database
|
|
|
|
function select($filename = '')
|
|
|
|
{
|
|
|
|
static $current_filename = DB_FILENAME;
|
|
|
|
|
2015-01-17 19:35:59 +01:00
|
|
|
// function gets called with a filename at least once the database
|
|
|
|
// connection is established
|
2015-01-18 01:53:54 +01:00
|
|
|
if (! empty($filename)) {
|
2015-01-17 19:35:59 +01:00
|
|
|
if (ENABLE_MULTIPLE_DB && in_array($filename, get_all())) {
|
|
|
|
$current_filename = $filename;
|
|
|
|
|
|
|
|
// unset the authenticated flag if the database is changed
|
|
|
|
if (empty($_SESSION['database']) || $_SESSION['database'] !== $filename) {
|
|
|
|
if (isset($_SESSION)) {
|
2015-01-22 23:12:06 +01:00
|
|
|
unset($_SESSION['loggedin']);
|
2015-01-17 19:35:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$_SESSION['database'] = $filename;
|
2015-01-18 00:53:40 +01:00
|
|
|
$_SESSION['config'] = Config\get_all();
|
2015-01-17 19:35:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-06 02:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $current_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get database path
|
|
|
|
function get_path()
|
|
|
|
{
|
2015-01-18 00:53:40 +01:00
|
|
|
return DATA_DIRECTORY.DIRECTORY_SEPARATOR.select();
|
2014-04-06 02:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of available databases
|
|
|
|
function get_all()
|
|
|
|
{
|
|
|
|
$listing = array();
|
|
|
|
|
2015-01-18 00:53:40 +01:00
|
|
|
$dir = new DirectoryIterator(DATA_DIRECTORY);
|
2014-04-06 02:24:13 +02:00
|
|
|
|
|
|
|
foreach ($dir as $fileinfo) {
|
|
|
|
if ($fileinfo->getExtension() === 'sqlite') {
|
|
|
|
$listing[] = $fileinfo->getFilename();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listing;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the formated db list
|
|
|
|
function get_list()
|
|
|
|
{
|
|
|
|
$listing = array();
|
|
|
|
|
|
|
|
foreach (get_all() as $filename) {
|
|
|
|
|
|
|
|
if ($filename === DB_FILENAME) {
|
|
|
|
$label = t('Default database');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$label = ucfirst(substr($filename, 0, -7));
|
|
|
|
}
|
|
|
|
|
|
|
|
$listing[$filename] = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listing;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate database form
|
|
|
|
function validate(array $values)
|
|
|
|
{
|
|
|
|
$v = new Validator($values, array(
|
|
|
|
new Validators\Required('name', t('The database name is required')),
|
|
|
|
new Validators\AlphaNumeric('name', t('The name must have only alpha-numeric characters')),
|
|
|
|
new Validators\Required('username', t('The user name is required')),
|
|
|
|
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
|
|
|
|
new Validators\Required('password', t('The password is required')),
|
|
|
|
new Validators\MinLength('password', t('The minimum length is 6 characters'), 6),
|
|
|
|
new Validators\Required('confirmation', t('The confirmation is required')),
|
2014-05-28 22:10:41 +02:00
|
|
|
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
|
2014-04-06 02:24:13 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$v->execute(),
|
|
|
|
$v->getErrors()
|
|
|
|
);
|
|
|
|
}
|