use CGI;
use DBI;

$query= new CGI;

$script=$query->script_name;
$ip=$query->remote_addr;

########################################################
push (@DADES, 'seris2006','tull8tre','dbi:mysql:serisenginyers');
$DB=pop(@DADES);
$passwd=pop(@DADES);
$usr=pop(@DADES);
########################################################

$login=($query->param("frmlogin"));
$password=($query->param("frmpassword"));

########################################################

sub DENEGAT {

print <<ENDHTML;

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SERIS Enginyers S.L.</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="Level3_3.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
body {
	background-color: #FFFFFF;
}
.Estilo39 {font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; font-size: 18px; font-style: italic; font-weight: bold; font-variant: normal; }
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--

function P7_JumpMenu(selObj,restore){ //v1.4 by Project Seven
	var theFullString = selObj.options[selObj.selectedIndex].value;
	if (restore) selObj.selectedIndex=0;
	var theLength = theFullString.length;
	var endPos = theFullString.lastIndexOf("~");
	var theUrl, theTarget, theParent;
	if (endPos > 0) {theUrl = theFullString.substring(0,endPos);}
	else {theUrl = theFullString;}
	endPos++
	if (endPos < theLength) {theTarget = theFullString.substring(endPos,theLength)}
	else {theTarget = "window:Main";}
	if (theTarget == "window:New") {window.open(theUrl);}
	else if (theTarget == "window:Main") {eval("parent.location='"+theUrl+"'");}
	else {eval("parent.frames[\'"+theTarget+"\'].location='"+theUrl+"'");}
}
//-->
</script>
</head>

<body>
<table width="588" height="575" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" align="center">
  <tr>
    <td><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="600" height="94">
      <param name="movie" value="cap_area_privada.swf" />
      <param name="quality" value="high" />
      <embed src="cap_area_privada.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="600" height="94"></embed>
    </object>
    <img src="images/cap_privada_bajo.jpg" width="600" height="15" /></td>
  </tr>
  <tr>
    <td height="30" bordercolor="#949EB2" bgcolor="#949EB2"><div align="center" class="titlebar">        </div>      
    <div align="right" class="footer">Zona Clients </div></td>
  </tr>
  <tr>
    <td height="440" valign="top" bordercolor="#949EB2"><p><br />
    </p>      
      <form name="form1" id="form1" method="post" action="">
        <p>&nbsp;</p>
        <p align="center" class="Estilo39">ACC&Eacute;S DENEGAT </p>
        <p>&nbsp;</p>
      </form>
      <p>&nbsp;      </p>
      <p>&nbsp;</p>      <br />      <div align="right"></div></td>
  </tr>
</table>
</body>
</html>


ENDHTML
;

exit;
}

########################################################


sub ERROR {

print <<ENDHTML;

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SERIS Enginyers S.L.</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="Level3_3.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
body {
	background-color: #FFFFFF;
}
.Estilo39 {font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; font-size: 18px; font-style: italic; font-weight: bold; font-variant: normal; }
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--

function P7_JumpMenu(selObj,restore){ //v1.4 by Project Seven
	var theFullString = selObj.options[selObj.selectedIndex].value;
	if (restore) selObj.selectedIndex=0;
	var theLength = theFullString.length;
	var endPos = theFullString.lastIndexOf("~");
	var theUrl, theTarget, theParent;
	if (endPos > 0) {theUrl = theFullString.substring(0,endPos);}
	else {theUrl = theFullString;}
	endPos++
	if (endPos < theLength) {theTarget = theFullString.substring(endPos,theLength)}
	else {theTarget = "window:Main";}
	if (theTarget == "window:New") {window.open(theUrl);}
	else if (theTarget == "window:Main") {eval("parent.location='"+theUrl+"'");}
	else {eval("parent.frames[\'"+theTarget+"\'].location='"+theUrl+"'");}
}
//-->
</script>
</head>

<body>
<table width="588" height="575" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" align="center">
  <tr>
    <td><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="600" height="94">
      <param name="movie" value="cap_area_privada.swf" />
      <param name="quality" value="high" />
      <embed src="cap_area_privada.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="600" height="94"></embed>
    </object>
    <img src="images/cap_privada_bajo.jpg" width="600" height="15" /></td>
  </tr>
  <tr>
    <td height="30" bordercolor="#949EB2" bgcolor="#949EB2"><div align="center" class="titlebar">        </div>      
    <div align="right" class="footer">Zona Clients </div></td>
  </tr>
  <tr>
    <td height="440" valign="top" bordercolor="#949EB2"><p><br />
    </p>      
      <form name="form1" id="form1" method="post" action="">
        <p>&nbsp;</p>
        <p align="center" class="Estilo39">S'HA PRODUIT UN ERROR </p>
        <p>&nbsp;</p>
      </form>
      <p>&nbsp;      </p>
      <p>&nbsp;</p>      <br />      <div align="right"></div></td>
  </tr>
</table>
</body>
</html>



ENDHTML
;

exit;

}

