<?php

	$reply = (isset($_GET['reply'])) ? $_GET['reply'] : 0;
	if(!isset($_SESSION['threads']))
	{
		$_SESSION['threads'] = true;
	}
	$threads = $_SESSION['threads'];
	
	$actions = array('add', 'delete', 'change_thread');
	
	(key_exists('action', $_GET) && in_array($_GET['action'], $actions)) ? $action = $_GET['action'] : $action = '';
	(key_exists('i', $_GET) && $_GET['i']>0) ? $i = $_GET['i'] : $i = 1;
	$sum = (key_exists('sum', $_POST)) ? $_POST['sum'] : 0;
	$num1 = (key_exists('num1', $_POST)) ? $_POST['num1'] : 0;
	$num2 = (key_exists('num2', $_POST)) ? $_POST['num2'] : 0;
	
	$nastranu = 15;
	$rewrite = false;
	
	if($action == 'change_thread')
	{
		$thr = (isset($_POST['threads']) && $_POST['threads'] == 0) ? false : true;
//		dump($thr);
		$_SESSION['threads'] = $thr;
	}
	
	if($action == 'delete' && session_is_registered('my_id') && in_array($_SESSION['my_id'], $admins))
	{
		$id = fnAdjustID();
		if($id != '')
		{
			$query = "DELETE FROM mobile_gbook WHERE id='$id' limit 1";
			$dba->ExecuteQuery($query);
		}
	}
	
	$get_reply = isset($_GET['reply']) ? '&amp;reply='.$_GET['reply'] : '';
	
	if(!isset($_SESSION['allow_write']))
	{
		$_SESSION['allow_write'] = false;
	}
	
//	if($action=='add' && $sum == ($num1 + $num2 - 2))
	if($action=='add')
	{
		$meno = (key_exists('meno', $_POST) && $_POST['meno']<>'') ? trim($_POST['meno']) : '';
		$text = (key_exists('text', $_POST) && $_POST['text']<>'') ? trim($_POST['text']) : '';
		
		$query = "SELECT * FROM mobile_gbook ORDER BY ID DESC limit 0, 1";
		$result = $dba->GetResult($query);
		$row = mysql_fetch_array($result);
		
		if($meno!='' && $text!='' && $_POST['chstr'] == 'dsfklhdsfiiusdfsdfnmn')
		{
			$lmc = (isset($_SESSION['last_message'])) ? $_SESSION['last_message'] : '';
			if(addslashes(htmlspecialchars($_POST['text']))!=$row['text'] && $lmc != addslashes(htmlspecialchars($_POST['text'])))
			{
				$meno = addslashes(htmlspecialchars($_POST['meno']));
				$mail = addslashes(htmlspecialchars($_POST['mail']));
				$text = addslashes(htmlspecialchars($_POST['text']));
				$datum = date("j.m.Y H:i:s");
				$admin = (fnIsAdmin()) ? 'a' : 'n';
				$notify = (isset($_POST['oznam']) && $_POST['oznam'] == 'a' && isset($_POST['submitter'])) ? $_POST['submitter'] : 0;
				$query = "INSERT INTO mobile_gbook (meno, mail, datum, text, admin, notify, ip) VALUES ('$meno', '$mail', '$datum', '$text', '$admin', '$notify', '".$_SERVER['REMOTE_ADDR']."')";
				$dba->ExecuteQuery($query);
				$get_reply = '';
				
				//vytvorenie vektora
				$max_id = $dba->Max('mobile_gbook', 'id');
				$vector = '';
				if($_POST['post_reply'] != 0)
				{
					$row = $dba->GetRow2('mobile_gbook', '*', 'id', $_POST['post_reply']);
					$vector .= $row['vector'];
					//notifikacia
					if($row['notify'] != 0)
					{
						$predmet = 'Odpoveď na Váš odkaz s ID '.$_POST['post_reply'];
						$notify_to = $row['notify'];
						$original_mess = $row['text'];
						$original_mess_date = $row['datum'];
						$sprava = "Na Váš odkaz pridaný o $original_mess_date:<br/>$original_mess<br/><br/>odpovedal $datum $meno:<br/>".$text;
						$query = "INSERT INTO mobile_posta (predmet, text, user_from, user_to, cas, visible_sender) values ('$predmet', '$sprava', -1, ".$notify_to.", NOW(), '0')";
						$dba->ExecuteQuery($query);
					}
					//END
				}
				else 
				{
					$vector .= '#';
				}
				$vector .= str_pad(100000 - $max_id, 5, '0', STR_PAD_LEFT).'#';
				$query = "UPDATE mobile_gbook SET vector = '$vector' WHERE id='$max_id' limit 1";
				$dba->ExecuteQuery($query);
				//koniec vektora
				
				$_SESSION['last_message'] = $text;
				$komu = "pesto@peterpech.com, miso85@azet.sk";
				$subject = "Nový odkaz na 4mobile";
		   		$sprava = $meno." - ".$mail." : ".$text;
		   		$headers = "From:4mobile <4mobile@4mobile.sk>\n";
		   		$headers .= "Content-Type: text/html; charset=iso-8859-2\n";
//		   		mail($komu,  $subject, $sprava, $headers);
	   		
				$action = '';
			}
			else
			{
				$add_err = 'Nemôžete dvakrát pridať ten istý odkaz!';
			}
		}
		else
		{
			$add_err = 'Nezadali ste svoje meno alebo text odkazu!';
			$rewrite = true;
		}
	}
