diff options
author | Frédéric Guillot <fguillot@users.noreply.github.com> | 2014-03-30 21:21:16 -0400 |
---|---|---|
committer | Frédéric Guillot <fguillot@users.noreply.github.com> | 2014-03-30 21:21:16 -0400 |
commit | 99953ab62a215e92c29d444870cd96d36430d3ba (patch) | |
tree | c2784aeb6f2a82e1bca26bf05a8d9b434ff57be3 /models | |
parent | d9dfd9d6199b15eba9510ef460e18c9245d9ab12 (diff) |
Add the possiblity to regenerate all tokens
Diffstat (limited to 'models')
-rw-r--r-- | models/config.php | 95 |
1 files changed, 93 insertions, 2 deletions
diff --git a/models/config.php b/models/config.php index d2cbe785..2c8d4021 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())); + } } } |