Add Tick model. Move Tick management functions there.
This commit is contained in:
parent
d5de7cf281
commit
79a161b0e6
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
// Sesion handling
|
||||||
// Start a session and create a csrf token if necessary
|
// Start a session and create a csrf token if necessary
|
||||||
// TODO - move these to an AuthController?
|
// TODO - move these to AuthController?
|
||||||
function start_session(){
|
function start_session(){
|
||||||
if (session_status() === PHP_SESSION_NONE) {
|
if (session_status() === PHP_SESSION_NONE) {
|
||||||
session_start();
|
session_start();
|
||||||
@ -13,12 +14,19 @@ function generate_csrf_token(bool $regenerate = false){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateCsrfToken($token) {
|
||||||
|
return hash_equals($_SESSION['csrf_token'], $token);
|
||||||
|
}
|
||||||
|
|
||||||
start_session();
|
start_session();
|
||||||
generate_csrf_token();
|
generate_csrf_token();
|
||||||
|
|
||||||
|
// TODO - I *think* what I want to do is define just this, then load up all the classes.
|
||||||
|
// Then I can define all this other boilerplate in Config or Util or whatever.
|
||||||
|
// I'll have one chicken-and-egg problem with the source directory, but that's not a big deal.
|
||||||
define('APP_ROOT', dirname(dirname(__FILE__)));
|
define('APP_ROOT', dirname(dirname(__FILE__)));
|
||||||
|
|
||||||
|
// TODO - move all this to a config class?
|
||||||
define('SRC_DIR', APP_ROOT . '/src');
|
define('SRC_DIR', APP_ROOT . '/src');
|
||||||
define('STORAGE_DIR', APP_ROOT . '/storage');
|
define('STORAGE_DIR', APP_ROOT . '/storage');
|
||||||
define('TEMPLATES_DIR', APP_ROOT . '/templates');
|
define('TEMPLATES_DIR', APP_ROOT . '/templates');
|
||||||
|
@ -10,7 +10,7 @@ class HomeController{
|
|||||||
|
|
||||||
$limit = $config->itemsPerPage;
|
$limit = $config->itemsPerPage;
|
||||||
$offset = ($page - 1) * $limit;
|
$offset = ($page - 1) * $limit;
|
||||||
$ticks = iterator_to_array($this->stream_ticks($limit, $offset));
|
$ticks = iterator_to_array(Tick::streamTicks($limit, $offset));
|
||||||
|
|
||||||
$view = new HomeView();
|
$view = new HomeView();
|
||||||
$tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit);
|
$tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit);
|
||||||
@ -36,7 +36,7 @@ class HomeController{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// save the tick
|
// save the tick
|
||||||
$this->save_tick($_POST['tick']);
|
Tick::save($_POST['tick']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the config
|
// get the config
|
||||||
@ -47,79 +47,4 @@ class HomeController{
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO - move to a Tick model
|
|
||||||
private function stream_ticks(int $limit, int $offset = 0): Generator {
|
|
||||||
$tick_files = glob(TICKS_DIR . '/*/*/*.txt');
|
|
||||||
usort($tick_files, fn($a, $b) => strcmp($b, $a)); // sort filenames in reverse chronological order
|
|
||||||
|
|
||||||
$count = 0;
|
|
||||||
foreach ($tick_files as $file) {
|
|
||||||
// read all the ticks from the current file and reverse the order
|
|
||||||
// so the most recent ones are first
|
|
||||||
//
|
|
||||||
// each file is a single day, so we never hold more than
|
|
||||||
// one day's ticks in memory
|
|
||||||
$lines = array_reverse(
|
|
||||||
file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
|
|
||||||
);
|
|
||||||
|
|
||||||
// split the path to the current file into the date components
|
|
||||||
$pathParts = explode('/', str_replace('\\', '/', $file));
|
|
||||||
|
|
||||||
// assign the different components to the appropriate part of the date
|
|
||||||
$year = $pathParts[count($pathParts) - 3];
|
|
||||||
$month = $pathParts[count($pathParts) - 2];
|
|
||||||
$day = pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME);
|
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
|
||||||
// just keep skipping ticks until we get to the starting point
|
|
||||||
if ($offset > 0) {
|
|
||||||
$offset--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ticks are pipe-delimited: timestamp|text
|
|
||||||
// But just in case a tick contains a pipe, only split on the first one that occurs
|
|
||||||
$tickParts = explode('|', $line, 2);
|
|
||||||
$time = $tickParts[0];
|
|
||||||
$tick = $tickParts[1];
|
|
||||||
|
|
||||||
// Build the timestamp from the date and time
|
|
||||||
// Ticks are always stored in UTC
|
|
||||||
$timestampUTC = "$year-$month-$day $time";
|
|
||||||
yield [
|
|
||||||
'timestamp' => $timestampUTC,
|
|
||||||
'tick' => $tick,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (++$count >= $limit) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO - move to a Tick model
|
|
||||||
private function save_tick(string $tick): void {
|
|
||||||
// build the tick path and filename from the current time
|
|
||||||
$now = new DateTime('now', new DateTimeZone('UTC'));
|
|
||||||
|
|
||||||
$year = $now->format('Y');
|
|
||||||
$month = $now->format('m');
|
|
||||||
$day = $now->format('d');
|
|
||||||
$time = $now->format('H:i:s');
|
|
||||||
|
|
||||||
// build the full path to the tick file
|
|
||||||
$dir = TICKS_DIR . "/$year/$month";
|
|
||||||
$filename = "$dir/$day.txt";
|
|
||||||
|
|
||||||
// create the directory if it doesn't exist
|
|
||||||
if (!is_dir($dir)) {
|
|
||||||
mkdir($dir, 0770, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// write the tick to the file (the file will be created if it doesn't exist)
|
|
||||||
$content = $time . "|" . $tick . "\n";
|
|
||||||
file_put_contents($filename, $content, FILE_APPEND);
|
|
||||||
}
|
|
||||||
}
|
}
|
77
src/Model/Tick/Tick.php
Normal file
77
src/Model/Tick/Tick.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
class Tick {
|
||||||
|
|
||||||
|
public static function streamTicks(int $limit, int $offset = 0): Generator {
|
||||||
|
$tick_files = glob(TICKS_DIR . '/*/*/*.txt');
|
||||||
|
usort($tick_files, fn($a, $b) => strcmp($b, $a)); // sort filenames in reverse chronological order
|
||||||
|
|
||||||
|
$count = 0;
|
||||||
|
foreach ($tick_files as $file) {
|
||||||
|
// read all the ticks from the current file and reverse the order
|
||||||
|
// so the most recent ones are first
|
||||||
|
//
|
||||||
|
// each file is a single day, so we never hold more than
|
||||||
|
// one day's ticks in memory
|
||||||
|
$lines = array_reverse(
|
||||||
|
file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
|
||||||
|
);
|
||||||
|
|
||||||
|
// split the path to the current file into the date components
|
||||||
|
$pathParts = explode('/', str_replace('\\', '/', $file));
|
||||||
|
|
||||||
|
// assign the different components to the appropriate part of the date
|
||||||
|
$year = $pathParts[count($pathParts) - 3];
|
||||||
|
$month = $pathParts[count($pathParts) - 2];
|
||||||
|
$day = pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME);
|
||||||
|
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
// just keep skipping ticks until we get to the starting point
|
||||||
|
if ($offset > 0) {
|
||||||
|
$offset--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ticks are pipe-delimited: timestamp|text
|
||||||
|
// But just in case a tick contains a pipe, only split on the first one that occurs
|
||||||
|
$tickParts = explode('|', $line, 2);
|
||||||
|
$time = $tickParts[0];
|
||||||
|
$tick = $tickParts[1];
|
||||||
|
|
||||||
|
// Build the timestamp from the date and time
|
||||||
|
// Ticks are always stored in UTC
|
||||||
|
$timestampUTC = "$year-$month-$day $time";
|
||||||
|
yield [
|
||||||
|
'timestamp' => $timestampUTC,
|
||||||
|
'tick' => $tick,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (++$count >= $limit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function save(string $tick): void {
|
||||||
|
// build the tick path and filename from the current time
|
||||||
|
$now = new DateTime('now', new DateTimeZone('UTC'));
|
||||||
|
|
||||||
|
$year = $now->format('Y');
|
||||||
|
$month = $now->format('m');
|
||||||
|
$day = $now->format('d');
|
||||||
|
$time = $now->format('H:i:s');
|
||||||
|
|
||||||
|
// build the full path to the tick file
|
||||||
|
$dir = TICKS_DIR . "/$year/$month";
|
||||||
|
$filename = "$dir/$day.txt";
|
||||||
|
|
||||||
|
// create the directory if it doesn't exist
|
||||||
|
if (!is_dir($dir)) {
|
||||||
|
mkdir($dir, 0770, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the tick to the file (the file will be created if it doesn't exist)
|
||||||
|
$content = $time . "|" . $tick . "\n";
|
||||||
|
file_put_contents($filename, $content, FILE_APPEND);
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
/*
|
|
||||||
if (session_status() === PHP_SESSION_NONE) {
|
|
||||||
session_start();
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateCsrfToken() {
|
|
||||||
if (empty($_SESSION['csrf_token'])) {
|
|
||||||
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
|
||||||
}
|
|
||||||
return $_SESSION['csrf_token'];
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
function validateCsrfToken($token) {
|
|
||||||
return hash_equals($_SESSION['csrf_token'], $token);
|
|
||||||
}
|
|
@ -9,7 +9,7 @@
|
|||||||
<title><?= $config->siteTitle ?></title>
|
<title><?= $config->siteTitle ?></title>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<link rel="stylesheet" href="<?= htmlspecialchars($config->basePath) ?>/css/tkr.css?v=<?= time() ?>">
|
<link rel="stylesheet" href="<?= htmlspecialchars($config->basePath) ?>css/tkr.css?v=<?= time() ?>">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="home-navbar">
|
<div class="home-navbar">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user