-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
172 lines (153 loc) · 4.5 KB
/
Copy pathindex.php
File metadata and controls
172 lines (153 loc) · 4.5 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
<?php
/**
* Step 1: Require the Slim Framework
*
* If you are not using Composer, you need to require the
* Slim Framework and register its PSR-0 autoloader.
*
* If you are using Composer, you can skip this step.
*/
require 'Slim/Slim.php';
require 'wims/conf/database.php';
require 'wims/interface/persistable.interface.php';
require 'wims/models/model.php';
require 'wims/models/User.php';
\Slim\Slim::registerAutoloader();
/**
* Step 2: Instantiate a Slim application
*
* This example instantiates a Slim application using
* its default settings. However, you will usually configure
* your Slim application now by passing an associative array
* of setting names and values into the application constructor.
*/
$app = new \Slim\Slim();
$app->config('db_connection', $db_connection);
/**
* Step 3: Define the Slim application routes
*
* Here we define several Slim application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete`
* is an anonymous function.
*/
// GET route
$app->get('/user/check/:username', function($username) use($app, $db_connection) {
$params = Array('username' => $username);
$user = new User($params);
$user->cursor($db_connection);
$res = $user->exist();
$response = $app->response();
if ($res)
$response->status(200);
else
$response->status(400);
$response['Content-Type'] = 'application/json';
$response->body(json_encode($res));
});
$app->get('/user/authenticate/:username/:password', function($username, $password) use($app, $db_connection) {
$params = Array('username' => $username, 'password' => $password);
$user = new User($params);
$user->cursor($db_connection);
$res = $user->authenticate();
$response = $app->response();
$data = [];
if ($res != false) {
$response->status(200);
$data['authenticate'] = true;
$data['token'] = $res;
}
else {
$response->status(400);
$data['authenticate'] = false;
$data['token'] = false;
}
$response['Content-Type'] = 'application/json';
$response->body(json_encode($data));
});
$app->get('/user/register/:username/:password', function($username, $password) use($app, $db_connection) {
$params = Array('username' => $username, 'password' => $password);
$user = new User($params);
$user->cursor($db_connection);
$res = $user->register();
$response = $app->response();
$data = [];
if ($res != false) {
$response->status(200);
$data['authenticate'] = true;
$data['token'] = $res;
}
else {
$response->status(400);
$data['authenticate'] = false;
$data['token'] = false;
}
$response['Content-Type'] = 'application/json';
$response->body(json_encode($data));
});
// POST route
$app->post('/login', function () use ($app, $db_connection) {
$params = array(
'username' => $app->request()->post('username'),
'password' => $app->request()->post('password')
);
$user = new User($params);
$user->cursor($db_connection);
$userInfo = $user->authenticate();
$response = $app->response();
$data = [];
if ($userInfo != false) {
$response->status(200);
$data['authenticate'] = true;
$data['user'] = $userInfo;
$data['message'] = "Success";
}
else {
$response->status(200);
$data['authenticate'] = false;
$data['user'] = false;
$data['message'] = "Invalid credentials. Please try again";
}
$response['Content-Type'] = 'application/json';
$response->body(json_encode($data));
});
$app->post('/register', function () use ($app, $db_connection) {
$params = array(
'username' => $app->request()->post('username'),
'password' => $app->request()->post('password'),
'email' => $app->request()->post('email'),
);
$user = new User($params);
$user->cursor($db_connection);
$res = $user->register();
$response = $app->response();
$data = [];
$response->status(200);
if ($res != false) {
$data['authenticate'] = true;
$data['user'] = $res;
$data['message'] = "Your account has been created";
}
else {
$data['authenticate'] = false;
$data['token'] = false;
$data['message'] = "Something wrong has happened. Please try again.";
}
$response['Content-Type'] = 'application/json';
$response->body(json_encode($data));
});
// PUT route
$app->put('/put', function () {
echo 'This is a PUT route';
});
// DELETE route
$app->delete('/delete', function () {
echo 'This is a DELETE route';
});
/**
* Step 4: Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();