1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00

Fix some missing files in sources

This commit is contained in:
mbugeia 2014-07-21 22:56:35 +02:00
parent e296d6f997
commit 96d17fc2f6
2 changed files with 309 additions and 0 deletions

View file

@ -0,0 +1,121 @@
<?php
namespace Controller;
/**
* Config controller
*
* @package controller
* @author Frederic Guillot
*/
class Config extends Base
{
/**
* Display the settings page
*
* @access public
*/
public function index()
{
$this->response->html($this->template->layout('config_index', array(
'db_size' => $this->config->getDatabaseSize(),
'user' => $_SESSION['user'],
'projects' => $this->project->getList(),
'languages' => $this->config->getLanguages(),
'values' => $this->config->getAll(),
'errors' => array(),
'menu' => 'config',
'title' => t('Settings'),
'timezones' => $this->config->getTimezones(),
'remember_me_sessions' => $this->rememberMe->getAll($this->acl->getUserId()),
'last_logins' => $this->lastLogin->getAll($this->acl->getUserId()),
)));
}
/**
* Validate and save settings
*
* @access public
*/
public function save()
{
$values = $this->request->getValues();
list($valid, $errors) = $this->config->validateModification($values);
if ($valid) {
if ($this->config->save($values)) {
$this->config->reload();
$this->session->flash(t('Settings saved successfully.'));
} else {
$this->session->flashError(t('Unable to save your settings.'));
}
$this->response->redirect('?controller=config');
}
$this->response->html($this->template->layout('config_index', array(
'db_size' => $this->config->getDatabaseSize(),
'user' => $_SESSION['user'],
'projects' => $this->project->getList(),
'languages' => $this->config->getLanguages(),
'values' => $values,
'errors' => $errors,
'menu' => 'config',
'title' => t('Settings'),
'timezones' => $this->config->getTimezones(),
'remember_me_sessions' => $this->rememberMe->getAll($this->acl->getUserId()),
'last_logins' => $this->lastLogin->getAll($this->acl->getUserId()),
)));
}
/**
* Download the Sqlite database
*
* @access public
*/
public function downloadDb()
{
$this->checkCSRFParam();
$this->response->forceDownload('db.sqlite.gz');
$this->response->binary($this->config->downloadDatabase());
}
/**
* Optimize the Sqlite database
*
* @access public
*/
public function optimizeDb()
{
$this->checkCSRFParam();
$this->config->optimizeDatabase();
$this->session->flash(t('Database optimization done.'));
$this->response->redirect('?controller=config');
}
/**
* Regenerate all application tokens
*
* @access public
*/
public function tokens()
{
$this->checkCSRFParam();
$this->config->regenerateTokens();
$this->session->flash(t('All tokens have been regenerated.'));
$this->response->redirect('?controller=config');
}
/**
* Remove a "RememberMe" token
*
* @access public
*/
public function removeRememberMeToken()
{
$this->checkCSRFParam();
$this->rememberMe->remove($this->request->getIntegerParam('id'));
$this->response->redirect('?controller=config&action=index#remember-me');
}
}

View file

@ -0,0 +1,188 @@
<?php
namespace Model;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use Core\Translator;
use Core\Security;
/**
* Config model
*
* @package model
* @author Frederic Guillot
*/
class Config extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'config';
/**
* 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()
{
$languages = array(
'de_DE' => t('German'),
'en_US' => t('English'),
'es_ES' => t('Spanish'),
'fr_FR' => t('French'),
'pl_PL' => t('Polish'),
'pt_BR' => t('Portuguese (Brazilian)'),
'sv_SE' => t('Swedish'),
'zh_CN' => t('Chinese (Simplified)'),
);
asort($languages);
return $languages;
}
/**
* 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 = '')
{
if (! isset($_SESSION['config'][$name])) {
$_SESSION['config'] = $this->getAll();
}
if (isset($_SESSION['config'][$name])) {
return $_SESSION['config'][$name];
}
return $default_value;
}
/**
* Get all settings
*
* @access public
* @return array
*/
public function getAll()
{
return $this->db->table(self::TABLE)->findOne();
}
/**
* Save settings in the database
*
* @access public
* @param $values array Settings values
* @return boolean
*/
public function save(array $values)
{
$_SESSION['config'] = $values;
return $this->db->table(self::TABLE)->update($values);
}
/**
* Reload settings in the session and the translations
*
* @access public
*/
public function reload()
{
$_SESSION['config'] = $this->getAll();
Translator::load($this->get('language', 'en_US'));
}
/**
* Validate settings modification
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validateModification(array $values)
{
$v = new Validator($values, array(
new Validators\Required('language', t('The language is required')),
new Validators\Required('timezone', t('The timezone is required')),
));
return array(
$v->execute(),
$v->getErrors()
);
}
/**
* 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;
}
/**
* Regenerate all tokens (projects and webhooks)
*
* @access public
*/
public function regenerateTokens()
{
$this->db->table(self::TABLE)->update(array(
'webhooks_token' => Security::generateToken(),
'api_token' => Security::generateToken(),
));
$projects = $this->db->table(Project::TABLE)->findAllByColumn('id');
foreach ($projects as $project_id) {
$this->db->table(Project::TABLE)->eq('id', $project_id)->update(array('token' => Security::generateToken()));
}
}
}