<?php
/*===================================================+
|| # HoloCMS - Website and Content Management System
|+===================================================+
|| # Copyright © 2008 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|+===================================================+
|| # HoloCMS is provided "as is" and comes without
|| # warrenty of any kind. HoloCMS is free software!
|+===================================================*/

require_once('../core.php');
if($hkzone !== true){ header("Location: index.php?throwBack=true"); exit; }
if(!session_is_registered(acp)){ header("Location: index.php?p=login"); exit; }
if(function_exists(SendMUSData) !== true){ include('../includes/mus.php'); }

$pagename = "User Badge Management";

if(isset($_POST['badge'])){
$badge = $_POST['badge'];
$key = addslashes($_POST['name']);

$check = mysql_query("SELECT id FROM users WHERE username = '".$key."' LIMIT 1") or die(mysql_error());
$exists = mysql_num_rows($check);

	if($exists > 0){

		if(strlen($badge) > 2 && strlen($badge) < 5){

			$row = mysql_fetch_assoc($check);
			$userid = $row['id'];

			$check = mysql_query("SELECT * FROM user_badges WHERE user_id = '".$userid."' AND badge_id = '".$badge."' LIMIT 1") or die(mysql_error());
			$already_has_badge = mysql_num_rows($check);

			if($already_has_badge < 1){

				mysql_query("UPDATE users SET badge_status = '1' WHERE username = '".$key."' LIMIT 1") or die(mysql_error()); 
				mysql_query("INSERT INTO user_badges (user_id,badge_id,badge_slot) VALUES ('".$userid."','".$badge."','1')") or die(mysql_error());

				$msg = "Distintivo (" . $badge . ") inviato con Successo!";

				mysql_query("INSERT INTO system_stafflog (action,message,note,userid,targetid,timestamp) VALUES ('Housekeeping','Gave user ".$badge." badge','badgetool.php','".$my_id."','".$userid."','".$date_full."')") or die(mysql_error());
                                
                                @SendMusData('UPRS' . $userid);

			} else {

                                mysql_query("UPDATE user_badges SET badge_slot = '0' WHERE userid = '" . $userid . "'");
				mysql_query("UPDATE user_badges SET badge_slot = '1' WHERE badge_id = '" . $badge . "' AND user_id = '" . $userid . "' LIMIT 1") or die(mysql_error());
                                
				$msg = "L'utente aveva gia il distintivo (" . $badge . ") ma è stato comunque aggiunto correttamente! ";

				mysql_query("INSERT INTO system_stafflog (action,message,note,userid,targetid,timestamp) VALUES ('Housekeeping','Assegnamento distintivo ".$badge."','badgetool.php','".$my_id."','".$userid."','".$date_full."')") or die(mysql_error());
                                
                                @SendMusData('UPRS' . $userid);

			}



		}

	} else {

		$msg = "Attenzione: nome utente non esistente !";

	}

}

@include('subheader.php');
@include('header.php');
?>
<table cellpadding='0' cellspacing='8' width='100%' id='tablewrap'>
<tr> <td width='22%' valign='top' id='leftblock'>
 <div>
 <!-- LEFT CONTEXT SENSITIVE MENU -->
<?php @include('usermenu.php'); ?>
 <!-- / LEFT CONTEXT SENSITIVE MENU -->
 </div>
 </td>
 <td width='78%' valign='top' id='rightblock'>
 <div><!-- RIGHT CONTENT BLOCK -->
 
<?php if(isset($msg)){ ?><p><strong><?php echo $msg; ?></p></strong><?php } ?>
 
<form action='index.php?p=badgetool&do=something' method='post' name='theAdminForm' id='theAdminForm'>
<div class='tableborder'>
<div class='tableheaderalt'>Dai distintivi</div>

<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'>
<tr>
<td class='tablerow1'  width='40%'  valign='middle'><strong>Nome utente</strong><div class='graytext'>Inserire il nome dell'utente, al quale si vuole aggiungere il distintivo!</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='name' value="" size='30' class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><strong>Codice del distintivo</strong><div class='graytext'>Ricorda: i codice distintivi vanno scritti sempre in MAIUSCOLO!<br><A HREF="http://homehabbouk.it.gg/Codici-Distintivi-ar-.htm"> QUÌ TROVI ALCUNI CODICI!</A></br></div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='badge' value="" size='30' class='textinput'></td>
</tr>

<tr>
<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value=' DAI DISTINTIVO ' class='realbutton' accesskey='s'></td></tr>
</form></table></div><br />

	 </div><!-- / RIGHT CONTENT BLOCK -->
	 </td></tr>
</table>
</div><!-- / OUTERDIV -->
<div align='center'><br />
<?php
$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;
printf('Ore: %.3f', $totaltime);
?>
</div>