speedtest/results/idObfuscation.php

73 lines
1.6 KiB
PHP
Raw Normal View History

<?php
Refactor `results/` PHP code (#369) * put code to insert and query data from database into separate file * simplify obfuscation salt file handling * use new DB interaction functions in telemetry.php * use new DB interaction functions in stats.php and fix indentation levels * format telemetry settings file * use new function for interacting with DB in index.php * move drawing of the image into function and try to comment each section with what it does * reorder lines for parts of the image to align with the order they appear on the image * bugfix: display obfuscated and deobfuscated id in stats if id obfuscation is enabled * improve error handling * add missing PHPDocs to functions * imageftbbox returns an array on success and false on failure so to check if the font is usable, check if we got an array * fix dsn for postgres * fix limit sql statement for postgresql * remove obsolete require statement * use require instead of require_once since the settings file might need to be loaded multiple times because it just contains plain variables which will just get loaded into the current scope * move require statements to the top of the file * make sure files are readable before requiring them * add constant to refer to the telemetry settings file and check if it is readable before loading it * return null if no speedtest result was found for the given id and show according message to the user instead of just exiting * use existing constant instead of string for telemetry settings file name * uniformly use single quotes instead of double quotes as most code places already used single quotes * somehow some tabs sneaked in, replace them to uniformly use spaces * mysql now uses pdo, too, reflect that in the requirements documentation * pass username and password as constructor parameters instead of via DSN
2020-10-20 05:43:48 +00:00
define('ID_OBFUSCATION_SALT_FILE', __DIR__.'/idObfuscation_salt.php');
/**
* @return string|int
*/
function getObfuscationSalt()
{
if (!file_exists(ID_OBFUSCATION_SALT_FILE)) {
$bytes = openssl_random_pseudo_bytes(4);
$saltData = "<?php\n\n\$OBFUSCATION_SALT = 0x".bin2hex($bytes).";\n";
file_put_contents(ID_OBFUSCATION_SALT_FILE, $saltData);
}
if (
file_exists(ID_OBFUSCATION_SALT_FILE)
&& is_readable(ID_OBFUSCATION_SALT_FILE)
) {
require ID_OBFUSCATION_SALT_FILE;
}
return isset($OBFUSCATION_SALT) ? $OBFUSCATION_SALT : 0;
}
Refactor `results/` PHP code (#369) * put code to insert and query data from database into separate file * simplify obfuscation salt file handling * use new DB interaction functions in telemetry.php * use new DB interaction functions in stats.php and fix indentation levels * format telemetry settings file * use new function for interacting with DB in index.php * move drawing of the image into function and try to comment each section with what it does * reorder lines for parts of the image to align with the order they appear on the image * bugfix: display obfuscated and deobfuscated id in stats if id obfuscation is enabled * improve error handling * add missing PHPDocs to functions * imageftbbox returns an array on success and false on failure so to check if the font is usable, check if we got an array * fix dsn for postgres * fix limit sql statement for postgresql * remove obsolete require statement * use require instead of require_once since the settings file might need to be loaded multiple times because it just contains plain variables which will just get loaded into the current scope * move require statements to the top of the file * make sure files are readable before requiring them * add constant to refer to the telemetry settings file and check if it is readable before loading it * return null if no speedtest result was found for the given id and show according message to the user instead of just exiting * use existing constant instead of string for telemetry settings file name * uniformly use single quotes instead of double quotes as most code places already used single quotes * somehow some tabs sneaked in, replace them to uniformly use spaces * mysql now uses pdo, too, reflect that in the requirements documentation * pass username and password as constructor parameters instead of via DSN
2020-10-20 05:43:48 +00:00
/**
* This is a simple reversible hash function I made for encoding and decoding test IDs.
* It is not cryptographically secure, don't use it to hash passwords or something!
*
* @param int|string $id
* @param bool $dec
*
* @return int|string
*/
function obfdeobf($id, $dec)
{
$salt = getObfuscationSalt() & 0xFFFFFFFF;
$id &= 0xFFFFFFFF;
if ($dec) {
$id ^= $salt;
$id = (($id & 0xAAAAAAAA) >> 1) | ($id & 0x55555555) << 1;
$id = (($id & 0x0000FFFF) << 16) | (($id & 0xFFFF0000) >> 16);
return $id;
}
$id = (($id & 0x0000FFFF) << 16) | (($id & 0xFFFF0000) >> 16);
$id = (($id & 0xAAAAAAAA) >> 1) | ($id & 0x55555555) << 1;
return $id ^ $salt;
}
Refactor `results/` PHP code (#369) * put code to insert and query data from database into separate file * simplify obfuscation salt file handling * use new DB interaction functions in telemetry.php * use new DB interaction functions in stats.php and fix indentation levels * format telemetry settings file * use new function for interacting with DB in index.php * move drawing of the image into function and try to comment each section with what it does * reorder lines for parts of the image to align with the order they appear on the image * bugfix: display obfuscated and deobfuscated id in stats if id obfuscation is enabled * improve error handling * add missing PHPDocs to functions * imageftbbox returns an array on success and false on failure so to check if the font is usable, check if we got an array * fix dsn for postgres * fix limit sql statement for postgresql * remove obsolete require statement * use require instead of require_once since the settings file might need to be loaded multiple times because it just contains plain variables which will just get loaded into the current scope * move require statements to the top of the file * make sure files are readable before requiring them * add constant to refer to the telemetry settings file and check if it is readable before loading it * return null if no speedtest result was found for the given id and show according message to the user instead of just exiting * use existing constant instead of string for telemetry settings file name * uniformly use single quotes instead of double quotes as most code places already used single quotes * somehow some tabs sneaked in, replace them to uniformly use spaces * mysql now uses pdo, too, reflect that in the requirements documentation * pass username and password as constructor parameters instead of via DSN
2020-10-20 05:43:48 +00:00
/**
* @param int $id
*
* @return string
*/
function obfuscateId($id)
{
return str_pad(base_convert(obfdeobf($id + 1, false), 10, 36), 7, 0, STR_PAD_LEFT);
}
Refactor `results/` PHP code (#369) * put code to insert and query data from database into separate file * simplify obfuscation salt file handling * use new DB interaction functions in telemetry.php * use new DB interaction functions in stats.php and fix indentation levels * format telemetry settings file * use new function for interacting with DB in index.php * move drawing of the image into function and try to comment each section with what it does * reorder lines for parts of the image to align with the order they appear on the image * bugfix: display obfuscated and deobfuscated id in stats if id obfuscation is enabled * improve error handling * add missing PHPDocs to functions * imageftbbox returns an array on success and false on failure so to check if the font is usable, check if we got an array * fix dsn for postgres * fix limit sql statement for postgresql * remove obsolete require statement * use require instead of require_once since the settings file might need to be loaded multiple times because it just contains plain variables which will just get loaded into the current scope * move require statements to the top of the file * make sure files are readable before requiring them * add constant to refer to the telemetry settings file and check if it is readable before loading it * return null if no speedtest result was found for the given id and show according message to the user instead of just exiting * use existing constant instead of string for telemetry settings file name * uniformly use single quotes instead of double quotes as most code places already used single quotes * somehow some tabs sneaked in, replace them to uniformly use spaces * mysql now uses pdo, too, reflect that in the requirements documentation * pass username and password as constructor parameters instead of via DSN
2020-10-20 05:43:48 +00:00
/**
* @param string $id
*
* @return int
*/
function deobfuscateId($id)
{
return obfdeobf(base_convert($id, 36, 10), true) - 1;
}