Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| MobileAPIKey | |
0.00% |
0 / 82 |
|
0.00% |
0 / 8 |
552 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| getByUserID | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| create | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
42 | |||
| updateUser | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| deleteKey | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| generateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| crypto_rand_secure | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| getToken | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Core; |
| 4 | |
| 5 | class MobileAPIKey extends Store { |
| 6 | private $db; |
| 7 | private $log; |
| 8 | |
| 9 | public $key; |
| 10 | public $user_id; |
| 11 | public $typeNumArray; |
| 12 | |
| 13 | public function __construct($key = null) |
| 14 | { |
| 15 | $this->log = new \KLogger("../logs/install_keys.txt", \KLogger::DEBUG); |
| 16 | global $db_name; |
| 17 | $this->db = dbConnectByName($db_name); |
| 18 | $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 19 | $this->log->LogDebug($key); |
| 20 | $this->typeNumArray = []; |
| 21 | if($key !== null) { |
| 22 | $stmt = $this->db->query("SELECT * FROM mobileAPI WHERE `key` = '".$key."'"); |
| 23 | if($stmt) { |
| 24 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 25 | $this->key = $row['key']; |
| 26 | $this->user_id = $row['user_id']; |
| 27 | $this->typeNumArray[] = $row['store']; |
| 28 | return true; |
| 29 | } |
| 30 | } else { |
| 31 | $this->log->LogDebug("STMT didn't work"); |
| 32 | return false; |
| 33 | } |
| 34 | } else { |
| 35 | $this->log->LogDebug("Key is NULL"); |
| 36 | return false; |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | public function getByUserID($userID) { |
| 41 | try { |
| 42 | $stmt = $this->db->prepare("SELECT * FROM mobileAPI WHERE user_id = :userID"); |
| 43 | $stmt->bindValue(":userID", $userID); |
| 44 | $stmt->execute(); |
| 45 | if($stmt) { |
| 46 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 47 | $this->key = $row['key']; |
| 48 | $this->user_id = $row['user_id']; |
| 49 | $this->typeNumArray[] = $row['store']; |
| 50 | return true; |
| 51 | } |
| 52 | } else { |
| 53 | $this->log->LogDebug("STMT didn't work"); |
| 54 | return false; |
| 55 | } |
| 56 | } catch(\PDOException $e) { |
| 57 | $this->log->LogError("DB ERROR:".$e->getMessage()); |
| 58 | |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | public function create($typeNumArray, $userID, $key = null) { |
| 63 | try { |
| 64 | if($key == null) { |
| 65 | $key = $this->generateKey(); |
| 66 | } |
| 67 | foreach($typeNumArray as $typeNum) { |
| 68 | $this->log->LogDebug("GroupName: ".$typeNum->name); |
| 69 | if(preg_match('/[a-z][a-z][0-9]{2,}/', $typeNum->name)) { |
| 70 | $this->log->LogDebug("TypeNum: ".$typeNum->name); |
| 71 | $this->log->LogDebug("Key: ".$key); |
| 72 | $this->log->LogDebug("UserID: ".$userID); |
| 73 | $stmt = $this->db->prepare("INSERT INTO mobileAPI (`key`, store, user_id) VALUES (:key, :typeNum, :userID)"); |
| 74 | $stmt->bindValue(":key", $key); |
| 75 | $stmt->bindValue(":typeNum", $typeNum->name); |
| 76 | $stmt->bindValue(":userID", (int)$userID); |
| 77 | if($stmt->execute()) { |
| 78 | |
| 79 | } else { |
| 80 | $this->log->LogError("DB ERROR: No Execute"); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } catch(\PDOException $e) { |
| 86 | $this->log->LogError("DB ERROR:".$e->getMessage()); |
| 87 | |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | public function updateUser($typeNumArray, $userID, $key) { |
| 92 | try { |
| 93 | $stmt = $this->db->prepare("DELETE FROM mobileAPI WHERE user_id = :userID"); |
| 94 | $stmt->bindValue(":userID", $userID); |
| 95 | $stmt->execute(); |
| 96 | return $this->create($typeNumArray, $userID, $key); |
| 97 | } catch(\PDOException $e) { |
| 98 | $this->log->LogError($e->getMessage()); |
| 99 | |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | public function deleteKey($key) { |
| 104 | try { |
| 105 | $stmt = $this->db->prepare("DELETE FROM mobileAPI WHERE key = :key"); |
| 106 | $stmt->bindValue(":key", $key); |
| 107 | $stmt->execute(); |
| 108 | } catch(\PDOException $e) { |
| 109 | $this->log->LogError($e->getMessage()); |
| 110 | |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | private function generateKey() { |
| 115 | return $this->key = $this->getToken(10); |
| 116 | } |
| 117 | |
| 118 | private function crypto_rand_secure($min, $max) |
| 119 | { |
| 120 | $range = $max - $min; |
| 121 | if ($range < 1) return $min; // not so random... |
| 122 | $log = ceil(log($range, 2)); |
| 123 | $bytes = (int) ($log / 8) + 1; // length in bytes |
| 124 | $bits = (int) $log + 1; // length in bits |
| 125 | $filter = (int) (1 << $bits) - 1; // set all lower bits to 1 |
| 126 | do { |
| 127 | $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
| 128 | $rnd = $rnd & $filter; // discard irrelevant bits |
| 129 | } while ($rnd > $range); |
| 130 | return $min + $rnd; |
| 131 | } |
| 132 | |
| 133 | private function getToken($length) |
| 134 | { |
| 135 | $token = ""; |
| 136 | $codeAlphabet = "ABCDEFGHIJKLMNPQRSTUVWXYZ"; |
| 137 | $codeAlphabet.= "123456789"; |
| 138 | $max = strlen($codeAlphabet); // edited |
| 139 | |
| 140 | for ($i=0; $i < $length; $i++) { |
| 141 | $token .= $codeAlphabet[$this->crypto_rand_secure(0, $max-1)]; |
| 142 | } |
| 143 | |
| 144 | return $token; |
| 145 | } |
| 146 | } |