Skip to content

Commit

Permalink
Added cors headers for website support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziga committed May 21, 2021
1 parent 513086b commit 03ea50f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
30 changes: 10 additions & 20 deletions php/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,16 @@ public static function getUserIpAddress() : string {
public static function userSentToManyRequests(string $action) : bool{
$timer = 0;

switch($action){
case 'createAccount':
$timer = Settings::$limiter_createAccount;
break;
case 'getPasswords':
$timer = Settings::$limiter_getPasswords;
break;
case 'savePassword':
$timer = Settings::$limiter_savePassword;
break;
case 'editPassword':
$timer = Settings::$limiter_editPassword;
break;
case 'deletePassword':
$timer = Settings::$limiter_deletePassword;
break;
case 'deleteAccount':
$timer = Settings::$limiter_deleteAccount;
break;
}
$timerOptions = [
'createAccount' => Settings::$limiter_createAccount,
'getPasswords' => Settings::$limiter_getPasswords,
'savePassword' => Settings::$limiter_savePassword,
'editPassword' => Settings::$limiter_editPassword,
'deletePassword' => Settings::$limiter_deletePassword,
'deleteAccount' => Settings::$limiter_deleteAccount
];

$timer = $timerOptions[$action];

$ips_array = json_decode(file_get_contents('ips.json'), true);

Expand Down
15 changes: 14 additions & 1 deletion php/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}

require_once "Display.php";
require_once "Database.php";
require_once "Settings.php";
Expand All @@ -9,7 +22,7 @@
}

switch($_GET['action']){
case "createAccount":
case "createAccount":
if(Database::userSentToManyRequests('createAccount')){
echo Display::json(429);
return;
Expand Down

0 comments on commit 03ea50f

Please sign in to comment.