| 50 | | if ($type == 'set') { |
| 51 | | // set a new token |
| 52 | | $h->csrfToken = $this->csrfkey($h); |
| 53 | | } else { |
| 54 | | // check existing token, then clear it |
| 55 | | $result = $this->checkcsrf($h); |
| 56 | | // set a new token |
| 57 | | $h->csrfToken = $this->csrfkey($h); |
| 58 | | // return result of check |
| 59 | | return $result; |
| | 29 | if ($check) { |
| | 30 | // Remove expired keys |
| | 31 | $sql = "DELETE FROM ".TABLE_TOKENS." WHERE token_stamp < ".time(); |
| | 32 | $h->db->query($sql); |
| | 33 | |
| | 34 | // Check existing token, then clear it |
| | 35 | |
| | 36 | // Try to get csrf token from POST |
| | 37 | $key = $h->cage->post->testAlnum('csrf'); |
| | 38 | // Try to get csrf token from GET |
| | 39 | $key = ($key) ? $key : $h->cage->get->testAlnum('csrf'); |
| | 40 | |
| | 41 | if ($key) { |
| | 42 | // Make sure the key is good |
| | 43 | if ($key === preg_replace('/[^a-z0-9]+/', '', $key)) { |
| | 44 | |
| | 45 | $sql = "SELECT token_sid FROM ".TABLE_TOKENS." WHERE token_sid = '".$sid."' AND token_key = '".$key."' AND token_action = '".$action."'"; |
| | 46 | $results = $h->db->get_results($sql); |
| | 47 | if ($results) { |
| | 48 | foreach ($results as $row) { |
| | 49 | $valid = $row->token_sid; |
| | 50 | } |
| | 51 | } |
| | 52 | |
| | 53 | if (isset($valid)) { |
| | 54 | //var_dump('nice token'); |
| | 55 | $sql = "DELETE FROM ".TABLE_TOKENS." WHERE token_sid = '".$valid."' AND token_key = '".$key."'"; |
| | 56 | $h->db->query($sql); |
| | 57 | $result = TRUE; |
| | 58 | } |
| | 59 | } |
| | 60 | } |
| 63 | | /** |
| 64 | | * |
| 65 | | * @param <type> $h |
| 66 | | * @return <type> |
| 67 | | */ |
| 68 | | public function csrfkey($h) |
| 69 | | { |
| 70 | | $key = md5(microtime().$this->sid.rand()); |
| 71 | | $stamp = time() + (60 * $this->life); |
| 72 | | $sql = "INSERT INTO ".$this->table." (token_sid, token_key, token_stamp, token_action) VALUES (%s, %s, %d, %s)"; |
| 73 | | $h->db->query($h->db->prepare($sql, $this->sid, $key, $stamp, $this->action)); |
| 74 | | |
| 75 | | return $key; |
| 76 | | } |
| 77 | | |
| 78 | | private function checkcsrf($h) |
| 79 | | { |
| 80 | | $this->cleanOld($h); |
| 81 | | |
| 82 | | // Try to get csrf token from POST |
| 83 | | $key = $h->cage->post->testAlnum('csrf'); |
| 84 | | |
| 85 | | // Try to get csrf token from GET |
| 86 | | if (!$key) { |
| 87 | | $key = $h->cage->get->testAlnum('csrf'); |
| 88 | | } |
| 89 | | |
| 90 | | if (!$key) { |
| 91 | | return FALSE; |
| 92 | | } |
| 93 | | |
| 94 | | $cleanKey = preg_replace('/[^a-z0-9]+/', '', $key); |
| 95 | | if (strcmp($key, $cleanKey) != 0) { |
| 96 | | return FALSE; |
| 97 | | } |
| 98 | | |
| 99 | | $sql = "SELECT token_sid FROM ".$this->table." WHERE token_sid = %s AND token_key = %s AND token_action = %s"; |
| 100 | | $results = $h->db->get_results($h->db->prepare($sql, $this->sid, $cleanKey, $this->action)); |
| 101 | | if ($results) { |
| 102 | | foreach ($results as $row) { |
| 103 | | $valid = $row->token_sid; |
| 104 | | } |
| 105 | | } |
| 106 | | if (isset($valid)) { |
| 107 | | $sql = "DELETE FROM ".$this->table." WHERE token_sid = %s AND token_key = %s"; |
| 108 | | $h->db->query($h->db->prepare($sql, $valid, $cleanKey)); |
| 109 | | return TRUE; |
| 110 | | } |
| 111 | | } |
| 112 | | |
| 113 | | private function cleanOld($h) |
| 114 | | { |
| 115 | | // Remove expired keys |
| 116 | | $exp = time(); |
| 117 | | $sql = "DELETE FROM ".$this->table." WHERE token_stamp < %d"; |
| 118 | | $h->db->query($h->db->prepare($sql, $exp)); |
| 119 | | return TRUE; |
| 120 | | } |
| 121 | | |
| 122 | | public function logout($h) |
| 123 | | { |
| 124 | | $sql = "DELETE FROM ".$this->table." WHERE token_sid = %s"; |
| 125 | | $h->db->query($h->db->prepare($sql, $this->sid)); |
| 126 | | return TRUE; |
| 127 | | } |