2014-07-21 22:56:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Model;
|
|
|
|
|
|
|
|
use SimpleValidator\Validator;
|
|
|
|
use SimpleValidator\Validators;
|
|
|
|
use Core\Translator;
|
|
|
|
use Core\Security;
|
2014-11-23 20:13:38 +01:00
|
|
|
use Core\Session;
|
2014-07-21 22:56:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Config model
|
|
|
|
*
|
|
|
|
* @package model
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
|
|
|
class Config extends Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* SQL table name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-11-23 20:13:38 +01:00
|
|
|
const TABLE = 'settings';
|
2014-07-21 22:56:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get available timezones
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTimezones()
|
|
|
|
{
|
|
|
|
$timezones = timezone_identifiers_list();
|
|
|
|
return array_combine(array_values($timezones), $timezones);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get available languages
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getLanguages()
|
|
|
|
{
|
2014-10-22 19:59:09 +02:00
|
|
|
// Sorted by value
|
|
|
|
return array(
|
2014-11-23 20:13:38 +01:00
|
|
|
'da_DK' => 'Dansk',
|
2014-10-22 19:59:09 +02:00
|
|
|
'de_DE' => 'Deutsch',
|
|
|
|
'en_US' => 'English',
|
|
|
|
'es_ES' => 'Español',
|
|
|
|
'fr_FR' => 'Français',
|
|
|
|
'it_IT' => 'Italiano',
|
|
|
|
'pl_PL' => 'Polski',
|
|
|
|
'pt_BR' => 'Português (Brasil)',
|
|
|
|
'ru_RU' => 'Русский',
|
|
|
|
'fi_FI' => 'Suomi',
|
|
|
|
'sv_SE' => 'Svenska',
|
|
|
|
'zh_CN' => '中文(简体)',
|
2014-11-23 20:13:38 +01:00
|
|
|
'ja_JP' => '日本語',
|
|
|
|
'th_TH' => 'ไทย',
|
2014-07-21 22:56:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a config variable from the session or the database
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $name Parameter name
|
|
|
|
* @param string $default_value Default value of the parameter
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get($name, $default_value = '')
|
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
if (! Session::isOpen()) {
|
|
|
|
$value = $this->db->table(self::TABLE)->eq('option', $name)->findOneColumn('value');
|
|
|
|
return $value ?: $default_value;
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:56:35 +02:00
|
|
|
if (! isset($_SESSION['config'][$name])) {
|
|
|
|
$_SESSION['config'] = $this->getAll();
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:59:09 +02:00
|
|
|
if (! empty($_SESSION['config'][$name])) {
|
2014-07-21 22:56:35 +02:00
|
|
|
return $_SESSION['config'][$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all settings
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAll()
|
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
return $this->db->table(self::TABLE)->listing('option', 'value');
|
2014-07-21 22:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save settings in the database
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param $values array Settings values
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function save(array $values)
|
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
foreach ($values as $option => $value) {
|
|
|
|
|
|
|
|
$result = $this->db->table(self::TABLE)->eq('option', $option)->update(array('value' => $value));
|
|
|
|
|
|
|
|
if (! $result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-21 22:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload settings in the session and the translations
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function reload()
|
|
|
|
{
|
|
|
|
$_SESSION['config'] = $this->getAll();
|
2014-11-23 20:13:38 +01:00
|
|
|
$this->setupTranslations();
|
2014-07-21 22:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-23 20:13:38 +01:00
|
|
|
* Load translations
|
2014-07-21 22:56:35 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-11-23 20:13:38 +01:00
|
|
|
public function setupTranslations()
|
2014-07-21 22:56:35 +02:00
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
$language = $this->get('application_language', 'en_US');
|
2014-07-21 22:56:35 +02:00
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
if ($language !== 'en_US') {
|
|
|
|
Translator::load($language);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set timezone
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function setupTimezone()
|
|
|
|
{
|
|
|
|
date_default_timezone_set($this->get('application_timezone', 'UTC'));
|
2014-07-21 22:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimize the Sqlite database
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function optimizeDatabase()
|
|
|
|
{
|
|
|
|
return $this->db->getconnection()->exec("VACUUM");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress the Sqlite database
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function downloadDatabase()
|
|
|
|
{
|
|
|
|
return gzencode(file_get_contents(DB_FILENAME));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Sqlite database size in bytes
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getDatabaseSize()
|
|
|
|
{
|
|
|
|
return DB_DRIVER === 'sqlite' ? filesize(DB_FILENAME) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-23 20:13:38 +01:00
|
|
|
* Regenerate a token
|
2014-07-21 22:56:35 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2014-11-23 20:13:38 +01:00
|
|
|
* @param string $option Parameter name
|
2014-07-21 22:56:35 +02:00
|
|
|
*/
|
2014-11-23 20:13:38 +01:00
|
|
|
public function regenerateToken($option)
|
2014-07-21 22:56:35 +02:00
|
|
|
{
|
2014-11-23 20:13:38 +01:00
|
|
|
return $this->db->table(self::TABLE)
|
|
|
|
->eq('option', $option)
|
|
|
|
->update(array('value' => Security::generateToken()));
|
2014-07-21 22:56:35 +02:00
|
|
|
}
|
|
|
|
}
|