Files
ansible/roles/retrodojo/templates/index.php.j2

111 lines
3.7 KiB
Django/Jinja

<?php
// conversion from the letter ID to the datetime+rank ID
define("CODE", "aQwZsXeDcRfVtGbYhNuJiKoLpMAqWzSxEdCrFvTgByHnUjIkOlPm");
define("MYSQL_USER", "{{ retrodojo_maria_user }}");
define("MYSQL_PASSWORD", "{{ retrodojo_maria_password }}");
define("MYSQL_DATABASE", "{{ retrodojo_maria_database }}");
function decode_article($code) {
$id = 0;
foreach(str_split($code) as $key => $value) {
$transposed = strrpos(CODE, $value);
$id += 10 ** (3 - $key) * ($transposed % 10);
}
return $id;
}
function get_current_guid_from_historical_id_for($conn, $article_type, $hist_id) {
$guid = "";
$query = <<<QUERY
SELECT guid
FROM wp_dojohistoricalid
INNER JOIN wp_posts ON wp_dojohistoricalid.current_id = wp_posts.ID
INNER JOIN wp_terms ON wp_dojohistoricalid.term_id = wp_terms.term_id
WHERE slug = "$article_type"
AND historical_id = $hist_id;
QUERY;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_row($result);
$guid = $row[0];
}
return $guid;
}
// get the article ID no matter what
if (isset($_GET["id"])) {
$article_id = substr($_GET["id"], 0, 4);
}
if (preg_match("/\/index\.php\/(.+)\/(.+)\/([A-Za-z]{4})-/", $_SERVER["DOCUMENT_URI"], $matches)) {
$article_id = $matches[3];
}
// connect to MySQL via WP login
$conn = mysqli_connect("localhost", MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if (isset($article_id)) {
if (preg_match("/\/nouvelles\//", $_SERVER["DOCUMENT_URI"])) {
/*
This part of the script decodes the Nouvelles ID and search the current DB for the right article
*/
$decoded_day = strrpos(CODE, $article_id[0]);
$decoded_month = strrpos(CODE, $article_id[1]);
$decoded_year = strrpos(CODE, $article_id[2]) + 2000;
$decoded_rank = strrpos(CODE, $article_id[3]);
$query = <<<QUERY
SELECT guid
FROM wp_posts INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
WHERE term_taxonomy_id = 3
AND YEAR(post_date) = $decoded_year
AND MONTH(post_date) = $decoded_month
AND DAY(post_date) = $decoded_day
ORDER BY post_date;
QUERY;
$result = mysqli_query($conn, $query);
mysqli_data_seek($result, $decoded_rank - 1);
$row = mysqli_fetch_row($result);
header("Location: " . str_replace("http:", "https:", $row[0]), true, 301);
} elseif(preg_match("/\/nezvroses\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "nezvroses", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} elseif(preg_match("/\/analyses\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "analyses", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} elseif(preg_match("/\/editoriaux\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "editos", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} elseif(preg_match("/\/apercus\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "apercus", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} else {
// any other matches (apercus, etc…) will be sent a 404 for now
http_response_code(404);
}
} else {
header("Location: https://www.nintendojo.fr", true, 307);
}
mysqli_close($conn);