summaryrefslogtreecommitdiff
path: root/lib/rddb.pm
blob: e4f797283958fc1f1e1f4b097b187f12a2a35a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/perl

use strict;
use Config::IniFiles;
use DBI;

package rddb;


sub opendb
{
  my $RD_CONF = "/etc/rd.conf";
  my $cfg = Config::IniFiles->new(-file => $RD_CONF)
    or return (undef , 'ERROR', "Config File Error: " . join("\n", @Config::IniFiles::errors));

  my $dbhost = $cfg->val('mySQL', 'Hostname');
  my $dbname = $cfg->val('mySQL', 'Database');
  my $dbuser = $cfg->val('mySQL', 'Loginname');
  my $dbpasswd = $cfg->val('mySQL', 'Password');

  my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpasswd")
    or return (undef, 'ERROR', "Database Error: " . $DBI::errstr);

  $dbh->do(qq{SET CHARACTER SET utf8;})
    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);

  return ($dbh, 'OK', 'success');
}

sub closedb
{
  my $dbh = shift;
  $dbh->disconnect();
}

sub get_token
{
  my ($dbh, $username) = @_;

  my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;};
  my $sth = $dbh->prepare($sql)
    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);

  $sth->execute($username)
    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);

  my ($token) = $sth->fetchrow_array;
  $sth->finish();

  unless(defined $token) {
    return (undef, 'ERROR', "user '" . $username . "' not known by rivendell")
  }
  return ($token, 'OK', 'success');
}

sub check_token
{
  my ($dbh, $username, $token) = @_;

  my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;};
  my $sth = $dbh->prepare($sql)
    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);

  $sth->execute($username)
    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);

  my ($token_result) = $sth->fetchrow_array;
  $sth->finish();

  unless(defined $token_result) {
    return (undef, 'ERROR', "user '" . $username . "' not known by rivendell")
  }

  if($token_result eq $token) {
    return (1, 'OK', 'success');
  }
  return (0, 'ERROR', "wrong password");
}

sub get_showtitle
{
  my ($dbh, $showid) = @_;

  my $sql = qq{select TITLE from CART where NUMBER = ?;};
  my $sth = $dbh->prepare($sql)
    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);

  $sth->execute($showid)
    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);

  my ($title) = $sth->fetchrow_array;
  $sth->finish();

  unless(defined $title) {
    return (undef, 'ERROR', "Show wid ID=" . $showid .  " not found!")
  }
  return ($title, 'OK', 'success');
}

sub get_dropboxes
{
  my ($dbh, $username) = @_;

  my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.PATH,DROPBOXES.TO_CART,DROPBOXES.LOG_PATH,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME= ? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME;};

  my $sth = $dbh->prepare($sql)
    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);

  $sth->execute($username)
    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);

  my @allowed_dbs;
  while(my ($group, $type, $showid, $log, $normlevel, $trimlevel, $params, $lowcart, $highcart, $groupdesc) = $sth->fetchrow_array()) {

    my $entry = {};
    $entry->{'GROUP'} = $group;
    $entry->{'GROUPDESC'} = $groupdesc;
    $entry->{'GROUPLOWCART'} = $lowcart;
    $entry->{'GROUPHIGHCART'} = $highcart;
    $entry->{'TYPE'} = $type;
    $entry->{'SHOWID'} = -1;
    $entry->{'LOG'} = $log;
    $entry->{'NORMLEVEL'} = $normlevel;
    $entry->{'TRIMLEVEL'} = $trimlevel;
    $entry->{'PARAM'} = $params;
    if($type eq "show") {
      $entry->{'SHOWID'} = $showid;

      my ($title, $status, $errorstring) = get_showtitle($dbh, $showid);
      unless (defined $title) {
	return (undef, $status, $errorstring);
      }
      $entry->{'SHOWTITLE'} = $title;

      if($params =~ /^([01]{4})-([1-7])-([0-9]{2})([0-9]{2})-([0-9]{3})$/) {
        $entry->{'RHYTHM'} = $1;
        $entry->{'DOW'} = int $2;
        $entry->{'DOW'} = 0 unless $entry->{'DOW'} < 7;
        $entry->{'STARTTIME'} = "$3:$4";
        $entry->{'LENGTH'} = int $5;
      }
    }

    push @allowed_dbs, $entry;
  }
  $sth->finish();

  return @allowed_dbs;
}


return 1;