-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontroller.php
285 lines (250 loc) · 7.69 KB
/
controller.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
require("model.php");
require("router.php");
class Flash{
public $msg;
public $type;
function __construct($msg, $type)
{
$this->msg = $msg;
$this->type = $type;
}
public function display(){
echo "<div class=\"flash " . $this->type . "\">" . $this->msg . "</div>";
}
}
class Controller{
//--------Variables------------
private $model;
private $router;
//--------Functions------------
//Constructor
function __construct(){
//initialize private variables
$this->model = new Model();
$this->router = new Router();
//Proccess Query String
if(strlen($_GET['query']) > 0)
{
$queryParams = explode("/", $_GET['query']);
}
else{
$queryParams = false;
}
$page = $_GET['page'];
//Handle Page Load
$endpoint = $this->router->lookup($page);
if($endpoint === false)
{
header("HTTP/1.0 404 Not Found");
}
else
{
$this->$endpoint($queryParams);
}
}
private function redirect($url){
header("Location: /" . $url);
}
//--- Framework Functions
private function loadView($view, $data = null){
if(is_array($data))
{
extract($data);
}
require("Views/" . $view . ".php");
}
private function loadPage($user, $view, $data = null, $flash = false){
$this->loadView("header", array('User' => $user));
if($flash !== false)
{
$flash->display();
}
$this->loadView($view, $data);
$this->loadView("footer");
}
//--- Security Functions
private function checkAuth(){
if(isset($_COOKIE['Auth']))
{
return $this->model->userForAuth($_COOKIE['Auth']);
}
else
{
return false;
}
}
private function indexPage($params){
$user = $this->checkAuth();
if($user !== false){ $this->redirect("buddies"); }
else
{
$flash = false;
if($params !== false)
{
$flashArr = array(
"0" => new Flash("Your Username and/or Password was incorrect.", "error"),
"1" => new Flash("There's already a user with that email address.", "error"),
"2" => new Flash("That username has already been taken.", "error"),
"3" => new Flash("Passwords don't match.", "error"),
"4" => new Flash("Your Password must be at least 6 characters long.", "error"),
"5" => new Flash("You must enter a valid Email address.", "error"),
"6" => new Flash("You must enter a username.", "error"),
"7" => new Flash("You have to be signed in to acces that page.", "warning")
);
$flash = $flashArr[$params[0]];
}
$this->loadPage($user, "home", array(), $flash);
}
}
private function signUp(){
if($_POST['email'] == "" || strpos($_POST['email'], "@") === false){
$this->redirect("home/5");
}
else if($_POST['username'] == ""){
$this->redirect("home/6");
}
else if(strlen($_POST['password']) < 6)
{
$this->redirect("home/4");
}
else if($_POST['password'] != $_POST['password2'])
{
$this->redirect("home/3");
}
else{
$pass = hash('sha256', $_POST['password']);
$signupInfo = array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'password' => $pass,
'name' => $_POST['name']
);
$resp = $this->model->signupUser($signupInfo);
if($resp === true)
{
$this->redirect("buddies/1");
}
else
{
$this->redirect("home/" . $resp);
}
}
}
private function login(){
$pass = hash('sha256', $_POST['password']);
$loginInfo = array(
'username' => $_POST['username'],
'password' => $pass
);
if($this->model->attemptLogin($loginInfo))
{
$this->redirect("buddies/0");
}
else
{
$this->redirect("home/0");
}
}
private function logout(){
$this->model->logoutUser($_COOKIE['Auth']);
$this->redirect("home");
}
private function buddies($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else
{
$userData = $this->model->getUserInfo($user);
$fribbits = $this->model->getFollowersRibbits($user);
$flash = false;
if(isset($params[0]))
{
$flashArr = array(
"0" => new Flash("Welcome Back, " . $user->name, "notice"),
"1" => new Flash("Welcome to Ribbit, Thanks for signing up.", "notice"),
"2" => new Flash("You have exceeded the 140 character limit for Ribbits", "error")
);
$flash = $flashArr[$params[0]];
}
$this->loadPage($user, "buddies", array('User' => $user, "userData" => $userData, "fribbits" => $fribbits), $flash);
}
}
private function newRibbit($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else{
$text = mysql_real_escape_string($_POST['text']);
if(strlen($text) > 140)
{
$this->redirect("buddies/2");
}
else
{
$this->model->postRibbit($user, $text);
$this->redirect("buddies");
}
}
}
private function publicPage($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else
{
$q = false;
if(isset($_POST['query']))
{
$q = $_POST['query'];
}
$ribbits = $this->model->getPublicRibbits($q);
$this->loadPage($user, "public", array('ribbits' => $ribbits));
}
}
private function profiles($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else{
$q = false;
if(isset($_POST['query']))
{
$q = $_POST['query'];
}
$profiles = $this->model->getPublicProfiles($user, $q);
$this->loadPage($user, "profiles", array('profiles' => $profiles));
}
}
private function follow($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else{
$this->model->follow($user, $params[0]);
$this->redirect("profiles");
}
}
private function unfollow($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else{
$this->model->unfollow($user, $params[0]);
$this->redirect("profiles");
}
}
/*
private function unfollow($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else{
$this->model->unfollow($user, $params[0]);
$this->redirect("profiles");
}
}
private function follow($params){
$user = $this->checkAuth();
if($user === false){ $this->redirect("home/7"); }
else{
$this->model->follow($user, $params[0]);
$this->redirect("profiles");
}
}
*/
}