/backend/backend_myalliance.php

https://gitlab.com/Toldierone/ReClop · PHP · 246 lines · 245 code · 0 blank · 1 comment · 55 complexity · 569a022036d8a9c1b75eb9bb773c4e67 MD5 · raw file

  1. <?php
  2. include("allfunctions.php");
  3. $sql=<<<EOSQL
  4. SELECT alliance_id
  5. FROM users
  6. WHERE user_id = '{$_SESSION['user_id']}'
  7. EOSQL;
  8. $rs = onelinequery($sql);
  9. if (!$rs['alliance_id']) {
  10. header("Location: overview.php");
  11. exit;
  12. } else {
  13. $allianceid = $rs['alliance_id'];
  14. }
  15. foreach ($_POST as $key => $value) {
  16. $mysql[$key] = $GLOBALS['mysqli']->real_escape_string($value);
  17. $display[$key] = htmlentities($value);
  18. }
  19. $sql=<<<EOSQL
  20. SELECT a.*, u.donator
  21. FROM alliances a
  22. LEFT JOIN users u ON a.owner_id = u.user_id
  23. WHERE a.alliance_id = '{$rs['alliance_id']}'
  24. EOSQL;
  25. $allianceinfo = onelinequery($sql);
  26. $displayeditpubdescription = htmlentities($allianceinfo['public_description'], ENT_SUBSTITUTE, "UTF-8");
  27. $displayeditdescription = htmlentities($allianceinfo['description'], ENT_SUBSTITUTE, "UTF-8");
  28. if ($allianceinfo['donator']) {
  29. $displaydescription = nl2br($allianceinfo['description']);
  30. } else {
  31. $displaydescription = nl2br(htmlentities($allianceinfo['description'], ENT_SUBSTITUTE, "UTF-8"));
  32. }
  33. if ($_POST && (($_POST['token_myalliance'] == "") || ($_POST['token_myalliance'] != $_SESSION['token_myalliance']))) {
  34. $errors[] = "Try again.";
  35. }
  36. if ($_POST || ($_SESSION['token_myalliance'] == "")) {
  37. $_SESSION['token_myalliance'] = sha1(rand() . $_SESSION['token_myalliance']);
  38. }
  39. if ($_SESSION['user_id'] == $allianceinfo['owner_id']) {
  40. $owner = true;
  41. }
  42. if (!$errors) {
  43. if ($owner) {
  44. if ($_POST['updatedescription']) {
  45. $sql=<<<EOSQL
  46. UPDATE alliances SET description = '{$mysql['alliancedescription']}' WHERE alliance_id = '{$allianceinfo['alliance_id']}'
  47. EOSQL;
  48. $GLOBALS['mysqli']->query($sql);
  49. $displayeditdescription = htmlentities($_POST['alliancedescription'], ENT_SUBSTITUTE, "UTF-8");
  50. if ($allianceinfo['donator']) {
  51. $displaydescription = nl2br($_POST['alliancedescription']);
  52. } else {
  53. $displaydescription = nl2br(htmlentities($_POST['alliancedescription'], ENT_SUBSTITUTE, "UTF-8"));
  54. }
  55. }
  56. if ($_POST['updatepubdescription']) {
  57. $sql=<<<EOSQL
  58. UPDATE alliances SET public_description = '{$mysql['alliancepubdescription']}' WHERE alliance_id = '{$allianceinfo['alliance_id']}'
  59. EOSQL;
  60. $GLOBALS['mysqli']->query($sql);
  61. $displayeditpubdescription = htmlentities($_POST['alliancepubdescription']);
  62. }
  63. if ($_POST['action'] == "Accept User") {
  64. $sql=<<<EOSQL
  65. SELECT * FROM alliance_requests WHERE user_id = '{$mysql['user_id']}' AND alliance_id = '{$allianceinfo['alliance_id']}'
  66. EOSQL;
  67. $rs = onelinequery($sql);
  68. if ($rs) {
  69. $sql=<<<EOSQL
  70. DELETE FROM alliance_requests WHERE user_id = '{$mysql['user_id']}'
  71. EOSQL;
  72. $GLOBALS['mysqli']->query($sql);
  73. $sql=<<<EOSQL
  74. UPDATE users SET alliance_id = {$allianceinfo['alliance_id']} WHERE user_id = '{$mysql['user_id']}'
  75. EOSQL;
  76. $GLOBALS['mysqli']->query($sql);
  77. $message = $GLOBALS['mysqli']->real_escape_string("Your application to {$allianceinfo['name']} was accepted.");
  78. $sql=<<<EOSQL
  79. INSERT INTO messages (fromuser, touser, fromdeleted, message, sent) VALUES (0, {$mysql['user_id']}, 1, '{$message}', NOW())
  80. EOSQL;
  81. $GLOBALS['mysqli']->query($sql);
  82. } else {
  83. $errors[] = "The user disappeared before you could accept!";
  84. }
  85. }
  86. if ($_POST['action'] == "Reject User") {
  87. $sql=<<<EOSQL
  88. SELECT * FROM alliance_requests WHERE user_id = '{$mysql['user_id']}' AND alliance_id = '{$allianceinfo['alliance_id']}'
  89. EOSQL;
  90. $GLOBALS['mysqli']->query($sql);
  91. if ($rs) {
  92. $sql=<<<EOSQL
  93. DELETE FROM alliance_requests WHERE user_id = '{$mysql['user_id']}' AND alliance_id = '{$allianceinfo['alliance_id']}'
  94. EOSQL;
  95. $GLOBALS['mysqli']->query($sql);
  96. $message = $GLOBALS['mysqli']->real_escape_string("Your application to {$allianceinfo['name']} was rejected.");
  97. $sql=<<<EOSQL
  98. INSERT INTO messages (fromuser, touser, fromdeleted, message, sent) VALUES (0, {$mysql['user_id']}, 1, '{$message}', NOW())
  99. EOSQL;
  100. $GLOBALS['mysqli']->query($sql);
  101. } else {
  102. $errors[] = "The member disappeared before you could reject!";
  103. }
  104. }
  105. if ($_POST['action'] == "Eject User") {
  106. $sql=<<<EOSQL
  107. UPDATE users SET alliance_id = 0 WHERE user_id = '{$mysql['user_id']}' AND alliance_id = '{$allianceinfo['alliance_id']}'
  108. EOSQL;
  109. $GLOBALS['mysqli']->query($sql);
  110. $message = $GLOBALS['mysqli']->real_escape_string("You were thrown out of {$allianceinfo['name']}!");
  111. $sql=<<<EOSQL
  112. INSERT INTO messages (fromuser, touser, fromdeleted, message, sent) VALUES (0, {$mysql['user_id']}, 1, '{$message}', NOW())
  113. EOSQL;
  114. $GLOBALS['mysqli']->query($sql);
  115. }
  116. if ($_POST['givealliance']) {
  117. $sql = "SELECT user_id, alliance_id FROM users WHERE username = '{$mysql['giveto']}'";
  118. $rs = onelinequery($sql);
  119. if (!$rs) {
  120. $errors[] = "User not found.";
  121. } else if ($rs['alliance_id'] != $allianceinfo['alliance_id']) {
  122. $errors[] = "That user is not in your alliance!";
  123. } else if ($rs['user_id'] == $_SESSION['user_id']) {
  124. $errors[] = "Silly.";
  125. } else if ($rs) {
  126. $givetoid = $rs['user_id'];
  127. $sql=<<<EOSQL
  128. UPDATE alliances SET owner_id = '{$rs['user_id']}' WHERE alliance_id = '{$allianceinfo['alliance_id']}'
  129. EOSQL;
  130. $GLOBALS['mysqli']->query($sql);
  131. $owner = false;
  132. }
  133. }
  134. if ($_POST['action'] == "Disband Alliance") {
  135. $sql=<<<EOSQL
  136. UPDATE users SET alliance_id = 0 WHERE alliance_id = '{$allianceinfo['alliance_id']}'
  137. EOSQL;
  138. $GLOBALS['mysqli']->query($sql);
  139. $sql=<<<EOSQL
  140. DELETE FROM alliance_requests WHERE alliance_id = '{$allianceinfo['alliance_id']}'
  141. EOSQL;
  142. $GLOBALS['mysqli']->query($sql);
  143. $sql=<<<EOSQL
  144. DELETE FROM alliances WHERE alliance_id = '{$allianceinfo['alliance_id']}'
  145. EOSQL;
  146. $GLOBALS['mysqli']->query($sql);
  147. //if alliance is older than a week (604 800 seconds)
  148. if ((time() - strtotime($allianceinfo['creationdate'])) > 604800) {
  149. $rawmessage = "The ".$allianceinfo['name']." Alliance has been disbanded!";
  150. $message = $GLOBALS['mysqli']->real_escape_string($rawmessage);
  151. $sql = "INSERT INTO news VALUES ('', '".$message."', NOW())";
  152. $GLOBALS['mysqli']->query($sql);
  153. }
  154. header("Location: overview.php");
  155. exit;
  156. }
  157. if ($_POST['bulkdelete']) {
  158. if (!ctype_digit($_POST['deletedays']) || $_POST['deletedays'] === "") {
  159. $errors[] = "Enter a number of days.";
  160. }
  161. if (!$errors) {
  162. $mysql['deletedays'] = (int)$_POST['deletedays'];
  163. $sql=<<<EOSQL
  164. DELETE FROM alliance_messages WHERE alliance_id = '{$allianceinfo['alliance_id']}' AND posted < DATE_SUB(NOW(), INTERVAL {$mysql['deletedays']} DAY)
  165. EOSQL;
  166. $GLOBALS['mysqli']->query($sql);
  167. }
  168. }
  169. }
  170. if ($_POST['action'] == "Leave Alliance") {
  171. $sql=<<<EOSQL
  172. UPDATE users SET alliance_id = 0, alliance_lastread = '0' WHERE user_id = '{$_SESSION['user_id']}'
  173. EOSQL;
  174. $GLOBALS['mysqli']->query($sql);
  175. header("Location: overview.php");
  176. exit;
  177. }
  178. if ($_POST['sendmessage']) {
  179. $sql=<<<EOSQL
  180. INSERT INTO alliance_messages SET alliance_id = '{$allianceinfo['alliance_id']}', user_id = '{$_SESSION['user_id']}', posted = NOW(), message = '{$mysql['message']}'
  181. EOSQL;
  182. $GLOBALS['mysqli']->query($sql);
  183. }
  184. if ($_POST['deletemessage']) {
  185. if ($owner) {
  186. $sql=<<<EOSQL
  187. SELECT user_id FROM alliance_messages WHERE alliance_id = '{$allianceinfo['alliance_id']}' AND message_id = '{$mysql['message_id']}'
  188. EOSQL;
  189. } else {
  190. $sql=<<<EOSQL
  191. SELECT user_id FROM alliance_messages WHERE user_id = '{$_SESSION['user_id']}' AND alliance_id = '{$allianceinfo['alliance_id']}' AND message_id = '{$mysql['message_id']}'
  192. EOSQL;
  193. }
  194. $rs = onelinequery($sql);
  195. if ($rs['user_id']) {
  196. $sql=<<<EOSQL
  197. DELETE FROM alliance_messages WHERE message_id = '{$mysql['message_id']}'
  198. EOSQL;
  199. $GLOBALS['mysqli']->query($sql);
  200. } else {
  201. $errors[] = "You can't delete that message.";
  202. }
  203. }
  204. }
  205. $alliancemembers = array();
  206. $requestingmembers = array();
  207. $sql=<<<EOSQL
  208. SELECT username, user_id, stasismode FROM users WHERE alliance_id = '{$allianceinfo['alliance_id']}' ORDER BY username
  209. EOSQL;
  210. $sth = $GLOBALS['mysqli']->query($sql);
  211. while ($rs = mysqli_fetch_array($sth)) {
  212. $alliancemembers[] = $rs;
  213. $sql=<<<EOSQL
  214. SELECT nation_id, name FROM nations WHERE user_id = {$rs['user_id']} ORDER BY name
  215. EOSQL;
  216. $sth2 = $GLOBALS['mysqli']->query($sql);
  217. while ($rs2 = mysqli_fetch_array($sth2)) {
  218. $nations[$rs['user_id']][] = $rs2;
  219. }
  220. }
  221. $sql=<<<EOSQL
  222. SELECT u.username, u.user_id FROM alliance_requests ar INNER JOIN users u ON ar.user_id = u.user_id WHERE ar.alliance_id = '{$allianceinfo['alliance_id']}' ORDER BY u.username
  223. EOSQL;
  224. $sth = $GLOBALS['mysqli']->query($sql);
  225. if ($sth) {
  226. while ($rs = mysqli_fetch_array($sth)) {
  227. $requestingmembers[] = $rs;
  228. }
  229. }
  230. $sql=<<<EOSQL
  231. SELECT u.username, u.user_id, am.message, am.posted, am.message_id FROM alliance_messages am INNER JOIN users u ON am.user_id = u.user_id WHERE am.alliance_id = '{$allianceinfo['alliance_id']}'
  232. ORDER BY am.posted DESC
  233. EOSQL;
  234. $sth = $GLOBALS['mysqli']->query($sql);
  235. if ($sth) {
  236. while ($rs = mysqli_fetch_array($sth)) {
  237. $rs['displaymessage'] = nl2br(htmlentities($rs['message'], ENT_SUBSTITUTE, "UTF-8"));
  238. $messages[] = $rs;
  239. }
  240. }
  241. $sql = <<<EOSQL
  242. UPDATE users SET alliance_lastread = (SELECT MAX(message_id) FROM alliance_messages WHERE alliance_id = '{$allianceid}') WHERE user_id = '{$_SESSION['user_id']}'
  243. EOSQL;
  244. $GLOBALS['mysqli']->query($sql) or die($mysqli->error);
  245. ?>