Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 8c26211

Browse files
author
Samuel Janda
committed
Minor fixes.
1 parent 857fa26 commit 8c26211

12 files changed

+207
-99
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
.htaccess
2-
model/.htaccess

.vscode/launch.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Listen for XDebug",
9+
"type": "php",
10+
"request": "launch",
11+
"port": 9000
12+
},
13+
{
14+
"name": "Launch currently open script",
15+
"type": "php",
16+
"request": "launch",
17+
"program": "${file}",
18+
"cwd": "${fileDirname}",
19+
"port": 9000
20+
}
21+
]
22+
}

controller/account.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
exit();
3737
}
3838

39-
$$raw_post_data = file_get_contents('php://input');
39+
$raw_post_data = file_get_contents('php://input');
4040

4141
if (!$json_data = json_decode($raw_post_data)) {
4242
$response = new Response();
@@ -47,12 +47,13 @@
4747
exit();
4848
}
4949

50-
if (!isset($json_data->businessName, $json_data->authContact, $json_data->phone, $json_data->streetAddress, $json_data->suburb, $json_data->state, $json_data->postcode, $json_data->password)) {
50+
if (!isset($json_data->businessName, $json_data->authContact, $json_data->email, $json_data->phone, $json_data->streetAddress, $json_data->suburb, $json_data->state, $json_data->postcode, $json_data->password)) {
5151
$response = new Response();
5252
$response->setHttpStatusCode(400);
5353
$response->setSuccess(false);
5454
(!isset($json_data->businessName) ? $response->addMessage("Error: request body does not contain a business name.") : false);
5555
(!isset($json_data->authContact) ? $response->addMessage("Error: request body does not contain an authorised contact.") : false);
56+
(!isset($json_data->email) ? $response->addMessage("Error: request body does not contain an email address.") : false);
5657
(!isset($json_data->phone) ? $response->addMessage("Error: request body does not contain a contact phone number.") : false);
5758
(!isset($json_data->streetAddress) ? $response->addMessage("Error: request body does not contain a street address.") : false);
5859
(!isset($json_data->suburb) ? $response->addMessage("Error: request body does not contain a suburb name.") : false);
@@ -74,7 +75,7 @@
7475
$location->address()->setSuburb(trim($json_data->suburb));
7576
$location->address()->setState(trim($json_data->state));
7677
$location->address()->setPostCode(trim($json_data->postcode));
77-
if (isset($json_data->email)) $location->setEmailAddress(trim($json_data->email));
78+
$location->setEmailAddress(trim($json_data->email));
7879
if (isset($json_data->abn)) $location->setABN(trim($json_data->abn));
7980

8081
$query_email = $location->getEmailAddress();
@@ -141,7 +142,7 @@
141142
$response->setHttpStatusCode(201);
142143
$response->setSuccess(true);
143144
$response->addMessage("Account successfully created.");
144-
$response->setData($responseData);
145+
$response->setData($response_data);
145146
$response->send();
146147
exit();
147148

@@ -159,7 +160,7 @@
159160
$response = new Response();
160161
$response->setHttpStatusCode(400);
161162
$response->setSuccess(false);
162-
$response->addMessage("Error: " . $e->getMessage());
163+
$response->addMessage("API Error: " . $e->getMessage());
163164
$response->send();
164165
exit();
165166
}

controller/authenticate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
exit();
5151
}
5252

