Files
scripts/Perl Skript template/confdb.pm
2024-10-14 00:08:40 +02:00

143 lines
4.2 KiB
Perl

package confdb;
require Exporter;
use database;
use HTTP::Request::Common;
use LWP::UserAgent;
use URL::Encode qw/:all/;
use Crypt::Lite;
use Digest::MD5 qw(md5_hex);
use JSON;
use CGI;
use vars qw($VERSION @ISA @EXPORT);
$VERSION = 1.00;
@ISA = qw(Exporter);
###############################################################################
@EXPORT = qw/
get_config
get_profiles
set_config
send_config
/;
our $db_name="1_configurations";
our $db_host="config.andregeissler.de";
our $db_type="mysql";
our $db_user="1_Config01";
our $db_pass="Config01";
our $db_port="3306";
our $db_hand;
sub get_profiles {
my $app_name=shift;
db_connect ($db_hand, $db_type, $db_name, $db_host, $db_port, $db_user, $db_pass);
my @ret=db_select($db_hand, " SELECT profiles.profilename FROM apps INNER JOIN profiles ON apps.ID = profiles.appid where apps.appname = '$app_name'");
#my @ret=db_select($db_hand, "SELECT profile FROM apps WHERE appname = '$app_name'");
db_disconnect ($db_hand);
my @rets;
foreach my $r (@ret) {
foreach (@$r) { push @rets, $_; }
}
#return @{$ret[0]};
return @rets;
}
sub get_config_from_db {
my $app_name=shift;
my $app_profile=shift;
my @re;
db_connect ($db_hand, $db_type, $db_name, $db_host, $db_port, $db_user, $db_pass);
$select="SELECT configs.config FROM apps INNER JOIN profiles ON apps.ID = profiles.appid INNER JOIN configs ON profiles.id = configs.profileid where apps.appname = '$app_name' and profiles.profilename = '$app_profile'";
my @ret=db_select($db_hand, $select);
db_disconnect ($db_hand);
return @{$ret[0]};
}
sub set_config_to_db {
my $app_name=shift;
my $app_profile=shift;
my $app_config=shift;
my @ret;
my $s;
db_connect ($db_hand, $db_type, $db_name, $db_host, $db_port, $db_user, $db_pass);
# id ermitteln um profil zuweisen zu können, select id from apps where apps.appname and apps.key
$s="select id from apps where apps.appname = '$app_name'";
@ret = db_select ($db_hand, $s);
my $app_id = ${$ret[0]}[0];
# id ermitteln um config zuweisen zu können, select id from profiles where profiles.appid=APPID
$s="select id from profiles where profiles.profilename = '$app_profile' and profiles.appid = '$app_id'";
@ret = db_select ($db_hand, $s);
my $profile_id = ${$ret[0]}[0];
# neue config anlegen, insert configs.config configs.profileid
$s="update configs set config='$app_config' where profileid = '$profile_id'";
db_exec ($db_hand, $s);
}
sub get_config {
$app_name = shift;
$app_profile = shift;
$app_pass = shift;
$app_daten = $app_name . ";" . $app_profile;
$md5_pass = md5_hex("$app_pass");
$c_daten = url_encode_utf8($app_daten);
$ua = LWP::UserAgent->new;
$re = $ua->request(POST 'http://config.andregeissler.de/cgi-bin/index.pl', [data => "$c_daten"]);
$content = $$re{_content};
@c_re_daten = $content =~ /###(.*)###/gm;
foreach (@c_re_daten) {
$crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' );
$re_daten = $crypt->decrypt($_, $md5_pass);
($aa, $bb) = $re_daten =~ /^(.{9}).*(.{9})$/;
}
$array_ref = decode_json($re_daten);
@array = @{$array_ref};
return @array;
}
sub set_config {
$app_name = shift;
$app_profile = shift;
$app_pass = shift;
$md5_pass = md5_hex("$app_pass");
@app_config = ( {key1 => "value1", key2 => "value2" }, 88, { key3 => "value3", key4 => "value4" }, { key5 => "value5" } );
$array_ref = \@app_config;
$app_config = encode_json($array_ref);
$crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' );
$c_daten = $crypt->encrypt($app_config, $md5_pass);
print "app_name $app_name\napp_pass $app_pass\nmd5_pass $md5_pass\napp_profile $app_profile\napp_config $app_config\nc_daten $c_daten\n";
set_config_to_db ($app_name, $app_profile, $c_daten);
}
sub send_config {
$titel = shift;
$q = new CGI;
$c_daten = $q->param("data");
$daten = url_decode_utf8($c_daten);
($app, $profil) = split/;/,$daten;
$titel .= " für $app $profil";
@ret = get_config_from_db($app, $profil);
printf "Content-type: text/html\n\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n<meta content=\"text/html\">\n<title>$titel</title>\n</head>\n<body>\n";
foreach (@ret) {
printf "###$_###\n";
}
printf "</body>\n</html>\n";
}
1;