PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/backend/allfunctions.php

https://gitlab.com/Toldierone/Compounds-mirror
PHP | 372 lines | 357 code | 0 blank | 15 comment | 63 complexity | cd5d5602266ecd61629247a921005e99 MD5 | raw file
  1. <?php
  2. $mysqli = new mysqli("localhost", "username", "password", "database");
  3. date_default_timezone_set("America/Chicago");
  4. /* ini_set("session.cookie_secure", 1); */
  5. session_start();
  6. if (!isset($_SESSION['SERVER_GENERATED_SID'])) {
  7. session_destroy();
  8. session_start();
  9. session_regenerate_id(true);
  10. $_SESSION['SERVER_GENERATED_SID'] = true;
  11. }
  12. function commas($nm) {
  13. for ($done=strlen($nm); $done > 3;$done -= 3) {
  14. $returnNum = ",".substr($nm,$done-3,3).$returnNum;
  15. }
  16. return substr($nm,0,$done).$returnNum;
  17. }
  18. function onelinequery($sql) {
  19. $sth = $GLOBALS['mysqli']->query($sql);
  20. if ($sth) {
  21. return mysqli_fetch_array($sth);
  22. } else {
  23. return false;
  24. }
  25. }
  26. if ($_SESSION['user_id']) {
  27. $sql=<<<EOSQL
  28. SELECT * FROM users WHERE user_id = {$_SESSION['user_id']} AND (stasismode = 0 OR (stasisdate < DATE_SUB(NOW(), INTERVAL 24 HOUR)) OR stasisdate IS NULL)
  29. EOSQL;
  30. $userinfo = onelinequery($sql);
  31. if (!$userinfo) {
  32. session_destroy();
  33. session_unset();
  34. header("Location: index.php");
  35. exit;
  36. }
  37. if ($userinfo['alliance_id']) {
  38. $sql=<<<EOSQL
  39. SELECT * FROM alliances
  40. WHERE alliance_id = '{$userinfo['alliance_id']}'
  41. EOSQL;
  42. $allianceinfo = onelinequery($sql);
  43. }
  44. }
  45. function needsuser() {
  46. if (!$GLOBALS['userinfo']['user_id']) {
  47. header("Location: index.php");
  48. exit;
  49. }
  50. }
  51. function needsalliance() {
  52. if ($GLOBALS['userinfo']['stasismode']) {
  53. header("Location: userinfo.php");
  54. exit;
  55. }
  56. if (!$GLOBALS['userinfo']['alliance_id']) {
  57. header("Location: index.php");
  58. exit;
  59. }
  60. }
  61. $sql=<<<EOSQL
  62. SELECT * FROM elementpositions
  63. EOSQL;
  64. $sth = $GLOBALS['mysqli']->query($sql);
  65. while ($rs = mysqli_fetch_array($sth)) {
  66. $positions[$rs['position']] = $rs['element'];
  67. }
  68. function withinsix($number) {
  69. if ($number > 6) {
  70. $number -= 6;
  71. }
  72. return $number;
  73. }
  74. function getcomplement($id) {
  75. $elementpositions = array_flip($positions);
  76. $newid = 0;
  77. if ($id & 32) $newid += $positions[withinsix($elementpositions['32'] + 3)];
  78. if ($id & 16) $newid += $positions[withinsix($elementpositions['16'] + 3)];
  79. if ($id & 8) $newid += $positions[withinsix($elementpositions['8'] + 3)];
  80. if ($id & 4) $newid += $positions[withinsix($elementpositions['4'] + 3)];
  81. if ($id & 2) $newid += $positions[withinsix($elementpositions['2'] + 3)];
  82. if ($id & 1) $newid += $positions[withinsix($elementpositions['1'] + 3)];
  83. return $newid;
  84. }
  85. function getelementname($id) {
  86. $sql=<<<EOSQL
  87. SELECT name FROM resourcedefs
  88. WHERE resource_id = '{$id}'
  89. EOSQL;
  90. $rs = onelinequery($sql);
  91. return $rs['name'];
  92. }
  93. function getelements($id) {
  94. $elementarray = array();
  95. if ($id & 32) $elementarray[32] = "Generosity";
  96. if ($id & 16) $elementarray[16] = "Honesty";
  97. if ($id & 8) $elementarray[8] = "Kindness";
  98. if ($id & 4) $elementarray[4] = "Laughter";
  99. if ($id & 2) $elementarray[2] = "Loyalty";
  100. if ($id & 1) $elementarray[1] = "Magic";
  101. return $elementarray;
  102. }
  103. function elementimages($id) {
  104. $return = "";
  105. if ($id & 1) {
  106. $return .=<<<EOFORM
  107. <img src="/images/magic.png"/>
  108. EOFORM;
  109. }
  110. if ($id & 2) {
  111. $return .=<<<EOFORM
  112. <img src="/images/loyalty.png"/>
  113. EOFORM;
  114. }
  115. if ($id & 4) {
  116. $return .=<<<EOFORM
  117. <img src="/images/laughter.png"/>
  118. EOFORM;
  119. }
  120. if ($id & 8) {
  121. $return .=<<<EOFORM
  122. <img src="/images/kindness.png"/>
  123. EOFORM;
  124. }
  125. if ($id & 16) {
  126. $return .=<<<EOFORM
  127. <img src="/images/honesty.png"/>
  128. EOFORM;
  129. }
  130. if ($id & 32) {
  131. $return .=<<<EOFORM
  132. <img src="/images/generosity.png"/>
  133. EOFORM;
  134. }
  135. return $return;
  136. }
  137. function shareselement($id1, $id2) {
  138. if ($id1 & 1 && $id2 & 1) return true;
  139. if ($id1 & 2 && $id2 & 2) return true;
  140. if ($id1 & 4 && $id2 & 4) return true;
  141. if ($id1 & 8 && $id2 & 8) return true;
  142. if ($id1 & 16 && $id2 & 16) return true;
  143. if ($id1 & 32 && $id2 & 32) return true;
  144. return false;
  145. }
  146. function elementsdropdown($initialblank = 0, $includevoid = 0) {
  147. $sql=<<<EOSQL
  148. SELECT rd.*, r.amount
  149. FROM resourcedefs rd
  150. LEFT JOIN resources r ON r.resource_id = rd.resource_id AND r.user_id = '{$_SESSION['user_id']}'
  151. ORDER BY resource_id ASC
  152. EOSQL;
  153. $return = "";
  154. $sth = $GLOBALS['mysqli']->query($sql);
  155. while ($rs = mysqli_fetch_array($sth)) {
  156. if (!$rs['amount']) $rs['amount'] = 0;
  157. $elementtiers[$rs['tier']][] = $rs;
  158. }
  159. if ($initialblank) {
  160. $return .= <<<EOFORM
  161. <option value=""></option>
  162. EOFORM;
  163. }
  164. if ($includevoid) {
  165. $return .= <<<EOFORM
  166. <option value="0">Void (Have {$elementtiers[0][0]['amount']})</option>
  167. EOFORM;
  168. }
  169. for ($i = 1; $i <= 5; $i++) {
  170. $return .= <<<EOFORM
  171. <optgroup label="Tier {$i}">
  172. EOFORM;
  173. foreach ($elementtiers[$i] as $rs) {
  174. $return .=<<<EOFORM
  175. <option value="{$rs['resource_id']}">{$rs['name']} ({$rs['elements']}) (Have {$rs['amount']})</option>
  176. EOFORM;
  177. }
  178. }
  179. $return .= <<<EOFORM
  180. <optgroup label="Tier 6">
  181. <option value="63">Harmony (Have {$elementtiers[6][0]['amount']})</option>
  182. </optgroup>
  183. EOFORM;
  184. return $return;
  185. }
  186. function alliancehasamount($id, $alliance_id, $amount) {
  187. //unsafe function, sanitize before calling
  188. $sql=<<<EOSQL
  189. SELECT amount FROM allianceresources
  190. WHERE alliance_id = '{$alliance_id}' AND resource_id = '{$id}'
  191. EOSQL;
  192. $rs = onelinequery($sql);
  193. if ($amount <= $rs['amount']) return true;
  194. else return false;
  195. }
  196. function hasamount($id, $user, $amount) {
  197. //unsafe function, sanitize before calling
  198. $sql=<<<EOSQL
  199. SELECT amount FROM resources
  200. WHERE user_id = '{$user}' AND resource_id = '{$id}'
  201. EOSQL;
  202. $rs = onelinequery($sql);
  203. if ($amount <= $rs['amount']) return true;
  204. else return false;
  205. }
  206. function amountof($id, $user) {
  207. //unsafe function, sanitize before calling
  208. $sql=<<<EOSQL
  209. SELECT amount FROM resources
  210. WHERE user_id = '{$user}' AND resource_id = '{$id}'
  211. EOSQL;
  212. $rs = onelinequery($sql);
  213. return $rs['amount'];
  214. }
  215. function addamount($id, $user, $amount) {
  216. //unsafe
  217. $sql=<<<EOSQL
  218. INSERT INTO resources (user_id, resource_id, amount)
  219. VALUES ({$user}, {$id}, {$amount})
  220. ON DUPLICATE KEY UPDATE amount = amount + {$amount}
  221. EOSQL;
  222. $GLOBALS['mysqli']->query($sql);
  223. $sql=<<<EOSQL
  224. DELETE FROM resources WHERE amount = 0
  225. EOSQL;
  226. $GLOBALS['mysqli']->query($sql);
  227. }
  228. function allianceaddamount($id, $alliance_id, $amount) {
  229. //unsafe
  230. $sql=<<<EOSQL
  231. INSERT INTO allianceresources (alliance_id, resource_id, amount)
  232. VALUES ({$alliance_id}, {$id}, {$amount})
  233. ON DUPLICATE KEY UPDATE amount = amount + {$amount}
  234. EOSQL;
  235. $GLOBALS['mysqli']->query($sql);
  236. $sql=<<<EOSQL
  237. DELETE FROM allianceresources WHERE amount = 0
  238. EOSQL;
  239. $GLOBALS['mysqli']->query($sql);
  240. }
  241. function amountbanked($id, $user) {
  242. $sql=<<<EOSQL
  243. SELECT amount FROM bankedresources
  244. WHERE user_id = '{$user}' AND resource_id = '{$id}'
  245. EOSQL;
  246. $rs = onelinequery($sql);
  247. return $rs['amount'];
  248. }
  249. function hasbanked($id, $user, $amount) {
  250. //unsafe function, sanitize before calling
  251. $sql=<<<EOSQL
  252. SELECT amount FROM bankedresources
  253. WHERE user_id = '{$user}' AND resource_id = '{$id}'
  254. EOSQL;
  255. $rs = onelinequery($sql);
  256. if ($amount <= $rs['amount']) return true;
  257. else return false;
  258. }
  259. function addbanked($id, $user, $amount) {
  260. //unsafe
  261. $sql=<<<EOSQL
  262. INSERT INTO bankedresources (user_id, resource_id, amount)
  263. VALUES ({$user}, {$id}, {$amount})
  264. ON DUPLICATE KEY UPDATE amount = amount + {$amount}
  265. EOSQL;
  266. $GLOBALS['mysqli']->query($sql);
  267. $sql=<<<EOSQL
  268. DELETE FROM bankedresources WHERE amount = 0
  269. EOSQL;
  270. $GLOBALS['mysqli']->query($sql);
  271. }
  272. function allianceamountbanked($id, $alliance) {
  273. $sql=<<<EOSQL
  274. SELECT amount FROM alliancebankedresources
  275. WHERE alliance_id = '{$alliance}' AND resource_id = '{$id}'
  276. EOSQL;
  277. $rs = onelinequery($sql);
  278. return $rs['amount'];
  279. }
  280. function alliancehasbanked($id, $alliance, $amount) {
  281. //unsafe function, sanitize before calling
  282. $sql=<<<EOSQL
  283. SELECT amount FROM alliancebankedresources
  284. WHERE alliance_id = '{$alliance}' AND resource_id = '{$id}'
  285. EOSQL;
  286. $rs = onelinequery($sql);
  287. if ($amount <= $rs['amount']) return true;
  288. else return false;
  289. }
  290. function allianceaddbanked($id, $alliance, $amount) {
  291. //unsafe
  292. $sql=<<<EOSQL
  293. INSERT INTO alliancebankedresources (alliance_id, resource_id, amount)
  294. VALUES ({$alliance}, {$id}, {$amount})
  295. ON DUPLICATE KEY UPDATE amount = amount + {$amount}
  296. EOSQL;
  297. $GLOBALS['mysqli']->query($sql);
  298. $sql=<<<EOSQL
  299. DELETE FROM alliancebankedresources WHERE amount = 0
  300. EOSQL;
  301. $GLOBALS['mysqli']->query($sql);
  302. }
  303. function hasability($name, $user) {
  304. //unsafe
  305. $sql=<<<EOSQL
  306. SELECT ua.turns FROM user_abilities ua
  307. INNER JOIN abilities a ON a.ability_id = ua.ability_id
  308. WHERE a.name = '{$name}' AND ua.user_id = {$user}
  309. EOSQL;
  310. $rs = onelinequery($sql);
  311. if ($rs['turns']) return true;
  312. else return false;
  313. }
  314. function alliancehasability($name, $alliance) {
  315. //unsafe
  316. $sql=<<<EOSQL
  317. SELECT ua.turns FROM alliance_groupabilities ua
  318. INNER JOIN groupabilities a ON a.ability_id = ua.ability_id
  319. WHERE a.name = '{$name}' AND ua.alliance_id = {$alliance}
  320. EOSQL;
  321. $rs = onelinequery($sql);
  322. if ($rs['turns']) return true;
  323. else return false;
  324. }
  325. function addreport($message, $user) {
  326. //safe
  327. $message = $GLOBALS['mysqli']->real_escape_string($message);
  328. $sql=<<<EOSQL
  329. INSERT INTO reports (report, user_id, time)
  330. VALUES ('{$message}', '{$user}', NOW())
  331. EOSQL;
  332. $GLOBALS['mysqli']->query($sql);
  333. }
  334. function allianceaddreport($message, $alliance) {
  335. //safe
  336. $message = $GLOBALS['mysqli']->real_escape_string($message);
  337. $sql=<<<EOSQL
  338. INSERT INTO alliancereports (report, alliance_id, time)
  339. VALUES ('{$message}', '{$alliance}', NOW())
  340. EOSQL;
  341. $GLOBALS['mysqli']->query($sql);
  342. }
  343. function getuserinfo($username) {
  344. //safe
  345. $username = $GLOBALS['mysqli']->real_escape_string($username);
  346. $sql=<<<EOSQL
  347. SELECT * FROM users WHERE username = '{$username}'
  348. EOSQL;
  349. $rs = onelinequery($sql);
  350. return $rs;
  351. }
  352. function alliancemembers($alliance_id, $excludeself) {
  353. $memberarray = array();
  354. if ($excludeself) {
  355. $extrasql=<<<EOSQL
  356. AND user_id != '{$_SESSION['user_id']}'
  357. EOSQL;
  358. } else {
  359. $extrasql = "";
  360. }
  361. $sql=<<<EOSQL
  362. SELECT username, user_id FROM users
  363. WHERE alliance_id = '{$alliance_id}'
  364. {$extrasql}
  365. EOSQL;
  366. $sth = $GLOBALS['mysqli']->query($sql);
  367. while ($rs = mysqli_fetch_array($sth)) {
  368. $memberarray[$rs['user_id']] = $rs['username'];
  369. }
  370. return $memberarray;
  371. }
  372. ?>