-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
109 lines (93 loc) · 3.55 KB
/
index.php
File metadata and controls
109 lines (93 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* The index.php file handles the request for each call to the application
* and calls the chosen controller and method after splitting the URL.
*
*/
// make the session variables avilable
session_set_cookie_params(60*60*24*14,"/");
session_start();
///////////////////////Define Error Handling///////////////////////
function errorPage() {
header("Location: error.html");
}
function errorMessage($errorcode, $errortext, $errorfile, $errorline) {
echo "{$errortext}; {$errorfile} -> {$errorline} <br/>";
}
// either "errorMessage" or "errorPage" according to the function that should get called
set_error_handler("errorPage");
///////////////////////load all nessesary files///////////////////////
require_once 'app/controller/abstract_controller.php';
require_once 'app/model/abstract_data_object.php';
require_once 'app/model/poll_data_object.php';
require_once 'app/model/poll_list_data_object.php';
require_once 'app/model/rating_list_data_object.php';
//////////////////Split URL and call the right controller//////////////////
//default controller
define("DEFAULT_CONTROLLER", "home");
define("DEFAULT_DOMAIN", "/LecturePoll/");
// set default values
/** Stores the controller from the split URL */
$controller = DEFAULT_CONTROLLER;
/** Stores the method from the split URL */
$function = 'index';
/** Stores the parameters from the split URL */
$params = [];
// parse url
if (isset ($_GET['url'])) {
// Explode a trimmed and sanitized URL by /
$url = explode ( '/', filter_var ( rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
// set controller
if (isset ($url[0])) {
if (file_exists('app/controller/' . $url[0] . '.php')) {
// if a valid controller is given use it as the controller
$controller = $url[0];
unset($url[0]);
} else {
// if $url[] is not a valid controller name for the selected controller,
// or the index-function gets called, redirect the client so that the URL
// gets shown accuratly
header("Location: " . DEFAULT_DOMAIN. DEFAULT_CONTROLLER);
}
}
// load the wanted controller class
require_once 'app/controller/' . $controller . '.php';
//creates on object of the controller
$controller = new $controller(array_merge($_GET, $_POST));
// set function
if (isset($url[1])) {
if(method_exists($controller, $url[1])) {
$function = $url[1];
unset($url[1]);
} else {
// if $url[1] is not a valid function name for the selected controller,
// or the index-function gets called, redirect the client so that the URL
// gets shown accuratly
header("Location: " . DEFAULT_DOMAIN. DEFAULT_CONTROLLER);
}
}
// set parameters
if (isset($url)) {
$params = array_values($url);
}
////////////////////////Authentication////////////////////////
// find out the usertype
$usertype = abstract_data_object::UNKOWN_USERTYPE;
if (isset($_SESSION["pollCode"])) {
$pollListDataObject = new poll_list_data_object();
if ($pollListDataObject->checkStudentPollCode($_SESSION["pollCode"])) {
$usertype = abstract_data_object::STUDENT_USERTYPE;
} else if ($pollListDataObject->checkTeacherPollCode($_SESSION["pollCode"])) {
$usertype = abstract_data_object::TEACHER_USERTYPE;
}
}
// check, if the user is allowed to access the wanted controller
if ($controller->authenticate($usertype)) {
// if user is allowed run the controller function
call_user_func_array([$controller, $function], $params);
} else {
// if user is not allowed to call the controller then link him to the default page
header("Location: " . DEFAULT_DOMAIN. DEFAULT_CONTROLLER);
}
?>