Skip to content

Commit 7b627bf

Browse files
committed
Started analysis.js refactoring
1 parent 2acb767 commit 7b627bf

20 files changed

+511
-372
lines changed

private/lib/getPostVar.php private/lib/getVariable.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ function getPostVar($varName) {
66
if (!isset($_POST[$varName])) respond(false, "No $varName value received.");
77
return $_POST[$varName];
88
}
9-
9+
10+
function getSessionVar($key) {
11+
if (!isset($_SESSION[$key])) respond(false, "No '$key' session variable.");
12+
return $_SESSION[$key];
13+
}
14+
1015
?>

private/lib/login.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
session_start();
4+
35
require_once('respond.php');
46

57
function login($username, $accountType) {
68
$_SESSION["username"] = $username;
79
$_SESSION["accountType"] = $accountType;
810
respond(true, $accountType);
911
}
10-
12+
1113
?>

private/lib/logout.php

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<?php
2+
3+
if (session_id() == "") session_start();
4+
5+
require_once("../lib/respond.php");
6+
27
session_unset();
38
session_destroy();
9+
10+
respond(true, "");
11+
412
?>

private/lib/setHeaders.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
header('Set-Cookie: cross-site-cookie=name; SameSite=Strict; Secure');
3+
?>

private/login/login.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<?php
22

3-
session_start();
4-
53
error_reporting(E_ALL);
64
ini_set('display_errors', 1);
75

6+
require_once("../lib/setHeaders.php");
87
require_once("../lib/connectDB.php");
9-
require_once("../lib/getPostVar.php");
8+
require_once("../lib/getVariable.php");
109
require_once("../lib/unboundQuery.php");
1110
require_once("../lib/respond.php");
1211
require_once("../lib/login.php");
1312

