diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/base.php | 15 | ||||
-rw-r--r-- | models/config.php | 10 | ||||
-rw-r--r-- | models/project.php | 7 | ||||
-rw-r--r-- | models/schema.php | 22 |
4 files changed, 39 insertions, 15 deletions
diff --git a/models/base.php b/models/base.php index 981c7839..3c071623 100644 --- a/models/base.php +++ b/models/base.php @@ -17,7 +17,7 @@ require __DIR__.'/schema.php'; abstract class Base { const APP_VERSION = 'master'; - const DB_VERSION = 2; + const DB_VERSION = 3; const DB_FILENAME = 'data/db.sqlite'; private static $dbInstance = null; @@ -46,4 +46,17 @@ abstract class Base die('Unable to migrate database schema!'); } } + + // Generate a random token from /dev/urandom or with uniqid() + public static function generateToken() + { + if (ini_get('open_basedir') === '') { + $token = file_get_contents('/dev/urandom', false, null, 0, 30); + } + else { + $token = uniqid(mt_rand(), true); + } + + return hash('crc32b', $token); + } } diff --git a/models/config.php b/models/config.php index fe4f6c99..f4d34986 100644 --- a/models/config.php +++ b/models/config.php @@ -66,16 +66,6 @@ class Config extends Base ); } - public static function generateToken() - { - if (ini_get('open_basedir') === '') { - return substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); - } - else { - return substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); - } - } - public function optimizeDatabase() { $this->db->getconnection()->exec("VACUUM"); diff --git a/models/project.php b/models/project.php index 10d7c572..cb96dccd 100644 --- a/models/project.php +++ b/models/project.php @@ -16,6 +16,11 @@ class Project extends Base return $this->db->table(self::TABLE)->eq('id', $project_id)->findOne(); } + public function getByToken($token) + { + return $this->db->table(self::TABLE)->eq('token', $token)->findOne(); + } + public function getFirst() { return $this->db->table(self::TABLE)->findOne(); @@ -92,12 +97,12 @@ class Project extends Base { $this->db->startTransaction(); + $values['token'] = self::generateToken(); $this->db->table(self::TABLE)->save($values); $project_id = $this->db->getConnection()->getLastId(); $boardModel = new \Model\Board; - $boardModel->create($project_id, array( t('Backlog'), t('Ready'), diff --git a/models/schema.php b/models/schema.php index 9ccb500f..84926d73 100644 --- a/models/schema.php +++ b/models/schema.php @@ -2,11 +2,27 @@ namespace Schema; +function version_3($pdo) +{ + $pdo->exec('ALTER TABLE projects ADD column token TEXT'); + + // For each existing project, assign a different token + $rq = $pdo->prepare("SELECT id FROM projects WHERE token IS NULL"); + $rq->execute(); + $results = $rq->fetchAll(\PDO::FETCH_ASSOC); + + if ($results !== false) { + + foreach ($results as &$result) { + $rq = $pdo->prepare('UPDATE projects SET token=? WHERE id=?'); + $rq->execute(array(\Model\Base::generateToken(), $result['id'])); + } + } +} + function version_2($pdo) { $pdo->exec('ALTER TABLE tasks ADD column date_completed INTEGER'); - - // For all existing completed tasks, set the date of creation as a date of completion $pdo->exec('UPDATE tasks SET date_completed=date_creation WHERE is_active=0'); } @@ -74,6 +90,6 @@ function version_1($pdo) $pdo->exec(" INSERT INTO config (language, webhooks_token) - VALUES ('en_US', '".\Model\Config::generateToken()."') + VALUES ('en_US', '".\Model\Base::generateToken()."') "); } |