Skip to content

Commit

Permalink
Merge pull request #3 from nicolabricot/v1.1.1
Browse files Browse the repository at this point in the history
Release v1.1.1
  • Loading branch information
Devenet committed Oct 28, 2014
2 parents 0d84363 + c7cfe76 commit be446fb
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
config.php
!library/core/config.php
data/api_tokens.php
data/api_tokens.db
data/mood_picker.db
data/cache/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Refer you to the [LICENSE file](https://github.com/nicolabricot/MoodPicker/blob/

## Want to contribute?

Source code is hosted on [Github](https://github.com/nicolabricot/MoodPicker) by [nicolabricot](http://nicolabricot.com). Feel free to fork it and to improve the application!
Source code is hosted on [Github](https://github.com/nicolabricot/MoodPicker) by [nicolabricot](http://nicolabricot.com).
Feel free to fork it and to improve the application!

Let me know if you use Mood Picker by sending me an email, I will be happy ;-)
5 changes: 5 additions & 0 deletions data/api_tokens.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE `api_tokens` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`token` TEXT NOT NULL,
`expire` INTEGER NOT NULL
);
File renamed without changes.
2 changes: 1 addition & 1 deletion library/core/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class Application {
private $modules;
private $url;

const VERSION = '1.1.0';
const VERSION = '1.1.1';

public function __construct() {
$this->checkRequirements();
Expand Down
3 changes: 1 addition & 2 deletions library/core/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ abstract class Config {
'copyright' => 'All rights reserved',
'database' => array(
'type' => 'sqlite',
'name' => 'mood_picker.db',
'init' => 'schema.txt'
'name' => 'mood_picker'
),
'themes' => array('default'),
'debug' => false
Expand Down
26 changes: 16 additions & 10 deletions library/database/sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@

class SQLite {

private static $instance;
const DB_EXTENSION = '.db';
const INIT_EXTENSION = '.sql';

private static $instances;
private static $access = 0;

static public function Instance() {
if (! isset(self::$instance)) {
$db_info = Config::Get('database');
$db_file = Config::Path(Config::DIR_DATA.DIRECTORY_SEPARATOR.$db_info['name']);
static public function Instance($database = NULL) {
if (! isset(self::$instances[$database])) {

if (is_null($database)) { $db_info = Config::Get('database'); }
else { $db_info = array( 'name' => $database ); }

$db_file = Config::Path(Config::DIR_DATA.DIRECTORY_SEPARATOR.$db_info['name'].SQLite::DB_EXTENSION);

if (! file_exists($db_file)) {
$schema = file_get_contents(Config::Path(Config::DIR_DATA.DIRECTORY_SEPARATOR.$db_info['init']));
$schema = file_get_contents(Config::Path(Config::DIR_DATA.DIRECTORY_SEPARATOR.$db_info['name'].SQLite::INIT_EXTENSION));
$schema = str_replace("\n", ' ', $schema);
$schema = str_replace("\r", ' ', $schema);

Expand All @@ -42,13 +48,13 @@ static public function Instance() {
}

// load database
self::$instance = new PDO('sqlite:'.$db_file);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
self::$instances[$database] = new PDO('sqlite:'.$db_file);
self::$instances[$database]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instances[$database]->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

}
self::$access++;
return self::$instance;
return self::$instances[$database];
}

static public function Access() {
Expand Down
43 changes: 29 additions & 14 deletions library/picker/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use Picker\Mood;
use Picker\MoodLevel;
use DataBase\File;
use DataBase\SQLite;
use Core\Config;

class API extends \Core\API {
Expand All @@ -40,14 +40,21 @@ class API extends \Core\API {
const P_TOKEN = 'token';
const P_MOOD = 'mood';

private $file;
private $tokens;
const DB_TOKENS = 'api_tokens';
private $db_tokens;

public function __construct() {
$this->file = File::Instance(self::TOKENS_FILE);
$this->tokens = $this->file->GetData();
$this->db_tokens = SQLite::Instance(self::DB_TOKENS);
}

protected function getTokens() {
$tokens = array();
$query = $this->db_tokens->query('SELECT id, token, expire from api_tokens');
while ($data = $query->fetch())
$tokens[] = array( 'token' => $data['token'], 'expire' => $data['expire'], 'id' => $data['id'] );
$query->closeCursor();
return $tokens;
}
protected function checkToken($data) {
if (! isset($data[self::P_TOKEN])) { $this->error(401, 'Bad token'); }
if (! $this->acceptToken($data[self::P_TOKEN])) { $this->error(401, 'Bad token'); }
Expand All @@ -58,25 +65,33 @@ private function generateToken() {
'token' => sha1(uniqid('', TRUE). '_' .mt_rand()),
'expire' => time() + 60*10
);
$this->tokens[] = $token;
$this->file->SaveData($this->tokens);

$query = $this->db_tokens->prepare('INSERT INTO api_tokens(token, expire) VALUES (:token, :expire)');
$query->execute(array(
'token' => $token['token'],
'expire' => $token['expire']
));
$query->closeCursor();

return $token;
}
private function acceptToken($token) {
$tokens = array();
$tokens = $this->getTokens();
$query = $this->db_tokens->prepare('DELETE FROM api_tokens WHERE id = :id');
$activeTokens = array();

//remove expired tokens
for ($i=0; $i<count($this->tokens); $i++) {
if (time() > $this->tokens[$i]['expire']) { array_splice($this->tokens, $i, 1); }
else { $tokens[] = $this->tokens[$i]['token']; }
for ($i=0; $i<count($tokens); $i++) {
if (time() > $tokens[$i]['expire']) { $query->execute(array( 'id' => $tokens[$i]['id'] )); }
else { $activeTokens[] = $tokens[$i]['token']; }
}

$position = array_search($token, $tokens);
$position = array_search($token, $activeTokens);
$found = $position >= 0 && $position !== FALSE;
// if accepted remove it
if ($found) { array_splice($this->tokens, $position, 1); }
if ($found) { $query->execute(array( 'id' => $tokens[$position]['id'] )); }
$query->closeCursor();

$this->file->SaveData($this->tokens);
return $found;
}
private function acceptCredentials($key, $token) {
Expand Down

0 comments on commit be446fb

Please sign in to comment.