########################################################

sub dades {


print <<ENDHTML;


<html>
<head>
<title>SERIS Enginyers S.L.</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="Level3_3.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.Estilo9 {font-family: Verdana, Arial, Helvetica, sans-serif;
	text-decoration: none;
	font-size: 10px;
	font-style: normal;
	font-weight: normal;
	font-variant: normal;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
body {
	background-color: #FFFFFF;
}
.Estilo39 {font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; font-size: 10px; font-style: italic; font-weight: bold; font-variant: normal; }
.Estilo40 {color: #000000}
-->
</style>

<script language="JavaScript" type="text/JavaScript">
function P7_JumpMenu(selObj,restore){ //v1.4 by Project Seven
	var theFullString = selObj.options[selObj.selectedIndex].value;
	if (restore) selObj.selectedIndex=0;
	var theLength = theFullString.length;
	var endPos = theFullString.lastIndexOf("~");
	var theUrl, theTarget, theParent;
	if (endPos > 0) {theUrl = theFullString.substring(0,endPos);}
	else {theUrl = theFullString;}
	endPos++
	if (endPos < theLength) {theTarget = theFullString.substring(endPos,theLength)}
	else {theTarget = "window:Main";}
	if (theTarget == "window:New") {window.open(theUrl);}
	else if (theTarget == "window:Main") {eval("parent.location='"+theUrl+"'");}
	else {eval("parent.frames[\'"+theTarget+"\'].location='"+theUrl+"'");}
}
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}
</script>


</head>

<body>
<table width="588" height="575" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" align="center">
  <tr>
    <td><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="600" height="94">
      <param name="movie" value="cap_area_privada.swf" />
      <param name="quality" value="high" />
      <embed src="cap_area_privada.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="600" height="94"></embed>
    </object>
    <img src="images/cap_privada_bajo.jpg" width="600" height="15" /></td>
  </tr>
  <tr>
    <td height="30" bordercolor="#949EB2" bgcolor="#949EB2"><div align="center" class="titlebar">        </div>      
    <div align="right" class="footer">Zona Clients </div></td>
  </tr>
  <tr>
    <td height="440" valign="top" bordercolor="#949EB2"><p><br />
    </p>      
      <form name="form1" id="form1" method="post" action="">
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <table width="340"  border="1" align="center" bordercolor="#326698">
          <tr>
            <td><table width="336" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td height="26" colspan="2" background="images/projectes_caps.jpg"><span class="titlebar"><div align="center">Zona restringida</div> </span></td>
              </tr>
              <tr>
                <td height="26" colspan="2"><div align="center"><span class="Estilo9"> </span></div></td>
                </tr>
              <tr>
                <td width="122" height="26"><div align="center"><span class="Estilo39">LOGIN</span></div></td>
                <td width="214"><input type="text" name="frmlogin" /></td>
              </tr>
              <tr>
                <td height="26"><div align="center"><span class="Estilo39">PASSWORD</span></div></td>
                <td width="214"><input type="password" name="frmpassword" /></td>
              </tr>
              <tr>
                <td height="26">&nbsp;</td>
                <td width="214"><input name="Submit" type="submit" class="areaprivada" value="Accedir" /></td>
              </tr>
            </table></td>
          </tr>
        </table>
        <p>&nbsp;</p>
        </form>
      <p>&nbsp;      </p>
      <p>&nbsp;</p>      <br />      <div align="right"></div></td>
  </tr>
</table>
</body>
</html>



ENDHTML
;

exit;

}