//	elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && $sum != ($num1 + $num2 - 2))
	elseif($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		$add_err = 'Na prispievanie musíte mať povolený JavaScript';
		$rewrite = true;
	}
	
	srand(microtime()*10000000);
	$num1 = rand(1, 49);
	$num2 = rand(1, 49);
	
?>
	<script language="javascript">
	function AddSmile(s)
	{
	   document.forms.frm.text.value += ' ' + s + ' ';
	   document.forms.frm.text.focus();
   	}
   	<?php if(session_is_registered('my_id') && in_array($_SESSION['my_id'], $admins)) { ?>
	function DeleteConfirm(link)
	{
		if (confirm('Zmazať?')) window.location = link;
	}
	<?php } ?>
	</script>
    <div id="register_form01">
	  <div id="menu01_head">:: guestbook</div>
	  <?php
	  
//	  if($_SERVER['REQUEST_METHOD'] != 'POST')
//	  {
//	  	$_SESSION['allow_write'] = true;
//	  }
	  	echo "<div class='alert'>Z dôvodu veľkého množstva špekulatívnych registrácíí sme sa rozhodli zaviesť registrácie viazané na emailovú adresu.<br/><br/>
				Všetky existujúce kontá je nutné aktivovať v časti nastavenia vyplnením platnej emailovaj adresy, na ktorú si kliknutím na odkaz dáte zaslať
				aktivačný mail, v ktorom Vám bude doručená aktivačná linka, po ktorej navštívení bude Vaše konto aktivované.<br/><br/>
				Pokiaľ Vaše konto nebude aktivované, NEBUDÚ VÁM PRIPOČÍTAVANÉ KREDITY!!!<br/><br/>";
		
			echo "Aktivovať konto si môžete v časti <a href='http://4mobile.peterpech.com/index.php?module=setup_form'>Nastavenia</a>. ";
		
			echo "</div>";
	  
	  
	  ?>
	  <div id="melodie_01">
	   <?php if(isset($add_err)) echo "<br /><span id=\"red\">$add_err</span>";
	    ?>
		<form name="frm" style="padding-top: 10px;" action="index.php?module=gbook&amp;action=add<?php echo $get_reply; ?>" method="post">
		<table border="0" cellspacing="0" cellpadding="0" align="center">
		  <tr> 
		    <td width="100" align="right">Meno:</td>
		    <td align="left" colspan="2"> 
		      <input id="form_field01" type="text" name="meno" size="51" maxlength="30" value="<?php if(key_exists('meno', $_POST) && $action=='add' && $rewrite)
				{
					echo $_POST["meno"]; 
				}elseif(session_is_registered('log'))
				{
					echo $_SESSION['log'];
				}
				
				?>"/>
		    </td>
		  </tr>
		  <tr> 
		    <td align="right">Mail:</td>
		    <td align="left" colspan="2"> 
		      <input id="form_field01" type="text" name="mail" size="51" maxlength="30" value="<? if(key_exists('mail', $_POST) && $action=='add' && $rewrite) echo $_POST["mail"]; ?>"/>
		    </td>
		  </tr>
		  <!--<tr> 
		    <td align="right"><?php echo $num1." + ".$num2." =&nbsp;";?></td>
		    <td align="left" colspan="2"> 
		      <input id="form_field01" type="text" name="sum" size="3" maxlength="3" value="???" onclick="this.value='';"/> Povinné!!!
		      <input type="hidden" name="num1" value="<?php echo ($num1+1); ?>">
		      <input type="hidden" name="num2" value="<?php echo ($num2+1); ?>">
		    </td>
		  </tr>-->
		  <tr> 
		    <td ></td>
		    <td colspan="2"> 
		      <input type="hidden" name="chstr" id="chstr" value="">
		    </td>
		  </tr>
		  <?php
		  if(isset($_SESSION['mobile_logged_in']) && isset($_SESSION['my_id']))
		  {
		  ?>
		  <tr> 
		  	<td><input type="hidden" name="submitter" value="<?php echo $_SESSION['my_id']; ?>"/></td>
		    <td align="left" valign="middle" colspan="2"><input type="checkbox" name='oznam' value='a' checked="checked"/> Chcem dostať oznámenie ak mi niekto odpovie</td>
		  </tr>
		  <?php
		  }
		  ?>
		  <?php
		  if($reply != 0 && $get_reply != '')
		  {
		  	$line = mysql_fetch_assoc(mysql_query("SELECT * FROM mobile_gbook WHERE id='$reply' limit 0,1"));
		  ?>
		  <tr> 
		    <td align="right" valign="top">Odpoveď na:</td>
		    <td align="left" colspan="2"> 
		      <div class="reply_gbook_div">
		      	<span id="red"><b><?php echo $line['meno']; ?></b></span> 
		      	| odoslané: <?php echo $line['datum']; ?><br />
		      	<?php echo fnSmile(fnSmileys(fnCutLong(stripslashes($line['text'])))); ?> 
		      </div>


		    </td>
		  </tr>
		  <?php
		  }
		  ?>
		  <tr> 
		      <td valign="top" align="right">Správa:</td>
		      <td valign="top" align="left"> 
		        <textarea id="form_field01" rows="12" cols="50" name="text"><? if(key_exists('text', $_POST) && $action=='add' && $rewrite) echo $_POST["text"]; ?></textarea><br />
		      </td>
		      <td valign="top" style='line-height: 10px;'><?php  
		      for($j=1;$j<=20;$j++)
		      {
		      	echo"<a href=\"javascript:AddSmile('s".$j."')\"><img src=\"images/smile/".$j.".gif\" border=\"0\" align=\"top\" style=\"padding-bottom: 1.5px;\" alt=\"\"/></a> ";
		      	if($j%3==0) echo "<br />";
		      }
		      ?></td>
		  </tr>
		  <tr> 
		    <td></td>
		    <td align="left" valign="top">
		            <input id="form_button01" type="submit" name="Submit" value="Pošli"/>
		            <input id="form_button01" type="reset" name="reset" value="Reset"/>
		            <input type="hidden" name="post_reply" value="<?php echo $reply; ?>"/>
		    </td>
		    </form>
		  </tr>
		   <tr> 
		  	
		    <td colspan="2" align="right">
		    	&nbsp;
		    </td>
		  </tr>
		  <tr> 
		  	<form name="frm2" action="index.php?module=gbook&amp;action=change_thread" method="post">
		    <td align="right">
		    	Vlákna:</td>
		    <td align="left"> <select name='threads' id="form_field01" onchange="document.forms.frm2.submit();">
		    	<option value='1'<?php if($threads) echo " selected='selected'"; ?>>Zapnuté</option>
		    	<option value='0'<?php if(!$threads) echo " selected='selected'"; ?>>Vypnuté</option>
		    	</select>
		    </td>
		    </form>
		  </tr>
		</table><br />
		<span class="cervena"><b>PREDTÝM AKO SA ČOKOĽVEK SPÝTATE, PREČÍTAJTE SI ČASŤ FAQ!!!</b></span>
		<?php
		
			$query = "SELECT id FROM mobile_gbook";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$stranok = $chunk[2];
			$order_by = ($threads) ? 'vector ASC' : 'id DESC';
			$query = "SELECT * FROM mobile_gbook ORDER BY $order_by limit $lim, $nastranu";
			$result = $dba->GetResult($query);
			
			fnNavi($i, $stranok, 'module=gbook');
			fnWriteGbook2($result);
			fnNavi($i, $stranok, 'module=gbook');
			
		
		?>
	  </div>
	</div>
	<script language="JavaScript" type="text/javascript" src="http://4mobile.peterpech.com/gbakt.js"></script>