-
Notifications
You must be signed in to change notification settings - Fork 1
/
contacts.php
executable file
·200 lines (169 loc) · 6.88 KB
/
contacts.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
<?php
// from https://gist.github.com/shiyaz/5990220
// z autentyfikacja
require 'Slim/Slim.php';
require 'RedBean/rb.php';
\Slim\Slim::registerAutoloader();
class ResourceNotFoundException extends Exception { }
class AuthenticateFailedException extends Exception { }
$db_host = 'localhost';
$db_name = 'cmgr';
$db_user = 'root';
$db_password = '';
R::setup("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
R::freeze(true);
$app = new \Slim\Slim(array(
'debug' => true,
'mode' => 'development',
'cookies.secret_key' => 'o_O o_o O_o',
'cookies.lifetime' => '30 minutes'
));
// Globally ensure if `id` is used as a route parameter, it is numeric,
// so handlers do not have to check an `id` parameter before mapping it
// to a numeric `id` field on the Contacts database table.
\Slim\Route::setDefaultConditions(array(
'id' => '[0-9]{1,}',
));
// stubbed for demo
function isValidLogin($username, $password)
{
// return true;
return ($username == 'demo' && $password == 'password');
}
$checkLoggedOn = function ($app) {
return function () use ($app) {
if (!isValidLogin($app->getEncryptedCookie('username'),
$app->getEncryptedCookie('password'))
) {
$app->halt(401); // Unauthorized access
}
};
};
$app->post('/login', function () use ($app) {
try {
$username = $app->request()->post('username');
$password = $app->request()->post('password');
if (isValidLogin($username, $password)) {
$app->setEncryptedCookie('username', $username, '1 day');
$app->setEncryptedCookie('password', $password, '1 day');
$app->response()->header('Content-Type', 'application/json');
$app->response()->status(200); // OK
echo json_encode(array('operation' => 'login', 'status' => 'ok'));
} else {
throw new AuthenticateFailedException();
}
} catch (AuthenticateFailedException $e) {
$app->response()->status(401);
$app->response()->header('X-Status-Reason', 'Login failure');
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->get('/logout', function () use ($app) {
try {
$app->deleteCookie('username');
$app->deleteCookie('password');
$app->response()->header('Content-Type', 'application/json');
$app->response()->status(200); // OK
echo json_encode(array('operation' => 'logout', 'status' => 'ok'));
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
// API for CRUD operations on Contacts
$app->get('/contacts', $checkLoggedOn($app), function () use ($app) {
try {
$contacts = R::find('contacts');
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($contacts));
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->get('/contacts/:id', $checkLoggedOn($app), function ($id) use ($app) {
try {
$contact = R::findOne('contacts', 'id=?', array($id));
if ($contact) {
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($contact));
} else {
throw new ResourceNotFoundException();
}
} catch (ResourceNotFoundException $e) {
$app->response()->status(404);
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->put('/contacts/:id', $checkLoggedOn($app), function ($id) use ($app) {
try {
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
$contact = R::findOne('contacts', 'id=?', array($id));
// only the fields passed in the request body are updated
if ($contact) {
$contact->title = isset($input->title) ? (string)$input->title : $contact->title;
$contact->forename = isset($input->forename) ? (string)$input->forename : $contact->title;
$contact->surname = isset($input->surname) ? (string)$input->surname : $contact->surname;
$contact->company = isset($input->company) ? (string)$input->company : $contact->company;
$contact->email = isset($input->email) ? (string)$input->email : $contact->email;
$contact->phone = isset($input->phone) ? (string)$input->phone : $contact->phone;
$contact->address = isset($input->address) ? (string)$input->address : $contact->address;
R::store($contact);
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($contact));
} else {
throw new ResourceNotFoundException();
}
} catch (ResourceNotFoundException $e) {
$app->response()->status(404);
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->post('/contacts', $checkLoggedOn($app), function () use ($app) {
try {
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
$contact = R::dispense('contacts');
$contact->title = (string)$input->title;
$contact->forename = (string)$input->forename;
$contact->surname = (string)$input->surname;
$contact->company = (string)$input->company;
$contact->email = (string)$input->email;
$contact->phone = (string)$input->phone;
$contact->address = (string)$input->address;
$id = R::store($contact);
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($contact));
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->delete('/contacts/:id', $checkLoggedOn($app), function ($id) use ($app) {
try {
$request = $app->request();
$contact = R::findOne('contacts', 'id=?', array($id));
if ($contact) {
R::trash($contact);
$app->response()->status(204);
} else {
throw new ResourceNotFoundException();
}
} catch (ResourceNotFoundException $e) {
$app->response()->status(404);
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->run();
?>