diff options
Diffstat (limited to 'tdd')
-rw-r--r-- | tdd/tdd-bootstrap.php | 9 | ||||
-rw-r--r-- | tdd/tdd-protocol.php | 46 |
2 files changed, 33 insertions, 22 deletions
diff --git a/tdd/tdd-bootstrap.php b/tdd/tdd-bootstrap.php index 6efbf7b..03dc621 100644 --- a/tdd/tdd-bootstrap.php +++ b/tdd/tdd-bootstrap.php @@ -14,7 +14,7 @@ class Protocol { static::$translations = json_decode(file_get_contents('translations.json'), TRUE); } $this->deals_by_tables = array(); - if(!file_exists($this->get_filename())) { + if (!file_exists($this->get_filename())) { throw new Exception('file not found: ' . $this->get_filename()); } } @@ -190,8 +190,9 @@ class Deal { define('TIMESTAMP_FILE', '.tdd-timestamps.cache'); define('RECORDS_FILE', '.tdd-records.cache'); -function load_deals_for_tables($prefix, $round, $board_in_teamy) { - $db = unserialize(file_get_contents(RECORDS_FILE)); +$board_database = unserialize(file_get_contents(RECORDS_FILE)); + +function load_deals_for_tables($db, $prefix, $round, $board_in_teamy) { if (isset($db[$prefix])) { if (isset($db[$prefix][$round])) { if (isset($db[$prefix][$round][$board_in_teamy])) { @@ -214,6 +215,7 @@ function get_files_timestamps($files = array()) { } function compile_record_database($files, $dbFile) { + global $board_database; $db = array(); foreach ($files as $filename) { $filename = basename($filename); @@ -263,6 +265,7 @@ function compile_record_database($files, $dbFile) { } } file_put_contents(RECORDS_FILE, serialize($db)); + $board_database = $db; } function refresh_board_database() { diff --git a/tdd/tdd-protocol.php b/tdd/tdd-protocol.php index 7130599..f8a6e3e 100644 --- a/tdd/tdd-protocol.php +++ b/tdd/tdd-protocol.php @@ -1,31 +1,39 @@ <?php -$prefix = $_GET['prefix']; -$round = (int)$_GET['round']; -$board = (int)$_GET['board']; - require_once('tdd-bootstrap.php'); refresh_board_database(); -$protocol = new Protocol($prefix, $round, $board); - -// security check -$html_filename = $protocol->get_filename(); -$len = strlen($html_filename); -$request_uri_ending = substr($_SERVER['REQUEST_URI'], -$len-1); -if($request_uri_ending != '/' . $html_filename) { +// parsing URI parts from full request string +$uri = explode('b-', basename($_SERVER['REQUEST_URI'])); +if (count($uri) < 2) { die('This script cannot be called directly!'); } -// -$deals_by_tables = load_deals_for_tables($prefix, $round, $board); -if (count($deals_by_tables) > 0) { - foreach($deals_by_tables as $table => $deal) { - $protocol->set_deal($table, $deal); +$board = (int)(array_pop($uri)); +$roundPrefix = implode('b-', $uri); + +try { + // GET parameters pre-parsed by mod_rewrite are used for HTML fallback + // in case {$prefix}{$round} combo is not matched against board DB + $protocol = new Protocol($_GET['prefix'], $_GET['round'], $board); + $html_filename = $protocol->get_filename(); + foreach ($board_database as $prefix => $rounds) { + foreach ($rounds as $round => $boards) { + if ($prefix . $round === $roundPrefix) { + $deals_by_tables = load_deals_for_tables($board_database, $prefix, $round, $board); + if (count($deals_by_tables) > 0) { + foreach($deals_by_tables as $table => $deal) { + $protocol->set_deal($table, $deal); + } + echo $protocol->output(); + exit(0); + } + } + } } - echo $protocol->output(); -} -else { readfile($html_filename); +} catch (Exception $e) { + header('HTTP/1.0 404 Not Found'); + die(); } |