2014-04-27 23:11:52 +04:00
|
|
|
package Database;
|
|
|
|
|
|
|
|
use DBI;
|
2014-04-28 12:04:17 +04:00
|
|
|
use GPG;
|
2014-04-27 23:11:52 +04:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
|
2014-04-28 12:17:17 +04:00
|
|
|
# Get home dir
|
2014-04-28 16:14:23 +04:00
|
|
|
my $home = $ENV{HOME};
|
2014-04-28 00:04:16 +04:00
|
|
|
|
2014-04-28 14:07:01 +04:00
|
|
|
my $gpg = GPG->new($home);
|
2014-04-28 12:17:17 +04:00
|
|
|
|
|
|
|
my $self = {
|
|
|
|
_home => $home,
|
|
|
|
_gpg => $gpg,
|
2014-04-27 23:11:52 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
bless $self, $class;
|
|
|
|
return $self;
|
|
|
|
}
|
2014-04-27 23:35:58 +04:00
|
|
|
|
2014-04-28 00:04:16 +04:00
|
|
|
sub connect {
|
2014-04-28 14:07:01 +04:00
|
|
|
my ( $self, $db_file ) = @_;
|
2014-04-28 12:17:17 +04:00
|
|
|
my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "" );
|
2014-04-28 00:04:16 +04:00
|
|
|
|
|
|
|
return $dbh;
|
|
|
|
}
|
|
|
|
|
2014-04-28 16:14:23 +04:00
|
|
|
# Query proccessing mechanism
|
2014-04-28 00:04:16 +04:00
|
|
|
sub mdo {
|
2014-04-28 16:14:23 +04:00
|
|
|
my ( $self, $query ) = @_;
|
|
|
|
my $db_file = $query->{file};
|
|
|
|
my $q = $query->{query};
|
|
|
|
my $name = $query->{name};
|
|
|
|
my $type = $query->{type};
|
|
|
|
|
|
|
|
my $dbh = Database->connect($db_file);
|
|
|
|
|
|
|
|
# Select
|
|
|
|
if ( $type eq 'select' ) {
|
|
|
|
my $sth = $dbh->prepare($q);
|
|
|
|
$sth->execute();
|
|
|
|
my ( $name, $resource, $password ) = $sth->fetchrow_array();
|
|
|
|
|
|
|
|
my $q_hash = {
|
|
|
|
name => $name,
|
|
|
|
resource => $resource,
|
|
|
|
password => $password,
|
|
|
|
};
|
|
|
|
return $q_hash;
|
|
|
|
}
|
|
|
|
elsif ( $type eq 'do' ) {
|
|
|
|
$dbh->do($q);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print STDERR "Something went wrong! $!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 1;
|
2014-04-28 00:04:16 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:07:01 +04:00
|
|
|
# Create config dirrectory and DB if not exist
|
2014-04-28 00:04:16 +04:00
|
|
|
sub create_base {
|
2014-04-28 12:17:17 +04:00
|
|
|
my ($self) = @_;
|
|
|
|
my $home = $self->{_home};
|
|
|
|
my $pm_dir = $home . "/.PM/";
|
2014-04-28 14:07:01 +04:00
|
|
|
my $gpg = $self->{_gpg};
|
2014-04-28 12:17:17 +04:00
|
|
|
|
|
|
|
# Check dir
|
|
|
|
if ( !( -d $pm_dir ) ) {
|
|
|
|
|
|
|
|
# Create dirrectory
|
|
|
|
my @mkdir_cmd = ( "mkdir", "$pm_dir" );
|
|
|
|
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
|
|
|
|
|
2014-04-28 14:07:01 +04:00
|
|
|
my $first_sqlite = '/tmp/db.sqlite';
|
|
|
|
|
2014-04-28 12:17:17 +04:00
|
|
|
# Create DB file
|
2014-04-28 14:07:01 +04:00
|
|
|
my @createdb_cmd = ( "touch", "$first_sqlite" );
|
2014-04-28 12:17:17 +04:00
|
|
|
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
|
|
|
|
|
2014-04-28 14:07:01 +04:00
|
|
|
# Create table.
|
|
|
|
my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" );
|
2014-04-28 12:17:17 +04:00
|
|
|
print "Create database schema\n";
|
|
|
|
my $q_table
|
|
|
|
= "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
|
|
|
|
$dbh->do($q_table);
|
|
|
|
|
|
|
|
# Encrypt db
|
2014-04-28 14:07:01 +04:00
|
|
|
$gpg->encrypt_db($first_sqlite);
|
2014-04-28 12:17:17 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Dirrectory is exist!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-28 00:04:16 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 12:17:17 +04:00
|
|
|
1;
|