diff --git a/GameEngine/Alliance.php b/GameEngine/Alliance.php index f1ee6ff6..0197ae4a 100755 --- a/GameEngine/Alliance.php +++ b/GameEngine/Alliance.php @@ -80,6 +80,16 @@ class Alliance { } } + /** + * Redirects to the option menu if some errors were generated + */ + + public function redirect() + { + header("location: allianz.php?s=5"); + exit; + } + public function procAlliForm($post) { if(isset($post['ft'])) { switch($post['ft']) { @@ -89,7 +99,7 @@ class Alliance { } } - if(isset($_POST['dipl']) and isset($_POST['a_name'])) { + if(isset($post['dipl']) && isset($post['a_name'])) { $this->changediplomacy($post); } @@ -177,9 +187,9 @@ class Alliance { global $database, $session; if($session->access != BANNED){ foreach($this->inviteArray as $invite) { - if($invite['id'] == $get['d']) { + if($invite['id'] == $get['d'] && $invite['uid'] == $session->uid) { $database->removeInvitation($get['d']); - $database->insertAlliNotice($invite['alliance'], '' . addslashes($session->username) . ' has rejected the invitation.'); + $database->insertAlliNotice($invite['alliance'], ''.addslashes($session->username).' has rejected the invitation.'); } } header("Location: build.php?gid=18"); @@ -198,10 +208,10 @@ class Alliance { if($session->access != BANNED){ $inviteArray = $database->getAliInvitations($session->alliance); foreach($inviteArray as $invite) { - if($invite['id'] == $get['d']) { - $invitename = $database->getUserArray($invite['uid'], 1); + if($invite['id'] == $get['d'] && $invite['alliance'] == $session->alliance && $this->userPermArray['opt4'] == 1) { + $invitename = $database->getUserArray($invite['uid'], 1); $database->removeInvitation($get['d']); - $database->insertAlliNotice($session->alliance, '' . addslashes($session->username) . ' has deleted the invitation for ' . addslashes($invitename['username']) . '.'); + $database->insertAlliNotice($session->alliance, ''.addslashes($session->username).' has deleted the invitation for '.addslashes($invitename['username']).'.'); } } header("Location: allianz.php?delinvite"); @@ -229,7 +239,7 @@ class Alliance { $database->updateUserField($invite['uid'], "alliance", $invite['alliance'], 1); $database->createAlliPermissions($invite['uid'], $invite['alliance'], '', 0, 0, 0, 0, 0, 0, 0, 0); // Log the notice - $database->insertAlliNotice($invite['alliance'], '' . addslashes($session->username) . ' has joined the alliance.'); + $database->insertAlliNotice($invite['alliance'], ''.addslashes($session->username).' has joined the alliance.'); } else { $accept_error = 1; $max = $alliance_info['max']; @@ -288,7 +298,7 @@ class Alliance { // Asign Permissions $database->createAlliPermissions($session->uid, $aid, 'Alliance founder', '1', '1', '1', '1', '1', '1', '1', '1'); // log the notice - $database->insertAlliNotice($aid, 'The alliance has been founded by ' . addslashes($session->username) . '.'); + $database->insertAlliNotice($aid, 'The alliance has been founded by '.addslashes($session->username).'.'); header("Location: build.php?gid=18"); exit; } @@ -303,35 +313,26 @@ class Alliance { *****************************************/ private function changeAliName($get) { global $form, $database, $session; - if($session->access != BANNED){ - if(!isset($get['ally1']) || $get['ally1'] == "") { - $form->addError("ally1", ATAG_EMPTY); - } - if(!isset($get['ally2']) || $get['ally2'] == "") { - $form->addError("ally2", ANAME_EMPTY); - } - if($database->aExist($get['ally1'], "tag")) { - $form->addError("tag", ATAG_EXIST); - } - if($database->aExist($get['ally2'], "name")) { - $form->addError("name", ANAME_EXIST); - } - if($this->userPermArray['opt3'] == 0) { - $form->addError("perm", NO_PERMISSION); - } - if($form->returnErrors() != 0) { - $_SESSION['errorarray'] = $form->getErrors(); - $_SESSION['valuearray'] = $post; - //header("Location: build.php?id=".$post['id']); - } else { - $database->setAlliName($session->alliance, $get['ally2'], $get['ally1']); - // log the notice - $database->insertAlliNotice($session->alliance, '' . addslashes($session->username) . ' has changed the alliance name.'); - } - }else{ + if($session->access == BANNED) { header("Location: banned.php"); exit; } + + if(!isset($get['ally1']) || $get['ally1'] == "") $form->addError("ally1", ATAG_EMPTY); + + if(!isset($get['ally2']) || $get['ally2'] == "") $form->addError("ally2", ANAME_EMPTY); + + if($database->aExist($get['ally1'], "tag")) $form->addError("ally1", ATAG_EXIST); + + if($database->aExist($get['ally2'], "name")) $form->addError("ally2", ANAME_EXIST); + + if($this->userPermArray['opt3'] == 0) $form->addError("perm", NO_PERMISSION); + + if($form->returnErrors() == 0) { + $database->setAlliName($session->alliance, $get['ally2'], $get['ally1']); + // log the notice + $database->insertAlliNotice($session->alliance, ''.addslashes($session->username).' has changed the alliance name.'); + } } /***************************************** @@ -346,11 +347,10 @@ class Alliance { if($form->returnErrors() != 0) { $_SESSION['errorarray'] = $form->getErrors(); $_SESSION['valuearray'] = $post; - //header("Location: build.php?id=".$post['id']); } else { $database->submitAlliProfile($session->alliance, $post['be2'], $post['be1']); // log the notice - $database->insertAlliNotice($session->alliance, '' . addslashes($session->username) . ' has changed the alliance description.'); + $database->insertAlliNotice($session->alliance, ''.addslashes($session->username).' has changed the alliance description.'); } }else{ header("Location: banned.php"); @@ -361,25 +361,33 @@ class Alliance { /***************************************** Function to change the user permissions *****************************************/ - private function changeUserPermissions($post) { + private function changeUserPermissions($post) + { global $database, $session, $form; - if($session->access != BANNED){ - if($this->userPermArray['opt1'] == 0) { - $form->addError("perm", NO_PERMISSION); - } - if($form->returnErrors() != 0) { - $_SESSION['errorarray'] = $form->getErrors(); - $_SESSION['valuearray'] = $post; - //header("Location: build.php?id=".$post['id']); - } else { - $database->updateAlliPermissions($post['a_user'], $session->alliance, $post['a_titel'], $post['e1'], $post['e2'], $post['e3'], $post['e4'], $post['e5'], $post['e6'], $post['e7']); - // log the notice - $database->insertAlliNotice($session->alliance, '' . addslashes($session->username) . ' has changed permissions.'); - } - }else{ + if($session->access == BANNED) + { header("Location: banned.php"); exit; } + + if($this->userPermArray['opt1'] == 0) $form->addError("perm", NO_PERMISSION); + elseif($database->getUserField($post['a_user'], "alliance", 0) != $session->alliance) $form->addError("perm", USER_NOT_IN_YOUR_ALLY); + elseif($post['a_user'] == $session->uid) $form->addError("perm", CANT_EDIT_YOUR_PERMISSIONS); + else + { + $database->updateAlliPermissions($post['a_user'], $session->alliance, $post['a_titel'], $post['e1'], $post['e2'], $post['e3'], $post['e4'], $post['e5'], $post['e6'], $post['e7']); + // log the notice + $database->insertAlliNotice($session->alliance, ''.addslashes($session->username).' has changed permissions of '.addslashes($database->getUserField($post['a_user'], "username", 0)).'.'); + $form->addError("perm", ALLY_PERMISSIONS_UPDATED); + } + + if($form->returnErrors() > 0) + { + $_SESSION['errorarray'] = $form->getErrors(); + $_SESSION['valuearray'] = $post; + header("Location: allianz.php?s=5"); + exit; + } } /***************************************** Function to kick a user from alliance @@ -391,20 +399,23 @@ class Alliance { $UserData = $database->getUserArray($post['a_user'], 1); if($this->userPermArray['opt2'] == 0) { $form->addError("perm", NO_PERMISSION); + } else if($database->getUserField($post['a_user'], "alliance", 0) != $session->alliance){ + $form->addError("perm", USER_NOT_IN_YOUR_ALLY); } else if($UserData['id'] != $session->uid){ $database->updateUserField($post['a_user'], 'alliance', 0, 1); $database->deleteAlliPermissions($post['a_user']); $database->deleteAlliance($session->alliance); // log the notice - $database->insertAlliNotice($session->alliance, '' . addslashes($post['a_user']) . ' has quit the alliance.'); - if($session->alliance && $database->isAllianceOwner($UserData['id']) == $session->alliance){ + $database->insertAlliNotice($session->alliance, ''.($kickedUsername = addslashes($database->getUserField($post['a_user'], "username", 0))).' has been expelled from the alliance by '.addslashes($session->username).'.'); + if($session->alliance && $database->isAllianceOwner($UserData['id']) == $session->alliance){ $newowner = $database->getAllMember2($session->alliance); $newleader = $newowner['id']; $q = "UPDATE " . TB_PREFIX . "alidata set leader = ".(int) $newleader." where id = ".(int) $session->alliance.""; $database->query($q); $database->updateAlliPermissions($newleader, 1, 1, 1, 1, 1, 1, 1, 1, 1); - Automation::updateMax($newleader); + Automation::updateMax($newleader); } + $form->addError("perm", $kickedUsername.ALLY_USER_KICKED); } } else { header("Location: banned.php"); @@ -415,17 +426,19 @@ class Alliance { Function to set forum link *****************************************/ public function setForumLink($post) { - global $database, $session; - if($session->access != BANNED){ - if(isset($post['f_link'])){ - $database->setAlliForumdblink($session->alliance, $post['f_link']); - header("Location: allianz.php?s=5"); - exit; - } - }else{ + global $database, $session, $form; + if($session->access == BANNED) + { header("Location: banned.php"); exit; } + + if($this->userPermArray['opt5'] == 0) $form->addError("perm", NO_PERMISSION); + else + { + $database->setAlliForumdblink($session->alliance, $post['f_link']); + $form->addError("perm", ALLY_FORUM_LINK_UPDATED); + } } /***************************************** Function to vote on forum survey @@ -452,9 +465,9 @@ class Alliance { global $database, $session, $form; if($session->access != BANNED){ if(!isset($post['pw']) || $post['pw'] == "") { - $form->addError("pw1", PW_EMPTY); + $form->addError("pw", PW_EMPTY); } elseif(!password_verify($post['pw'], $session->userinfo['password'])) { - $form->addError("pw2", PW_ERR); + $form->addError("pw", LOGIN_PW_ERROR); } else { // check whether this is not the founder leaving and if he is, see whether // his replacement has been selected @@ -465,7 +478,7 @@ class Alliance { ) { // check that we have a valid new founder if (!isset($post['new_founder'])) { - $form->addError("founder", 'founder was not selected'); + $form->addError("founder", 'Founder was not selected.'); return; } else { $post['new_founder'] = (int) $post['new_founder']; @@ -481,8 +494,8 @@ class Alliance { } } - if (!$validMemberFound) { - $form->addError("founder2", 'founder is not valid'); + if (!$validMemberFound || $post['new_founder'] == $session->uid) { + $form->addError("founder", 'Invalid founder.'); return; } @@ -523,44 +536,49 @@ class Alliance { private function changediplomacy($post) { global $database, $session, $form; - if($session->access != BANNED){ - $aName = $_POST['a_name']; - $aType = (int)intval($_POST['dipl']); - if($database->aExist($aName, "tag")) { - $allianceID = $database->getAllianceID($aName); - if($allianceID != $session->alliance) { - if($aType >= 1 and $aType <= 3) { - if(!$database->diplomacyInviteCheck2($session->alliance, $allianceID)) { - $database->diplomacyInviteAdd($session->alliance, $allianceID, $aType); - if($aType == 1){ - $notice = "offer a confederation to"; - }else if($aType == 2){ - $notice = "offer non-aggression pact to"; - }else if($aType == 3){ - $notice = "declare war on"; - } - $database->insertAlliNotice($session->alliance, '' . $database->getAllianceName($session->alliance) . ' '. $notice .' ' . $aName . '.'); - $form->addError("name", "Invite sended"); - } else { - $form->addError("name", "You have already sended them a invite"); - } - - } else { - $form->addError("name", "wrong choice made"); - } - } else { - $form->addError("name", "You can not invite your own alliance"); - } - } else { - $form->addError("name", "Alliance does not exist"); - } - }else{ + if($session->access == BANNED) { header("Location: banned.php"); exit; } + if($this->userPermArray['opt6'] == 1){ + if(!empty($post['a_name']) || !empty($post['dipl'])){ + $aName = $post['a_name']; + $aType = (int)intval($post['dipl']); + if($database->aExist($aName, "tag")) { + $allianceID = $database->getAllianceID($aName); + if($allianceID != $session->alliance) { + if($aType >= 1 and $aType <= 3) { + if(!$database->diplomacyInviteCheck2($session->alliance, $allianceID)) { + if($database->diplomacyCheckLimits($session->alliance, $aType)){ + $database->diplomacyInviteAdd($session->alliance, $allianceID, $aType); + if($aType == 1){ + $notice = OFFERED_CONFED_TO; + }else if($aType == 2){ + $notice = OFFERED_NON_AGGRESION_PACT_TO; + }else if($aType == 3){ + $notice = DECLARED_WAR_ON; + } + $database->insertAlliNotice($session->alliance, ''.$database->getAllianceName($session->alliance).' '.$notice.' '.$aName.'.'); + $database->insertAlliNotice($allianceID, ''.$database->getAllianceName($session->alliance).' '.$notice.' '.$aName.'.'); + $form->addError("name", INVITE_SENT); + + } + else $form->addError("name", ALLY_TOO_MUCH_PACTS); + } + else $form->addError("name", INVITE_ALREADY_SENT); + } + else $form->addError("name", WRONG_DIPLOMACY); + } + else $form->addError("name", CANNOT_INVITE_SAME_ALLY); + } + else $form->addError("name", ALLY_DOESNT_EXISTS); + } + else $form->addError("name", NAME_OR_DIPL_EMPTY); + } + else $form->addError("name", NO_PERMISSION); } - } +} - $alliance = new Alliance; +$alliance = new Alliance; ?> diff --git a/GameEngine/Database.php b/GameEngine/Database.php index cc8268af..c8628adf 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -3107,10 +3107,10 @@ class MYSQLi_DB implements IDbConnection { return mysqli_query($this->dblink,$q); } - function diplomacyOwnOffers($session_alliance) { - list($session_alliance) = $this->escape_input((int) $session_alliance); + function diplomacyOwnOffers($sessionAlliance) { + list($sessionAlliance) = $this->escape_input((int) $sessionAlliance); - $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $session_alliance AND accepted = 0"; + $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $sessionAlliance AND accepted = 0"; $result = mysqli_query($this->dblink,$q); return $this->mysqli_fetch_all($result); } @@ -3125,32 +3125,32 @@ class MYSQLi_DB implements IDbConnection { return $dbarray['id']; } - function diplomacyCancelOffer($id) { - list($id) = $this->escape_input((int) $id); + function diplomacyCancelOffer($id, $sessionAlliance) { + list($id, $sessionAlliance) = $this->escape_input((int) $id, (int) $sessionAlliance); - $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id"; + $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli1 = $sessionAlliance"; return mysqli_query($this->dblink,$q); } - function diplomacyInviteAccept($id, $session_alliance) { - list($id, $session_alliance) = $this->escape_input((int) $id, (int) $session_alliance); + function diplomacyInviteAccept($id, $sessionAlliance) { + list($id, $sessionAlliance) = $this->escape_input((int) $id, (int) $sessionAlliance); - $q = "UPDATE " . TB_PREFIX . "diplomacy SET accepted = 1 WHERE id = $id AND alli2 = $session_alliance"; + $q = "UPDATE " . TB_PREFIX . "diplomacy SET accepted = 1 WHERE id = $id AND alli2 = $sessionAlliance"; return mysqli_query($this->dblink,$q); } - function diplomacyInviteDenied($id, $session_alliance) { - list($id, $session_alliance) = $this->escape_input((int) $id, (int) $session_alliance); + function diplomacyInviteDenied($id, $sessionAlliance) { + list($id, $sessionAlliance) = $this->escape_input((int) $id, (int) $sessionAlliance); - $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $session_alliance"; + $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $sessionAlliance"; return mysqli_query($this->dblink,$q); } // no need to cache this method - function diplomacyInviteCheck($session_alliance) { - list($session_alliance) = $this->escape_input((int) $session_alliance); + function diplomacyInviteCheck($sessionAlliance) { + list($sessionAlliance) = $this->escape_input((int) $sessionAlliance); - $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $session_alliance AND accepted = 0"; + $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $sessionAlliance AND accepted = 0"; $result = mysqli_query($this->dblink,$q); return $this->mysqli_fetch_all($result); } @@ -3159,7 +3159,7 @@ class MYSQLi_DB implements IDbConnection { function diplomacyInviteCheck2($ally1, $ally2) { list($ally1, $ally2) = $this->escape_input((int) $ally1, (int) $ally2); - $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $ally1 AND alli2 = $ally2 AND accepted = 0"; + $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE (alli1 = $ally1 OR alli2 = $ally1) AND (alli1 = $ally2 OR alli2 = $ally2)"; $result = mysqli_query($this->dblink,$q); return $this->mysqli_fetch_all($result); } @@ -3237,45 +3237,29 @@ class MYSQLi_DB implements IDbConnection { } // no need to cache this method - function diplomacyExistingRelationships($session_alliance) { - list($session_alliance) = $this->escape_input((int) $session_alliance); + function diplomacyExistingRelationships($sessionAlliance) { + list($sessionAlliance) = $this->escape_input((int) $sessionAlliance); - $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $session_alliance AND accepted = 1"; + $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE (alli1 = $sessionAlliance OR alli2 = $sessionAlliance) AND accepted = 1"; $result = mysqli_query($this->dblink,$q); return $this->mysqli_fetch_all($result); } - // no need to cache this method - function diplomacyExistingRelationships2($session_alliance) { - list($session_alliance) = $this->escape_input((int) $session_alliance); + function diplomacyCancelExistingRelationship($id, $sessionAlliance) { + list($id, $sessionAlliance) = $this->escape_input((int) $id, (int) $sessionAlliance); - $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $session_alliance AND accepted = 1"; - $result = mysqli_query($this->dblink,$q); - return $this->mysqli_fetch_all($result); - } - - function diplomacyCancelExistingRelationship($id, $session_alliance) { - list($id, $session_alliance) = $this->escape_input((int) $id, (int) $session_alliance); - - $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $session_alliance OR id = $id AND alli1 = $session_alliance"; + $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE (alli1 = $sessionAlliance OR alli2 = $sessionAlliance) AND id = $id "; return mysqli_query($this->dblink,$q); } // no need to cache this method - function checkDiplomacyInviteAccept($aid, $type) { + function diplomacyCheckLimits($aid, $type) { list($aid, $type) = $this->escape_input((int) $aid, (int) $type); - - $q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $aid AND type = $type AND accepted = 1 OR alli2 = $aid AND type = $type AND accepted = 1"; + if($type == 3) return true; + + $q = "SELECT Count(case when alli1 = $aid then 1 end) as Total1, Count(case when alli2 = $aid then 1 end) as Total2 FROM " . TB_PREFIX . "diplomacy WHERE type = $type"; $result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC); - if($type == 3){ - return true; - }else{ - if($result['Total'] < 4) { - return true; - } else { - return false; - } - } + return $result['Total1'] < 3 && $result['Total2'] < 3; } function setAlliForumdblink($aid, $dblink) { diff --git a/GameEngine/Lang/en.php b/GameEngine/Lang/en.php index 3000fe7a..0639c349 100755 --- a/GameEngine/Lang/en.php +++ b/GameEngine/Lang/en.php @@ -72,7 +72,23 @@ define("ANAME_EMPTY","Name empty"); define("ATAG_EXIST","Tag taken"); define("ANAME_EXIST","Name taken"); define("ALREADY_ALLY_MEMBER","You're already in an alliance"); -define("ALLY_TOO_LOW", "You must have a level 3 or greater alliance"); +define("ALLY_TOO_LOW", "You must have a level 3 or greater embassy"); +define("USER_NOT_IN_YOUR_ALLY","This user is not in your alliance!"); +define("CANT_EDIT_YOUR_PERMISSIONS","You can't edit your own permissions!"); +define("NO_PERMISSION", "You don't have enough permissions!"); +define("NAME_OR_DIPL_EMPTY", "Name or diplomacy empty"); +define("ALLY_DOESNT_EXISTS","Alliance does not exist"); +define("CANNOT_INVITE_SAME_ALLY","You cannot invite your own alliance"); +define("WRONG_DIPLOMACY","Wrong choice made"); +define("INVITE_ALREADY_SENT","Either you have already sent a pact to this alliance, they sent it to yours or you already have a pact with them"); +define("INVITE_SENT","Invite sent"); +define("DECLARED_WAR_ON","declared war to"); +define("OFFERED_NON_AGGRESION_PACT_TO","offered non-aggression pact to"); +define("OFFERED_CONFED_TO","offered a confederation to"); +define("ALLY_TOO_MUCH_PACTS","Either You cannot offer more pacts of this kind or this alliance has reached the limit for this kind of patcs"); +define("ALLY_PERMISSIONS_UPDATED","Permissions updated"); +define("ALLY_FORUM_LINK_UPDATED", "Forum link updated"); +define("ALLY_USER_KICKED"," has been kicked from the alliance"); define("NOT_OPENED_YET","Server not started yet."); define("REGISTER_CLOSED","The register is closed. You can't register to this server."); define("NAME_EMPTY","Please insert name"); diff --git a/Templates/Alliance/alli_menu.tpl b/Templates/Alliance/alli_menu.tpl index 0619593b..7854a853 100644 --- a/Templates/Alliance/alli_menu.tpl +++ b/Templates/Alliance/alli_menu.tpl @@ -1,7 +1,7 @@ alliance == $aid) { ?>