Controller.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 IVV
  10. */
  11. require_once(__DIR__ . "/Context.php");
  12. require_once(__DIR__ . "/helper/DB_Helper.php");
  13. require_once(__DIR__ . "/helper/TEXT_Helper.php");
  14. require_once(__DIR__ . "/helper/NET_Helper.php");
  15. require_once(__DIR__ . "/helper/HTML_Helper.php");
  16. require_once(__DIR__ . "/util/Log.php");
  17. require_once(__DIR__ . "/util/Path.php");
  18. require_once(__DIR__ . "/util/URL.php");
  19. //require_once(__DIR__ . "/helper/net.php");
  20. //require_once(__DIR__ . "/Application.php");
  21. //require_once(__DIR__ . "/util/Log.php");
  22. /**
  23. * Application controller.
  24. *
  25. * Handles every request, creating the required models and selecting the
  26. * view.
  27. *
  28. * @category Controller
  29. */
  30. class Controller extends Context{
  31. /**
  32. * @var string[] GET parameters received by the controller.
  33. */
  34. private $params = [];
  35. /**
  36. * @var Context Publicly available context.
  37. */
  38. private $context;
  39. private $command = "";
  40. private $status = 201;
  41. private $message = "OK";
  42. /**
  43. * Retrieves the application context.
  44. *
  45. * @return Context The context.
  46. */
  47. public function get_context(){
  48. return $this->context;
  49. }
  50. /**
  51. * Controller constructor.
  52. *
  53. * Initializes a database connection and sets up the public context.
  54. */
  55. public function __construct(){
  56. $this->context = new Context();
  57. session_start();
  58. $configuration = parse_ini_file(__DIR__ . "/config/config.ini", true);
  59. $this->db = DB::start($configuration["database"]);
  60. $this->context->set_db($this->db);
  61. Log::debug("Controller created succesfully");
  62. }
  63. private function execute_action($action){
  64. require_once(PATH::ACTION . $action . ".php");
  65. action();
  66. return;
  67. }
  68. /**
  69. * Handles the request parameters.
  70. *
  71. * Receives the request params and gets itself ready. Authenticates the user (if needed)
  72. * and validates the URL.
  73. *
  74. * @param string[] $params GET parameters of the request.
  75. */
  76. public function prepare($params){
  77. // Parse parameters.
  78. foreach($params as $p){
  79. if (strlen($p) > 0){
  80. Log::debug("Controller parameter: $p");
  81. array_push($this->params, $p);
  82. }
  83. }
  84. // Obtain the command and process it
  85. if (sizeof($this->params) > 0){
  86. $this->command = strtoupper($this->params[0]);
  87. }
  88. Log::debug("Controller command set: '" . $this->command . "'");
  89. switch ($this->command){
  90. case "": // Main page
  91. break;
  92. case "API": // An API call
  93. return; // End preparation here, the API controller will deal with everything.
  94. break;
  95. case "ACTION": // Execute an action
  96. //$auth_required = true;
  97. break;
  98. case "PLAYER": // A player page
  99. if (sizeof($this->params) > 1){
  100. $player_id = $this->params[1];
  101. }
  102. else{
  103. $this->status = 400;
  104. $this->message = "Bad request";
  105. return;
  106. }
  107. break;
  108. case "LOGIN": // The login page
  109. break;
  110. case "REGISTER": // Registration page
  111. break;
  112. case "SEARCH": // Search results
  113. break;
  114. case "DUNGEON": // Dungeon static pages
  115. break;
  116. case "BUILDING": // Building static pages
  117. break;
  118. case "FUSION": // Fusion static pages
  119. break;
  120. case "HELP": // Help static page
  121. break;
  122. case "ERROR": // Error page
  123. break;
  124. default: // Unknown page
  125. }
  126. }
  127. /**
  128. * Executes the required action.
  129. *
  130. * Must be called after {@see Controller::prepare()}.
  131. * @return void|number
  132. */
  133. public function action(){
  134. Log::debug("Executing action");
  135. // If there was an error in preparation, it's time to throw it.
  136. if ($this->status >= 400 && $this->status < 500){
  137. Log::debug("Executing action error code: " . $this->status);
  138. require_once(PATH::PAGE . "Error_Page.php");
  139. $page = new Error_Page($this->status, $this->message);
  140. require_once($page->get_view());
  141. http_response_code($this->status);
  142. return $this->status;
  143. }
  144. // Process the command
  145. $page = null;
  146. switch ($this->command){
  147. case "": // Main page
  148. require_once(PATH::PAGE . "Home_Page.php");
  149. $page = new Home_Page();
  150. break;
  151. case "ACTION": // Execute an action
  152. // If no action specified
  153. if (sizeof($this->params) < 2){
  154. require_once(PATH::PAGE . "Error_Page.php");
  155. $page = new Error_Page(400, "No action specified.");
  156. require_once($page->get_view());
  157. http_response_code(400);
  158. return 400;
  159. }
  160. // Process action
  161. $action = strtoupper($this->params[1]);
  162. switch ($action){
  163. // Special cases where the output must be printed:
  164. case "OPTIMIZE_LIST":
  165. require_once(PATH::ACTION . "Optimize_List_Action.php");
  166. $action = new Optimize_List_Action();
  167. break;
  168. default:
  169. require_once(PATH::PAGE . "Error_Page.php");
  170. $page = new Error_Page(404, "Action not found");
  171. require_once($page->get_view());
  172. http_response_code(404);
  173. return;
  174. }
  175. $action->execute();
  176. if (strlen($action->get_output()) > 0){
  177. echo($action->get_output());
  178. }
  179. http_response_code($action->get_code());
  180. return $action->get_code();
  181. break;
  182. case "PLAYER": // A player page
  183. if ($this->context->get_player() == null || $this->context->get_player()->get_id() == null){
  184. require_once(PATH::PAGE . "Error_Page.php");
  185. $page = new Error_Page(404, "Player not found");
  186. require_once($page->get_view());
  187. http_response_code(404);
  188. return;
  189. }
  190. if (
  191. $this->context->get_player()->is_public() == false &&
  192. (
  193. $this->context->get_user() == null ||
  194. $this->context->get_player()->get_user() != $this->context->get_user()->get_id()
  195. )
  196. ){
  197. require_once(PATH::PAGE . "Error_Page.php");
  198. $page = new Error_Page(401, "Player profile set to private");
  199. require_once($page->get_view());
  200. http_response_code(401);
  201. }
  202. $subcommand = "";
  203. if (sizeof($this->params) > 2){
  204. $subcommand = strtoupper($this->params[2]);
  205. }
  206. $page = null;
  207. switch ($subcommand){
  208. case "": // Player profile
  209. require_once(PATH::PAGE . "Player_Page.php");
  210. $page = new Player_Page($this->context->get_player()->get_id());
  211. break;
  212. case "TEAMS":
  213. require_once(PATH::PAGE . "Teams_Page.php");
  214. $page = new Teams_Page();
  215. break;
  216. }
  217. break;
  218. case "BUILDINGS": // Building static pages
  219. if (sizeof($this->params) > 1){
  220. require_once(PATH::PAGE . "Building_Page.php");
  221. $page = new Building_Page($this->params[1]);
  222. }
  223. else{
  224. require_once(PATH::PAGE . "Buildings_Page.php");
  225. $page = new Buildings_Page();
  226. }
  227. break;
  228. break;
  229. /*case "FUSION": // Fusion static pages
  230. require_once(PATH::PAGE . "Fusion_Page.php");
  231. $page = new Fusion_Page();
  232. break;*/
  233. case "HELP": // Help static page
  234. require_once(PATH::PAGE . "Help_Page.php");
  235. $page = new Help_Page();
  236. break;
  237. }
  238. // If page is not set, it's a not found.
  239. if ($page == null){
  240. require_once(PATH::PAGE . "Error_Page.php");
  241. $page = new Error_Page(404, "Page not found");
  242. }
  243. // Once the page is loaded, chack if it can be seen
  244. // TODO verify
  245. if ($page->get_code() >= 400 && $page->get_code() < 600){
  246. require_once(PATH::PAGE . "Error_Page.php");
  247. $page = new Error_Page($page->get_code(), $page->get_message());
  248. http_response_code($page->get_code());
  249. }
  250. // Load the view, or set an error code.
  251. Log::debug("Loading view: " . $page->get_view());
  252. require_once($page->get_view());
  253. http_response_code($page->get_code());
  254. }
  255. /**
  256. * Constructor.
  257. *
  258. * Handles every request, creating the required models and selecting the
  259. * view.
  260. */
  261. /*public function __construct($params){
  262. global $path;
  263. global $base_url;
  264. global $static;
  265. global $static_url;
  266. global $base_dir;
  267. $page;
  268. header("Content-Type: text/html; charset=utf8");
  269. // Read configuration
  270. // TODO: Read the other params and do something with them.
  271. $configuration = parse_ini_file(__DIR__ . "/config/config.ini", true);
  272. $db = DB::start($configuration["database"]);
  273. // Parse parameters, get lang and build the route.
  274. $pars = [];
  275. foreach($params as $p){
  276. if (strlen($p) > 0){
  277. array_push($pars, $p);
  278. }
  279. }
  280. $route = [];
  281. if (sizeof($pars) > 0 && preg_match("/^[a-zA-Z]{2}$/", $pars[0])){
  282. $lang = $pars[0];
  283. array_splice($pars, 0, 1);
  284. // Override global to include language in URL
  285. $base_url = get_protocol() . $_SERVER["HTTP_HOST"] . "/" . $lang;
  286. }
  287. else{
  288. $lang = select_language($db);
  289. }
  290. foreach($pars as $p){
  291. array_push($route, $p);
  292. }
  293. // Select the model to load.
  294. if (sizeof($route) == 0){
  295. require_once($path["page"] . "Home_Page.php");
  296. $page = new Home_Page($db, $lang);
  297. }
  298. else{
  299. if (strtoupper($route[0]) == "HELP"){
  300. require_once($path["page"] . "Help_Page.php");
  301. $page = new Help_Page($db, $lang);
  302. }
  303. if (strtoupper($route[0]) == "PROFILE"){
  304. require_once($path["page"] . "Profile_Page.php");
  305. $page = new Profile_Page($db, $lang);
  306. }
  307. elseif (strtoupper($route[0]) == "PROJECT"){
  308. if (sizeof($route) > 1){
  309. // Project page
  310. require_once($path["page"] . "Project_Page.php");
  311. $page = new Project_Page($db, $lang, $route[1]);
  312. }
  313. else{
  314. // Project list page
  315. require_once($path["page"] . "Projects_Page.php");
  316. $page = new Projects_Page($db, $lang);
  317. }
  318. }
  319. }
  320. // Load the view, or set an error code.
  321. if (!isset($page)){
  322. http_response_code(404);
  323. require_once($path["page"] . "Error_Page.php");
  324. $page = new Error_Page($db, $lang);
  325. }
  326. require_once($page->view);
  327. }*/
  328. }
  329. ?>