14-
function getAccountTypeResult($conn, $username, $password) {
15-
$sql = "SELECT accountType FROM Accounts WHERE username='$username' AND password='$password'";
13+
function getAccount($conn, $username) {
14+
$sql = "SELECT password, accountType FROM Accounts WHERE username='$username'";
1615
return getQueryResult($conn, $sql);
1716
}
1817

@@ -21,11 +20,15 @@ function getAccountTypeResult($conn, $username, $password) {
2120
$username = getPostVar("username");
2221
$password = getPostVar("password");
2322

24-
$accountTypeRows = getAccountTypeResult($conn, $username, $password);
23+
$accountRows = getAccount($conn, $username);
24+
25+
if (mysqli_num_rows($accountRows) === 0) respond(false, 'Username was not recognised.');
26+
27+
$account = $accountRows->fetch_assoc();
2528

26-
if (mysqli_num_rows($accountTypeRows) === 0) respond(false, 'Account details were not recognised.');
29+
if (!password_verify($password, "" . $account['password'])) respond(false, 'Password was not recognised.');
2730

28-
switch ($accountTypeRows->fetch_assoc()['accountType']) {
31+
switch ($account['accountType']) {
2932
case "reader":
3033
login($username, "reader");
3134
break;

private/reader/getUnreadTextString.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
error_reporting(E_ALL);
66
ini_set('display_errors', 1);
77

8+
require_once("../lib/setHeaders.php");
89
require_once("../lib/connectDB.php");
910
require_once("../lib/unboundQuery.php");
1011
require_once("../lib/respond.php");
@@ -42,7 +43,7 @@ function getUnreadTextString($conn, $reader) {
4243

4344
$conn = connectDB();
4445

45-
$reader = $_SESSION['username'];
46+
$reader = getSessionVar('username');
4647

4748
$textString = getUnreadTextString($conn, $reader);
4849

private/reader/uploadReadingData.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
error_reporting(E_ALL);
66
ini_set('display_errors', 1);
7-
7+
8+
require_once("../lib/setHeaders.php");
89
require_once("../lib/connectDB.php");
9-
require_once("../lib/getPostVar.php");
10+
require_once("../lib/getVariable.php");
1011
require_once("../lib/boundQuery.php");
1112
require_once("../lib/respond.php");
1213

13-
function createReadingEntry($conn, $title, $version, $reader, $availWidth, $availHeight) {
14+
function createReadingEntry($conn, $title, $version, $reader, $wpm, $innerWidth, $innerHeight) {
1415
// Make a bound query for a single insert into the Readings table
15-
$sql = "INSERT INTO Readings (title, version, reader, availWidth, availHeight) VALUES (?, ?, ?, ?, ?)";
16-
$typeString = "sssii";
17-
$valueArray = array(&$title, &$version, &$reader, &$availWidth, &$availHeight);
16+
$sql = "INSERT INTO Readings (title, version, reader, wpm, innerWidth, innerHeight) VALUES (?, ?, ?, ?, ?, ?)";
17+
$typeString = "sssiii";
18+
$valueArray = array(&$title, &$version, &$reader, &$wpm, &$innerWidth, &$innerHeight);
1819
makeBoundQuery($conn, $sql, $typeString, $valueArray);
1920
}
2021

@@ -40,16 +41,17 @@ function createLogEntry($conn, $title, $version, $reader, $log) {
4041

4142
$conn = connectDB();
4243

43-
if (!$title = $_SESSION["title"]) respond(false, "No 'title' session variable.");
44-
if (!$version = $_SESSION["version"]) respond(false, "No 'version' session variable.");
45-
$reader = $_SESSION['username'];
46-
$availWidth = getPostVar("availWidth");
47-
$availHeight = getPostVar("availHeight");
44+
$title = getSessionVar("title");
45+
$version = getSessionVar("version");
46+
$username = getSessionVar("username");
47+
$wpm = getPostVar("wpm");
48+
$innerWidth = getPostVar("innerWidth");
49+
$innerHeight = getPostVar("innerHeight");
4850
$log = json_decode(getPostVar("log"), true);
4951
// Group all queries into a single transaction
5052
if (!$conn->autocommit(false)) respond(false, "Failed to start transaction: $conn->error");
5153

52-
createReadingEntry($conn, $title, $version, $reader, $availWidth, $availHeight);
54+
createReadingEntry($conn, $title, $version, $reader, $wpm, $availWidth, $availHeight);
5355
createLogEntry($conn, $title, $version, $reader, $log);
5456

5557
if (!$conn->commit()) respond(false, "Commit failed: $conn->error");

private/register/register.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
session_start();
44

5+
require_once("../lib/setHeaders.php");
56
require_once("../lib/connectDB.php");
6-
require_once("../lib/getPostVar.php");
7+
require_once("../lib/getVariable.php");
78
require_once("../lib/boundQuery.php");
89
require_once("../lib/respond.php");
910
require_once("../lib/login.php");
@@ -17,16 +18,18 @@ function isTaken($conn, $username) {
1718
}
1819

1920
function createReaderAccount($conn, $username, $password, $dob, $gender, $isImpaired) {
21+
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
2022
$sql = "INSERT INTO Readers (username, password, dob, gender, isImpaired) VALUES (?, ?, ?, ?, ?)";
2123
$typeString = "ssssi";
22-
$valueArray = array(&$username, &$password, &$dob, &$gender, &$isImpaired);
24+
$valueArray = array(&$username, &$passwordHash, &$dob, &$gender, &$isImpaired);
2325
makeBoundQuery($conn, $sql, $typeString, $valueArray);
2426
}
2527

2628
function createResearcherAccount($conn, $username, $password) {
29+
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
2730
$sql = "INSERT INTO Researchers (username, password) VALUES (?, ?)";
2831
$typeString = "ss";
29-
$valueArray = array(&$username, &$password);
32+
$valueArray = array(&$username, &$passwordHash);
3033
makeBoundQuery($conn, $sql, $typeString, $valueArray);
3134
}
3235

private/researcher/getAvailableTexts.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
error_reporting(E_ALL);
66
ini_set('display_errors', 1);
77

8+
require_once("../lib/setHeaders.php");
89
require_once("../lib/connectDB.php");
10+
require_once("../lib/getVariable.php");
911
require_once("../lib/unboundQuery.php");
1012
require_once("../lib/respond.php");
1113

@@ -42,7 +44,7 @@ function getAvailableTexts($conn, $username) {
4244

4345
$conn = connectDB();
4446

45-
$username = $_SESSION["username"];
47+
$username = getSessionVar("username");
4648
$texts = getAvailableTexts($conn, $username);
4749

4850
respond(true, json_encode($texts));

private/researcher/getReaders.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,35 @@
33
error_reporting(E_ALL);
44
ini_set('display_errors', 1);
55

6+
require_once("../lib/setHeaders.php");
67
require_once("../lib/connectDB.php");
7-
require_once("../lib/getPostVar.php");
8+
require_once("../lib/getVariable.php");
89
require_once("../lib/unboundQuery.php");
910
require_once("../lib/respond.php");
1011

11-
function getReaders($conn, $title, $version) {
12-
// Retrieve the usernames of all reader accounts that have been assigned the given text
12+
function getReadings($conn, $title, $version) {
13+
// Retrieve data on all reader accounts that have been assigned the given text
1314
$sql = "
14-
SELECT DISTINCT username, dob, gender, isImpaired
15+
SELECT DISTINCT SHA2(username, 224) AS usernameHash, dob, gender, isImpaired, wpm, readDate
1516
FROM Readers
16-
INNER JOIN Windows ON username = reader
17-
WHERE title='$title' AND version='$version'
17+
INNER JOIN Readings ON reader = username
18+
WHERE title = '$title' AND version = '$version'
1819
";
1920
$readerRows = getQueryResult($conn, $sql);
2021
// Process the result object
2122
$readerArray = array();
22-
$curDate = date_create();
2323
while ($readerRow = $readerRows->fetch_assoc()) {
24-
$reader = array(
25-
"username" => $readerRow["username"],
26-
"age" => (int) date_interval_format(date_diff($curDate, date_create($readerRow["dob"])), "%y"),
24+
// Calculate the reader's age at the time of reading
25+
$readDate = date_create($readerRow["readDate"]);
26+
$birthDate = date_create($readerRow["dob"]);
27+
$age = date_diff($readDate, $birthDate);
28+
// Record data
29+
$readerArray[$readerRow["usernameHash"]] = array(
30+
"wpm" => $readerRow["wpm"],
31+
"age" => (int) date_interval_format($age, "%y"), // Get the year part without leading/trailing zeroes
2732
"gender" => $readerRow["gender"],
28-
"isImpaired" => (bool) $readerRow["isImpaired"],
33+
"isImpaired" => (bool) $readerRow["isImpaired"]
2934
);
30-
array_push($readerArray, $reader);
3135
}
3236
// Return the result array
3337
return $readerArray;

private/researcher/getTextString.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
error_reporting(E_ALL);
44
ini_set('display_errors', 1);
55

6+
require_once("../lib/setHeaders.php");
67
require_once("../lib/connectDB.php");
7-
require_once("../lib/getPostVar.php");
8+
require_once("../lib/getVariable.php");
89
require_once("../lib/getTextString.php");
910
require_once("../lib/respond.php");
1011

private/researcher/getWindows.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
error_reporting(E_ALL);
44
ini_set('display_errors', 1);
55

6+
require_once("../lib/setHeaders.php");
67
require_once("../lib/connectDB.php");
7-
require_once("../lib/getPostVar.php");
8+
require_once("../lib/getVariable.php");
89
require_once("../lib/unboundQuery.php");
910
require_once("../lib/respond.php");
1011

11-
function getWindows($conn, $title, $version, $reader) {
12+
function getWindows($conn, $title, $version, $readerHash) {
1213
// Get all measurements from the given reading session
1314
$sql = "
1415
SELECT focalChar, leftmostChar, rightmostChar, duration
1516
FROM Windows
16-
WHERE title='$title' AND version='$version' AND reader='$reader'
17+
WHERE title = '$title' AND version = '$version' AND SHA2(reader, 224) = '$readerHash'
1718
ORDER BY sequenceNumber
1819
";
1920
$dataRows = getQueryResult($conn, $sql);
@@ -33,9 +34,9 @@ function getWindows($conn, $title, $version, $reader) {
3334

3435
$title = getPostVar("title");
3536
$version = getPostVar("version");
36-
$reader = getPostVar("reader");
37+
$readerHash = getPostVar("readerHash");
3738

38-
$windows = getWindows($conn, $title, $version, $reader);
39+
$windows = getWindows($conn, $title, $version, $readerHash);
3940

4041
respond(true, json_encode($windows));
4142

private/researcher/uploadText.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
session_start();
44

5+
require_once("../lib/setHeaders.php");
56
require_once("../lib/connectDB.php");
6-
require_once("../lib/getPostVar.php");
7+
require_once("../lib/getVariable.php");
78
require_once("../lib/boundQuery.php");
89
require_once("../lib/respond.php");
910

@@ -56,6 +57,7 @@ function createCharactersEntry($conn, $title, $version, $text) {
5657
// Connect to the database
5758
$conn = connectDB();
5859
// Retrieve mandatory arguments
60+
$username = getSessionVar("username");
5961
$isNew = getPostVar('isNew');
6062
$text = getPostVar('text');
6163
$title = getPostVar('title');
@@ -69,7 +71,7 @@ function createCharactersEntry($conn, $title, $version, $text) {
6971
if (!$conn->autocommit(false)) respond(false, "Failed to start transaction: $conn->error");
7072
// Make queries
7173
if ($isNew == true) {
72-
createTextEntry($conn, $title, $_SESSION['username']);
74+
createTextEntry($conn, $title, $username);
7375
}
7476
createVersionEntry($conn, $title, $version, $isPublic, $targetAgeMin, $targetAgeMax, $targetGender);
7577
createCharactersEntry($conn, $title, $version, $text);

private/setup/data.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function addReader($conn, $username, $password, $dob, $gender, $isImpaired) {
1414
function addResearcher($conn, $username, $password) {
1515
$sql = $conn->prepare("INSERT INTO Researchers(username, password) VALUES (?, ?)");
1616
$sql->bind_param("ss", $username, $password);
17-
add($conn, "Reviewers", $sql);
17+
add($conn, "Researchers", $sql);
1818
}
1919

2020
function add($conn, $table, $sql) {
@@ -44,7 +44,7 @@ function add($conn, $table, $sql) {
4444
$conn = connectDB();
4545

4646
for ($i = 0; $i < 100; $i++) {
47-
addResearcher($conn, "reviewer" . $i, "reviewer" . $i);
47+
addResearcher($conn, "researcher$i", password_hash("researcher$i", PASSWORD_DEFAULT));
4848
}
4949

5050
for ($i = 0; $i < 100; $i++) {
@@ -55,7 +55,7 @@ function add($conn, $table, $sql) {
5555
$gender = "f";
5656
}
5757
$isImpaired = rand(0,1);
58-
addReader($conn, "reader" . $i, "reader" . $i, $dob, $gender, $isImpaired);
58+
addReader($conn, "reader$i", password_hash("reader$i", PASSWORD_DEFAULT), $dob, $gender, $isImpaired);
5959
}
6060

6161

0 commit comments

Comments
 (0)