Index: trunk/admin/admin.php
===================================================================
--- trunk/admin/admin.php	(revision 359)
+++ trunk/admin/admin.php	(revision 360)
@@ -139,5 +139,5 @@
 
 	try{
-	$dbi = new PDO("mysql:host=$dbhost;charset=latin1", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); 
+	$dbi = new PDO("mysql:host=$dbhost;charset=utf8", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); 
      	$sql = "use $dbname";
     	$dbi->exec($sql);
@@ -147,6 +147,18 @@
 	    echo $sql . "<br>" . $e->getMessage();
 	}                                                                                       	
-
-
+	$sth = $dbi->prepare("SET SESSION character_set_connection = 'utf8' ");
+	$sth->execute();
+	$sth = $dbi->prepare("SET SESSION character_set_client = 'utf8' ");
+	$sth->execute();
+	$sth = $dbi->prepare("SET SESSION character_set_database = 'utf8' ");
+	$sth->execute();
+	$sth = $dbi->prepare("SET CHARACTER SET utf8");
+	$sth->execute();
+
+	$sth = $dbi->prepare("SET NAMES 'utf8'");
+	$sth->execute();
+	$sth = $dbi->prepare("select * from ".$prefix."_config");
+	$sth->execute();
+	
 #		$dbi=mysql_connect($dbhost, $dbuname, $dbpass) or die("Connessione non riuscita: " . mysql_error());
 #		mysql_select_db($dbname)or die("Connessione non riuscita:" . mysql_error());
@@ -264,15 +276,23 @@
 $id_comune=$_SESSION['id_comune'];
 if($id_comune==0) $rifcomune='58047'; else $rifcomune=$id_comune;
-if (isset($_GET['id_cons_gen'])) $id_cons_gen=intval($_GET['id_cons_gen']);
+if (isset($_GET['id_cons_gen'])) {$id_cons_gen=intval($_GET['id_cons_gen']);}
 else {
-			$sql="SELECT t1.id_cons_gen FROM ".$prefix."_ele_cons_comune as t1, ".$prefix."_ele_comuni as t2 where t1.id_cons=t2.id_cons and  t2.id_comune='$id_comune'";
-			$sth = $dbi->prepare($sql);
-			$sth->execute();	
-			$row = $sth->fetch(PDO::FETCH_BOTH);
-			if($sth->rowCount())
-				$id_cons_gen=$row[0];
-			else
-			$id_cons_gen=0; #die("TEST IN CORSO : idconsgen: $id_cons_gen -- sql:select id_cons_gen  from ".$prefix."_ele_cons_comune where preferita='1' and (id_comune='$id_comune' or id_comune=0)");
-}
+	$oggi=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-3,date("Y")));
+	$sql="select t1.id_cons_gen from ".$prefix."_ele_consultazione as t1, ".$prefix."_ele_cons_comune as t2 where t1.id_cons_gen=t2.id_cons_gen and t2.id_comune=$id_comune and t1.data_fine>'$oggi' and t2.id_cons in (select id_cons from ".$prefix."_ele_operatori where aid='$aid' and id_sez>0 and permessi>0) limit 0,1";
+	$rese = $dbi->prepare("$sql");
+	$rese->execute();
+	if($rese->rowCount()) 
+	{list($id_cons_gen)=$rese->fetch(PDO::FETCH_NUM); }
+	else {
+		$sql="SELECT t1.id_cons_gen FROM ".$prefix."_ele_cons_comune as t1, ".$prefix."_ele_comuni as t2 where t1.id_cons=t2.id_cons and  t2.id_comune='$id_comune'";
+		$sth = $dbi->prepare($sql);
+		$sth->execute();	
+		$row = $sth->fetch(PDO::FETCH_BOTH);
+		if($sth->rowCount())
+			$id_cons_gen=$row[0];
+		else
+		$id_cons_gen=0; #die("TEST IN CORSO : idconsgen: $id_cons_gen -- sql:$sql");
+	}
+} 
 $currentlang=$_SESSION['lang'];
 #$bgcolor1=$_SESSION['bgcolor1'];
@@ -281,6 +301,6 @@
 $session=$_SESSION['remote'];
 
-$perms=ChiSei($id_cons_gen);
-if($perms==0) logout();
+$perms=ChiSei($id_cons_gen); #die("qui".$_SESSION['aid']);
+if($perms==0) {logout();} 
 }
 
@@ -296,5 +316,4 @@
 $pwd=$_SESSION['pwd'];
 $id_comune=$_SESSION['id_comune'];
-
 
 $perms=0;
@@ -325,4 +344,5 @@
 		if($row[0]==0) $msglogout=1;
 	if ($sth->rowCount()==1) $perms=$row[0]; else {	$msglogout=1; $perms=0; }
+
 	return $perms;
 #} else return 0;
@@ -404,4 +424,5 @@
         Header("Location: admin.php?id_comune=$id_comune&language=$lang");
 */
+
 global $siteistat,$perms,$msglogout; 
 $language=$_SESSION['lang'];
@@ -432,14 +453,24 @@
 		$sth = $dbi->prepare("$sql");
 		$sth->execute();	
-		if ($sth->rowCount()) list($id_cons) = $sth->fetch(PDO::FETCH_NUM);
-		$_SESSION['id_cons']=$id_cons;
+		if ($sth->rowCount()) {
+			list($id_cons) = $sth->fetch(PDO::FETCH_NUM);
+			$_SESSION['id_cons']=$id_cons;
+		}
 	} 
 	if(isset($id_cons)) {
-		$sql="SELECT id_sez FROM ".$prefix."_ele_operatori where id_cons=$id_cons and aid='$aid' and id_comune=$id_comune";
+		$sql="SELECT id_sez FROM ".$prefix."_ele_operatori where id_sez>0 and aid='$aid' and id_comune=$id_comune";
 		$resmod = $dbi->prepare("$sql");
 		$resmod->execute();				
-		list($id_sez) = $resmod->fetch(PDO::FETCH_NUM);
-		if($id_sez) $tema='Futura2';#die( "TEST id_cons:$id_cons:");
-		$_SESSION['tema']=$tema;
+		list($id_sez) = $resmod->fetch(PDO::FETCH_NUM); #die("qui:$id_sez:$sql");
+		if($id_sez) { 
+			$oggi=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-3,date("Y")));
+			$sql="select t1.id_cons_gen,t1.descrizione,t2.id_cons from ".$prefix."_ele_consultazione as t1, ".$prefix."_ele_cons_comune as t2 where t1.id_cons_gen=t2.id_cons_gen and t2.id_comune=$id_comune and t1.data_fine>'$oggi' and t2.id_cons in (select id_cons from ".$prefix."_ele_operatori where aid='$aid' and id_sez>0 and permessi>0)";
+			$resmod = $dbi->prepare("$sql");
+			$resmod->execute();
+			if ($resmod->rowCount()>0) {
+				$tema='Futura2'; #die( "TEST id_cons:$id_cons:".$_SESSION['aid']);
+				$_SESSION['tema']=$tema;
+			} else logout();
+		}
 	}
 }
@@ -449,5 +480,6 @@
 if (isset($param['op'])) $op=addslashes($param['op']); else $op='ele';
 //if (isset($param['op'])) $op=$param['op']; else $op='ele';
-#die( "OP: $op");
+#
+#die("TEST: qui $aid $id_cons $id_sez ".$_SESSION['aid']);
 if (isset($_SESSION['aid']) AND $_SESSION['remote']==$_SERVER['REMOTE_ADDR']) {
 	if($tema=='Futura2' and $op!='logout') 
