|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ApiKey extends Model |
|
4
|
|
|
{ |
|
5
|
|
|
protected $name; |
|
6
|
|
|
|
|
7
|
|
|
protected $owner; |
|
8
|
|
|
|
|
9
|
|
|
protected $key; |
|
10
|
|
|
|
|
11
|
|
|
protected $status; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The name of the database table used for queries |
|
15
|
|
|
*/ |
|
16
|
|
|
const TABLE = "api_keys"; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
1 |
|
protected function assignResult($key) |
|
22
|
|
|
{ |
|
23
|
1 |
|
$this->name = $key['name']; |
|
24
|
1 |
|
$this->owner = Player::get($key['owner']); |
|
25
|
1 |
|
$this->key = $key['key']; |
|
26
|
1 |
|
$this->status = $key['status']; |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
public static function createKey($name, $owner) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$key = self::create(array( |
|
32
|
1 |
|
'name' => $name, |
|
33
|
1 |
|
'owner' => $owner, |
|
34
|
1 |
|
'key' => self::generateKey(), |
|
35
|
|
|
'status' => "active" |
|
36
|
1 |
|
)); |
|
37
|
|
|
|
|
38
|
1 |
|
return $key; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function getKeyByOwner($owner) |
|
42
|
|
|
{ |
|
43
|
|
|
$key = self::fetchIdFrom($owner, "owner"); |
|
44
|
|
|
|
|
45
|
|
|
if ($key == null) { |
|
46
|
|
|
return self::createKey("Automatically generated key", $owner); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return self::get($key); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getKey() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->key; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getName() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->name; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function getOwner() |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->owner; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getStatus() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->status; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function getKeys($owner = -1) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($owner > 0) { |
|
75
|
|
|
$ids = self::fetchIdsFrom("owner", array($owner), false, "WHERE status = 'active'"); |
|
76
|
|
|
return self::arrayIdToModel($ids); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$ids = self::fetchIdsFrom("status", array("active")); |
|
80
|
|
|
return self::arrayIdToModel($ids); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
protected static function generateKey() |
|
84
|
|
|
{ |
|
85
|
1 |
|
list($usec, $sec) = explode(' ', microtime()); |
|
86
|
1 |
|
$seed = (float) $sec + ((float) $usec * 100000); |
|
87
|
1 |
|
srand($seed); |
|
88
|
|
|
|
|
89
|
1 |
|
$key = rand(); |
|
90
|
|
|
|
|
91
|
1 |
|
return substr(sha1($key), 24); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|