forked from martijnvogten/oaiprovider-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.php
78 lines (67 loc) · 1.75 KB
/
tokens.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace oaiprovider\tokens;
interface TokenStore {
function storeToken($token, $data, $expirationdate);
function fetchToken($token);
}
class DatabaseTokenStore implements TokenStore {
function assertTable() {
DB::query("
CREATE TABLE IF NOT EXISTS oai_resumptiontoken (
`token` varchar(255) NOT NULL,
`data` longtext NOT NULL,
`expirationdate` datetime NOT NULL,
PRIMARY KEY (`token`)
)
");
}
function storeToken($token, $data, $expirationdate) {
$this->assertTable();
DB::query("
INSERT INTO oai_resumptiontoken
(token, data, expirationdate)
VALUES
(
" . DB::quote($token) . ",
" . DB::quote($data) . ",
" . DB::quote(date('Y-m-d H:i:s', $expirationdate)) . "
)
");
}
function fetchToken($token) {
$this->assertTable();
$result = DB::query("SELECT data FROM oai_resumptiontoken WHERE token=" . DB::quote($token));
$tokens = array();
foreach($result as $row) {
$tokens[] = $row['data'];
}
if (count($tokens) == 1) {
return $tokens[0];
}
return null;
}
}
class DB {
const DSN = 'mysql:host=localhost;dbname=oai_tokens';
const USER = 'root';
const PASS = '';
public static function getConnection() {
static $conn;
if ($conn == null) {
$conn = new \PDO(self::DSN, self::USER, self::PASS);
}
return $conn;
}
public static function fetchRow($sql) {
foreach(self::query($sql) as $row) {
$rows[] = $row;
}
return $rows[0];
}
public static function query($sql) {
return self::getConnection()->query($sql);
}
public static function quote($val) {
return self::getConnection()->quote($val);
}
}