feat: add wrapper function to log page access and exit
This commit is contained in:
parent
70a2c45509
commit
b089bfc551
2 changed files with 44 additions and 34 deletions
|
@ -5,18 +5,9 @@ $data_time_start = microtime(true);
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
require_once 'post_adapter.php';
|
require_once 'post_adapter.php';
|
||||||
|
|
||||||
// $sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire');
|
|
||||||
|
|
||||||
$adapter = new PostHandler();
|
$adapter = new PostHandler();
|
||||||
|
|
||||||
//if (!$sql)
|
|
||||||
// {
|
|
||||||
// echo 'Connection failed<br>';
|
|
||||||
// echo 'Error number: ' . mysqli_connect_errno() . '<br>';
|
|
||||||
// echo 'Error message: ' . mysqli_connect_error() . '<br>';
|
|
||||||
// die();
|
|
||||||
// }
|
|
||||||
|
|
||||||
$loader = new \Twig\Loader\FilesystemLoader(['./templates', './user_content']);
|
$loader = new \Twig\Loader\FilesystemLoader(['./templates', './user_content']);
|
||||||
|
|
||||||
$twig = new \Twig\Environment($loader,['debug' => true]);
|
$twig = new \Twig\Environment($loader,['debug' => true]);
|
||||||
|
@ -26,6 +17,42 @@ use Twig\Extra\Markdown\DefaultMarkdown;
|
||||||
use Twig\Extra\Markdown\MarkdownRuntime;
|
use Twig\Extra\Markdown\MarkdownRuntime;
|
||||||
use Twig\RuntimeLoader\RuntimeLoaderInterface;
|
use Twig\RuntimeLoader\RuntimeLoaderInterface;
|
||||||
|
|
||||||
|
function deduce_user_agent() {
|
||||||
|
$real_agent=$_SERVER['HTTP_USER_AGENT'];
|
||||||
|
|
||||||
|
if(preg_match('/(Googlebot|\w*Google\w*)/', $real_agent, $match)) {
|
||||||
|
return "bot/google/" . $match[1];
|
||||||
|
}
|
||||||
|
elseif(preg_match('/(Mozilla|Chrome|Chromium)/', $real_agent, $match)) {
|
||||||
|
return "user/" . $match[1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return "unidentified";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function log_and_die($path, $die_code = 0, $referrer = null) {
|
||||||
|
global $data_time_start;
|
||||||
|
global $adapter;
|
||||||
|
|
||||||
|
$data_time_end = microtime(true);
|
||||||
|
|
||||||
|
if(!isset($referrer)) {
|
||||||
|
$referrer = 'magic';
|
||||||
|
|
||||||
|
if(isset($_SERVER['HTTP_REFERER'])) {
|
||||||
|
$referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$adapter->log_post_access($path,
|
||||||
|
deduce_user_agent(),
|
||||||
|
$referrer,
|
||||||
|
$data_time_end - $data_time_start);
|
||||||
|
|
||||||
|
die($die_code);
|
||||||
|
}
|
||||||
|
|
||||||
$twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
|
$twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
|
||||||
public function load($class) {
|
public function load($class) {
|
||||||
if (MarkdownRuntime::class === $class) {
|
if (MarkdownRuntime::class === $class) {
|
||||||
|
@ -34,22 +61,6 @@ $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function deduce_user_agent() {
|
|
||||||
$real_agent=$_SERVER['HTTP_USER_AGENT'];
|
|
||||||
|
|
||||||
if(preg_match('/(?:Mozilla|Chrome|Chromium)/', $real_agent)) {
|
|
||||||
return "web";
|
|
||||||
}
|
|
||||||
elseif(preg_match('/(?:google)/', $real_agent)) {
|
|
||||||
return "bot/google";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return "unidentified";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$user_agent = deduce_user_agent();
|
|
||||||
|
|
||||||
function generate_website($SURI) {
|
function generate_website($SURI) {
|
||||||
global $twig;
|
global $twig;
|
||||||
global $adapter;
|
global $adapter;
|
||||||
|
@ -72,7 +83,7 @@ function generate_website($SURI) {
|
||||||
"authorized" => false
|
"authorized" => false
|
||||||
]);
|
]);
|
||||||
|
|
||||||
die();
|
log_and_die('/api/401');
|
||||||
}
|
}
|
||||||
|
|
||||||
if($SURI = '/api/admin/upload') {
|
if($SURI = '/api/admin/upload') {
|
||||||
|
@ -84,6 +95,9 @@ function generate_website($SURI) {
|
||||||
if($SURI == '/api/post_counters') {
|
if($SURI == '/api/post_counters') {
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
echo json_encode($adapter->get_post_access_counters());
|
echo json_encode($adapter->get_post_access_counters());
|
||||||
|
} elseif($SURI == '/api/metrics') {
|
||||||
|
header('Content-Type: application/line');
|
||||||
|
echo $adapter->get_post_access_counters_line();
|
||||||
} elseif(preg_match('/^\/api\/posts(.*)$/', $SURI, $match)) {
|
} elseif(preg_match('/^\/api\/posts(.*)$/', $SURI, $match)) {
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
@ -112,14 +126,14 @@ function generate_website($SURI) {
|
||||||
"post" => $post
|
"post" => $post
|
||||||
]);
|
]);
|
||||||
|
|
||||||
die();
|
log_and_die('/404', referrer: ($_SERVER['HTTP_REFERER'] ?? 'magic'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if($post['post_metadata']['type'] == 'directory') {
|
if($post['post_metadata']['type'] == 'directory') {
|
||||||
if(preg_match('/^(.*[^\/])((?:#.*)?)$/', $SURI, $match)) {
|
if(preg_match('/^(.*[^\/])((?:#.*)?)$/', $SURI, $match)) {
|
||||||
header('Location: ' . $match[1] . '/' . $match[2]);
|
header('Location: ' . $match[1] . '/' . $match[2]);
|
||||||
|
|
||||||
return;
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
echo $twig->render('post_types/directory.html', [
|
echo $twig->render('post_types/directory.html', [
|
||||||
|
@ -150,10 +164,6 @@ function generate_website($SURI) {
|
||||||
|
|
||||||
generate_website($_SERVER['REQUEST_URI']);
|
generate_website($_SERVER['REQUEST_URI']);
|
||||||
|
|
||||||
$data_time_end = microtime(true);
|
log_and_die($_SERVER['REQUEST_URI']);
|
||||||
|
|
||||||
$adapter->log_post_access($_SERVER['REQUEST_URI'],
|
|
||||||
$user_agent,
|
|
||||||
$data_time_end - $data_time_start)
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
{%endblock%}
|
{%endblock%}
|
||||||
|
|
||||||
<span id="content_footer">
|
<span id="content_footer">
|
||||||
This article was created on {{ post.post_create_time }}, last edited {{ post.post_update_time }}, and was viewed {{ post.post_access_counter }} times~
|
This article was created on {{ post.post_create_time }}, last edited {{ post.post_update_time }}, and was viewed {{ post.post_access_count }} times~
|
||||||
</span>
|
</span>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue