<?php

include("config.php");
include("core.php");


if($mime== WEB)
{
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

}else{
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

	echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"". " \"http://www.wapforum.org/DTD/wml_1.1.xml\">";
}

	$bcon = connectdb();
	if (!$bcon)
	{
if($mime== WEB)
{
	echo dberror($mime);

}else{
	echo dberror($mime);
}
	}
$getuthm = mysql_fetch_array(mysql_query("SELECT theme FROM ibwf_users WHERE id='".$uid."'"));
$uth = $getuthm[0];
if(!$uth=="")
{
$uthm = "$uth";
}else{
$uthm = "default.css";
}
$action = $_GET["action"];
$page = $_GET["page"];
$who = $_GET["who"];
$brws = explode(" ",$HTTP_USER_AGENT);
$ubr = $brws[0];
$uip = getip();
$id=$_GET["id"];
$botid = "eeb070e74e366473";
$input = $_POST["input"];
$custid=$_POST["custid"];
$hostname = "www.pandorabots.com";
$hostpath = "/pandora/talk-xml";

    	if((islogged($ses)==false)||($uid==0))
    	{
if($mime== WEB)
{
	echo notlogged($mime);

}else{
	echo notlogged($mime);
}

    	} 

 

	if(isipbanned($uip,$ubr))
    	{

      if(!isshield(getuid_ses($ses)))
      {
if($mime== WEB)
{
	echo useripbanned($mime,$uip,$ubr);

}else{
	echo useripbanned($mime,$uip,$ubr);
}

      }
    	}

	//echo isbanned($uid);
	if(isbanned($uid))
    	{
if($mime== WEB)
{
	echo userbanned($mime,$uid);

}else{
	echo userbanned($mime,$uid);
}

    	}
	if($mime== WEB)
	{
		echo webhead($uthm,$stitle);
                  
        	echo "<div class=\"center\">";
        	echo "<br/>";
        
        
        	addonline(getuid_ses($ses),"Chatting to Chatbot","");
    		if ($input!="")
    		{

        	$sendData = "botid=".$botid."&input=".urlencode($input)."&custid=".$custid;
    		// Send the request to Pandorabot
    		$result = PostToHost($hostname, $hostpath, $sendData);
    		//TODO: Process the returned XML as an XML document instead of a big string.
    		// Use string manipulations to pull out the 'custid' and 'that' values.
    		$pos = strpos($result, "custid=\"");

    		// Extract the custid
    		if ($pos === false) {
    		$custid = "";
    		} else {
    		$pos += 8;
    		$endpos = strpos($result, "\"", $pos);
    		$custid = substr($result, $pos, $endpos - $pos);
    		}
    		// Extrat <that> - this is the reply from the Pandorabot
    		$pos = strpos($result, "<that>");
    		if ($pos === false) {
    		$reply = "";
    		} else {
    		$pos += 6;
    		$endpos = strpos($result, "</that>", $pos);
    		$reply = unhtmlspecialchars(substr($result, $pos, $endpos - $pos));
    		}

        	//echo htmlspecialchars( $reply);
        	$hers = $reply;
        	$hers = parsemsg($hers);
            $input=htmlspecialchars($input);
            $nick = getnick_uid($uid);
            echo "<br/><b>$nick: </b>$input<br/>";
            echo "<b>Chatbot: </b>$hers<br/>";

		echo "<form action=\"chatbot.php?ses=$ses\" method=\"post\">";
        	echo "<input type=\"text\" name=\"input\" maxlength=\"120\" value=\"$input\"/>";
		echo "<input type=\"hidden\" name=\"custid\" value=\"$custid\"/>";
		echo "<input type=\"submit\" value=\"Say\"/>";
		echo "</form><br/>";
    		}else{
      	echo "Hello, now you can chat with our chatbot<br/> have fun<br/>";
		echo "<form action=\"chatbot.php?ses=$ses\" method=\"post\">";
        	echo "<input type=\"text\" name=\"input\" maxlength=\"120\" value=\"$input\"/>";
		echo "<input type=\"hidden\" name=\"custid\" value=\"$custid\"/>";
		echo "<input type=\"submit\" value=\"Say\"/>";
		echo "</form><br/>";
    		}
    
    		echo "<br/><br/><a href=\"index.php?action=main&amp;ses=$ses\"><img src=\"images/home.gif\" alt=\"*\"/>Home</a><br/>";
		echo "</div>";

		echo webfoot();

	}else{
		echo waphead($stitle);
                  
        	echo "<p align=\"center\">";
        	echo "<br/>";
        
        
        	addonline(getuid_ses($ses),"Chatting to Chatbot","");
    		if ($input!="")
    		{

        	$sendData = "botid=".$botid."&input=".urlencode($input)."&custid=".$custid;
    		// Send the request to Pandorabot
    		$result = PostToHost($hostname, $hostpath, $sendData);
    		//TODO: Process the returned XML as an XML document instead of a big string.
    		// Use string manipulations to pull out the 'custid' and 'that' values.
    		$pos = strpos($result, "custid=\"");

    		// Extract the custid
    		if ($pos === false) {
    		$custid = "";
    		} else {
    		$pos += 8;
    		$endpos = strpos($result, "\"", $pos);
    		$custid = substr($result, $pos, $endpos - $pos);
    		}
    		// Extrat <that> - this is the reply from the Pandorabot
    		$pos = strpos($result, "<that>");
    		if ($pos === false) {
    		$reply = "";
    		} else {
    		$pos += 6;
    		$endpos = strpos($result, "</that>", $pos);
    		$reply = unhtmlspecialchars(substr($result, $pos, $endpos - $pos));
    		}

        	//echo htmlspecialchars( $reply);
        	$hers = $reply;
        	$hers = parsemsg($hers);
            $input=htmlspecialchars($input);
            $nick = getnick_uid($uid);
            echo "<br/><b>$nick: </b>$input<br/>";
            echo "<b>Chatbot: </b>$hers<br/>";
        	echo "<br/><input type=\"text\" name=\"input\" maxlength=\"120\" value=\"$input\"/><anchor>";
        	echo " Say!";
		echo "<go href=\"chatbot.php?ses=$ses\" method=\"post\">";
		echo "<postfield name=\"input\" value=\"$(input)\"/>";
		echo "<postfield name=\"custid\" value=\"$custid\"/>";
		echo "</go>";
		echo "</anchor><br/>";
    		}else{
      	echo "Hello, now you can chat with our chatbot<br/> have fun<br/>";
      	echo "<input type=\"text\" name=\"input\" maxlength=\"120\" value=\"$input\"/><anchor>";
		echo " Say!";
		echo "<go href=\"chatbot.php?ses=$ses\" method=\"post\">";
		echo "<postfield name=\"input\" value=\"$(input)\"/>";
		echo "<postfield name=\"custid\" value=\"$custid\"/>";
		echo "</go>";
		echo "</anchor><br/>";
    		}
    
    		echo "<br/><br/><a href=\"index.php?action=main&amp;ses=$ses\"><img src=\"images/home.gif\" alt=\"*\"/>Home</a><br/>";
		echo "</p>";

		echo wapfoot();

	}

?>

