PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/website/database.php

https://gitlab.com/kidaa/SeventhUmbral
PHP | 426 lines | 363 code | 59 blank | 4 comment | 34 complexity | b81c4bc67fa858ef85bbd28ec55300e9 MD5 | raw file
  1. <?php
  2. include("config.php");
  3. mysqli_report(MYSQLI_REPORT_STRICT);
  4. function CreateDatabaseConnection($server, $username, $password, $database)
  5. {
  6. try
  7. {
  8. $dataConnection = new mysqli($server, $username, $password);
  9. }
  10. catch(Exception $e)
  11. {
  12. die("Error while connecting to the database");
  13. }
  14. $dataConnection->select_db($database);
  15. $dataConnection->query("SET NAMES 'utf8'");
  16. return $dataConnection;
  17. }
  18. $g_databaseConnection = CreateDatabaseConnection($db_server, $db_username, $db_password, $db_database);
  19. function GenerateRandomSha224()
  20. {
  21. mt_srand(microtime(true) * 100000 + memory_get_usage(true));
  22. return hash("sha224", uniqid(mt_rand(), true));
  23. }
  24. function VerifyUser($dataConnection, $username, $password)
  25. {
  26. $statement = $dataConnection->prepare("SELECT id, passhash, salt FROM ffxiv_users WHERE name = ?");
  27. if(!$statement)
  28. {
  29. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  30. }
  31. try
  32. {
  33. $statement->bind_param('s', $username);
  34. if(!$statement->execute())
  35. {
  36. throw new Exception(__FUNCTION__ . " failed.");
  37. }
  38. $statement->bind_result($id, $storedPasshash, $salt);
  39. if(!$statement->fetch())
  40. {
  41. throw new Exception(__FUNCTION__ . " failed.");
  42. }
  43. $saltedPassword = $password . $salt;
  44. $hashedPassword = hash("sha224", $saltedPassword);
  45. if($hashedPassword !== $storedPasshash)
  46. {
  47. throw new Exception(__FUNCTION__ . " failed.");
  48. }
  49. return $id;
  50. }
  51. finally
  52. {
  53. $statement->close();
  54. }
  55. }
  56. function InsertUser($dataConnection, $username, $passhash, $salt, $email)
  57. {
  58. {
  59. $statement = $dataConnection->prepare("INSERT INTO ffxiv_users (name, passhash, salt, email) VALUES (?, ?, ?, ?)");
  60. if(!$statement)
  61. {
  62. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  63. }
  64. try
  65. {
  66. $statement->bind_param('ssss', $username, $passhash, $salt, $email);
  67. if(!$statement->execute())
  68. {
  69. throw new Exception(__FUNCTION__ . " failed.");
  70. }
  71. }
  72. finally
  73. {
  74. $statement->close();
  75. }
  76. }
  77. //Insert new character
  78. {
  79. //This kinda suck, but we don't have many choices for now...
  80. $statement = $dataConnection->prepare("
  81. INSERT INTO ffxiv_characters
  82. (
  83. userId, name, tribe, size, voice, skinColor, hairStyle, hairColor, hairOption, eyeColor, faceType, faceBrow, faceEye, faceIris,
  84. faceNose, faceMouth, faceJaw, faceCheek, faceOption1, faceOption2, guardian, birthMonth, birthDay, allegiance,
  85. weapon1, weapon2, headGear, bodyGear, legsGear, handsGear, feetGear, waistGear,
  86. rightEarGear, leftEarGear, rightFingerGear, leftFingerGear
  87. )
  88. VALUES
  89. (
  90. LAST_INSERT_ID(), 'Miraudont', 6, 0, 13, 66, 4, 63, 0, 59, 7, 2, 4, 0,
  91. 5, 3, 0, 2, 30, 3, 7, 4, 19, 1,
  92. 0, 0, 19503, 14598, 3268, 14560, 13475, 0,
  93. 0, 0, 0, 0
  94. )
  95. ");
  96. if(!$statement)
  97. {
  98. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  99. }
  100. try
  101. {
  102. if(!$statement->execute())
  103. {
  104. throw new Exception(__FUNCTION__ . " failed.");
  105. }
  106. }
  107. finally
  108. {
  109. $statement->close();
  110. }
  111. }
  112. }
  113. function RefreshOrCreateSession($dataConnection, $userId)
  114. {
  115. try
  116. {
  117. $sessionId = GetSessionFromUserId($dataConnection, $userId);
  118. RefreshSession($dataConnection, $sessionId);
  119. }
  120. catch(Exception $e)
  121. {
  122. $sessionId = CreateSession($dataConnection, $userId);
  123. }
  124. return $sessionId;
  125. }
  126. function CreateSession($dataConnection, $userId)
  127. {
  128. //Delete any session that might be active
  129. {
  130. $statement = $dataConnection->prepare("DELETE FROM ffxiv_sessions WHERE userId = ?");
  131. if(!$statement)
  132. {
  133. throw new Exception("Failed to create session: " . $dataConnection->error);
  134. }
  135. try
  136. {
  137. $statement->bind_param('i', $userId);
  138. if(!$statement->execute())
  139. {
  140. throw new Exception("Failed to create session: " . $dataConnection->error);
  141. }
  142. }
  143. finally
  144. {
  145. $statement->close();
  146. }
  147. }
  148. //Create new session
  149. {
  150. $sessionId = GenerateRandomSha224();
  151. $statement = $dataConnection->prepare("INSERT INTO ffxiv_sessions (id, userid, expiration) VALUES (?, ?, NOW() + INTERVAL " . FFXIV_SESSION_LENGTH . " HOUR)");
  152. if(!$statement)
  153. {
  154. throw new Exception("Failed to create session: " . $dataConnection->error);
  155. }
  156. try
  157. {
  158. $statement->bind_param('si', $sessionId, $userId);
  159. if(!$statement->execute())
  160. {
  161. throw new Exception("Failed to create session: " . $dataConnection->error);
  162. }
  163. }
  164. finally
  165. {
  166. $statement->close();
  167. }
  168. return $sessionId;
  169. }
  170. }
  171. function GetSessionFromUserId($dataConnection, $userId)
  172. {
  173. $statement = $dataConnection->prepare("SELECT id FROM ffxiv_sessions WHERE userId = ? AND expiration > NOW()");
  174. if(!$statement)
  175. {
  176. throw new Exception("Failed to get session id: " . $dataConnection->error);
  177. }
  178. try
  179. {
  180. $statement->bind_param('i', $userId);
  181. if(!$statement->execute())
  182. {
  183. throw new Exception("Failed to get session id: " . $dataConnection->error);
  184. }
  185. $statement->bind_result($sessionId);
  186. if(!$statement->fetch())
  187. {
  188. throw new Exception("Failed to get session id: " . $dataConnection->error);
  189. }
  190. return $sessionId;
  191. }
  192. finally
  193. {
  194. $statement->close();
  195. }
  196. }
  197. function RefreshSession($dataConnection, $sessionId)
  198. {
  199. $statement = $dataConnection->prepare("UPDATE ffxiv_sessions SET expiration = NOW() + INTERVAL " . FFXIV_SESSION_LENGTH . " HOUR WHERE id = ?");
  200. if(!$statement)
  201. {
  202. throw new Exception("Failed to refresh session: " . $dataConnection->error);
  203. }
  204. try
  205. {
  206. $statement->bind_param('s', $sessionId);
  207. if(!$statement->execute())
  208. {
  209. throw new Exception("Failed to refresh session: " . $dataConnection->error);
  210. }
  211. }
  212. finally
  213. {
  214. $statement->close();
  215. }
  216. }
  217. function GetUserIdFromSession($dataConnection, $sessionId)
  218. {
  219. $statement = $dataConnection->prepare("SELECT userId FROM ffxiv_sessions WHERE id = ? AND expiration > NOW()");
  220. if(!$statement)
  221. {
  222. throw new Exception("Could not get user id.");
  223. }
  224. try
  225. {
  226. $statement->bind_param('s', $sessionId);
  227. if(!$statement->execute())
  228. {
  229. throw new Exception("Could not get user id.");
  230. }
  231. $statement->bind_result($userId);
  232. if(!$statement->fetch())
  233. {
  234. throw new Exception("Could not get user id.");
  235. }
  236. return $userId;
  237. }
  238. finally
  239. {
  240. $statement->close();
  241. }
  242. }
  243. function GetUserInfo($dataConnection, $userId)
  244. {
  245. $statement = $dataConnection->prepare("SELECT name FROM ffxiv_users WHERE id = ?");
  246. if(!$statement)
  247. {
  248. throw new Exception("Failed to get user information: " . $dataConnection->error);
  249. }
  250. try
  251. {
  252. $statement->bind_param('i', $userId);
  253. if(!$statement->execute())
  254. {
  255. throw new Exception("Failed to get user information: " . $dataConnection->error);
  256. }
  257. $result = $statement->get_result();
  258. if(!$result)
  259. {
  260. throw new Exception("Failed to get user information: " . $dataConnection->error);
  261. }
  262. $row = $result->fetch_assoc();
  263. if(!$row)
  264. {
  265. throw new Exception("Failed to get user information: " . $dataConnection->error);
  266. }
  267. return $row;
  268. }
  269. finally
  270. {
  271. $statement->close();
  272. }
  273. }
  274. function GetUserCharacters($dataConnection, $userId)
  275. {
  276. $statement = $dataConnection->prepare("SELECT id, name FROM ffxiv_characters WHERE userId = ?");
  277. if(!$statement)
  278. {
  279. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  280. }
  281. try
  282. {
  283. $statement->bind_param('i', $userId);
  284. if(!$statement->execute())
  285. {
  286. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  287. }
  288. $result = $statement->get_result();
  289. if(!$result)
  290. {
  291. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  292. }
  293. $characters = array();
  294. while(1)
  295. {
  296. $row = $result->fetch_assoc();
  297. if(!$row)
  298. {
  299. break;
  300. }
  301. array_push($characters, $row);
  302. }
  303. return $characters;
  304. }
  305. finally
  306. {
  307. $statement->close();
  308. }
  309. }
  310. function GetCharacterInfo($dataConnection, $userId, $characterId)
  311. {
  312. $query = sprintf("SELECT * FROM ffxiv_characters WHERE userId = '%d' AND id = '%d'",
  313. $userId, $characterId);
  314. $result = $dataConnection->query($query);
  315. if(!$result)
  316. {
  317. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  318. }
  319. $row = $result->fetch_assoc();
  320. if(!$row)
  321. {
  322. throw new Exception(__FUNCTION__ . " failed: " . $dataConnection->error);
  323. }
  324. return $row;
  325. }
  326. function UpdateCharacterInfo($dataConnection, $characterId, $characterInfo)
  327. {
  328. $statement = $dataConnection->prepare("UPDATE ffxiv_characters SET
  329. name = ?, tribe = ?, size = ?, voice = ?, skinColor = ?, hairStyle = ?, hairColor = ?, hairOption = ?,
  330. eyeColor = ?, faceType = ?, faceBrow = ?, faceEye = ?, faceIris = ?, faceNose = ?, faceMouth = ?, faceJaw = ?,
  331. faceCheek = ?, faceOption1 = ?, faceOption2 = ?, guardian = ?, birthMonth = ?, birthDay = ?, allegiance = ?,
  332. weapon1 = ?, weapon2 = ?, headGear = ?, bodyGear = ?, legsGear = ?, handsGear = ?, feetGear = ?,
  333. waistGear = ?, rightEarGear = ?, leftEarGear = ?, rightFingerGear = ?, leftFingerGear = ?
  334. WHERE id = ?");
  335. if(!$statement)
  336. {
  337. throw new Exception("Failed to update character information: " . $dataConnection->error);
  338. }
  339. try
  340. {
  341. if(!$statement->bind_param("siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii",
  342. $characterInfo["name"], $characterInfo["tribe"], $characterInfo["size"], $characterInfo["voice"],
  343. $characterInfo["skinColor"], $characterInfo["hairStyle"], $characterInfo["hairColor"],
  344. $characterInfo["hairOption"], $characterInfo["eyeColor"], $characterInfo["faceType"],
  345. $characterInfo["faceBrow"], $characterInfo["faceEye"], $characterInfo["faceIris"],
  346. $characterInfo["faceNose"], $characterInfo["faceMouth"], $characterInfo["faceJaw"],
  347. $characterInfo["faceCheek"], $characterInfo["faceOption1"], $characterInfo["faceOption2"],
  348. $characterInfo["guardian"], $characterInfo["birthMonth"], $characterInfo["birthDay"], $characterInfo["allegiance"],
  349. $characterInfo["weapon1"], $characterInfo["weapon2"], $characterInfo["headGear"], $characterInfo["bodyGear"],
  350. $characterInfo["legsGear"], $characterInfo["handsGear"], $characterInfo["feetGear"],
  351. $characterInfo["waistGear"], $characterInfo["rightEarGear"], $characterInfo["leftEarGear"],
  352. $characterInfo["rightFingerGear"], $characterInfo["leftFingerGear"],
  353. $characterId))
  354. {
  355. throw new Exception("Failed to update character information: " . $dataConnection->error);
  356. }
  357. if(!$statement->execute())
  358. {
  359. throw new Exception("Failed to update character information: " . $dataConnection->error);
  360. }
  361. }
  362. finally
  363. {
  364. $statement->close();
  365. }
  366. }
  367. ?>