real_escape_string( self::$tblPubkeys ); $id = self::$db->real_escape_string( $id ); $pubkey = self::$db->real_escape_string( $pubkey ); $sql = "INSERT INTO `{$tblPubkeys}` SET `id` = '{$id}', `pubkey` = '{$pubkey}' ON DUPLICATE KEY UPDATE `pubkey` = '{$pubkey}'"; $result = self::$db->query( $sql ); if ( ! $result || ! self::$db->affected_rows ){ throw new Exception('Could not store public key'); } } static function get_pubkey( $id ){ $tblPubkeys = self::$db->real_escape_string( self::$tblPubkeys ); $id = self::$db->real_escape_string( $id ); $sql = "SELECT `pubkey` FROM `{$tblPubkeys}` WHERE `id` = '{$id}'"; $result = self::$db->query( $sql ); if ( ! $result || ! $result->num_rows ){ return FALSE; } $row = $result->fetch_assoc(); return $row['pubkey']; } static function store_keypair( $id, $pubkey, $privkey, $overwrite = FALSE ){ $tblKeypairs = self::$db->real_escape_string( self::$tblKeypairs ); $pubkey = self::$db->real_escape_string( $pubkey ); $privkey = self::$db->real_escape_string( $privkey ); $sql = "INSERT INTO `{$tblKeypairs}` SET `id` = '{$id}', `pubkey` = '{$pubkey}', `privkey` = '{$privkey}'"; if ( $overwrite ){ $sql .= " ON DUPLICATE KEY UPDATE `pubkey` = '{$pubkey}', `privkey` = '{$privkey}'"; } $result = self::$db->query( $sql ); if ( ! $result || ! self::$db->affected_rows ){ throw new Exception('Could not insert key pair into database'); } } static function exists_keypair( $id ){ $tblKeypairs = self::$db->real_escape_string( self::$tblKeypairs ); $id = self::$db->real_escape_string( $id ); $sql = "SELECT 1 FROM `{$tblKeypairs}` WHERE `id` = '{$id}'"; $result = self::$db->query( $sql ); return $result && $result->num_rows > 0; } static function get_keypair( $id ){ $tblKeypairs = self::$db->real_escape_string( self::$tblKeypairs ); $id = self::$db->real_escape_string( $id ); $sql = "SELECT `pubkey`, `privkey` FROM `{$tblKeypairs}` WHERE `id` = '{$id}'"; $result = self::$db->query( $sql ); if ( ! $result || ! $result->num_rows ){ return FALSE; } $row = $result->fetch_assoc(); return [ $row['pubkey'], $row['privkey'] ]; } static function remove_keypair( $id ){ $tblKeypairs = self::$db->real_escape_string( self::$tblKeypairs ); $id = self::$db->real_escape_string( $id ); $sql = "DELETE FROM `{$tblKeypairs}` WHERE `id` = '{$id}'"; self::$db->query( $sql ); return self::$db->affected_rows; } static function list_keypairs( $pattern = NULL ){ $tblKeypairs = self::$db->real_escape_string( self::$tblKeypairs ); $sql = "SELECT `id` FROM `{$tblKeypairs}`"; if ( $pattern ){ $pattern = self::$db->real_escape_string( $pattern ); $sql .= " WHERE `id` LIKE '{$pattern}%'"; } $result = self::$db->query( $sql ); if ( ! $result || ! $result->num_rows ){ return []; } $list = []; while ( $row = $result->fetch_row() ){ $list[] = implode( $row ); } return $list; } } // end of file storage_db.class.php