From: Kilian Saffran Date: Fri, 28 Sep 2018 07:50:58 +0000 (+0200) Subject: creorga localserver removed X-Git-Tag: 3.23.3 X-Git-Url: http://cloud.dks.lu/git/?a=commitdiff_plain;h=4468a2c28dcca5fcc38510c7425cc4bbd91543b0;p=creorga.git creorga localserver removed --- diff --git a/Tools/Creorgasrv.pl b/Tools/Creorgasrv.pl deleted file mode 100644 index c64e7ae..0000000 --- a/Tools/Creorgasrv.pl +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/perl -use strict; -use File::Basename; -use Getopt::Long; -use Time::HiRes; -use Data::Dumper; -#if ($^O ne "MSWin32" ){ -# eval("use lib (dirname($0)); -# use lib ($ENV{HOME}.'/perl5/lib/perl5'); -# use lib ('C:/USers/ksaff/Workspace/DKSService');"); -# -#} -use lib (dirname($0)); -if ($^O eq "darwin"){ - eval("use lib ($ENV{HOME}.'/perl5/lib/perl5');"); -} -use Plack::Builder; -use Plack::App::File; -use Plack::App::WrapCGI; -use Plack::Middleware::Auth::Basic; -use Plack::Request; -use Plack::Runner; -use Module::Service; -use Module::Test; -use Module::SQLite; -use Module::PDFExtract; -#use Module::Audio; -use Module::System; -use Module::OpenVPN; -#use Module::SSH; -#use JSON::PP; - -print $^O."\n"; - - -my @match = grep { /par-.*inc$/} @INC; - -my $basedir = dirname($0); -if (scalar(@match) > 0){ - $basedir = $match[0]; -} - -my $cfgpath = ""; -#print "BASEDIR:".$basedir."\n"; - - -sub version { - require Twiggy; - print "Twiggy $Twiggy::VERSION\n"; -} - -#sub authen_cb { -# my($username, $password, $env) = @_; -# return $username eq 'admin' && $password eq 'admin'; -# } -#my $name = basename($0); -#$name =~ s/srv\.pl$//; -#$name =~ s/srv\.exe$//; -#my $appcfgpath = ""; -#if ($^O eq "MSWin32"){ -# $appcfgpath = $ENV{APPDATA}.'/'.$name; -#}else { -# $appcfgpath = $ENV{HOME}.'/Library/Application Support/'.$name; -#} - - -my $allapp = builder { - mount "/app" => Module::Service->new(); - mount "/system" => Module::System->new(); - mount "/test" => Module::Test->new(); - mount "/openvpn" => Module::OpenVPN->new(); - mount "/sqlite" => Module::SQLite->new(); - mount "/pdfextract" => Module::PDFExtract->new(); -}; - - -my @args = ("-p","6060"); -my $runner = Plack::Runner->new(server => 'Twiggy', env => 'deployment', version_cb => \&version);#env => development, test -$runner->parse_options(@args); -$runner->run($allapp); - -print "Started\n"; - - diff --git a/Tools/Module/OpenVPN.pm b/Tools/Module/OpenVPN.pm deleted file mode 100644 index c7d8065..0000000 --- a/Tools/Module/OpenVPN.pm +++ /dev/null @@ -1,207 +0,0 @@ -package Module::OpenVPN; - -use strict; -use warnings; -use parent qw(Plack::Component); -use Plack::Request; -use Data::Dumper; -use File::Find::Rule; -use File::Basename; -use JSON::PP; -use File::Copy; -use File::Path qw(make_path); - -sub call { - my($self, $env) = @_; - #$self->_app->($env); - if (($env->{REMOTE_ADDR} =~ "^127\.0\.") && - ($env->{REMOTE_ADDR} =~ "^10\.") && - ($env->{REMOTE_ADDR} =~ "^172\.16\.") && - ($env->{REMOTE_ADDR} =~ "^192\.168\.")) { - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Sorry no remote access allowed!" ] - ]; - } - if ($env->{PATH_INFO} =~ /^\/connect/){ - return $self->vpnconnect($env); - } elsif ($env->{PATH_INFO} =~ /^\/disconnect/){ - return $self->vpndisconnect($env); - } elsif ($env->{PATH_INFO} =~ /^\/installprofile/){ - return $self->vpninstallprofile($env); - } elsif ($env->{PATH_INFO} =~ /^\/listprofiles/){ - return $self->vpnprofilelist($env); - } - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Unknown System Request!" ] - ]; -} - -sub vpnconnect(){ - my $self = shift; - my $env = shift; - my $html->{result} = 0; - my $req = Plack::Request->new($env); - my $uprofile = ""; - #is gui or vpn running - - if (exists($req->query_parameters->{vpnprofile})){ - my $status = $self->vpnstatus(); - if (!exists($status->{active}->{$req->query_parameters->{vpnprofile}})){ - if ($^O eq "MSWin32"){ - if (exists($status->{gui})){ - system('taskkill.exe /F /IM openvpn.exe'); - system('taskkill.exe /F /IM openvpn-gui.exe'); - sleep(1); - } - my $st = system(1,'start /b "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect "'.$req->query_parameters->{vpnprofile}.'.ovpn"'); - if ($st == 0){ - - my $bconn = 0; - my $i = 30; - while ($bconn == 0 || $i > 0){ - $status = $self->vpnstatus(); - if (exists($status->{active}->{$req->query_parameters->{vpnprofile}})){ - $html->{result} = $status; - $bconn = 1; - } - $i--; - sleep(1); - } - } - } - } else { - $html->{result} = $status; - } - } - return [ - 200, - [ 'Content-Type' => "application/json",'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub vpnstatus(){ - my $self = shift; - my $status = (); - if ($^O eq "MSWin32"){ - - my $tasklist = `tasklist`; - my @task = split("\n",$tasklist); - my @ovpntasks = grep(/openvpn-gui\.exe/,@task); - if (scalar(@ovpntasks) > 0){ - $status->{gui} = "running"; - } - @ovpntasks = grep(/openvpn.exe/,@task); - #$status->{active_connections} = scalar(@ovpntasks); - if (scalar(@ovpntasks) > 0){ - my $ff = File::Find::Rule->new(); - $ff->file; - $ff->name('*.log'); - my @loglist =$ff->in($ENV{USERPROFILE}.'/OpenVPN/log'); - foreach my $c (@loglist){ - open(CLOG,$c); - my @data = ; - close(CLOG); - my $laststate=$data[scalar(@data)-1]; - chomp($laststate); - if ($laststate =~ /CONNECTED/){ - my ($time,$ip,$server,$port) = $laststate =~ /.+MANAGEMENT:\s>STATE:(\d+),CONNECTED,SUCCESS,(.+),(.+),(.+),,$/; - if (!exists($status->{connection}->{$ip})){ - $status->{connection}->{$ip}->{config} = substr(basename($c),0,-4);; - $status->{connection}->{$ip}->{server} = $server; - $status->{connection}->{$ip}->{port} = $port; - $status->{connection}->{$ip}->{connected_since} = $time; - }else { - if ($time >= $status->{connection}->{$ip}->{connected_since}){ - $status->{connection}->{$ip}->{config} = substr(basename($c),0,-4); - $status->{connection}->{$ip}->{server}= $server; - $status->{connection}->{$ip}->{port} = $port; - $status->{connection}->{$ip}->{connected_since} = $time; - } - } - } - } - my @notactive = (); - my $active = (); - foreach my $c (keys(%{$status->{connection}})){ - my $routeslist = `route print -4`; - my @routes = split("\n",$routeslist); - my @activetest = grep(/$c/,@routes); - if (scalar(@activetest) == 0){ - push @notactive,$c; - } else { - $active->{$status->{connection}->{$c}->{config}} = $c; - } - } - foreach my $na (@notactive){ - delete $status->{connection}->{$na}; - } - $status->{active} = $active; - } - } - return $status; -} - -sub vpndisconnect(){ - my $self = shift; - my $env = shift; - my $html->{result} = 1; - if ($^O eq "MSWin32"){ - system('taskkill.exe /F /IM openvpn.exe'); - system('taskkill.exe /F /IM openvpn-gui.exe'); - } - return [ - 200, - [ 'Content-Type' => "application/json",'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub vpninstallprofile(){ - my $self = shift; - my $env = shift; - my $req = Plack::Request->new($env); - my $html->{result} = 0; - if ($^O eq "MSWin32"){ - if ( ! -d $ENV{USERPROFILE}.'/OpenVPN'){ - make_path($ENV{USERPROFILE}.'/OpenVPN'); - } - if (exists($req->query_parameters->{vpnprofile}) && (-e $req->query_parameters->{vpnprofile}) && ($req->query_parameters->{vpnprofile} =~ /\.ovpn$/)){ - copy(req->query_parameters->{vpnprofile},$ENV{USERPROFILE}.'/OpenVPN/'.basename($req->query_parameters->{vpnprofile})); - $html->{result} = 1; - } - } - return [ - 200, - [ 'Content-Type' => "application/json",'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub vpnprofilelist(){ - my $self = shift; - my $env = shift; - my $html->{result} = (); - if ($^O eq "MSWin32"){ - my $ff = File::Find::Rule->new(); - $ff->file; - $ff->name('*.ovpn'); - my @vpnlist =$ff->in($ENV{USERPROFILE}.'/OpenVPN'); - foreach (my $p=0;$p{result} = \@vpnlist; - } - return [ - 200, - [ 'Content-Type' => "application/json",'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - - -1; \ No newline at end of file diff --git a/Tools/Module/PDFExtract.pm b/Tools/Module/PDFExtract.pm deleted file mode 100644 index 281fd0a..0000000 --- a/Tools/Module/PDFExtract.pm +++ /dev/null @@ -1,409 +0,0 @@ -package Module::PDFExtract; - -use strict; -use warnings; -use parent qw(Plack::Component); -use Plack::Request; -use File::Basename; -use Data::Dumper; -use PDF::API2; -use File::Path qw/make_path/; - -sub call { - my($self, $env) = @_; - #$self->_app->($env); - my $html->{result} = "unknown function"; - if (($env->{REMOTE_ADDR} =~ "^127\.0\.") && - ($env->{REMOTE_ADDR} =~ "^10\.") && - ($env->{REMOTE_ADDR} =~ "^172\.16\.") && - ($env->{REMOTE_ADDR} =~ "^192\.168\.")) { - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Sorry no remote access allowed!" ] - ]; - } - if ($env->{PATH_INFO} =~ /^\/pdfsplit/){ - return $self->pdfsplit($env); - } - if ($env->{PATH_INFO} =~ /^\/pdfpagenumbers/){ - return $self->pdfpagesnumbers($env); - } -# if ($env->{PATH_INFO} =~ /^\/pdfextract/){ -# return $self->pdfextract($env); -# } - if ($env->{PATH_INFO} =~ /^\/parsedata/){ - return $self->parsedata($env); - } - if ($env->{PATH_INFO} =~ /^\/parsestatement/){ - return $self->parsestatement($env); - } - return [ - 404, - [ 'Content-Type' => 'text/html','Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ "unknown function" ] - ]; -} - -sub pdfpagesnumbers(){ - my $self = shift; - my $env = shift; - my $ct="application/json"; - my $status=200; - my $req = Plack::Request->new($env); - my $html->{result}->{pagenumbers} = 0; - if (exists($req->query_parameters->{file}) && ($req->query_parameters->{file} =~ /\.pdf$/)){ - my $pdf = PDF::API2->open($req->query_parameters->{file}); - $html->{result}->{pagenumbers} = $pdf->pages; - } - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub pdfsplit(){ - my $self = shift; - my $env = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - my $req = Plack::Request->new($env); - my @nfiles = (); - my $outputdir = $ENV{TEMP}; - if (exists($req->query_parameters->{file}) && exists($req->query_parameters->{prefix})){ - my $basepdf = basename($req->query_parameters->{file}); - $outputdir =~ s/\\/\//g; - - my $oldpdf = PDF::API2->open($req->query_parameters->{file}); - my $xx = $oldpdf->pages; - for my $page_nb (1..$xx) { - my $newpdf = PDF::API2->new; - my $page = $newpdf->importpage($oldpdf, $page_nb); - - my $npdfname = $outputdir.'/'.$req->query_parameters->{prefix}.substr($basepdf,0,-4).".".$page_nb.".pdf"; - push @nfiles,$npdfname; - if (-e $npdfname){ unlink($npdfname); } - $newpdf->saveas($npdfname); - } - } - foreach my $n (@nfiles){ - my $r = $self->pdfextract($n); - } - $html->{result}->{files} = \@nfiles; - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ JSON::PP::encode_json($html) ] - ]; -}; - -sub pdfextract(){ - my $self = shift; - my $file = shift; -# my $html->{result} = (); -# my $ct="application/json"; -# my $status=200; -# my $req = Plack::Request->new($env); - my $pdftotext; - my $sep = "/"; - if ($^O eq "MSWin32") { - $sep = "\\"; - $pdftotext=dirname($0).$sep.'pdftotext.exe'; - }else { - $pdftotext=dirname($0).$sep.'pdftotext'; - } - if (-e $file.'.txt'){ - unlink($file.'.txt'); - } - my $cmd = 'start /b "" "'.$pdftotext.'" -q -table -eol unix "'.$file.'" "'.$file.'.txt"'; - my $st = `$cmd`;#'system(1,$cmd)' ; - #print $cmd."->".$st."\n"; - return $st; -} - -sub parsedata(){ - my $self = shift; - my $env = shift; - my $req = Plack::Request->new($env); - if (exists($req->query_parameters->{type}) && exists($req->query_parameters->{file})){ - if ($req->query_parameters->{type} eq "inv"){ - return $self->parseinvoice($req->query_parameters->{file}); - } elsif ($req->query_parameters->{type} eq "invold"){ - return $self->parseoldinvoice($req->query_parameters->{file}); - } elsif ($req->query_parameters->{type} eq "stmt") { - return $self->parsestatement($req->query_parameters->{file}); - } else { - return [ - 404, - [ 'Content-Type' => 'text/html','Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ "unknown function" ] - ]; - } - } -} - -sub parseinvoice(){ - my $self = shift; - my $file = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - my $pxdata = (); - if (-e $file){ - my @invoicedata = (); - open(EXT,$file); - while (my $l = ){ - chomp($l); - push(@invoicedata,$l); - } - close(EXT); - foreach my $p (@invoicedata){ - if ($p =~ /^N. Facture/) { - my ($tmp) = $p =~ m/.+\s(\d{4,}.\d{1,2}.\d{4,})\s.+$/; - $pxdata->{reference} = $tmp; - } - if ($p =~ /^Date de la facture/) { - my ($d,$m,$y) = $p =~ m/.+\s(\d{1,2}).(\d{1,2}).(\d{4,}).+$/; - if (length($d) == 1) { $d = "0".$d;} - if (length($m) == 1) { $m = "0".$m;} - $pxdata->{invoicedate} = $y.'-'.$m.'-'.$d; - } -# if (($p =~ /facture/) && ($pxdata->{invoicedate} eq "--")) { -# my ($d,$m,$y) = $p =~ m/.+\s(\d{1,2}).(\d{1,2}).(\d{4,})$/; -# if (length($d) == 1) { $d = "0".$d;} -# if (length($m) == 1) { $m = "0".$m;} -# $pxdata->{invoicedate} = $y.'-'.$m.'-'.$d; -# } - if ($p =~ /^Enfant/) { - my ($tmp) = $p =~ m/.+\s\((\d+)\).+$/; - $pxdata->{checkservice} = $tmp; - } - if (($p =~ /^\s+\(\d+\)$/) && (!defined($pxdata->{checkservice}))) { - my ($tmp) = $p =~ m/\s+\((\d+)\)$/; - $pxdata->{checkservice} = $tmp; - } - if ($p =~ /Heures.+\sh\s/) { - my ($hrs,$p1,$e1) = $p =~ m/.+Heures.+\s+([\s|\d]+,\d{1,2})\sh\s+([\s|\d]+,\d{1,2})\s+([\s|\d]+,\d{1,2}).+$/; - $p1 =~ s/,/\./; - $e1 =~ s/,/\./; - $p1 =~ s/\ //; - $e1 =~ s/\ //; - if (exists($pxdata->{hoursamount})){ - $pxdata->{hoursamount} = $pxdata->{hoursamount} + $p1 + $e1; - } else { - $pxdata->{hoursamount} = $p1 + $e1; - } - - } - if ($p =~ /Repas/) { - my ($rn,$p1,$e1) = $p =~ m/.+Repas.+\s+(\d+)\s+([\s|\d]+,\d{1,2})\s+([\s|\d]+,\d{1,2}).+$/; - $p1 =~ s/,/\./; - $e1 =~ s/,/\./; - $p1 =~ s/\ //; - $e1 =~ s/\ //; - $pxdata->{lunchnum} = $rn; - $pxdata->{lunchamount} = $p1 + $e1; - } - if ($p =~ /Participation totale de l.Etat/){ - my ($e1) = $p =~ m/.+Participation totale de l.Etat\s+([\s|\d]+,\d{1,2}).+$/; - $e1 =~ s/,/\./; - $e1 =~ s/\ //; - $pxdata->{benefitamount} = $e1; - } - if ($p =~ /Montant\s.\sr.gler/) { - my ($m1) = $p =~ m/.+Montant.+\s+([\s|\d]+,\d{1,2}).+$/; - $m1 =~ s/,/\./; - $m1 =~ s/\ //; - $pxdata->{totalamount} = $m1; - } - #print Dumper(@pdata); - } - } - $html->{result} = $pxdata; - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub parseoldinvoice(){ - my $self = shift; - my $file = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - my $pxdata = (); - if (-e $file){ - my @invoicedata = (); - open(EXT,$file); - while (my $l = ){ - chomp($l); - push(@invoicedata,$l); - } - close(EXT); - foreach my $p (@invoicedata){ - if ($p =~ /N. Facture/) { - my ($tmp) = $p =~ m/.+\s(\d{4,}.\d{1,2}.\d{4,})\s.+$/; - $pxdata->{reference} = $tmp; - } - if ($p =~ /Date de la/) { - my ($d,$m,$y) = $p =~ m/.+\s(\d{1,2}).(\d{1,2}).(\d{4,})$/; - if (length($d) == 1) { $d = "0".$d;} - if (length($m) == 1) { $m = "0".$m;} - $pxdata->{invoicedate} = $y.'-'.$m.'-'.$d; - } - if (($p =~ /facture/) && ($pxdata->{invoicedate} eq "--")) { - my ($d,$m,$y) = $p =~ m/.+\s(\d{1,2}).(\d{1,2}).(\d{4,})$/; - if (length($d) == 1) { $d = "0".$d;} - if (length($m) == 1) { $m = "0".$m;} - $pxdata->{invoicedate} = $y.'-'.$m.'-'.$d; - } - if ($p =~ /Carte N./) { - my ($tmp) = $p =~ m/.+\s(\d+)$/; - $pxdata->{checkservice} = $tmp; - } - if ($p =~ /Heure/) { - my ($tmp1,$tmp2) = $p =~ m/.+\s(\d+).(\d+)+$/; - $pxdata->{hoursamount} = $tmp1.'.'.$tmp2; - } - if ($p =~ /Repas/) { - my ($tmp1,$tmp2) = $p =~ m/.+\s(\d+).(\d+)+$/; - $pxdata->{lunchamount} = $tmp1.'.'.$tmp2; - } - if ($p =~ /Montant\s.\spayer/) { - my ($tmp1,$tmp2) = $p =~ m/.+\s(\d+).(\d+)+$/; - $pxdata->{totalamount} = $tmp1.'.'.$tmp2; - } - #print Dumper(@pdata); - } - } - $html->{result} = $pxdata; - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub parsestatement(){ - my $self = shift; - my $file = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - my $sxdata = (); - my $cmonth = "none"; - my $frmonth = {"Janvier" => '01',"Février"=> '02',"Mars" => '03',"Avril" => '04', "Mai" => '05',"Juin" => '06',"Juillet" => '07',"Août" => '08',"Septembre" => '09',"Octobre" => '10',"Novembre" => '11',"Décembre" => '12'}; - if (-e $file){ - my @xstmtdata = (); - open(EXT,$file); - while (my $l = ){ - chomp($l); - push(@xstmtdata,$l); - } - close(EXT); - my $sxdata = (); - - foreach my $p (@xstmtdata){ - if ($p =~ /P.riode/) { - my ($m1,$y1,$m2,$y2) = $p =~ m/.+\s(.+)\s+(\d+)\s+.\s+(.+)\s+(\d+)$/; - if (($m1 eq $m2) && ($y1 eq $y2)){ - $cmonth=$y1.'-'.$frmonth->{$m1}; - } - } - if ($p =~ /\d{13,}/) { - my ($csnum,$am) = $p =~ m/.+\s+(\d{13,})\s+([\d|\ |,]+)$/; - $am =~ s/\s+//; - $am =~ s/,/./; - $sxdata->{$cmonth}->{$csnum}=$am; - } - } - } - $html->{result} = $sxdata; - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ JSON::PP::encode_json($html) ] - ]; -} - - -# -#sub importstatementdata(){ -# my $simpdata = shift; -# my $fname = shift; -# #print Dumper($simpdata); -# my $n=0; -# foreach my $m (keys(%{$simpdata})){ -# $n++; -# if (-e dirname($fname).$sep."prestation.".$m."-".$n.".pdf"){ -# unlink(dirname($fname).$sep."prestation.".$m."-".$n.".pdf"); -# } -# rename($fname,dirname($fname).$sep."prestation.".$m."-".$n.".pdf"); -# foreach my $csnum (keys(%{$simpdata->{$m}})){ -# $simpdata->{$m}->{fnum} = $n; -# } -# } -# foreach my $m (keys(%{$simpdata})){ -# $n++; -# if ($m =~ /\d{4,}-\d{2,}/) { -# foreach my $csnum (keys(%{$simpdata->{$m}})){ -# print "Import Check-Service no: " + $csnum + "\n"; -# if (defined($db)){ -# my $child = $db->dbquerysorted("select uuid from childs where replace(checkservicenumber,' ','') = '".$csnum."';"); -# if (keys(%{$child}) == 1) { -# my $accdata = $db->dbquerysorted("select accmonth,childuuid from accounting where childuuid='".$child->{0}->{uuid}."' and accmonth=date('".substr($m,0,4).'-'.substr($m,5,2)."-01');"); -# if (keys(%{$accdata}) == 1) { -# #make update -# my @upd = (); -# push @upd,"benefitamount='".$simpdata->{$m}->{$csnum}."'"; -# push @upd,"benefitfile='prestation.".$m."-".$simpdata->{$m}->{fnum}.".pdf'"; -# my $sql = "update accounting set ".join(',',@upd)." where childuuid='".$child->{0}->{uuid}."' and accmonth=date('".substr($m,0,4).'-'.substr($m,5,2)."-01');"; -# #print $sql."\n"; -# my $r = $db->dbexec($sql); -# if (($log ne "") && (-e $log)){ -# if (!defined($r)) { -# open(LOG,">>".$log); -# print LOG localtime().":ERROR:".$sql."\n"; -# close(LOG); -# } else { -# open(LOG,">>".$log); -# print LOG localtime().":SUCCESS:".$sql."\n"; -# close(LOG); -# } -# } -# }else { -# my @ins1 = (); -# my @ins2 = (); -# push(@ins1,"accmonth");push (@ins2,"date('".substr($m,0,4).'-'.substr($m,5,2)."-01')"); -# push(@ins1,"childuuid");push (@ins2,"'".$child->{0}->{uuid}."'"); -# push(@ins1,"benefitamount");push (@ins2,"".$simpdata->{$m}->{$csnum}.""); -# push(@ins1,"benefitfile");push (@ins2,"'prestation.".$m."-".$simpdata->{$m}->{fnum}.".pdf'"); -# -# #accmonth,childuuid,invoicedate,invoiceamount,reference -# my $sql = "insert into accounting (".join(',',@ins1).") VALUES (".join(',',@ins2).");"; -# #print $sql."\n"; -# my $r = $db->dbexec($sql); -# if (($log ne "") && (-e $log)){ -# if (!defined($r)) { -# open(LOG,">>".$log); -# print LOG localtime().":ERROR:".$sql."\n"; -# close(LOG); -# } else { -# open(LOG,">>".$log); -# print LOG localtime().":SUCCESS:".$sql."\n"; -# close(LOG); -# } -# } -# } -# } -# } -# } -# } -# } -#} - -1; \ No newline at end of file diff --git a/Tools/Module/SQLite.pm b/Tools/Module/SQLite.pm deleted file mode 100644 index 2e17a56..0000000 --- a/Tools/Module/SQLite.pm +++ /dev/null @@ -1,381 +0,0 @@ -package Module::SQLite; - -use strict; -use warnings; -use parent qw(Plack::Component); -use Plack::Request; -#use Data::Dumper; -use DBI; -use DBD::SQLite; -use Encode; -use JSON::PP; -use File::Copy; - -sub call { - my($self, $env) = @_; - if (($env->{REMOTE_ADDR} =~ "^127\.0\.") && - ($env->{REMOTE_ADDR} =~ "^10\.") && - ($env->{REMOTE_ADDR} =~ "^172\.16\.") && - ($env->{REMOTE_ADDR} =~ "^192\.168\.")) { - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Sorry no remote access allowed!" ] - ]; - } - if ($env->{PATH_INFO} =~ /^\/createdb/){ - return $self->createdb($env); - } elsif ($env->{PATH_INFO} =~ /^\/checkdb/){ - return $self->checkdb($env); - }else { - return $self->sqlite($env); - } - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "no such function!" ] - ]; -} - -sub sqlite { - my $self = shift; - my $env = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - my $req = Plack::Request->new($env); - my $res = (); - #print $req->query_parameters->{db}.":".$req->query_parameters->{type}.":".decode_base64($req->query_parameters->{sql})."\n------------------\n"; - $html->{req}->{db} = $req->query_parameters->{db}; - $html->{req}->{type} = $req->query_parameters->{type}; - $html->{req}->{sql} = $req->query_parameters->{sql}; - #$html->{req}->{sqldecoded} = $req->query_parameters->{sql}; - if (( -f $req->query_parameters->{db} ) && (exists($req->query_parameters->{sql})) && (exists($req->query_parameters->{type})) ) { - - $self->{dbfile} = $req->query_parameters->{db}; - #my $db = sqlite->new(); - my $q = $req->query_parameters->{sql}; - my $t = $req->query_parameters->{type}; - if ($t eq "query"){ - $res = $self->dbquery($req->query_parameters->{key},$q); - } elsif ($t eq "querysorted"){ - $res = $self->dbquerysorted($q); - } elsif ($t eq "queryarray"){ - $res = $self->dbqueryarray($q); - } elsif ($t eq "exec"){ - $res = $self->dbexec($q); - } - - } - $html->{result}->{sqldata} = $res; - return [ - 200, - [ 'Content-Type' => $ct.'; charset=utf-8','Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -}; - - - -sub strreplace(){ - my $self = shift; - my $text = shift; - $text =~ s/'/''/g; - return $text; -} - -sub dbquery(){ - my $self = shift; - my $key = shift; - my $stat = shift; - my $retdata =(); - my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or return $retdata->{error} = "dbquery Connection Error!".$!; - #$stat = encode("utf8", $stat); - - #open FILE,">>/tmp/sql.log"; - # print FILE "key:".$key.";$stat\n"; - # close FILE; - my $sth = $dbh->prepare($stat); - $sth->execute() or print "dbquery: ".$sth->errstr; - while(my $data = $sth->fetchrow_hashref()) - { - if (exists $data->{$key}){ - foreach my $k (keys %{$data}){ - $retdata->{$data->{$key}}{$k} = decode( "utf8", $data->{$k}); - } - } - } - if (keys(%{$retdata}) == 0){ - $retdata =(); - } - $sth->finish(); - $dbh->disconnect(); - return $retdata; -} - -sub dbquerysorted(){ - my $self = shift; - my $stat = shift; - my $retdata = (); - my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or return $retdata->{error} = "dbquery Connection Error!".$!; - #$stat = encode("utf8", $stat); - #open FILE,">>/tmp/sql.log"; - #print "$stat\n"; - # close FILE; - my $sth = $dbh->prepare($stat); - - $sth->execute() or print "dbquery: ".$sth->errstr; - my $count = 0; - while(my $data = $sth->fetchrow_hashref()) - { - foreach my $k (keys %{$data}){ - $retdata->{$count}->{$k} = decode( "utf8", $data->{$k}); - } - $count++; - } - - $sth->finish(); - $dbh->disconnect(); - #%retdata = sort {$a <=> $b} keys %retdata; - return $retdata; -} - -sub dbqueryarray(){ - my $self = shift; - my $stat = shift; - my @retdata = (); - my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or return $retdata[0]->{error} = "dbquery Connection Error!".$!; - #$stat = encode("utf8", $stat); - #open FILE,">>/tmp/sql.log"; - #print "$stat\n"; - # close FILE; - my $sth = $dbh->prepare($stat); - - $sth->execute() or print "dbquery: ".$sth->errstr; - my $count = 0; - - while(my $valdata = $sth->fetchrow_arrayref()) - { - if (!defined($valdata)){ last;} - my @rdata = (); - foreach my $k (@{$valdata}){ - push @rdata,decode( "utf8", $k); - } - push @retdata,\@rdata; - } - - $sth->finish(); - $dbh->disconnect(); - #%retdata = sort {$a <=> $b} keys %retdata; - return \@retdata; -} - -sub dbexec(){ - my $self = shift; - my $stat = shift; - my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,AutoCommit=>1}) or return "dbexec Connection Error!".$!; - #$stat = encode("utf8", $stat); - #print $stat."\n"; - #open FILE,">>/Users/kilian/sql.log"; - #print FILE "$stat\n"; - #close FILE; - my $sth = $dbh->prepare($stat); - my $rv =$dbh->do($stat) or print "Failed dbexec:\n'".$stat. "'\n\n"; - $dbh->disconnect(); - return $rv; -} - -sub createdb(){ - my $self = shift; - my $env = shift; - my $html->{result} = 0; - my $ct="application/json"; - my $status=200; - my $req = Plack::Request->new($env); - if (exists($req->query_parameters->{templatedb}) && exists($req->query_parameters->{newdb}) && (-f $req->query_parameters->{templatedb})){ - my $r = copy($req->query_parameters->{templatedb},$req->query_parameters->{newdb}); - $html->{result} = $r; - } - return [ - 200, - [ 'Content-Type' => $ct.'; charset=utf-8','Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub checkdb(){ - my $self = shift; - my $env = shift; - my $html->{result} = 1; - my $ct="application/json"; - my $status=200; - my $req = Plack::Request->new($env); - if (exists($req->query_parameters->{templatedb}) && exists($req->query_parameters->{db}) && (-f $req->query_parameters->{templatedb}) && (-f $req->query_parameters->{db})){ - my $templatedb = $req->query_parameters->{templatedb}; - my $dborig = $req->query_parameters->{templatedb}; - $self->{dbfile} = $templatedb; - my $dbdefsql = "SELECT type, name,tbl_name,sql FROM sqlite_master order by name,tbl_name,type;"; - my $defdbschemacfg = $self->dbquerysorted($dbdefsql); - $self->{dbfile} = $dborig; - my $tcurcfg = $self->dbquerysorted($dbdefsql); - my $keycnt = keys(%{$defdbschemacfg}); - my $bvaccum = 0; - my $stexec = 0; - foreach my $pd (sort {$a <=> $b} keys(%{$defdbschemacfg})){ - if ($defdbschemacfg->{$pd}->{'type'} eq 'table') { - my $bupdate = 0; - my $bexists = 0; - my $cucols = ''; - my $oldobj = (); - foreach my $pc (keys(%{$tcurcfg})){ - if (($tcurcfg->{$pc}->{tbl_name} eq $defdbschemacfg->{$pd}->{tbl_name}) && ($tcurcfg->{$pc}->{type} eq $defdbschemacfg->{$pd}->{type})){ - #print $defdbschemacfg->{$pd}->{type}.": ".$defdbschemacfg->{$pd}->{tbl_name}. "\n"; - if ($tcurcfg->{$pc}->{sql} ne $defdbschemacfg->{$pd}->{sql}){ $bupdate = 1; $oldobj= $self->getcoldef($tcurcfg->{$pc}->{sql}); } - $bexists = 1; last; - } - } - if (($bexists==1) && ($bupdate== 1)){ - my $sql_installnew = $defdbschemacfg->{$pd}->{sql}; - my $newobj = $self->getcoldef($defdbschemacfg->{$pd}->{sql}); - my @copycols = (); - for my $x (keys(%{$newobj})){ - if (exists($oldobj->{$x})) { push @copycols,$x; } - } - my @ainssql = (); - push(@ainssql,"DROP TABLE IF EXISTS new_".$defdbschemacfg->{$pd}->{tbl_name}. ";"); - my $sql_tmptbl = $sql_installnew; - $sql_tmptbl =~ s/CREATE\ TABLE\ /CREATE TABLE new_/; - $sql_tmptbl =~ s/"//g; - push(@ainssql,$sql_tmptbl); - push(@ainssql,"INSERT INTO new_".$defdbschemacfg->{$pd}->{tbl_name}." (".join(',',@copycols).") SELECT ".join(',',@copycols)." FROM ".$defdbschemacfg->{$pd}->{tbl_name}.";"); - push(@ainssql,"DROP TABLE ".$defdbschemacfg->{$pd}->{tbl_name}.";"); - push(@ainssql,"ALTER TABLE new_".$defdbschemacfg->{$pd}->{tbl_name}. " RENAME TO ".$defdbschemacfg->{$pd}->{tbl_name}.";"); - $bvaccum = 1; - #print Dumper(@ainssql); - my $stexec = 0; - for(my $s=0;$sdbexec($ainssql[$s]); } - } - #print "tbl done\n"; - } - elsif ($bexists == 0){ - my $sql_installnew = $defdbschemacfg->{$pd}->{sql}; - $self->dbexec($sql_installnew); - } - } elsif (($defdbschemacfg->{$pd}->{'type'} eq 'trigger') || ($defdbschemacfg->{$pd}->{'type'} eq 'index')) { - my $bexists = 0; my $bupdate = 0; - foreach my $pc (keys(%{$tcurcfg})){ - if (($tcurcfg->{$pc}->{tbl_name} eq $defdbschemacfg->{$pd}->{tbl_name}) && ($tcurcfg->{$pc}->{type} eq $defdbschemacfg->{$pd}->{type})){ - # print $defdbschemacfg->{$pd}->{type}.": ".$defdbschemacfg->{$pd}->{tbl_name}. "\n"; - if ($tcurcfg->{$pc}->{sql} ne $defdbschemacfg->{$pd}->{sql}){ $bupdate = 1; } - $bexists = 1; last; - } - } - if (($bexists==1) && ($bupdate== 1)){ - $bvaccum = 1; - my @ainssql = (); - if ($defdbschemacfg->{$pd}->{type} eq 'trigger'){ - push @ainssql,"DROP TRIGGER IF EXISTS ".$defdbschemacfg->{$pd}->{'name'}.";"; - } elsif ($defdbschemacfg->{$pd}->{type} eq 'index') { - push @ainssql,"DROP INDEX IF EXISTS ".$defdbschemacfg->{$pd}->{'name'}.";"; - } - push @ainssql,$defdbschemacfg->{$pd}->{'sql'}; - my $stexec = 0; - for(my $s=0;$sdbexec($ainssql[$s]); } - } - }elsif ($bexists == 0) { - my $sql_installnew = $defdbschemacfg->{$pd}->{sql}; - $self->dbexec($sql_installnew); #Test check - } - } - } - if ($bvaccum == 1) { $self->dbexec("vacuum;"); } - } - return [ - 200, - [ 'Content-Type' => $ct.'; charset=utf-8','Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - - -sub getcoldef($){ - my $self = shift; - my $strddl = shift; - my $curddl = $strddl; - $curddl =~ s/\s+/\ /g; - my $bi = index($curddl,'(')+1; - my $ei = rindex($curddl,')'); - - $curddl = substr($curddl,$bi,length($curddl)-$bi-(length($curddl)-$ei));#curddl.substring(curddl.indexOf('(')+1,curddl.lastIndexOf(')')).trim().replace(/\s+/g," "); - my @colsfull = split(/,/,$curddl);# curddl =curddl.replace(new RegExp("\\b(" + appdb.keywords.join("|") + ")\\b", "g"), ""); - my $tblobj = (); - foreach my $c (@colsfull){ - $c =~ s/^\s+//; - $c =~ s/\s+$//; - my @coldef = split(/\ /,$c); - my $type = uc($coldef[1]); - if (($type =~ /^TEXT/) || ($type =~ /^REAL/) || ($type =~ /^INTEGER/) || ($type =~ /^BOOLEAN/) || ($type =~ /^DATE/) || ($type =~ /^DATETIME/)) { - $tblobj->{$coldef[0]} = $type; - } - } - return $tblobj; -} - -#sub dbbackup(){ -# my $self = shift; -# my $path = shift; -# my $type = shift; -# -# my @dx = localtime(); -# $dx[5] = $dx[5] +1900; -# $dx[4] = $dx[4] +1; -# if ($dx[4] < 10){$dx[4] = '0'.$dx[4];} -# if ($dx[3] < 10){$dx[3] = '0'.$dx[3];} -# if ($dx[2] < 10){$dx[2] = '0'.$dx[2];} -# if ($dx[1] < 10){$dx[1] = '0'.$dx[1];} -# if ($dx[0] < 10){$dx[0] = '0'.$dx[0];} -# my $xdd = $dx[5].$dx[4].$dx[3].'_'.$dx[2].$dx[1].$dx[0]; -# my $bfile = ""; -# if ($type eq "binary" ) { -# $bfile = $path.'/'.basename(substr($self->{dbfile},0,rindex($self->{dbfile},'.'))).'_'.$xdd.'.sqlite'; -# my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or die "dbexec Connection Error!".$!; -# $dbh->sqlite_backup_to_file($bfile); -# $dbh->disconnect(); -# }elsif($type eq "sql"){ -# $bfile = $path.'/'.basename($self->{dbfile}).'_'.$xdd.'.sql'; -# my $st = system('sqlite3 "'.$self->{dbfile}.'" ".dump" > '.$bfile); -# } -# return $bfile; -#} -# -#sub dbrestore(){ -# my $self = shift; -# my $file = shift; -# my $type = shift; -# if ($type eq "binary" ) { -# my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or die "dbexec Connection Error!".$!; -# $dbh->sqlite_backup_from_file($file); -# $dbh->disconnect(); -# }elsif($type eq "sql"){ -# open(REST,$file) or die "cannot open restore file $file!\n"; -# my $rsql = ""; -# while (my $l = ) { -# $rsql .= $l; -# } -# close(REST); -# unlink($self->{dbfile}); -# $self->dbexec($rsql); -# } -#} -# -#sub dbrepair(){ -# my $self = shift; -# my $bfile = $self->dbbackup($ENV{'TMPDIR'},'sql'); -# $self->dbrestore($bfile,'sql'); -# unlink($bfile); -#} - - -1; \ No newline at end of file diff --git a/Tools/Module/Service.pm b/Tools/Module/Service.pm deleted file mode 100644 index 5537893..0000000 --- a/Tools/Module/Service.pm +++ /dev/null @@ -1,155 +0,0 @@ -package Module::Service; - -use strict; -use warnings; -use File::Path qw(make_path); -use File::Basename; -use parent qw(Plack::Component); - - -sub call { - my($self, $env) = @_; - if (($env->{REMOTE_ADDR} =~ "^127\.0\.") && - ($env->{REMOTE_ADDR} =~ "^10\.") && - ($env->{REMOTE_ADDR} =~ "^172\.16\.") && - ($env->{REMOTE_ADDR} =~ "^192\.168\.")) { - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Sorry no access allowed!" ] - ]; - } - return $self->service($env); -} - -sub service() { - my $self = shift; - my $env = shift; - my $html = "Unknown service!"; - my $ct="application/json"; - my $status=200; - - if ($env->{PATH_INFO} =~ /^\/info/){ - return $self->appinfo($env); - } - if ($env->{PATH_INFO} =~ /^\/preferences/){ - return $self->preferences($env); - } - if (($env->{PATH_INFO} =~ /^\/stop/) || ($env->{PATH_INFO} =~ /^\/unload/)){ - exit(0); - } - if ($env->{PATH_INFO} =~ /^\/shutdown/){ - system('sudo shudown -h now'); - $html = "shutdown launched"; - } - if ($env->{PATH_INFO} =~ /^\/restart/){ - system('sudo shudown -r now'); - $html = "restart launched"; - } - - return [ - 200, - [ 'Content-Type' => 'text/html','Cache-Control' => 'no-store, no-cache, must-revalidate' , 'Access-Control-Allow-Origin'=> '*'], - [ $html ] - ]; -}; - -sub preferences(){ - my $self = shift; - my $env =shift; - my $name = basename($0); - $name =~ s/srv\.pl$//; - $name =~ s/srv\.exe$//; - my $appcfgpath = ""; - if ($^O eq "MSWin32"){ - $appcfgpath = $ENV{APPDATA}.'/'.$name; - } elsif ($^O eq "darwin"){ - $appcfgpath = $ENV{HOME}.'/Library/Application Support/'.$name; - } - - $appcfgpath =~ s/\\/\//g; - my $pref->{result}= (); - my $req = Plack::Request->new($env); - if (exists($req->query_parameters->{page})){ - if (-e $appcfgpath.'/'.$req->query_parameters->{page}.'.json'){ - open(PREF,$appcfgpath.'/'.$req->query_parameters->{page}.'.json'); - my $strpref = ""; - while (my $l = ){ - $strpref .= $l; - } - close(PREF); - $pref->{result}=JSON::PP::decode_json($strpref); - } - if (exists($req->query_parameters->{set})){ - my $newpref = JSON::PP::decode_json($req->query_parameters->{set}); - foreach my $p (keys(%{$newpref})){ - $pref->{result}->{$p} = $newpref->{$p}; - } - open(PREF,">".$appcfgpath.'/'.$req->query_parameters->{page}.'.json'); - print PREF JSON::PP::encode_json($pref->{result}); - close(PREF); - } - } - return [ - 200, - [ 'Content-Type' => "application/json",'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($pref) ] - ]; -} - -sub appinfo(){ - my $self = shift; - my $env = shift; - my $html->{result} = (); - my $req = Plack::Request->new($env); - my $name = basename($0); - $name =~ s/srv\.pl$//; - $name =~ s/srv\.exe$//; - $html->{result}->{OS} = $^O; - $html->{result}->{app} = $name; - if ($^O eq "MSWin32"){ - $html->{result}->{home} = $ENV{USERPROFILE}; - $html->{result}->{user} = $ENV{USERNAME}; - $html->{result}->{appcfgpath} = $ENV{APPDATA}.'/'.$name; - $html->{result}->{hostname} = $ENV{COMPUTERNAME}; - $html->{result}->{arch} = $ENV{PROCESSOR_ARCHITEW6432}; - $html->{result}->{appcfgpath} =~ s/\\/\//g; - $html->{result}->{home} =~ s/\\/\//g; - } else { - $html->{result}->{home} = $ENV{HOME}; - $html->{result}->{user} = $ENV{USER}; - if ($^O eq "darwin"){ - $html->{result}->{appcfgpath} = $ENV{HOME}.'/Library/Application Support/'.$name; - } else { - $html->{result}->{appcfgpath} = $ENV{HOME}.'/.'.$name.'/'; - } - $html->{result}->{hostname} = `hostname -s`; - chomp($html->{result}->{hostname}); - $html->{result}->{arch} = `uname -m`; - chomp($html->{result}->{arch}); - } - if (! -e $html->{result}->{appcfgpath}){ - make_path($html->{result}->{appcfgpath}); - } - if (-e $html->{result}->{appcfgpath}.'/service.json'){ - open(LCFG,$html->{result}->{appcfgpath}.'/service.json'); - my $strprofile = ""; - while (my $l = ){ - $strprofile .= $l; - } - close(LCFG); - if ($strprofile ne ""){ - $html->{result}->{appconfig} = JSON::PP::decode_json($strprofile); - } - } - if (!exists($html->{result}->{appconfig})){ - $html->result->{appconfig} = undef; - } - return [ - 200, - [ 'Content-Type' => "application/json",'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -1; \ No newline at end of file diff --git a/Tools/Module/System.pm b/Tools/Module/System.pm deleted file mode 100644 index 7c81e46..0000000 --- a/Tools/Module/System.pm +++ /dev/null @@ -1,394 +0,0 @@ -package Module::System; - -use strict; -use warnings; -use parent qw(Plack::Component); -use Plack::Request; -use Data::Dumper; -use File::Find::Rule; -use File::Basename; -use JSON::PP; -use File::Path qw(make_path remove_tree); -use File::Copy; -use MIME::Types; -if ($^O eq "MSWin32"){ - eval('use Win32::File; use Win32::GUI;'); -} - -sub call { - my($self, $env) = @_; - if (($env->{REMOTE_ADDR} =~ "^127\.0\.") && - ($env->{REMOTE_ADDR} =~ "^10\.") && - ($env->{REMOTE_ADDR} =~ "^172\.16\.") && - ($env->{REMOTE_ADDR} =~ "^192\.168\.")) { - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Sorry no remote access allowed!" ] - ]; - } - if ($env->{PATH_INFO} =~ /^\/search/){ - return $self->search($env); - } elsif ($env->{PATH_INFO} =~ /^\/directory/) { - return $self->directory($env); - } elsif ($env->{PATH_INFO} =~ /^\/file/) { - return $self->file($env); - } elsif ($env->{PATH_INFO} =~ /^\/userenv/){ - return $self->userenv($env); - } - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Unknown System Request!" ] - ]; -} - -sub search() { - my $self = shift; - my $env = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - my $req = Plack::Request->new($env); - my $ff = File::Find::Rule->new; - if (exists($req->query_parameters->{name})){ - my $namesearch = $req->query_parameters->{name}; - $ff->name($req->query_parameters->{name}) - } - if (exists($req->query_parameters->{type})){ - if ($req->query_parameters->{type} eq 'd'){ - $ff->directory; - } else { - $ff->file; - } - } - if (exists($req->query_parameters->{relative})){ - $ff->relative; - } - if (exists($req->query_parameters->{osspec})){ - $ff->canonpath; - } - my @data = $ff->in($req->query_parameters->{path}); - if (exists($req->query_parameters->{sorted})){ - @data = sort {$a cmp $b} @data; - if ($req->query_parameters->{sorted} eq "desc"){ - @data = reverse(@data); - } - } - $html->{result} = \@data; - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub directory() { - my $self = shift; - my $env = shift; - my $html; - my $ct="application/json"; - my $status=200; - - my $req = Plack::Request->new($env); - if ($env->{PATH_INFO} =~ /^\/directory\/list/){ - my $mt = MIME::Types->new(); - $html->{result} = 0; - if (exists($req->query_parameters->{path})){ - my $dir = $req->query_parameters->{path}; - #print $dir."\n"; - $html->{result} = {'path' => $dir}; - if (-d $dir){ - my @dirs = (); - my @files = (); - opendir(LDIR,$dir); - while (my $f = readdir(LDIR)){ - if ($f =~ /^\./){ next; } - - if (-d $dir.'/'.$f){ - my $bok =1 ; - if ($^O eq "MSWin32"){ - eval ('my $attr; - Win32::File::GetAttributes($dir.\'/\'.$f,$attr); - if ($attr & HIDDEN){ - $bok = 0; - }'); - } - if ($bok == 1){ - push(@dirs,$f); - } - - } elsif (-f $dir.'/'.$f) { - my $bok =1 ; - if ($^O eq "MSWin32"){ - eval ('my $attr; - Win32::File::GetAttributes($dir.\'/\'.$f,$attr); - if ($attr & HIDDEN){ - $bok = 0; - }'); - } - if ($bok == 1){ - print $f."\n"; - my $fi->{name} = $f; - my $mtf = $mt->mimeTypeOf($f); - $fi->{mimetype} = (exists($mtf->{MT_simplified})?$mtf->{MT_simplified}:'unknown'); - - push(@files,$fi); - } - } - } - closedir(LDIR); - $html->{result}->{directory} = \@dirs; - $html->{result}->{file} = \@files; - } - } - } - if ($env->{PATH_INFO} =~ /^\/directory\/exists/){ - $html->{result} = 0; - if (exists($req->query_parameters->{path})){ - if (-d $req->query_parameters->{path}){ - $html->{result} = 1; - } - } - } - if ($env->{PATH_INFO} =~ /^\/directory\/make/){ - make_path($req->query_parameters->{path}); - $html->{result} = 0; - if (-d $req->query_parameters->{path}){ - $html->{result} = 1; - } - } - if ($env->{PATH_INFO} =~ /^\/directory\/delete/){ - my $keep_root = 0; - if (exists($req->query_parameters->{keep_root})){ - $keep_root = 1; - } - $html->{result} = 0; - if (-d $req->query_parameters->{path}){ - remove_tree( $req->query_parameters->{path}, {keep_root => $keep_root} ); - $html->{result} = 1; - } - } - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -sub file() { - my $self = shift; - my $env = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - - my $req = Plack::Request->new($env); - if ($env->{PATH_INFO} =~ /^\/file\/exists/){ - $html->{result} = 0; - if (exists($req->query_parameters->{path})){ - if (-f $req->query_parameters->{path}){ - $html->{result} = 1; - } - } - } - if ($env->{PATH_INFO} =~ /^\/file\/write/){ - $html->{result} = 0; - if (exists($req->query_parameters->{path})){ - if (! -d (dirname($req->query_parameters->{path}))){ - make_path(dirname($req->query_parameters->{path})) - } - my $fwrite = ">"; - if (exists($req->query_parameters->{append})){ - $fwrite = ">>"; - } - my $datax = $req->body_parameters->{data}; - print $req->body_parameters->{data}."\n"; - open(WFI,$fwrite.$req->query_parameters->{path}); - print WFI $req->body_parameters->{data}; - close(WFI); - $html->{result} = 1; - } - } - if ($env->{PATH_INFO} =~ /^\/file\/read/){ - $html->{result} = ""; - if (exists($req->query_parameters->{path})){ - if (-f $req->query_parameters->{path}){ - my $rdata = ""; - open(RFI,$req->query_parameters->{path}); - while ( my $l = ){ - $rdata .= $l; - } - close(RFI); - $html->{result} = $rdata; - } - } - } - if ($env->{PATH_INFO} =~ /^\/file\/copy/){ - $html->{result} = ""; - if (exists($req->query_parameters->{src})){ - if (-f $req->query_parameters->{src}){ - my $dest = $req->query_parameters->{dest}; - if (! -d dirname($req->query_parameters->{dest})){ - make_path(dirname($req->query_parameters->{dest})) - } - if ($req->query_parameters->{src} ne $req->query_parameters->{dest}){ - my $cp = copy($req->query_parameters->{src},$req->query_parameters->{dest}); - $html->{result} = $cp; - } else { - $html->{result} = 1; - } - } - } - - } - if ($env->{PATH_INFO} =~ /^\/file\/move/){ - $html->{result} = ""; - if (exists($req->query_parameters->{src})){ - if (-f $req->query_parameters->{src}){ - my $dest = $req->query_parameters->{dest}; - if (! -d dirname($req->query_parameters->{dest})){ - make_path(dirname($req->query_parameters->{dest})) - } - if ($req->query_parameters->{src} ne $req->query_parameters->{dest}){ - my $cp = move($req->query_parameters->{src},$req->query_parameters->{dest}); - $html->{result} = $cp; - } else { - $html->{result} = 0; - } - } - } - - } - if ($env->{PATH_INFO} =~ /^\/file\/dialog/){ - if ($^O eq "MSWin32"){ - my $multisel = 0; - if (exists($req->query_parameters->{multisel})){ - $multisel = $req->query_parameters->{multisel}; - } - my $title = "select file"; - if (exists($req->query_parameters->{title})){ - $title = $req->query_parameters->{title}; - } - my $lastdir = $ENV{USERPROFILE}; - if (exists($req->query_parameters->{dir})){ - $lastdir = $req->query_parameters->{dir} - } - my @filters = ['All Files - *', '*']; - if(exists($req->query_parameters->{filters})){ - my @newfilters = split(',',$req->query_parameters->{filters}); - push(@newfilters,'All Files - *','*'); - $filters[0] = \@newfilters; - } - my ( @file); - my ( @parms ); - push (@parms,-filter => @filters,-directory => $lastdir,-title => $title,-multisel => $multisel,-filemustexist => 1,-pathmustexist => 1); - @file = Win32::GUI::GetOpenFileName ( @parms ); - $html->{result}->{files} = \@file; - } - } - if ($env->{PATH_INFO} =~ /^\/file\/open/){ - if ( (exists($req->query_parameters->{file})) && (-e $req->query_parameters->{file}) ){ - if ($^O eq "MSWin32"){ - my $st = system('start /b "" "'.$req->query_parameters->{file}.'"'); - $html->{result} = $st; - } - } else { - $html->{result} = -1000; - } - - } - if ($env->{PATH_INFO} =~ /^\/file\/delete/){ - if ( (exists($req->query_parameters->{file})) && (-e $req->query_parameters->{file}) ){ - unlink($req->query_parameters->{file}); - $html->{result} = 1; - } else { - $html->{result} = 0; - } - - } - if ($env->{PATH_INFO} =~ /^\/file\/rename/){ - $html->{result} = ""; - if (exists($req->query_parameters->{src})){ - if (-f $req->query_parameters->{src}){ - my $dest = $req->query_parameters->{dest}; - if (! -d dirname($req->query_parameters->{dest})){ - make_path(dirname($req->query_parameters->{dest})) - } - if ($req->query_parameters->{src} ne $req->query_parameters->{dest}){ - my $cp = rename($req->query_parameters->{src},$req->query_parameters->{dest}); - $html->{result} = $cp; - } else { - $html->{result} = 1; - } - } - } - - } - - - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - - -sub userenv() { - my $self = shift; - my $env = shift; - my $html->{result} = (); - my $ct="application/json"; - my $status=200; - - my $req = Plack::Request->new($env); - foreach my $k (keys(%ENV)){ - $html->{result}->{$k} = $ENV{$k}; - } - - return [ - 200, - [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], - [ JSON::PP::encode_json($html) ] - ]; -} - -#sub filedialog() { -# my $self = shift; -# my $env = shift; -# my $html->{result} = (); -# my $ct="application/json"; -# my $status=200; -# my $req = Plack::Request->new($env); -# -# if ($^O eq "MSWin32"){ -# my $multisel = 0; -# if (exists($req->query_parameters->{multisel})){ -# $multisel = $req->query_parameters->{multisel}; -# } -# my $title = "select file"; -# if (exists($req->query_parameters->{title})){ -# $title = $req->query_parameters->{title}; -# } -# my $lastdir = $ENV{USERPROFILE}; -# if (exists($req->query_parameters->{dir})){ -# $lastdir = $req->query_parameters->{dir} -# } -# my @filters = ['All Files - *', '*']; -# if(exists($req->query_parameters->{filters})){ -# unshift(@filters,$req->query_parameters->{filters}); -# } -# my ( @file); -# my ( @parms ); -# push (@parms,-filter => @filters,-directory => $lastdir,-title => $title,-multisel => $multisel,-filemustexist => 1,-pathmustexist => 1,); -# @file = Win32::GUI::GetOpenFileName ( @parms ); -# $html->{result}->{files} = \@file; -# } -# return [ -# 200, -# [ 'Content-Type' => $ct,'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Access-Control-Allow-Origin'=> '*' ], -# [ JSON::PP::encode_json($html) ] -# ]; -#} -1; \ No newline at end of file diff --git a/Tools/Module/Test.pm b/Tools/Module/Test.pm deleted file mode 100644 index f5e8c5f..0000000 --- a/Tools/Module/Test.pm +++ /dev/null @@ -1,53 +0,0 @@ -package Module::Test; - -use strict; -use warnings; -use parent qw(Plack::Component); -use Plack::Request; -use Data::Dumper; - -sub call { - my($self, $env) = @_; - #$self->_app->($env); - if (($env->{REMOTE_ADDR} =~ "^127\.0\.") && - ($env->{REMOTE_ADDR} =~ "^10\.") && - ($env->{REMOTE_ADDR} =~ "^172\.16\.") && - ($env->{REMOTE_ADDR} =~ "^192\.168\.")) { - return [ - 404, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ "Sorry no remote access allowed!" ] - ]; - } - return $self->test($env); -} - -sub test(){ - my $self = shift; - my $env = shift; - my $html = ""; - my $ct="application/json"; - my $status=200; - my $request = Plack::Request->new($env); - $html .= "

System Environement

"; - foreach my $k (keys(%ENV)){ - $html.= ''.$k.':'.$ENV{$k}."
\n"; - } - $html .= "

Request Header

"; - foreach my $k (keys(%{$env})){ - $html .= ''.$k.':'.$env->{$k}."
\n"; - } - $html .= "

GET PARAMETERS

"; - $html .= Dumper($request->query_parameters); - $html .= "

POST PARAMETERS

"; - $html .= Dumper($request->body_parameters); - print "Test Called!\n"; - return [ - 200, - [ 'Content-Type' => "text/html",'Cache-Control' => 'no-store, no-cache, must-revalidate' ], - [ $html ] - ]; -}; - - -1; \ No newline at end of file