########################################################################
# Yaomiqui is Powerful tool for Automation + Easy to use Web UI
# Written in freestyle Perl + CGI + Apache + MySQL + Javascript + CSS
# Common libraries for Yaomiqui 1.0
# 
# Yaomiqui and its logo are registered trademark by Hugo Maza Moreno
# Copyright (C) 2019
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# 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, see <http://www.gnu.org/licenses/>.
########################################################################

sub get_vars {
	my %VARS;
	open my $file, "<../yaomiqui.conf";
	while ( <$file> ) {
		$_ =~ s/\n$//;
		$_ =~ s/^\s*//;
		$_ =~ s/\s*$//;
		unless ( $_ =~ /\#/ ) {
			my ($key, $val) = split(/\s*\=\s*/, $_);
			$VARS{$key} = $val if $val;
		}
	}
	close $file;
	
	use DBI;
	my $dbh = DBI->connect("DBI:mysql:$VARS{'DB'}:$VARS{'DBHOST'}", $VARS{'DBUSER'}, $VARS{'DBPASSWD'}) or die "Error... $DBI::errstr mysql_error()<br>";
	my $sth = $dbh->prepare("SELECT varName, varValue FROM configVars");
	$sth->execute();
	my $cnfVar = $sth->fetchall_arrayref;
	$sth->finish;
	$dbh->disconnect if ($dbh);
	
	for my $i ( 0 .. $#{$cnfVar} ) {
		$VARS{ $cnfVar->[$i][0] } = $cnfVar->[$i][1] if $cnfVar->[$i][1];
	}
	
	return %VARS;
}

sub get_conf_vars {
	my %VARS;
	connected();
	my $sth = $dbh->prepare("SELECT varName, varValue FROM configVars");
	$sth->execute();
	my $cnfVar = $sth->fetchall_arrayref;
	$sth->finish;
	$dbh->disconnect if ($dbh);
	
	for my $i ( 0 .. $#{$cnfVar} ) {
		$VARS{ $cnfVar->[$i][0] } = $cnfVar->[$i][1] if $cnfVar->[$i][1];
	}
	
	return %VARS;
}

sub get_theme {
	my $module = shift;
	my $theme = 'classic_cloud';
	
	if ( $username ne 'Guest' ) {
		connected();
		my $sth = $dbh->prepare("SELECT theme FROM users WHERE username = '$username'");
		$sth->execute();
		($theme) = $sth->fetchrow_array;
		$sth->finish;
		$dbh->disconnect if ($dbh);
	}
	return $theme;
}

sub getEncKey {
	my $o = tie my @array, 'Tie::File', $VAR{enc_key} or die "I can't open yaomiqui encrypted key: $VAR{enc_key}\n";
	my $encKey = $array[0];
	untie @array;
	return $encKey;
}

sub connected {
	use DBI;
	$dbh = DBI->connect("DBI:mysql:$VAR{'DB'}:$VAR{'DBHOST'}", $VAR{'DBUSER'}, $VAR{'DBPASSWD'}) or die "Error... $DBI::errstr mysql_error()<br>";
}

sub get_permissions {
	connected();
	my $sth = $dbh->prepare("SELECT permissions.* FROM users, permissions WHERE users.username = '$username' and users.idUser = permissions.idUser");
	$sth->execute();
	my @perm = $sth->fetchrow_array;
	$sth->finish;
	$dbh->disconnect if ($dbh);
	
	my %PERM;
	$PERM{login} = 1;
	$PERM{logout} = 1;
	$PERM{init} = $perm[2];
	$PERM{overview} = $perm[3];
	$PERM{design} = $perm[4];
	$PERM{accounts} = $perm[5];
	$PERM{accounts_edit} = $perm[6];
	$PERM{settings} = $perm[7];
	$PERM{tickets} = $perm[8];
	$PERM{tickets_form} = $perm[9];
	$PERM{logs} = $perm[10];
	$PERM{charts} = $perm[11];
	$PERM{reports} = $perm[12];
	$PERM{about} = $perm[13];
	$PERM{config} = $perm[14];
	$PERM{my_account} = $perm[15];
	$PERM{alerts} = $perm[16];
	$PERM{alerts_config} = $perm[17];
	
	return %PERM;
}

sub vermod {
	require "$VAR{'modules_dir'}/$module_file";
}

