Controller.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * Controller file.
  4. *
  5. * Provides a class to handle all posible request.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(__DIR__ . "/config.php");
  12. require_once(PATH::HELPER . "DB_Helper.php");
  13. require_once(PATH::HELPER . "TEXT_Helper.php");
  14. require_once(PATH::HELPER . "NET_Helper.php");
  15. require_once(PATH::HELPER . "HTML_Helper.php");
  16. require_once(PATH::HELPER . "APPLICATION_Helper.php");
  17. require_once(PATH::ENTITY . "User.php");
  18. require_once(PATH::ENTITY . "Player.php");
  19. require_once(__DIR__ . "/Constant.php");
  20. require_once(__DIR__ . "/Context.php");
  21. /**
  22. * Application controller.
  23. *
  24. * Handles every request, creating the required models and selecting the
  25. * view.
  26. *
  27. * @category Controller
  28. */
  29. class Controller extends Context{
  30. /**
  31. * @var string[] GET parameters received by the controller.
  32. */
  33. private $params = [];
  34. /**
  35. * @var Context Publicly available context.
  36. */
  37. private $context;
  38. /**
  39. * Retrieves the application context.
  40. *
  41. * @return Context The context.
  42. */
  43. public function get_context(){
  44. return $this->context;
  45. }
  46. /**
  47. * Controller constructor.
  48. *
  49. * Initializes a database connection and sets up the public context.
  50. */
  51. public function __construct(){
  52. $this->context = new Context();
  53. session_start();
  54. $this->db = DB::start();
  55. $this->context->set_db($this->db);
  56. }
  57. /**
  58. * Handles the request parameters.
  59. *
  60. * Receives the request params and gets itself ready. Authenticates the user (if needed)
  61. * and validates the URL.
  62. *
  63. * @param string[] $params GET parameters of the request.
  64. */
  65. public function prepare($params){
  66. // Parse parameters.
  67. foreach($params as $p){
  68. if (strlen($p) > 0){
  69. array_push($this->params, $p);
  70. }
  71. }
  72. if (
  73. count($this->params) > 1 &&
  74. (
  75. strtoupper($this->params[0]) == "ACTION"
  76. ) ||
  77. strtoupper($this->params[0]) == "API"
  78. ){
  79. // For API calls, or login action calls, skip authentication
  80. return;
  81. }
  82. // Authentication
  83. if (array_key_exists("user_token", $_COOKIE) && array_key_exists("user_id", $_COOKIE)){
  84. // Authenticated user, check token.
  85. $statement = $this->db->prepare("
  86. SELECT count(token) AS c
  87. FROM token
  88. WHERE
  89. user = :user AND
  90. token = :token AND
  91. strftime('%s', datetime(expiry, 'unixepoch', 'localtime')) > strftime('%s','now')
  92. ");
  93. $statement->bindValue(":user", $_COOKIE["user_id"], SQLITE3_INTEGER);
  94. $statement->bindValue(":token", hash('sha256', $_COOKIE["user_token"]), SQLITE3_TEXT);
  95. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  96. if ($r["c"] == 0){
  97. // Token is invalid, expired, clear cookies, session, and logout.
  98. require_once(PATH::ACTION . "logout.php");
  99. action();
  100. return;
  101. }
  102. else{
  103. // Token is valid, renew session, and assign user.
  104. $_SESSION["user_id"] = $_COOKIE["user_id"];
  105. $_SESSION["session"] = true;
  106. $this->user = new User($_COOKIE["user_id"]);
  107. $this->context->set_user($this->user);
  108. }
  109. }
  110. else{
  111. // Unauthenticated user. Continue
  112. }
  113. // Validate URL (check if it has the player ID).
  114. $this->player = null;
  115. if (sizeof($this->params) > 0 && preg_match("/^[0-9]{6,12}$/", $this->params[0]) == 1){
  116. // Player ID passed in URL. Check it in database
  117. $statement = $this->db->prepare("
  118. SELECT
  119. id,
  120. user,
  121. public
  122. FROM player
  123. WHERE id = :id
  124. ");
  125. $statement->bindValue(":id", $this->params[0], SQLITE3_TEXT);
  126. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  127. if ($r){
  128. // The player exists...
  129. if (null != $this->user && $r["user"] == $this->user->get_id()){
  130. // ... and it belongs to the user. Proceed.
  131. $this->player = new Player($r["id"]);
  132. $this->context->set_player($this->player);
  133. }
  134. elseif (null == $this->user || $r["user"] != $this->user->get_id()){
  135. // ... and it doesn't belong to the user...
  136. if ($r["public"] == 1){
  137. // ... and it belongs to a public profile. Proceed.
  138. $this->player = new Player($r["id"]);
  139. $this->context->set_player($this->player);
  140. }
  141. else{
  142. // ... and it belongs to a private profile. Deny access.
  143. // TODO
  144. }
  145. }
  146. }
  147. }
  148. else{
  149. // Player ID is not passed as URL...
  150. if (null == $this->user){
  151. // ... and no user logged in. Go to the landing page.
  152. require_once(PATH::PAGE . "Landing_Page.php");
  153. $page = new Landing_Page();
  154. require_once($page->get_view());
  155. return;
  156. }
  157. else{
  158. // ... and the user is logged in. Get last accessed account.
  159. $statement = $this->db->prepare("
  160. SELECT id
  161. FROM player
  162. WHERE user = :user
  163. ORDER BY last_access DESC
  164. ");
  165. $statement->bindValue(":user", $this->user->get_id(), SQLITE3_INTEGER);
  166. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  167. if ($r){
  168. header("Location: /" . $r["id"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
  169. return;
  170. }
  171. else{
  172. require_once(PATH::PAGE . "Landing_Page.php");
  173. $page = new Landing_Page();
  174. require_once($page->get_view());
  175. return;
  176. }
  177. }
  178. }
  179. // Get game mode
  180. $statement = $this->db->prepare("SELECT mode FROM player WHERE id = :id;");
  181. $statement->bindValue(":id", $this->player->get_id(), SQLITE3_INTEGER);
  182. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  183. if ($r && $r["mode"] == GAME_MODE_ID::RTA){
  184. $this->mode = GAME_MODE_ID::RTA;
  185. }
  186. else{
  187. $this->mode = GAME_MODE_ID::NORMAL;
  188. }
  189. $this->context->set_game_mode($this->mode);
  190. }
  191. /**
  192. * Executes the required action.
  193. *
  194. * Must be called after {@see Controller::prepare()}.
  195. * @return void|number
  196. */
  197. public function action(){
  198. // API call: Use API controller
  199. if (count($this->params) > 0 && strtoupper($this->params[0]) == "API"){
  200. if (count($this->params) == 1){
  201. // TODO: API MAIN PAGE
  202. }
  203. else{
  204. $version = strtolower($this->params[1]);
  205. $api_params = $this->params;
  206. array_shift($api_params);
  207. array_shift($api_params);
  208. switch ($version){
  209. case "v1":
  210. require_once(__DIR__ . "/API/v1/API_Controller.php");
  211. new API_Controller($this->params);
  212. break;
  213. }
  214. }
  215. return;
  216. }
  217. // Special case: Actions.
  218. // Not redirecting to page, just execute a function
  219. // TODO: Secure actions
  220. elseif (count($this->params) > 1 && strtoupper($this->params[0]) == "ACTION"){
  221. switch (strtoupper($this->params[1])){
  222. case "CHANGE_GAME_MODE":
  223. require_once(PATH::ACTION . "change_game_mode.php");
  224. action();
  225. return;
  226. break;
  227. case "LOGIN":
  228. require_once(PATH::ACTION . "login.php");
  229. action();
  230. return;
  231. break;
  232. case "LOGOUT":
  233. require_once(PATH::ACTION . "logout.php");
  234. action();
  235. return;
  236. break;
  237. case "NEW_TEAM":
  238. require_once(PATH::ACTION . "new_team.php");
  239. action();
  240. return;
  241. break;
  242. case "DELETE_TEAM":
  243. require_once(PATH::ACTION . "delete_team.php");
  244. action();
  245. return;
  246. break;
  247. case "RATE_TEAM":
  248. require_once(PATH::ACTION . "rate_team.php");
  249. action();
  250. return;
  251. break;
  252. case "SAVE_RUN_TEAM":
  253. require_once(PATH::ACTION . "save_run_team.php");
  254. action();
  255. return;
  256. break;
  257. case "OPTIMIZE_GET_RUNE_LIST":
  258. require_once(PATH::ACTION . "optimize_get_rune_list.php");
  259. $response = action();
  260. if (strlen($response) > 1){
  261. echo($response);
  262. return;
  263. }
  264. else{
  265. return;
  266. }
  267. break;
  268. case "OPTIMIZE_GET_OPTIONS":
  269. require_once(PATH::ACTION . "optimize_get_options.php");
  270. $response = action();
  271. if (strlen($response) > 1){
  272. echo($response);
  273. return;
  274. }
  275. else{
  276. return;
  277. }
  278. break;
  279. case "EDIT_PROFILE":
  280. require_once(PATH::ACTION . "edit_profile.php");
  281. $response = action();
  282. if (strlen($response) > 1){
  283. echo($response);
  284. return;
  285. }
  286. else{
  287. return;
  288. }
  289. break;
  290. default:
  291. require_once(PATH::PAGE . "Error_Page.php");
  292. $page = new Error_Page(404);
  293. http_response_code(404);
  294. }
  295. }
  296. // Select the model to load.
  297. array_shift($this->params); // Remove first one, should be player_id
  298. if (sizeof($this->params) == 0){
  299. require_once(PATH::PAGE . "Home_Page.php");
  300. $page = new Home_Page();
  301. }
  302. else{
  303. if (strtoupper($this->params[0]) == "RUNES"){
  304. require_once(PATH::PAGE . "Runes_Page.php");
  305. $page = new Runes_Page();
  306. }
  307. elseif (strtoupper($this->params[0]) == "ENCHANTMENTS"){
  308. require_once(PATH::PAGE . "Enchantments_Page.php");
  309. $page = new Enchantments_Page();
  310. }
  311. elseif (strtoupper($this->params[0]) == "ARTIFACTS"){
  312. require_once(PATH::PAGE . "Artifacts_Page.php");
  313. $page = new Artifacts_Page();
  314. }
  315. elseif (strtoupper($this->params[0]) == "GUILD"){
  316. require_once(PATH::PAGE . "Guild_Page.php");
  317. $page = new Guild_Page();
  318. }
  319. elseif (strtoupper($this->params[0]) == "RUNS"){
  320. require_once(PATH::PAGE . "Runs_Page.php");
  321. $page = new Runs_Page();
  322. }
  323. elseif (strtoupper($this->params[0]) == "STATS"){
  324. require_once(PATH::PAGE . "Stats_Page.php");
  325. $page = new Stats_Page();
  326. }
  327. elseif (strtoupper($this->params[0]) == "TEAMS"){
  328. require_once(PATH::PAGE . "Teams_Page.php");
  329. $page = new Teams_Page();
  330. }
  331. elseif (strtoupper($this->params[0]) == "PROFILE"){
  332. require_once(PATH::PAGE . "Profile_Page.php");
  333. $page = new Profile_Page();
  334. }
  335. elseif (strtoupper($this->params[0]) == "UNITS"){
  336. if (count($this->params) == 1){
  337. require_once(PATH::PAGE . "Units_Page.php");
  338. $page = new Units_Page();
  339. }
  340. else{
  341. require_once(PATH::PAGE . "Unit_Page.php");
  342. $page = new Unit_Page($this->params[1]);
  343. }
  344. }
  345. elseif (strtoupper($this->params[0]) == "MONSTERS"){
  346. if (count($this->params) == 1){
  347. require_once(PATH::PAGE . "Monsters_Page.php");
  348. $page = new Monsters_Page();
  349. }
  350. else{
  351. require_once(PATH::PAGE . "Monster_Page.php");
  352. $page = new Monster_Page($this->params[1]);
  353. }
  354. }
  355. elseif (strtoupper($this->params[0]) == "REPORT"){
  356. if (count($this->params) == 1){
  357. require_once(PATH::PAGE . "Report_Page.php");
  358. $page = new Report_Page();
  359. }
  360. elseif (strtoupper($this->params[1]) == "SKILLUP"){
  361. require_once(PATH::PAGE . "Report_Skillup_Page.php");
  362. $page = new Report_Skillup_Page($this->params[1]);
  363. }
  364. elseif (strtoupper($this->params[1]) == "RUNE_ENCHANTMENT"){
  365. require_once(PATH::PAGE . "Report_Rune_Enchantment_Page.php");
  366. $page = new Report_Rune_Enchantment_Page($this->params[1]);
  367. }
  368. }
  369. elseif (strtoupper($this->params[0]) == "OPTIMIZER"){
  370. if (count($this->params) == 1){
  371. require_once(PATH::PAGE . "Optimizer_Page.php");
  372. $page = new Optimizer_Page();
  373. }
  374. else{
  375. require_once(PATH::PAGE . "Optimizer_Unit_Page.php");
  376. $page = new Optimizer_Unit_Page($this->params[1]);
  377. }
  378. }
  379. }
  380. // Load the view, or set an error code.
  381. if (isset($page)){
  382. require_once($page->get_view());
  383. return 200;
  384. }
  385. else{
  386. require_once(PATH::PAGE . "Error_Page.php");
  387. $page = new Error_Page(404);
  388. require_once($page->get_view());
  389. http_response_code(404);
  390. return 404;
  391. }
  392. }
  393. }