sub comprova {


$login=($query->param("frmlogin"));
$password=($query->param("frmpassword"));


$dbh=DBI->connect( $DB , $usr , $passwd , {PrintError=>0}) || &DENEGAT;

$sth=$dbh->prepare(qq{SELECT ID_client,login,password,curdate() FROM client where (login='$login' && password='$password')}) || &ERROR;
$rc=$sth->execute;
@pass=$sth->fetchrow_array;
if ($pass[0] eq "") {
&DENEGAT;
}
$curdata=$pass[3];
$idclient=$pass[0];
$sth=finish;


@data=split(/\-/,$curdata);
$mes=$data[1];
$any=$data[0];

$id=time();
$id=$id. $login;

$sth=$dbh->do( qq{INSERT into session (id,login,pwd,data,ip) values ('$id','$login','$password','$curdata','$ip')}) || &ERROR;
$sth=finish;

### borrem les sessions caducades

$sth=$dbh->do( qq{update session set pwd="" where (data<'$curdata' and pwd!="")}) || &ERROR;
$sth=finish;

### Acces correcte, generem la pàgina de benvinguda


$sth=$dbh->prepare(qq{SELECT nom,empresa FROM client where (ID_client='$idclient')}) || &ERROR;
$rc=$sth->execute;
@pass=$sth->fetchrow_array;
$nomclient=$pass[0];
$nomempresa=$pass[1];
$sth=finish;

print <<ENDHTML;


<html>
<head>
<title>SERIS Enginyers S.L.</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="Level3_3.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.Estilo34 {font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #666666;
	text-decoration: none;
}
.Estilo33 {color: #000000}
.Estilo9 {font-family: Verdana, Arial, Helvetica, sans-serif;
	text-decoration: none;
	font-size: 10px;
	font-style: normal;
	font-weight: normal;
	font-variant: normal;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
.Estilo35 {font-weight: bold}
body {
	background-color: #FFFFFF;
}
.Estilo36 {color: #FFFFFF}

</style>
<script language="JavaScript" type="text/JavaScript">
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}
function P7_JumpMenu(selObj,restore){ //v1.4 by Project Seven
	var theFullString = selObj.options[selObj.selectedIndex].value;
	if (restore) selObj.selectedIndex=0;
	var theLength = theFullString.length;
	var endPos = theFullString.lastIndexOf("~");
	var theUrl, theTarget, theParent;
	if (endPos > 0) {theUrl = theFullString.substring(0,endPos);}
	else {theUrl = theFullString;}
	endPos++
	if (endPos < theLength) {theTarget = theFullString.substring(endPos,theLength)}
	else {theTarget = "window:Main";}
	if (theTarget == "window:New") {window.open(theUrl);}
	else if (theTarget == "window:Main") {eval("parent.location='"+theUrl+"'");}
	else {eval("parent.frames[\'"+theTarget+"\'].location='"+theUrl+"'");}
}
</script>
</head>


<body>
<table width="588" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" align="center">
  <tr>
    <td colspan="2"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="600" height="94">
      <param name="movie" value="cap_area_privada.swf" />
      <param name="quality" value="high" />
      <embed src="cap_area_privada.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="600" height="94"></embed>
    </object>
    <img src="images/cap_privada_bajo.jpg" width="600" height="15" /></td>
  </tr>
  <tr>
    <td width="60%" height="30" bordercolor="#949EB2" bgcolor="#949EB2"><a href="mailto:seris\@serisenginyers.com"><img src="images/mail_ico.jpg" border="0" alt="Contacta"></a> <a href="ftp://clientes:seris2006\@clientes.serisenginyers.com/httpdocs" target="_blank"><img src="images/ftp_ico.jpg" border="0" alt="Accedeix FTP"></a></td>
    <td height="30"  bordercolor="#949EB2" bgcolor="#949EB2"><div align="center" class="titlebar">
      <div align="left">      </div>
    </div>      <div align="center"></div>
    <div align="right"><span class="footer">Benvingut Sr/a. $nomclient</span> </div></td>
  </tr>
  <tr>
    <td height="440" valign="top" bordercolor="#949EB2"><table width="336" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="26" colspan="2" background="images/projectes_caps.jpg"><span class="titlebar"><strong><img src="images/projectes_puntet.gif" width="14" height="10" /></strong></span><span class="titlebar">Selecciona el projecte que vols visualitzar </span></td>
        </tr>
      <tr>
        <td>&nbsp;</td>
        <td width="198">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2"><div align="center"><span class="Estilo9">
              <select name="select3" id="select2" onchange="P7_JumpMenu(this,1)">
                <option>Seleccio de projecte</option>

ENDHTML
;

$sth=$dbh->prepare(qq{SELECT ID_projecte,titol FROM projecte where (client='$idclient') order by data_creacio desc}) || &ERROR;
$rc=$sth->execute;
while (@pass=$sth->fetchrow_array) {

print <<ENDHTML;

                <option value="fitxa_projecte.pl?frmid=$pass[0]~window:Main">$pass[1]</option>

ENDHTML
;

}
$sth=finish;


print <<ENDHTML;
 
              </select>
        </span></div></td>
        </tr>
    </table>
      <p>&nbsp;</p>
      <p>&nbsp;      </p>      <table width="336" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="26" colspan="2" background="images/projectes_caps.jpg"><span class="titlebar"><img src="images/projectes_puntet.gif" width="14" height="10" />Legislaci&oacute; Aplicable </span></td>
        </tr>

ENDHTML
;


$sth=$dbh->prepare(qq{SELECT ID_legislacio,titol,arxiu FROM legislacio where (client='$idclient') order by ID_legislacio}) || &ERROR;
$rc=$sth->execute;
while (@pass=$sth->fetchrow_array) {

print <<ENDHTML;

        <tr>
          <td height="26"><div align="center"><span class="Estilo9"><img src="images/bullet_blanca.gif" width="8" height="9" /></span></div></td>
          <td width="303"><span class="Estilo9"><a href="legislacio/$pass[2]" target="_blank" class="Estilo9">$pass[1]</a></span></td>
        </tr>

ENDHTML
;

}
$sth=finish;

print <<ENDHTML;

      </table>      
      <div align="right">
        <p>&nbsp;</p>
        <p><br />
          <span class="Estilo9"><strong>Tota la informaci&oacute; est&agrave; en format PDF</strong></span> <a href="http://www.adobe.es/products/acrobat/readstep2.html" target="_blank"><img src="images/logopdf.gif" width="88" height="31" border="0" /></a> </p>
    </div></td>
    <td valign="top" bordercolor="#FFFFFF">
      <div align="right">
        <table width="246" height="358" border="0" align="right" cellpadding="2" cellspacing="0" bordercolor="#CCCCCC">
          <tr>
            <td height="24" bordercolor="#FFFFFF" background="images/projectes_caps.jpg" class="Estilo34"><div align="left"><span class="titlebar"><strong><img src="images/projectes_puntet.gif" width="14" height="10" /></strong>SERIS INFORMA: </span></div></td>
          </tr>
          <tr>
            <td width="395" height="330" bgcolor="#FFFFCC" class="Estilo34"><div align="left">

ENDHTML
;


$sth=$dbh->prepare(qq{SELECT data,noticia FROM noticiesgeneral where (client='$idclient') order by data desc}) || &ERROR;
$rc=$sth->execute;
while (@pass=$sth->fetchrow_array) {

@data=split(/\-/,$pass[0]);
$dia=$data[2];
$mes=$data[1];
$any=$data[0];

$noticia=$pass[1];
$noticia=~ s/\n/<br>/g;

print <<ENDHTML;


                <p align="justify" class="Estilo33"><span class="Estilo35">$dia/$mes/$any </span><br />$noticia</p>


ENDHTML
;

}
$sth=finish;

print <<ENDHTML;

            </div>              </td>
          </tr>
          <tr><td>&nbsp;</td></tr>

        </table>
    </div></td>
  </tr>
</table>
</body>
</html>


ENDHTML
;


$dbh->disconnect();

exit;
}

####################


###############3REGISTRO###########################
$ruta="";
$ruta2="";
($s,$m,$h, @resto) = localtime(time);
$horaactual=$h.":".$m.":".$s;
$suip=$ENV{'REMOTE_ADDR'};
foreach $key (sort keys(%ENV)) { 
$ruta .= $key." = ".$ENV{$key}."\r\n";
} 
for $var (sort $query->param){
$ruta2 .= $query->b($var)." = ".$query->param($var)."\r\n";
}
$dbh=DBI->connect( $DB , $usr , $passwd , {PrintError=>0}) || &DENEGAT;
$sth=$dbh->prepare(qq{SELECT curdate() FROM client where (1)}) || &ERROR;
$rc=$sth->execute;
@pasr=$sth->fetchrow_array;
$curdatd=$pasr[0];
$sth=finish;
$sth=$dbh->do( qq{INSERT into registro (pagina,data,hora,ip,opciones,variables) values ("valida.pl",'$curdatd','$horaactual','$suip','$ruta','$ruta2')}) || &ERROR;
$sth=finish;
$dbh->disconnect();
##################################

if ($login ne "" && $password ne "") { &comprova;
}else{
&dades;
}

