<?php
session_start();
$this_country=0;
include "lib.php";
foreach($HTTP_POST_VARS as $key => $value ){
$cgi[$key]=$value;
}
foreach($HTTP_GET_VARS as $key => $value ){
$cgi[$key]=$value;
}
$db = @mysql_connect('host', $conf["holywarsadmin"], $conf["password"]);
if (!$db) {
alert (mysql_error());
}
if (!@mysql_select_db($conf["holywarsadmin"], $db)) {
$str=mysql_error();
if ($str) alert ($str);
}
if ($cgi["uname"]){
//echo $cgi["uname"].$cgi["uemail"].$cgi["psword"];
$isLogined1=isLogined($cgi["uname"],$cgi["uemail"],$cgi["psword"]);
if ($isLogined1){
$usT=getUserDetails($isLogined1," active ");
logIP($isLogined1);
if ($usT->active==1){
$_SESSION["isLogined"]=$isLogined1;
$sN=$HTTP_SERVER_VARS['SCRIPT_NAME'];
//echo "==".$sN."==";
if (($sN==$conf["path"].'/index.php')||($sN==$conf["path"].'/battlefield.php')||($sN==$conf["path"].'/help.php')||($sN==$conf["path"].'/aboutus.php')||($sN==$conf["path"].'/register.php')||($sN==$conf["path"].'/forgotpass.php')||($sN==$conf["path"].'/spam.php')||($sN==$conf["path"].'/privacy.php')||($sN==$conf["path"].'/advertising.php')||($sN==$conf["path"].'/tos.php')||($sN==$conf["path"].'/activate.php')||($sN==$conf["path"].'/forgotpass.php')){
header("Location: base.php");
exit;
}
}elseif($usT->active==2){
$MessageStr="You are banned.<br> Try to contact admins of the site to get to know why this have taken place.";
include "message.php";
exit;
}elseif($usT->active==3){
$MessageStr="You are supporter.<br> Right now this status is not supported. <br> Try to contact admins of the site to get to know why this have taken place.";
include "message.php";
exit;
}elseif($usT->active==4){
setcookie ("isLogin", "1",time()+3600);
header("Location: admin/index.php");
exit;
}else{
$_SESSION["activationID"]=$isLogined1;
header("Location: activate.php");
}
}else {
$MessageStr="The login and password you have entered do not match.<br> Try to retype them again.";
include "message.php";
exit;
}
}
if ($_SESSION['isLogined']){
setLastSeen($_SESSION['isLogined'],time());
$sN=$HTTP_SERVER_VARS['SCRIPT_NAME'];
if (($sN==$conf["path"].'/index.php')
||($sN==$conf["path"].'/register.php')
||($sN==$conf["path"].'/activate.php')
||($sN==$conf["path"].'/recruit.php')
||($sN==$conf["path"].'/forgotpass.php')){
header("Location: base.php");
exit;
}
}
//-------------------------------------------- FUNCTIONS --------------------------------------------------------
function alert($msg){
echo "<script>alert(\"{$msg}\")</script>";
}
function isAdminLogined($uname,$psword){
global $conf;
if (($conf['admin_password']==$psword)&&($conf['admin_login']=$uname))return 1;
else return 0;
}
function isLogined($uname,$uemail,$psword){
$str="select * from `UserDetails` where userName='$uname' and e_mail='$uemail' and password='$psword'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st->ID;
}
}
function getUserByUniqId($uniqueLink ,$fields=" ID "){
$str="select $fields from `UserDetails` where uniqueLink ='$uniqueLink' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
$st->untrainedSold=floor($st->untrainedSold);
return $st;
}
}
function getUserDetailsByName($name,$fields=" ID "){
$str="select $fields from `UserDetails` where userName='$name' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
$st->untrainedSold=floor($st->untrainedSold);
return $st;
}
}
function getUserDetailsByEmail($email,$fields=" ID "){
$str="select $fields from `UserDetails` where e_mail='$email' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
$st->untrainedSold=floor($st->untrainedSold);
return $st;
}
}
function getUserDetails($id,$fields="*"){
$str="select $fields from `UserDetails` where ID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
$st->untrainedSold=floor($st->untrainedSold);
return $st;
}
}
function getUserIncome($user){
global $conf;
$income=0;
$income+=$user->trainedAttackSold;
$income+=$user->trainedDefSold;
$income+=$user->untrainedSold;
$income*=$conf["gold_from_soldier"];
if ($conf["race"][$user->race]["income"]){
$income+=round(($income*$conf["race"][$user->race]["income"])/100);
}
return $income;
}
function getWeaponAllocation($user,$weaponA,$trainedCount,$untrainedCount){
if (count($weaponA)>$trainedCount){
$trainedW=$trainedCount;
$untrainedUnW=$untrainedCount-count($weaponA)+$trainedW;
if ($untrainedUnW<0)$untrainedUnW=0;
$untrainedW=$untrainedCount-$untrainedUnW;
$trainedUnW=0;
}else{
$trainedW=count($weaponA);
$untrainedW=0;
$trainedUnW=$trainedCount-$trainedW;
$untrainedUnW=$untrainedCount;
}
if ($untrainedW<0)$untrainedW=0;
$wepAlloc["trainedW"]=$trainedW;
$wepAlloc["untrainedUnW"]=$untrainedUnW;
$wepAlloc["untrainedW"]=$untrainedW;
$wepAlloc["trainedUnW"]=$trainedUnW;
return $wepAlloc;
}
function getWeaponArray($weaponA1){
$k=0;
for ($i=0;$i<count($weaponA1);$i++){
for ($j=0; $j<$weaponA1[$i]->weaponCount ;$j++){
$weaponA[$k]=$weaponA1[$i];
$k++;
}
}
return $weaponA;
}
function getStrikeAction($user){
global $conf;
$num=0;
$trainedCount=$user->trainedAttackSold+$user->trainedAttackMerc;
$untrainedCount=$user->untrainedMerc+$user->untrainedSold;
$weaponA1=getUserWeapon($user);
if (!count($weaponA1)&&!$trainedCount) return 0;
/*$k=0;
for ($i=0;$i<count($weaponA1);$i++){
for ($j=0; $j<$weaponA1[$i]->weaponCount ;$j++){
$weaponA[$k]=$weaponA1[$i];
$k++;
}
}*/
$weaponA=getWeaponArray($weaponA1);
if ($weaponA){
$wepAlloc=getWeaponAllocation($user,$weaponA,$trainedCount,$untrainedCount);
$wI=0;
for ($i=0; $i<$wepAlloc["trainedW"];$i++,$wI++){
$num+=$weaponA[$wI]->weaponStrength*5;
}
for ($i=0; $i<$wepAlloc["untrainedW"];$i++,$wI++){
$num+=$weaponA[$wI]->weaponStrength*4;
}
}else{
$wepAlloc["trainedUnW"]=$trainedCount;
$wepAlloc["untrainedUnW"]=$untrainedCount;
}
$num+=$wepAlloc["trainedUnW"]*5;
$num+=$wepAlloc["untrainedUnW"]*4;
for ($i=0; $i<=$user->siegeLevel;$i++){
if ($conf["race"][$user->race]["siege"][$i]["attack"]){
$num+=round(($num*$conf["race"][$user->race]["siege"][$i]["attack"])/100);
}
}
if ($conf["race"][$user->race]["attack"]){
$num+=round(($num*$conf["race"][$user->race]["attack"])/100);
}
return $num;
}
function getDefenseAction($user){
global $conf;
$num=0;
$trainedCount=$user->trainedDefSold+$user->trainedDefMerc;
$untrainedCount=$user->untrainedMerc+$user->untrainedSold;
$weaponA1=getDefUserWeapon($user);
if (!count($weaponA1)&&!$trainedCount) return 0;
$k=0;
for ($i=0;$i<count($weaponA1);$i++){
for ($j=0; $j<$weaponA1[$i]->weaponCount ;$j++){
$weaponA[$k]=$weaponA1[$i];
$k++;
}
}
if ($weaponA){
$wepAlloc=getWeaponAllocation($user,$weaponA,$trainedCount,$untrainedCount);
$wI=0;
for ($i=0; $i<$wepAlloc["trainedW"];$i++,$wI++){
$num+=$weaponA[$wI]->weaponStrength*5;
}
for ($i=0; $i<$wepAlloc["untrainedW"];$i++,$wI++){
$num+=$weaponA[$wI]->weaponStrength*4;
}
}else{
$wepAlloc["trainedUnW"]=$trainedCount;
$wepAlloc["untrainedUnW"]=$untrainedCount;
}
$num+=$wepAlloc["trainedUnW"]*5;
$num+=$wepAlloc["untrainedUnW"]*4;
for ($i=0; $i<=$user->fortificationLevel ;$i++){
if ($conf["race"][$user->race]["fortification"][$i]["defence"]){
$num+=round(($num*$conf["race"][$user->race]["fortification"][$i]["defence"])/100);
}
}
if ($conf["race"][$user->race]["defend"]){
$num+=round(($num*$conf["race"][$user->race]["defend"])/100);
}
return $num;
}
function getCovertAction($user){
global $conf;
$num=0;
if (!$user->spies){return 0;}
$num=round($user->spies*pow(2,$user->currentSpySkill));
if ($conf["race"][$user->race]["spy"]){
$num+=round(($num*$conf["race"][$user->race]["spy"])/100);
}
return $num;
}
function setWeapon($id,$fields){
$str = "update `Weapon` set $fields WHERE ID='$id' ";
//echo "$str<br>";
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
}
function delWeapon($id){
$str = "DELETE FROM `Weapon` WHERE ID='$id'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}
function getUserAllWeapon($user){
$str="SELECT * FROM `Weapon` Where userID='{$user->ID}' ORDER BY `weaponStrength` DESC ";
//print $str;
$q = @mysql_query($str);
if (!$q) { print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) { return; }
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getUserWeapon($user){
$str="SELECT * FROM `Weapon` Where isAtack='1' and userID='{$user->ID}' ORDER BY `weaponStrength` DESC ";
//print $str;
$q = @mysql_query($str);
if (!$q) { print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) { return; }
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getDefUserWeapon($user){
$str="SELECT * FROM `Weapon` Where isAtack='0' and userID='{$user->ID}' ORDER BY `weaponStrength` DESC";
//print $str;
$q = @mysql_query($str);
if (!$q) { print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) { return; }
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getTotalFightingForce($user){
$count=0;
$count+=$user->trainedAttackSold;
$count+=$user->trainedAttackMerc ;
$count+=$user->trainedDefSold ;
$count+=$user->trainedDefMerc ;
$count+=$user->untrainedSold ;
$count+=$user->untrainedMerc ;
$count+=$user->spies ;
return $count;
}
function getActiveUsers($fields="*"){
$str="SELECT $fields FROM `UserDetails` WHERE active='1'";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$row->untrainedSold=floor($row->untrainedSold);
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getRanksList($page){
global $conf;
$start=($page-1)*$conf['users_per_page'];
$str="SELECT userID,rank FROM `Ranks` WHERE rank<>0 ORDER BY `rank` ASC LIMIT $start,{$conf['users_per_page']} ";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getRanksUsersList($page, $fields=" ID, userName ,trainedAttackSold ,trainedAttackMerc ,trainedDefSold ,trainedDefMerc ,untrainedSold, untrainedMerc, spies, race ,gold "){
//global $conf;
$users=getRanksList($page);
for ($i=0; $i<count($users);$i++){
$usersA[$i]=getUserDetails($users[$i]->userID,$fields);
$usersA[$i]->rank=$users[$i]->rank;
}
return $usersA;
}
function searchRanksUsersListCount($str){
$str="SELECT COUNT(*) FROM `UserDetails`,`Ranks` WHERE UserDetails.ID = Ranks.userID AND rank<>0 AND active='1' AND userName LIKE '$str' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function searchRanksUsersList($page, $str,$fields=" UserDetails.ID, userName ,trainedAttackSold ,trainedAttackMerc ,trainedDefSold ,trainedDefMerc ,untrainedSold, untrainedMerc, spies, race ,gold, rank "){
global $conf;
$start=($page-1)*$conf['users_per_page'];
$str="SELECT $fields FROM `UserDetails`,`Ranks` WHERE UserDetails.ID = Ranks.userID AND rank<>0 AND active='1' AND userName LIKE '$str' ORDER BY `rank` ASC LIMIT $start,{$conf['users_per_page']} ";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$row->untrainedSold=floor($row->untrainedSold);
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getUserRanks($id){
$str="select * from `Ranks` where userID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
$st->rank='unranked';
$st->strikeActionRank='unranked';
$st->defenceActionRank ='unranked';
$st->covertActionRank ='unranked';
return $st;
}
else{
$st="";
$st = mysql_fetch_object($q);
if (!$st->rank){
$st->rank='unranked';
}
if (!$st->strikeActionRank){
$st->strikeActionRank='unranked';
}
if (!$st->defenceActionRank ){
$st->defenceActionRank ='unranked';
}
if (!$st->covertActionRank ){
$st->covertActionRank ='unranked';
}
return $st;
}
}
function createUser($userName,$race,$e_mail,$password,$commander,$active=0,$uniqueLink="",$fortificationLevel=0,$siegeLevel=0,
$gold=2800,$lastTurnTime=0,$attackTurns=17,$currentUnitProduction=0,$currentSpySkill=0,
$trainedAttackSold=0,$trainedAttackMerc=0,$trainedDefSold=0,$trainedDefMerc=0,$untrainedSold=1,$untrainedMerc=0,$spies=0){
if (!$lastTurnTime){$lastTurnTime=time();}
$uniqueLink=genUniqueLink();
$str = "INSERT INTO `UserDetails` (userName,race,e_mail,password,commander,active,uniqueLink,fortificationLevel,siegeLevel,
gold,lastTurnTime,attackTurns,currentUnitProduction,currentSpySkill,
trainedAttackSold,trainedAttackMerc,trainedDefSold,trainedDefMerc,untrainedSold,untrainedMerc,spies) VALUES ('$userName','$race','$e_mail','$password','$commander','$active','$uniqueLink','$fortificationLevel','$siegeLevel',
'$gold','$lastTurnTime','$attackTurns','$currentUnitProduction','$currentSpySkill',
'$trainedAttackSold','$trainedAttackMerc','$trainedDefSold','$trainedDefMerc','$untrainedSold','$untrainedMerc','$spies')";
echo "$str<br>";
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
$us=getUserDetailsByName($userName);
$userID=$us->ID;
$str= "INSERT INTO `Ranks` (userID) VALUES ('$userID') ";
$q = @mysql_query($str);
return $q;
}
function getActiveUsersCount(){
$str="SELECT COUNT(*) FROM `UserDetails` where active='1'";
//echo $str;
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
//echo "--{$st[0]}---";
}
function getOnlineUsersCount(){
global $conf;
$time=time()-$conf["minutes_per_turn"]*60;
$str="SELECT COUNT(*) FROM `UserDetails` where lastTurnTime>'$time' and active='1'";
//echo time()."<br>";
//echo $str;
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
//echo "--{$st[0]}---";
}
function getOldUsers(){
global $conf;
$time=time()-$conf["days_of_inactivity_before_delete_this_user"]*24*60*60;
$str="SELECT ID, active FROM `UserDetails` where lastTurnTime<'$time'";
//alert($str);
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function updateUser($id, $str){
$str = "update `UserDetails` set $str WHERE ID='$id' ";
//echo "$str<br>";
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
return $q;
}
function updateMercenary($str){
$str = "update `Mercenaries` set $str ";
//echo "$str<br>";
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
}
function setUserRank($id, $rank, $strikeActionRank, $defenceActionRank ,$covertActionRank ){
$str = "update `Ranks` set rank='$rank' , strikeActionRank='$strikeActionRank', defenceActionRank='$defenceActionRank', covertActionRank='$covertActionRank' WHERE userID='$id' ";
//echo "$str<br>";
//return;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
}
function setLastSeen($id, $date){
updateUser($id," lastTurnTime = '$date' ");
}
function setLastTurnTime($date){
updateMercenary(" lastTurnTime = '$date' ");
}
function deleteUserWeapon($id,$weaponID=""){
if ($weaponID ){$str2=" AND weaponID='$weaponID' ";}
$str = "DELETE FROM `Weapon` WHERE userID='$id' $str2";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}
function clearRanks($id){
$str = "update `Ranks` set rank ='0', strikeActionRank ='0',defenceActionRank ='0',covertActionRank ='0' WHERE userID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}
function deleteUser($id){
$str = "DELETE FROM `UserDetails` WHERE ID='$id'";
//echo $str;
$q = @mysql_query($str);
$str = "DELETE FROM `Ranks` WHERE userID='$id'";
$q = @mysql_query($str);
deleteUserWeapon($id);
deleteIP($id);
deleteAtacksOfUser($id);
deleteSpyLogsOfUser($id);
deleteMessagesOfUser($id);
$str = "update `UserDetails` set commander=0 WHERE commander='$id' ";
$q = @mysql_query($str);
/*
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}*/
}
function deleteOldUsers(){
$users=getOldUsers();
//echo 1;
for ($i=0;$i<count($users);$i++){
if (($users[$i]->active==1)||((!$users[$i]->active)))
deleteUser($users[$i]->ID);
}
//echo 2;
}
function addTurns($id,$addTurns,$lastTurnTime){
//$str = "INSERT INTO `UserDetails` (attackTurns ,lastTurnTime ) VALUES ('/banners/$bname')";
$str = "update `UserDetails` set attackTurns='$addTurns', lastTurnTime='$lastTurnTime' where ID='$id'";
//echo $str;
mysql_query($str);
}
function getNextTurn($user){
global $conf;
$info=getCommonInfo();
$lastTurnTime=$info->lastTurnTime;
$thisTime=time();
$dif=$thisTime-$lastTurnTime;
//$nextTurn=$dif;
$nextTurnMin=$dif/60;
/*if ($nextTurnMin>$conf["minutes_per_turn"]){
//echo "--------";
$addTurnsF=$dif/($conf["minutes_per_turn"]*60);
$addTurns=floor($addTurnsF);
//$nextTurn1=($addTurnsF-$addTurns)*$conf["minutes_per_turn"];
$nextTurn1=$dif-(($conf["minutes_per_turn"]*60)*$addTurns);
//echo $addTurns."##".$nextTurn1."##".($thisTime-$nextTurn1)."<br>";
$nextTurn=round($nextTurn1/60);
$addTurns+=$user->attackTurns;
addTurns($user->ID,$addTurns,$thisTime-$nextTurn1);
}else*/
{
$nextTurn=round($nextTurnMin);
}
//echo $dif."<br>";
$nextTurn=$conf["minutes_per_turn"]-$nextTurn;
if ($nextTurn<0)$nextTurn=0;
return $nextTurn;//($conf["minutes_per_turn"]-$nextTurn);
}
function getCommonInfo(){
$str="select * from `Mercenaries`";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st;
}
}
//----------------------------Messages-----------------------------------------------
function getMessagesCount($userID){
$str = "SELECT COUNT(*) FROM `Messages` where userID='$userID' ";
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getAllMessages($userID ){
$str="SELECT * FROM `Messages` WHERE userID='$userID'";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$st[$i]->subject =urldecode ($st[$i]->subject);
$st[$i]->text =urldecode ($st[$i]->text);
$i++;
}
return $st;
}
}
function getMessage($messID){
$str="select * from `Messages` where ID='$messID' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
$st->subject =urldecode ($st->subject);
$st->text =urldecode ($st->text);
return $st;
}
}
function sendMessage($id,$toid,$subject,$text){
$text=urlencode($text);
$subject=urlencode($subject);
$date=time();
$str = "INSERT INTO `Messages` (fromID , userID ,subject ,text,date ) VALUES ('$id','$toid','$subject','$text','$date')";
$q = @mysql_query($str);
return $q;
}
function deleteMessage($mesID){
$str = "DELETE FROM `Messages` WHERE ID='$mesID'";
//echo $str;
$q = @mysql_query($str);
}
function deleteMessagesOfUser($id){
$str = "DELETE FROM `Messages` WHERE userID='$id'";
//echo $str;
$q = @mysql_query($str);
}
//----------------------------END Messages-----------------------------------------------
//----------------------------Officers-----------------------------------------------
function getOfficers($id, $page, $fields="userID,userName, rank, trainedAttackSold ,trainedAttackMerc ,trainedDefSold ,trainedDefMerc ,untrainedSold, untrainedMerc, spies, race"){
global $conf;
$start=($page-1)*$conf['users_per_page'];
$str="SELECT $fields FROM `UserDetails`,`Ranks` WHERE Ranks.userID=UserDetails.ID AND commander='$id' AND active='1' AND rank<>'0' ORDER BY `rank` ASC LIMIT $start,{$conf['users_per_page']} ";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getOfficersCount($id){
$str="SELECT COUNT(*) FROM `UserDetails`,`Ranks` where Ranks.userID=UserDetails.ID AND commander='$id' AND active='1' AND rank<>'0'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
//echo "--{$st[0]}---";
}
//----------------------------END Officers-----------------------------------------------
//-----------------------------Convertions--------------------------------------------
function numecho ($str){
if ($str=="unranked"){echo $str;}
elseif($str=="None"){echo $str;}
else{
//echo $str;
echo number_format($str);
}
}
function vDate($time){
$timenow=time();
$timenow=date ("M d, Y",$timenow);
$time1=date ("M d, Y",$time);
if ($timenow==$time1){
$time1=date ("H:i",$time);
}
return $time1;
}
function genRandomPas(){
$pas=rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
return $pas;
}
function genUniqueLink(){
$time=time();
$str=chr( rand(ord('a'),ord('z')) ).chr( rand(ord('a'),ord('z')) ).$time;
return $str;
/*
for ($i=0; $i<strlen($time);$i++){
//$str.=chr(ord('a'))
}
*/
}
function genUniqueTxt($n){
for ($i=0;$i<$n;$i++){
if (rand(0,1)){
$str.=chr( rand(ord('A'),ord('Z')) );
}else{
$str.=chr( rand(ord('0'),ord('9')) );
}
}
return $str;
/*
for ($i=0; $i<strlen($time);$i++){
//$str.=chr(ord('a'))
}
*/
}
//-----------------------------End Convertions--------------------------------------------
//------------------------------Security---------------------------------------------
function addIP($ip,$userID){
$time=time();
$str= "INSERT INTO `IPs` (ip,userID,time) VALUES ('$ip','$userID','$time') ";
$q = @mysql_query($str);
if (!$q) { print ('Query failed: '.mysql_error()); return; }
}
function isIP($ip){
return 0;
$str="SELECT * FROM `IPs` WHERE ip='$ip' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function isIPandUser($ip,$id){
$str="SELECT * FROM `IPs` WHERE ip='$ip' AND userID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function isIPNewerThen($ip,$time){
//return 0;
$time=time()-$time;
$str="SELECT * FROM `IPs` WHERE ip='$ip' AND time>'$time' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_object($q);
return $st;
}else{return 0;}
}
function getIP($id){
$str="select * from `IPs` where userID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st;
}
}
function getUserIPs($id){
$str="SELECT * FROM `IPs` Where userID='$id' ORDER BY `time` DESC ";
//print $str;
$q = @mysql_query($str);
if (!$q) { print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) { return; }
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row; $i++;
}
return $st;
}
}
function deleteIP($id){
$str = "DELETE FROM `IPs` WHERE userID='$id'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}
function deleteIPByIP($ip){
$str = "DELETE FROM `IPs` WHERE ip='$ip'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}
function deleteIPByID($id){
$str = "DELETE FROM `IPs` WHERE ID='$id'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}
function logIP($id){
global $HTTP_SERVER_VARS,$conf;
$ip=$HTTP_SERVER_VARS['REMOTE_ADDR'];
if (!isIPandUser($ip,$id)){
$IPs=getUserIPs($id);
if (count ($IPs)>=$conf["ips_to_hold_per_user"]){
//print_r ($IPs);
//echo $IPs[count($IPs)-1]->ID;
//echo "##".count($IPs);
deleteIPByID($IPs[count($IPs)-1]->ID);
}
addIP($ip,$id);
}
}
//------------------------------END Security---------------------------------------------
//-----------------------------Atack---------------------------------------------------
function getAtackCount($userID){
$str = "SELECT COUNT(*) FROM `AtackLog` where userID='$userID' ";
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getDefenceCount($userID){
$str = "SELECT COUNT(*) FROM `AtackLog` where toUserID='$userID' ";
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getAllAtacks($userID ){
$str="SELECT * FROM `AtackLog` WHERE userID='$userID'";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getAllDefences($userID ){
$str="SELECT * FROM `AtackLog` WHERE toUserID='$userID'";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getAtack($id){
$str="select * from `AtackLog` where ID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st;
}
}
function getAtackByAtackerCount($id){
$str="SELECT COUNT(*) FROM `AtackLog` where userID='$id'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getAtackByAtacker($id,$page){
global $conf;
$start=($page-1)*$conf['users_per_page'];
//ORDER BY `rank` ASC LIMIT $start,{$conf['users_per_page']}
$str="select * from `AtackLog` where userID='$id' ORDER BY `time` DESC LIMIT $start,{$conf['users_per_page']}";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$st[$i]->subject =urldecode ($st[$i]->subject);
$st[$i]->text =urldecode ($st[$i]->text);
$i++;
}
return $st;
}
}
function getAtackByDefenderCount($id){
$str="SELECT COUNT(*) FROM `AtackLog` where toUserID='$id'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getAtackByDefender($id,$page){
global $conf;
$start=($page-1)*$conf['users_per_page'];
$str="select * from `AtackLog` where toUserID ='$id' ORDER BY `time` DESC LIMIT $start,{$conf['users_per_page']}";
//echo $str;
$q = @mysql_query($str);
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$st[$i]->subject =urldecode ($st[$i]->subject);
$st[$i]->text =urldecode ($st[$i]->text);
$i++;
}
return $st;
}
}
function getAtackByUser1User2AndTime($User1,$User2,$time, $fields="*"){
$str="select $fields from `AtackLog` where userID='$User1' AND toUserID='$User2' AND time='$time' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st;
}
}
function addAtack($id,$toid,$fields,$values){
$text=urlencode($text);
$subject=urlencode($subject);
$date=time();
$str = "INSERT INTO `AtackLog` (userID, toUserID, $fields ) VALUES ($id,$toid, $values )";
//echo $str;
$q = @mysql_query($str);
return $q;
}
function deleteAtack($id){
$str = "DELETE FROM `AtackLog` WHERE ID='$id'";
//echo $str;
$q = @mysql_query($str);
}
function deleteOldAtacks(){
$time=time()-$conf["days_to_hold_logs"]*24*60*60;
$str="DELETE FROM `AtackLog` where time<'$time'";
}
function deleteAtacksOfUser($userID){
$str = "DELETE FROM `AtackLog` WHERE userID='$userID'";
//echo $str;
$q = @mysql_query($str);
}
//-----------------------------End Atack---------------------------------------------------
//-----------------------------Spy---------------------------------------------------
function getSpyCount($userID){
$str = "SELECT COUNT(*) FROM `SpyLog` where userID='$userID' ";
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getSpyDefenceCount($userID){
$str = "SELECT COUNT(*) FROM `SpyLog` where toUserID='$userID' ";
$q = @mysql_query($str);
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getAllSpys($userID ){
$str="SELECT * FROM `SpyLog` WHERE userID='$userID'";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getAllSpyDefences($userID ){
$str="SELECT * FROM `SpyLog` WHERE toUserID='$userID'";
//print $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$i++;
}
return $st;
}
}
function getSpy($id){
$str="select * from `SpyLog` where ID='$id' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st;
}
}
function getSpyBySpyerCount($id){
$str="SELECT COUNT(*) FROM `SpyLog` where userID='$id'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getSpyBySpyer($id,$page){
global $conf;
$start=($page-1)*$conf['users_per_page'];
//ORDER BY `rank` ASC LIMIT $start,{$conf['users_per_page']}
$str="select * from `SpyLog` where userID='$id' ORDER BY `time` DESC LIMIT $start,{$conf['users_per_page']}";
//echo $str;
$q = @mysql_query($str);
if (!$q) {
print ('Query failed: '.mysql_error());
return;
}
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$st[$i]->subject =urldecode ($st[$i]->subject);
$st[$i]->text =urldecode ($st[$i]->text);
$i++;
}
return $st;
}
}
function getSpyByDefenderCount($id){
$str="SELECT COUNT(*) FROM `SpyLog` where toUserID='$id' AND isSuccess='0'";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error()); return; }
if ($q){
$st = mysql_fetch_array($q);
return $st[0];
}else{return 0;}
}
function getSpyByDefender($id,$page){
global $conf;
$start=($page-1)*$conf['users_per_page'];
$str="select * from `SpyLog` where toUserID ='$id' AND isSuccess='0' ORDER BY `time` DESC LIMIT $start,{$conf['users_per_page']}";
//echo $str;
$q = @mysql_query($str);
if (!@mysql_num_rows($q)) {return;}
else{
$st="";
$i=0;
while ($row = mysql_fetch_object($q)){
$st[$i]=$row;
$st[$i]->subject =urldecode ($st[$i]->subject);
$st[$i]->text =urldecode ($st[$i]->text);
$i++;
}
return $st;
}
}
function getSpyByUser1User2AndTime($User1,$User2,$time, $fields="*"){
$str="select $fields from `SpyLog` where userID='$User1' AND toUserID='$User2' AND time='$time' ";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error());return;}
if (!@mysql_num_rows($q)) {
return 0;
}
else{
$st="";
$st = mysql_fetch_object($q);
return $st;
}
}
function addSpy($id,$toid,$fields,$values){
$text=urlencode($text);
$subject=urlencode($subject);
$date=time();
$str = "INSERT INTO `SpyLog` (userID, toUserID, $fields ) VALUES ($id,$toid, $values )";
//echo $str;
$q = @mysql_query($str);
if (!$q) {print ('Query failed: '.mysql_error());return;}
return $q;
}
function deleteSpy($id){
$str = "DELETE FROM `SpyLog` WHERE ID='$id'";
//echo $str;
$q = @mysql_query($str);
}
function deleteOldSpyLogs(){
$time=time()-$conf["days_to_hold_logs"]*24*60*60;
$str="DELETE FROM `SpyLog` where time<'$time'";
}
function deleteSpyLogsOfUser($userID){
$str = "DELETE FROM `SpyLog` WHERE userID='$userID'";
//echo $str;
$q = @mysql_query($str);
}
//-----------------------------End Spy---------------------------------------------------
//------------------------------Mitia-------------------------------------------------
function RepairWeapon($wepid,$wal,$user,$type)
{
global $conf;
if($type)$tp='weapon';
else $tp='defenseweapon';
$q = @mysql_query("select weaponCount,weaponStrength from `Weapon` where weaponID='$wepid' and userID='$user->ID' and isAtack='$type' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($conf["race"][$user->race][$tp][$wepid]["strength"]!=0)
$pris=round(700/$conf["race"][$user->race][$tp][$wepid]["strength"])*$wal*$el["weaponCount"];
else return;
if($pris <= ($user->gold)){
if(($wal+$el["weaponStrength"]) <= $conf["race"][$user->race][$tp][$wepid]["strength"]){
$q = @mysql_query("update `Weapon` set weaponStrength=weaponStrength+'$wal' where weaponID='$wepid' and userID='$user->ID' and isAtack='$type' ");
$q = @mysql_query("update `UserDetails` set gold=gold-'$pris' where ID='$user->ID' ");}}
else return 'NO GOLD LEFT!!!';
}
function ScrapSell($wepid,$wal,$a,$user,$type)
{
global $conf;
if($type)$tp='weapon';
else $tp='defenseweapon';
$q = @mysql_query("select weaponCount,weaponStrength from `Weapon` where weaponID='$wepid' and userID='$user->ID' and isAtack='$type' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($conf["race"][$user->race][$tp][$wepid]["strength"]!=0)
{$pris=round($conf["race"][$user->race][$tp][$wepid]["price"]*($el["weaponStrength"]/$conf["race"][$user->race][$tp][$wepid]["strength"]-0.2))*$wal;}
else return;
if($wal<$el["weaponCount"]){
$q = @mysql_query("update `Weapon` set weaponCount=weaponCount-'$wal' where weaponID='$wepid' and userID='$user->ID' and isAtack='$type' ");
if($a!='Scrap'){$q = @mysql_query("update `UserDetails` set gold=gold+'$pris' where ID='$user->ID' ");}}
if($wal==$el["weaponCount"]){
$q = @mysql_query("delete from `Weapon` where weaponID='$wepid' and userID='$user->ID' and isAtack='$type' ");
if($a!='Scrap'){$q = @mysql_query("update `UserDetails` set gold=gold+'$pris' where ID='$user->ID' ");}}
}
function BuyWeapon($wepid,$wal,$at,$user)
{
global $conf;
if($at)$tp='weapon';
else $tp='defenseweapon';
$pris=$conf["race"][$user->race][$tp][$wepid]["price"]*$wal;
$stren=$conf["race"][$user->race][$tp][$wepid]["strength"];
if($pris <= ($user->gold)){
$q = @mysql_query("select weaponCount from `Weapon` where weaponID='$wepid' and userID='$user->ID' and isAtack='$at' ");
if (@mysql_num_rows($q)){
$q = @mysql_query("update `Weapon` set weaponCount=weaponCount+'$wal' where weaponID='$wepid' and userID='$user->ID' and isAtack='$at' ");
$q = @mysql_query("update `UserDetails` set gold=gold-'$pris' where ID='$user->ID' ");}
else{
$q = @mysql_query("insert into `Weapon` (weaponID, weaponStrength, weaponCount, isAtack, userID) values ('$wepid', '$stren', '$wal', '$at', '$user->ID')");
$q = @mysql_query("update `UserDetails` set gold=gold-'$pris' where ID='$user->ID' ");}}
else return 'NO GOLD LEFT!!!';
}
function Upgrade($user,$type,$wtd)
{
global $conf;
if($wtd=='No more upgrades available') return;
else{
if($type=='fortification'){
$pris=$conf["race"][$user->race]["fortification"][$user->fortificationLevel+1]["price"];
if($pris <= ($user->gold)){
$q = @mysql_query("update `UserDetails` set fortificationLevel=fortificationLevel+'1', gold=gold-'$pris' where ID='$user->ID' ");}
else return 'NO GOLD LEFT!!!';}
if($type=='siege'){
$pris=$conf["race"][$user->race]["siege"][$user->siegeLevel+1]["price"];
if($pris <= ($user->gold)){
$q = @mysql_query("update `UserDetails` set siegeLevel=siegeLevel+'1', gold=gold-'$pris' where ID='$user->ID' ");}
else return 'NO GOLD LEFT!!!';}
}
}
function Train($user,$wal,$type)
{
$nogold="Not enough gold!";
$nosold="Not enough untrained soldies!";
if($type==0){
$pris=2000*$wal;
if($pris <= ($user->gold)){
$q = @mysql_query("select untrainedSold from `UserDetails` where ID='$user->ID' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($el[untrainedSold]>=$wal){
$q = @mysql_query("update `UserDetails` set trainedAttackSold=trainedAttackSold+'$wal', untrainedSold=untrainedSold-'$wal', gold=gold-'$pris' where ID='$user->ID' ");
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}else return $nosold;
}
else return $nogold;
}
elseif($type==1){
$pris=2000*$wal;
if($pris <= ($user->gold)){
$q = @mysql_query("select untrainedSold from `UserDetails` where ID='$user->ID' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($el[untrainedSold]>=$wal){
$q = @mysql_query("update `UserDetails` set trainedDefSold=trainedDefSold+'$wal', untrainedSold=untrainedSold-'$wal', gold=gold-'$pris' where ID='$user->ID' ");
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}else return $nosold;
}
else return $nogold;
}
elseif($type==2){
$pris=3500*$wal;
if($pris <= ($user->gold)){
$q = @mysql_query("select untrainedSold from `UserDetails` where ID='$user->ID' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($el[untrainedSold]>=$wal){
$q = @mysql_query("update `UserDetails` set spies=spies+'$wal', untrainedSold=untrainedSold-'$wal', gold=gold-'$pris' where ID='$user->ID' ");
if (!$q) {print ('Query failed: '.mysql_error()); return; }
}else return $nosold;
}
else return $nogold;
}
elseif($type==3){
$q = @mysql_query("select trainedAttackSold from `UserDetails` where ID='$user->ID' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($el[trainedAttackSold])$q = @mysql_query("update `UserDetails` set trainedAttackSold=trainedAttackSold-'$wal', untrainedSold=untrainedSold+'$wal' where ID='$user->ID' ");
}
elseif($type==4){
$q = @mysql_query("select trainedDefSold from `UserDetails` where ID='$user->ID' ");
$el=mysql_fetch_array($q, MYSQL_ASSOC);
if($el[trainedDefSold])$q = @mysql_query("update `UserDetails` set trainedDefSold=trainedDefSold-'$wal', untrainedSold=untrainedSold+'$wal' where ID='$user->ID' ");
}
}
function Trainupgrade($user,$type)
{
if($type=='spy'){
$pris=pow(2,$user->currentSpySkill)*12000;
if($pris <= ($user->gold))
$q = @mysql_query("update `UserDetails` set currentSpySkill=currentSpySkill+'1', gold=gold-'$pris' where ID='$user->ID' ");
else return 'NO GOLD LEFT!!!';}
if($type=='unit'){
$pris=$user->currentUnitProduction*10000+10000;
if($pris <= ($user->gold))
$q = @mysql_query("update `UserDetails` set currentUnitProduction=currentUnitProduction+'1', gold=gold-'$pris' where ID='$user->ID' ");
else return 'NO GOLD LEFT!!!';}
}
?>