-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.php
More file actions
231 lines (228 loc) · 6.05 KB
/
controller.php
File metadata and controls
231 lines (228 loc) · 6.05 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
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
<?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{
private $model; // will model class
private $router; // will router class
function __construct(){
//initialize private variables
$this->model = new Model();
$this->router = new Router();
// process query string
$queryParams = false;
if(strlen($_GET['query']) > 0)
{
$queryParams = explode("/", $_GET['query']);
}
// handle page load
$page = $_GET['page'];
$endpoint = $this->router->lookup($page);
if($endpoint === false)
{
//header("HTTP/1.0 404 Not Found");
echo "Insert 404 Page here.";
} else
{
$this->$endpoint($queryParams);
}
}
private function redirect($url){
header("Location: /" . $url);
}
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");
}
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 access 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"),
"3" => new Flash("A Ribbit cannot be blank.", "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 if(strlen($text) == 0)
{
$this->redirect("buddies/3");
} 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");
}
}
}
?>