#!/usr/bin/perl -Tw # # LMS version 1.8.1 Shyla _ moded by ReYu # # (C) 2001-2006 LMS Developers # # Please, see the doc/AUTHORS for more information about authors! # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License Version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA. # # $Id: lms-cashimport-Inteligo,v 1.1.2.2 2006/01/16 09:35:17 reyu Exp $ use strict; use DBI; use Config::IniFiles; use Getopt::Long; use HTTP::Cookies; use HTTP::Request::Common qw(POST GET); use Crypt::SSLeay; use LWP::UserAgent; use POSIX; use POSIX qw(mktime strftime); use HTML::TokeParser; use Time::Local; use vars qw($configfile $quiet $help $version); use Digest::MD5; use Text::Iconv; my $_version = '1.8.1 Shyla moded by ReYu'; my %options = ( "--config-file|C=s" => \$configfile, "--quiet|q" => \$quiet, "--help|h" => \$help, "--version|v" => \$version, ); Getopt::Long::config("no_ignore_case"); GetOptions(%options); if($help) { print STDERR < $configfile; print @Config::IniFiles::errors; my $dbtype = $ini->val('database', 'type') || 'mysql'; my $dbhost = $ini->val('database', 'host') || 'localhost'; my $dbuser = $ini->val('database', 'user') || 'root'; my $dbpasswd = $ini->val('database', 'password') || ''; my $dbname = $ini->val('database', 'database') || 'lms'; my $login_i = $ini->val('cashimport_inteligo', 'id') || 'identyfikator'; my $passwd_i = $ini->val('cashimport_inteligo', 'pass') || 'haslo'; my $konto = $ini->val('cashimport_inteligo', 'account') || 'konto'; my $time_y = $ini->val('cashimport_inteligo', 'wyciag_r') || strftime "%Y", localtime(time-86400); my $time_m = $ini->val('cashimport_inteligo', 'wyciag_m') || strftime "%m", localtime(time-86400); my $time_d = $ini->val('cashimport_inteligo', 'wyciag_d') || strftime "%d", localtime(time-86400); my $trans_type = $ini->val('cashimport_inteligo', 'trans_type') || 'CREDIT'; my $file_out_dir = $ini->val('cashimport_inteligo', 'history_dir') || '/www'; my $dbase; if($dbtype eq "mysql") { $dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); } elsif($dbtype eq "postgres") { $dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); } else { print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n"; exit 1; } # Definicja Agenta http my $sd =''; my $cookie_jar = HTTP::Cookies->new; my $ua = new LWP::UserAgent; $ua->timeout(30); $ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4'); $ua->env_proxy(); # Wchodzimy na strone my $get = new HTTP::Request(GET => 'https://inteligo.pl/secure'); my $res = $ua->request($get); if ($res->content =~ /Przepraszamy/ ){ print "ERROR: System chwilowo niedostepny", "\n"; exit 1; } if ($res->content =~ /Identyfikator/ ){ my $p = HTML::TokeParser->new(\$res->content); while ( my $token1 = $p->get_tag("input")){ if (my $ur = $token1->[1]{name} =~ /sd/){ my $url = $token1->[1]{name} || "-"; my $url2 = $token1->[1]{value} || "-"; $sd = $url2; } } } print "Logujemy do Serwisu ... \t"; my $post = POST 'https://inteligo.pl/secure', [ sd => $sd, form_name => "login", btn_ok => "", menu => "", cif => $login_i, password => $passwd_i, btn_ok => "", ]; my $res1 = $ua->request($post); if (!$res1->content =~ /Stan Twoich/ ){exit 1;} if ($res1->content =~ /Stan Twoich/ ){ print "Jestesmy zalogowani", "\n"; my $p = HTML::TokeParser->new(\$res1->content); while ( my $token1 = $p->get_tag("input")){ if (my $ur = $token1->[1]{name} eq "sd"){ my $url = $token1->[1]{name} || "-"; my $url2 = $token1->[1]{value} || "-"; $sd = $url2; } } } print "Przechodzimy do historii ...\t"; my $post1 = POST 'https://inteligo.pl/secure/', [ sd => $sd, section => "mojerachunki", return_point => "1", form_name => "accounts", menu => "history", button => "", ]; $res = $ua->request($post1); if ($res->content =~ /Historia rachunku/ ){ print "Strona Historii Rachunku \n"; my $p1 = HTML::TokeParser->new(\$res->content); while ( my $token2 = $p1->get_tag("input")){ if (my $ur = $token2->[1]{name} eq "sd"){ my $url = $token2->[1]{name} || "-"; my $url2 = $token2->[1]{value} || "-"; $sd = $url2; } } } print "Zmieniamy filtr historii....\t"; my $post2 = POST 'https://inteligo.pl/secure', [ sd => $sd, section => "mojerachunki", return_point => "1", form_name => "history", menu => "", button => "filter", selected_acc => $konto, beg_date_y => $time_y, beg_date_m => $time_m, beg_date_d => $time_d, end_date_y => $time_y, end_date_m => $time_m, end_date_d => $time_d, sel_tx_filter_type => $trans_type, search_text => "", search_type => "all_words", search_account => "", amount_min => '', amount_max => '', export_type => 'CSV', ]; print "$time_y"; print "$time_m"; print $time_d+1; $res = $ua->request($post2); if ($res->content =~ /Historia rachunku/ ){ print "Zmieniono filtr \n"; my $p1 = HTML::TokeParser->new(\$res->content); while (my $token2 = $p1->get_tag("input")){ if (my $ur = $token2->[1]{name} eq "sd"){ my $url = $token2->[1]{name} || "-"; my $url2 = $token2->[1]{value} || "-"; $sd = $url2; } } } print "Pobieramy plik historii....\t"; my $post3 = POST 'https://inteligo.pl/secure', [ sd => $sd, section => "mojerachunki", return_point => "1", form_name => "history", menu => "", button => "export_list", selected_acc => $konto, beg_date_y => $time_y, beg_date_m => $time_m, beg_date_d => $time_d, end_date_y => $time_y, end_date_m => $time_m, end_date_d => $time_d, sel_tx_filter_type => $trans_type, search_text => "", search_type => "all_words", search_account => "", amount_min => "", amount_max => "", export_type => "CSV", ]; $res = $ua->request($post3); my $post4 = 'https://inteligo.pl' . $res->header('Location'); $post4 =GET $post4; $res =$ua->request($post4); print "Pobrano plik\n"; my @dana = split("\n", $res->content); my $file_name = $file_out_dir."/". $konto ."_". time(); if ($file_out_dir){ open (DOUT,">$file_name"); print DOUT "Ilosc pozycji". scalar(@dana) ."\n" } for (my $x=1;$xnew("ISO-8859-2", "UTF-8"); $line = $converter->convert($line); my @linia = split(",",$line); my @da = split('-',$linia[2]); $linia[2] = mktime(0,0,0,$da[2],$da[1]-1, $da[0]-1900); $linia[4] =~ s/[^0-9.]//g; $linia[9] = $linia[9] || ""; $linia[10] = $linia[10] || ""; $linia[11] = $linia[11] || ""; $linia[12] = $linia[12] || ""; if ($file_out_dir){ print DOUT $line ."\n"; } my $date = $linia[2]; my $customer = $linia[8]; my $description = $linia[9] ." ". $linia[10] ." ". $linia[11] ." ". $linia[12]; my $value = $linia[4]; my $customerid = '0'; if ($description =~ m/SALDO/i){ my $customerid_t = $description; $customerid_t =~ s/.+SALDO//gi; $customerid_t =~ s/[^0-9 ]//g; $customerid_t =~ s/^\s+//; $customerid_t =~ s/[ ].*//; $customerid_t =~ s/\s+$//; $customerid = $customerid_t; } if($customerid == '0'){ @da = split(" ", $linia[8]); my $sdbq = $dbase->prepare("SELECT id FROM customers WHERE UPPER(lastname)=UPPER('$da[0]') and UPPER(name)=UPPER('$da[1]')"); $sdbq->execute; my $id = '0'; if (my $row = $sdbq->fetchrow_hashref()){ $customerid = $row->{'id'}; }else { my $sdbq = $dbase->prepare("SELECT id FROM customers WHERE UPPER(lastname)=UPPER('$da[1]') and UPPER(name)=UPPER('$da[0]')"); $sdbq->execute; if (my $row = $sdbq->fetchrow_hashref()){ $customerid = $row->{'id'}; } } } my $dx = new Digest::MD5; my $hash = $dx->md5_hex($date.$value.$customer.$description); my $sdbq = $dbase->prepare("SELECT id FROM cashimport WHERE hash = '$hash'"); $sdbq->execute(); my $row = $sdbq->fetchrow_hashref(); if ($row->{'id'}) { print "ERROR: Hash exists, ignoring $customer $description", "\n"; } else { my $sdbq = $dbase->prepare("INSERT INTO cashimport ( Date , Value , Customer , Description , CustomerId , Hash ) VALUES ('$date', '$value', '$customer', '$description', '$customerid', '$hash');"); $sdbq->execute(); print $date ."|".$value."|".$customerid ."|". $customer."|".$description ."|". $hash ."\n"; } } close DOUT; exit 0; print "ERROR: Nieznany blad"; exit 1;