From 99953ab62a215e92c29d444870cd96d36430d3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 30 Mar 2014 21:21:16 -0400 Subject: [PATCH] Add the possiblity to regenerate all tokens --- controllers/config.php | 42 +++++++++++++-- locales/fr_FR/translations.php | 2 + locales/pl_PL/translations.php | 2 + models/config.php | 95 +++++++++++++++++++++++++++++++++- templates/config_index.php | 1 + 5 files changed, 136 insertions(+), 6 deletions(-) diff --git a/controllers/config.php b/controllers/config.php index c26013d8d..0adf1d54f 100644 --- a/controllers/config.php +++ b/controllers/config.php @@ -4,9 +4,19 @@ namespace Controller; require_once __DIR__.'/base.php'; +/** + * Config controller + * + * @package controller + * @author Frederic Guillot + */ class Config extends Base { - // Settings page + /** + * Display the settings page + * + * @access public + */ public function index() { $this->response->html($this->template->layout('config_index', array( @@ -22,7 +32,11 @@ class Config extends Base ))); } - // Validate and save settings + /** + * Validate and save settings + * + * @access public + */ public function save() { $values = $this->request->getValues(); @@ -53,18 +67,38 @@ class Config extends Base ))); } - // Download the database + /** + * Download the Sqlite database + * + * @access public + */ public function downloadDb() { $this->response->forceDownload('db.sqlite.gz'); $this->response->binary($this->config->downloadDatabase()); } - // Optimize the database + /** + * Optimize the Sqlite database + * + * @access public + */ public function optimizeDb() { $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->config->regenerateTokens(); + $this->session->flash(t('All tokens have been regenerated.')); + $this->response->redirect('?controller=config'); + } } diff --git a/locales/fr_FR/translations.php b/locales/fr_FR/translations.php index 362d29c2f..50fefd09c 100644 --- a/locales/fr_FR/translations.php +++ b/locales/fr_FR/translations.php @@ -267,4 +267,6 @@ return array( 'Current password for the user "%s"' => 'Mot de passe actuel pour l\'utilisateur « %s »', 'The current password is required' => 'Le mot de passe actuel est obligatoire', 'Wrong password' => 'Mauvais mot de passe', + 'Reset all tokens' => 'Réinitialiser tous les jetons de sécurité', + 'All tokens have been regenerated.' => 'Tous les jetons de sécurité ont été réinitialisés.', ); diff --git a/locales/pl_PL/translations.php b/locales/pl_PL/translations.php index 25c3e6376..565c6ef28 100644 --- a/locales/pl_PL/translations.php +++ b/locales/pl_PL/translations.php @@ -270,4 +270,6 @@ return array( // 'Current password for the user "%s"' => '', // 'The current password is required' => '', // 'Wrong password' => '', + // 'Reset all tokens' => '', + // 'All tokens have been regenerated.' => '', ); diff --git a/models/config.php b/models/config.php index d2cbe7853..2c8d40210 100644 --- a/models/config.php +++ b/models/config.php @@ -7,16 +7,39 @@ require_once __DIR__.'/base.php'; use \SimpleValidator\Validator; use \SimpleValidator\Validators; +/** + * 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( @@ -30,6 +53,14 @@ class Config extends Base return $languages; } + /** + * Get a config variable from the session or the database + * + * @access public + * @param string $name Parameter name + * @param mixed $default_value Default value of the parameter + * @return mixed + */ public function get($name, $default_value = '') { if (! isset($_SESSION['config'][$name])) { @@ -43,17 +74,35 @@ class Config extends Base 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(); @@ -62,10 +111,18 @@ class Config extends Base if ($language !== 'en_US') \Translator\load($language); } + /** + * 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( @@ -74,18 +131,52 @@ class Config extends Base ); } + /** + * Optimize the Sqlite database + * + * @access public + * @return boolean + */ public function optimizeDatabase() { - $this->db->getconnection()->exec("VACUUM"); + 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 filesize(DB_FILENAME); + 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' => $this->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' => $this->generateToken())); + } } } diff --git a/templates/config_index.php b/templates/config_index.php index fef8909d1..ba971b61f 100644 --- a/templates/config_index.php +++ b/templates/config_index.php @@ -23,6 +23,7 @@