miniflux-legacy/app/models/database.php

102 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Model\Database;
2015-01-18 00:53:40 +01:00
use Schema;
use DirectoryIterator;
use Model\Config;
// Create a new database for a new user
function create($filename, $username, $password)
{
$filename = DATA_DIRECTORY.DIRECTORY_SEPARATOR.$filename;
if (ENABLE_MULTIPLE_DB && ! file_exists($filename)) {
$db = new \PicoDb\Database(array(
'driver' => 'sqlite',
'filename' => $filename,
));
2015-01-18 00:53:40 +01:00
if ($db->schema()->check(Schema\VERSION)) {
$credentials = array(
'username' => $username,
2015-01-18 00:53:40 +01:00
'password' => password_hash($password, PASSWORD_BCRYPT)
);
2015-01-28 05:26:36 +01:00
$db->hashtable('settings')->put($credentials);
return true;
}
}
return false;
}
// Get or set the current database
function select($filename = '')
{
static $current_filename = DB_FILENAME;
// function gets called with a filename at least once the database
// connection is established
if (! empty($filename)) {
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)) {
unset($_SESSION['loggedin']);
}
$_SESSION['database'] = $filename;
2015-01-18 00:53:40 +01:00
$_SESSION['config'] = Config\get_all();
}
2016-04-18 01:44:45 +02:00
} else {
return false;
}
}
return $current_filename;
}
// Get database path
function get_path()
{
2015-01-18 00:53:40 +01:00
return DATA_DIRECTORY.DIRECTORY_SEPARATOR.select();
}
// Get the list of available databases
function get_all()
{
$listing = array();
2015-01-18 00:53:40 +01:00
$dir = new DirectoryIterator(DATA_DIRECTORY);
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');
2016-04-18 01:44:45 +02:00
} else {
$label = ucfirst(substr($filename, 0, -7));
}
$listing[$filename] = $label;
}
return $listing;
}