|
1
|
|
|
<?php |
|
2
|
|
|
class ProfilesAPI extends Http\Rest\RestAPI |
|
3
|
|
|
{ |
|
4
|
|
|
public function setup($app) |
|
5
|
|
|
{ |
|
6
|
|
|
$app->post('/login[/]', array($this, 'login')); |
|
7
|
|
|
$app->post('/logout[/]', array($this, 'logout')); |
|
8
|
|
|
$app->post('/zip[/]', array($this, 'validateZip')); |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
public function login($request, $response, $args) |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
$params = $request->getParams(); |
|
14
|
|
|
if(!isset($params['username']) || !isset($params['password'])) |
|
15
|
|
|
{ |
|
16
|
|
|
return $response->withStatus(400); |
|
17
|
|
|
} |
|
18
|
|
|
$auth = AuthProvider::getInstance(); |
|
19
|
|
|
$res = $auth->login($params['username'], $params['password']); |
|
20
|
|
|
if($res === false) |
|
21
|
|
|
{ |
|
22
|
|
|
return $response->withStatus(403); |
|
23
|
|
|
} |
|
24
|
|
|
else |
|
25
|
|
|
{ |
|
26
|
|
|
return $response->withJson($res); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function logout($request, $response, $args) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
FlipSession::end(); |
|
33
|
|
|
return $response->withJson(true); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function validateZip($request, $response, $args) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$obj = $request->getQueryParams(); |
|
39
|
|
|
if(empty($obj)) |
|
40
|
|
|
{ |
|
41
|
|
|
$obj = (array)$request->getParsedBody(); |
|
42
|
|
|
} |
|
43
|
|
|
$ret = false; |
|
|
|
|
|
|
44
|
|
|
if($obj['c'] == 'US') |
|
45
|
|
|
{ |
|
46
|
|
|
if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $obj['postalCode'])) |
|
47
|
|
|
{ |
|
48
|
|
|
$contents = file_get_contents('http://ziptasticapi.com/'.$obj['postalCode']); |
|
49
|
|
|
$resp = json_decode($contents); |
|
50
|
|
|
if(isset($resp->error)) |
|
51
|
|
|
{ |
|
52
|
|
|
$ret = $resp->error; |
|
53
|
|
|
} |
|
54
|
|
|
else |
|
55
|
|
|
{ |
|
56
|
|
|
$ret = true; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
else |
|
60
|
|
|
{ |
|
61
|
|
|
$ret = 'Invalid Zip Code!'; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
else |
|
65
|
|
|
{ |
|
66
|
|
|
$ret = true; |
|
67
|
|
|
} |
|
68
|
|
|
return $request->withJson($ret); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
|
72
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.