53-
if ($_isActive !== 1) {
53+
if (!$_isActive) {
5454
$response = new Response();
5555
$response->setHttpStatusCode(401);
5656
$response->setSuccess(false);

controller/entryexit.php

+97-56
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,117 @@
2020

2121
include('authenticate.php');
2222

23-
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
24-
$response = new Response();
25-
$response->setHttpStatusCode(405);
26-
$response->setSuccess(false);
27-
$response->addMessage('Server request method not allowed.');
28-
$response->send();
29-
exit();
30-
}
23+
try {
3124

32-
if ($_SERVER['CONTENT_TYPE'] !== 'application/json') {
33-
$response = new Response();
34-
$response->setHttpStatusCode(400);
35-
$response->setSuccess(false);
36-
$response->addMessage("Error: content type header not set to JSON.");
37-
$response->send();
38-
exit();
39-
}
25+
if ($_SERVER['REQUEST_METHOD'] === 'POST' && array_key_exists('l', $_GET)) { //ENTRY
4026

41-
$raw_post_data = file_get_contents('php://input');
27+
if ($_SERVER['CONTENT_TYPE'] !== 'application/json') {
28+
$response = new Response();
29+
$response->setHttpStatusCode(400);
30+
$response->setSuccess(false);
31+
$response->addMessage("Error: content type header not set to JSON.");
32+
$response->send();
33+
exit();
34+
}
4235

43-
if (!$json_data = json_decode($raw_post_data)) {
44-
$response = new Response();
45-
$response->setHttpStatusCode(400);
46-
$response->setSuccess(false);
47-
$response->addMessage("Error: request body is not valid JSON.");
48-
$response->send();
49-
exit();
50-
}
36+
$raw_post_data = file_get_contents('php://input');
5137

52-
if (!isset($jsonData->name, $jsonData->phone)) {
53-
$response = new Response();
54-
$response->setHttpStatusCode(400);
55-
$response->setSuccess(false);
56-
(!isset($jsonData->name) ? $response->addMessage("Error: request body does not contain a visitor name.") : false);
57-
(!isset($jsonData->phone) ? $response->addMessage("Error: request body does not contain a phone number.") : false);
58-
$response->send();
59-
exit();
60-
}
38+
if (!$json_data = json_decode($raw_post_data)) {
39+
$response = new Response();
40+
$response->setHttpStatusCode(400);
41+
$response->setSuccess(false);
42+
$response->addMessage("Error: request body is not valid JSON.");
43+
$response->send();
44+
exit();
45+
}
46+
47+
if (!isset($json_data->name, $json_data->phone)) {
48+
$response = new Response();
49+
$response->setHttpStatusCode(400);
50+
$response->setSuccess(false);
51+
(!isset($json_data->name) ? $response->addMessage("Error: request body does not contain a visitor name.") : false);
52+
(!isset($json_data->phone) ? $response->addMessage("Error: request body does not contain a phone number.") : false);
53+
$response->send();
54+
exit();
55+
}
6156

62-
try {
57+
$visitor = new Visitor();
58+
$visitor->setName(trim($json_data->name));
59+
$visitor->setPhoneNumber(trim($json_data->phone));
6360

64-
$visitor = new Visitor();
65-
$visitor->setName(trim($json_data->name));
66-
$visitor->setPhoneNumber(trim($json_data->phone));
61+
$query_name = $visitor->getName();
62+
$query_phone = $visitor->getPhoneNumber();
63+
$query_account_id = intval($_GET['l']);
64+
$query = $writeDB->prepare("INSERT INTO `contacts`(`account_id`,`name`, `phone`) VALUES (:a, :n, :p)");
65+
$query->bindParam(':a', $query_account_id, PDO::PARAM_INT);
66+
$query->bindParam(':n', $query_name, PDO::PARAM_STR);
67+
$query->bindParam(':p', $query_phone, PDO::PARAM_STR);
68+
$query->execute();
6769

68-
$query_name = $visitor->getName();
69-
$query_phone = $visitor->getPhoneNumber();
70-
$query_arr = new DateTime();
71-
$query = $writeDB->prepare("INSERT INTO `contacts`(`name`, `phone`) VALUES (:n, :p)");
72-
$query->bindParam(':n', $query_name, PDO::PARAM_STR);
73-
$query->bindParam(':p', $query_phone, PDO::PARAM_STR);
74-
$query->execute();
70+
$row_count = $query->rowCount();
71+
if ($rowCount === 0) {
72+
$response = new Response();
73+
$response->setHttpStatusCode(409);
74+
$response->setSuccess(false);
75+
$response->addMessage("Error: New arrival not added.");
76+
$response->send();
77+
exit();
78+
}
7579

76-
$row_count = $query->rowCount();
77-
if ($rowCount === 0) {
7880
$response = new Response();
79-
$response->setHttpStatusCode(409);
81+
$response->setHttpStatusCode(201);
82+
$response->setSuccess(true);
83+
$response->addMessage("Visitor successfully checked in.");
84+
$response->send();
85+
exit();
86+
87+
} elseif ($_SERVER['REQUEST_METHOD'] === 'PATCH' && array_key_exists('v', $_GET)) { //EXIT
88+
89+
$query_id = intval($_GET['v']);
90+
$query = $writeDB->prepare("SELECT * FROM `contacts` WHERE id=:id");
91+
$query->bindParam(':id', $query_id, PDO::PARAM_INT);
92+
$query->execute();
93+
94+
$row_count = $query->rowCount();
95+
if ($rowCount === 0) {
96+
$response = new Response();
97+
$response->setHttpStatusCode(409);
98+
$response->setSuccess(false);
99+
$response->addMessage("Error: Arrival entry not found.");
100+
$response->send();
101+
exit();
102+
}
103+
104+
$query = $writeDB->prepare("UPDATE `contacts` SET `dep`=CURRENT_TIMESTAMP() WHERE id=:id");
105+
$query->bindParam(':id', $query_id, PDO::PARAM_INT);
106+
$query->execute();
107+
108+
$row_count = $query->rowCount();
109+
if ($rowCount === 0) {
110+
$response = new Response();
111+
$response->setHttpStatusCode(409);
112+
$response->setSuccess(false);
113+
$response->addMessage("Error: New arrival not added.");
114+
$response->send();
115+
exit();
116+
}
117+
118+
$response = new Response();
119+
$response->setHttpStatusCode(201);
120+
$response->setSuccess(true);
121+
$response->addMessage("Visitor successfully checked out.");
122+
$response->send();
123+
exit();
124+
125+
} else {
126+
$response = new Response();
127+
$response->setHttpStatusCode(405);
80128
$response->setSuccess(false);
81-
$response->addMessage("Error: New arrival not added.");
129+
$response->addMessage('Server request method not allowed.');
82130
$response->send();
83131
exit();
84132
}
85133

86-
$response = new Response();
87-
$response->setHttpStatusCode(201);
88-
$response->setSuccess(true);
89-
$response->addMessage("Visitor successfully checked in.");
90-
$response->send();
91-
exit();
92-
93134
} catch (PDOException $e) {
94135
error_log("Exception: " . $e->getMessage());
95136
$response = new Response();

controller/sessions.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require_once './db.php';
3+
require_once '../model/DB.php';
44
require_once '../model/Response.php';
55

66
try{
@@ -134,12 +134,12 @@
134134
$_accountID = $row->accountID;
135135
$_accessToken = $row->access_token;
136136
$_refreshToken = $row->refresh_token;
137-
$_isActive = $row->is_active;
137+
$_is_active = $row->is_active;
138138
$_loginAttempts = $row->login_attempts;
139139
$_accessExpiry = $row->access_token_expiry;
140140
$_refreshExpiry = $row->refresh_token_expiry;
141141

142-
if (!$_isActive === 'Y') {
142+
if ($_is_active !== 1) {
143143
$response = new Response();
144144
$response->setHttpStatusCode(401);
145145
$response->setSuccess(false);
@@ -168,7 +168,7 @@
168168

169169
$accessToken = base64_encode(bin2hex(openssl_random_pseudo_bytes(24)).time());
170170
$refreshToken = base64_encode(bin2hex(openssl_random_pseudo_bytes(24)).time());
171-
$accessExpiry = 1200;
171+
$accessExpiry = 16*60*60;
172172
$refreshExpiry = 28*24*60*60;
173173

174174
$query= $writeDB->prepare("UPDATE `sessions` SET
@@ -306,12 +306,11 @@
306306
}
307307

308308
$row = $query->fetch(PDO::FETCH_OBJ);
309-
$id = $row->id;
310-
$accountID = $row->account_id;
311-
$name = $row->name;
309+
$account_id = $row->id;
310+
$name = $row->business_name;
312311
$email = $row->email;
313312
$dbPassword = $row->auth;
314-
$isActive = $row->is_active;
313+
$is_active = $row->is_active;
315314
$loginAttempts = $row->login_attempts;
316315

317316
if ($loginAttempts > 2) {
@@ -325,7 +324,7 @@
325324

326325
if (!password_verify($password, $dbPassword)) {
327326
$query = $writeDB->prepare("UPDATE `accounts` SET `login_attempts`=`login_attempts`+1 WHERE id=:id");
328-
$query->bindParam(":id", $id, PDO::PARAM_INT);
327+
$query->bindParam(":id", $account_id, PDO::PARAM_INT);
329328
$query->execute();
330329

331330
$response = new Response();
@@ -336,7 +335,7 @@
336335
exit();
337336
}
338337

339-
if ($isActive !== 'Y') {
338+
if (!$is_active) {
340339
$response = new Response();
341340
$response->setHttpStatusCode(401);
342341
$response->setSuccess(false);
@@ -362,13 +361,13 @@
362361

363362
$writeDB->beginTransaction();
364363
$query = $writeDB->prepare("UPDATE `accounts` SET `login_attempts`=0 WHERE id=:id");
365-
$query->bindParam(":id", $id, PDO::PARAM_INT);
364+
$query->bindParam(":id", $account_id, PDO::PARAM_INT);
366365
$query->execute();
367366

368367
$query = $writeDB->prepare("INSERT INTO `sessions`
369368
(`account_id`, `access_token`, `access_token_expiry`, `refresh_token`, `refresh_token_expiry`)
370369
VALUES (:accountID, :accessToken, DATE_ADD(NOW(), INTERVAL :accessExpiry SECOND), :refreshToken, DATE_ADD(NOW(), INTERVAL :refreshExpiry SECOND))");
371-
$query->bindParam(":accountID", $id, PDO::PARAM_INT);
370+
$query->bindParam(":accountID", $account_id, PDO::PARAM_INT);
372371
$query->bindParam(":accessToken", $accessToken, PDO::PARAM_STR);
373372
$query->bindParam(":accessExpiry", $accessExpiry, PDO::PARAM_INT);
374373
$query->bindParam(":refreshToken", $refreshToken, PDO::PARAM_STR);

model/Address.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
<?php
22

3-
require_once './Config.php';
3+
require_once '../model/Config.php';
44

55
class Address {
6-
private string $_street_address;
7-
private string $_suburb;
8-
private string $_state;
9-
private string $_postcode;
6+
/**
7+
* @var string
8+
*/
9+
private $_street_address;
10+
/**
11+
* @var string
12+
*/
13+
private $_suburb;
14+
/**
15+
* @var string
16+
*/
17+
private $_state;
18+
/**
19+
* @var string
20+
*/
21+
private $_postcode;
1022

1123
public function __toString():string {
1224
return "{$this->_street_address}\r\n{$this->_suburb} {$this->_state} {$this->_postcode}";

model/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Config {
99
* @return bool Returns `true` if the phone number entered is a valid Australian number; false upon failure.
1010
*/
1111
public static function ValidatePhoneNumber(string $pn):bool {
12-
if (strlen($pn !== 12)) return false; //Phone number length confirmed
12+
if (strlen($pn) !== 12) return false; //Phone number length confirmed
1313
if (substr($pn, 0, 3) !== "+61") return false; //Australian telephone number confirmed
1414
$vb = intval(substr($pn, 4, 2)); //substring to be evaluated against valid number ranges
1515
if (substr($pn, 3, 1) === "2" && ($vb < 37 && $vb !== 33)) return false; //NSW/ACT Number confirmed

0 commit comments

Comments
 (0)