sub loadLang {
	my $module = shift;
	my $lang = 'en_US';
	
	if ( $username ne 'Guest' ) {
		connected();
		my $sth = $dbh->prepare("SELECT language FROM users WHERE username = '$username'");
		$sth->execute();
		($lang) = $sth->fetchrow_array;
		$sth->finish;
		$dbh->disconnect if ($dbh);
	}
	
	open (LANGFILE, "<$VAR{lang_dir}/$lang") or "Error: I can't open $lang file. $!";
	my @array = <LANGFILE>;
	close LANGFILE;
	chomp @array;
	
	my %MSG;
	my $takeLang;
	
	foreach my $line( @array ) {
		$line =~ s/\n$//;
		$line =~ s/^\s+//;
		$line =~ s/\s+\n//;
		$line =~ s/\n\n/\n/;
		$line =~ s/\s+\=\s+/\=/;
		next if $line =~ /^#/;
		
		if ( $line eq "\[$module\]" ) {
			$takeLang = 1;
			next;
		}
		
		if ( $takeLang ) {
			if ( $line =~ /^\[/ ) {
				return %MSG;
				last;
			}
			
			my ($key, $val) = split(/\=/, $line, 2);
			$MSG{$key} = $val;
		}
	}
}
sub crypt_ {my $text=shift;$text =~ tr{\x20-\x7d}{\x4f-\x7d\x20-\x4e};return $text}
sub get_session {
	my $vendor = "yaomiqui";
	my $cookie_sesion;
	my $username = 'Guest';
	
	my @nvpairs=split(/; /, $ENV{HTTP_COOKIE});
	foreach my $pair (@nvpairs) {
		#yaomiqui=jk345k34h3luQejcuwcy7r2F3D06b2lB
		my ($cookie_vendor, $coockie_datos) = split(/=/, $pair);
		if ($cookie_vendor eq $vendor)	{
			$cookie_sesion = $coockie_datos;
		}
	}
	
	open (SESSION, "<$VAR{session_file}");	#session|username
		while (<SESSION>) {
			my ($session, $usernamesession) = split(/\|/, $_);
			chomp $usernamesession;
			if ( $session eq $cookie_sesion ) {
				$username = $usernamesession;
				last;
			}
		}
	close SESSION;
	
	return $username;
}

sub get_cookie_Sector {
	my $vendor = "yaomiquiSector";
	my $cookie_Sector;
	
	my @nvpairs=split(/; /, $ENV{HTTP_COOKIE});
	
	foreach my $pair (@nvpairs) {
		#yaomiquiSector=1
		my ($cookie_vendor, $coockie_datos) = split(/=/, $pair);
		if ($cookie_vendor eq $vendor)	{
			$cookie_Sector = $coockie_datos;
		}
	}
	
	return $cookie_Sector;
}

sub delMalCode {
    my $input = shift;
    $input =~ s/<|>|\%3C|\%3E|script|java|onmouse|onkey|onload|onerror|onunload|onresize|onclick|onchange|onblur|onfocus|onselect|onsubmit|\#|\!//gi;
    return $input;
}

sub set_cookie_Sector {
	my $vendor = "yaomiquiSector";
	my $cookie_Sector = shift;
	
	# use CGI qw/:standard/;
    use CGI::Cookie;
    
	$c = CGI::Cookie->new(
		-name    =>  $vendor,
		-value   =>  $cookie_Sector,
		-expires =>  '+3M'
	);
	
	 print "Set-Cookie: $c\n";
}

sub login {
	return qq~
	<!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.01//EN">
	<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title>LogIn :: YAOMIQUI :: Automation Platform :: For business repetitive tasks</title>
			<link rel="icon" type="image/png" href="images/favicon.ico" />
			<meta name="keywords" content="rpa,automation,tasks,repetitive" />
			<meta name="description" content="Automation for bussines repetitive task,ITPA,RPA" />
		</head>
		<style>
			html {
				font-family: verdana, sans, sans-serif, helvetica;
				font-size: 95%;
				color: #292929;
				margin: 0px;
				padding: 0px;
			}
	
			body {
				font-family: verdana, sans, sans-serif, helvetica;
				font-size: 95%;
				color: #292929;
				margin: 0px;
				padding: 0px;
				background-color: #FFFFFF;
			}
			.topContent {
				border-radius: 10px 10px 0px 0px;
				background: #407692;
				width: 500px;
				margin: auto;
				margin-top: 150px;
				color: #FFFFFF;
				font-size: 14px;
				font-weight: bold;
				padding: 12px 20px;
				border: 1px solid #407692;
			}
			.content {
				border-radius: 0px 0px 10px 10px;
				background: #FBFBFB;
				width: 500px;
				margin: auto;
				color: #4D4D4D;
				padding: 70px 20px;
				border-right: 1px solid #407692;
				border-bottom: 1px solid #407692;
				border-left: 1px solid #407692;
				/*min-height: 100px;*/
				/*vertical-align: middle;*/
				text-align: center;
			}
			input[type=text] {
				padding: 10px 10px;
				border: 1px solid #DDE9EF;
				border-radius: 4px;
				width: 200px;
				-webkit-transition: 0.5s;
				transition: 0.5s;
				outline: none;
			}
			input[type=text]:focus { 
				background-color: #F4FCFF;
				border: 1px solid #BDBDBD;
			}
			input[type=password] {
				padding: 10px 10px;
				border: 1px solid #DDE9EF;
				border-radius: 4px;
				width: 200px;
				-webkit-transition: 0.5s;
				transition: 0.5s;
				outline: none;
			}
			input[type=password]:focus { 
				background-color: #F4FCFF;
				border: 1px solid #BDBDBD;
			}
			input[type=button], input[type=submit], input[type=reset] {
				padding: 6px 10px;
				border-radius: 2px;
				border: 1px solid #46719B;
				background-color: #1E90FF;
				cursor: pointer;
                color: #FFFFFF;
			}
		</style>
		<body>
			<div class="topContent" title="Version $VAR{Version}">
				$MSG{Yaomiqui_Login}
			</div>
			
			<div class="content">
				<form method="post" action="index.cgi">
				<input type="hidden" name="mod" value="login">
				<table cellpadding="0" cellspacing="0" align="center" style="margin: auto;">
					<tr><td align="left">$MSG{Username}</td></tr>
					<tr><td align="left"><input autocomplete="off" type="text" name="user" value="$input{user}" class="LogIn" autofocus placeholder="Your username..."></td></tr>
					<tr><td align="left">&nbsp;</td></tr>
					<tr><td align="left">$MSG{Password}</td></tr>
					<tr><td align="left"><input type="password" name="pass" class="LogIn" placeholder="Your password..."></td></tr>
					<tr><td align="left">&nbsp;</td></tr>
					<tr><td align="right"><input type="submit" value="       $MSG{Log_In}       "></td></tr>
				</table>
				</form>
			</div>
			
		</body>
	</html>
	 ~;
}

1;
