Add YOURLS Amazon KDP Marketplace Redirect plugin
This commit is contained in:
commit
8de72714c1
6 files changed
+521
No files matched your search
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
// Invoked by run.php in a fresh PHP CGI process for each request.
|
||||
if (getenv('SEBSTR_KDP_TEST_URL') === false || PHP_SAPI !== 'cgi-fcgi') {
|
||||
exit;
|
||||
}
|
||||
|
||||
define('YOURLS_ABSPATH', dirname(__DIR__, 4) . '/');
|
||||
require YOURLS_ABSPATH . 'includes/functions-plugins.php';
|
||||
require YOURLS_ABSPATH . 'includes/functions.php';
|
||||
|
||||
function yourls_get_option($name, $default = false)
|
||||
{
|
||||
return $name === 'sebstr_amazon_kdp_asins' ? ['TESTBOOK01' => 'Standard', '1234567890' => 'Left-handed'] : $default;
|
||||
}
|
||||
|
||||
$clicks = $logs = 0;
|
||||
yourls_add_filter('shunt_update_clicks', static function () use (&$clicks) {
|
||||
++$clicks;
|
||||
return true;
|
||||
});
|
||||
yourls_add_filter('shunt_log_redirect', static function () use (&$logs) {
|
||||
++$logs;
|
||||
return true;
|
||||
});
|
||||
yourls_add_filter('shunt_robots_tag_header', static fn () => true);
|
||||
register_shutdown_function(static function () use (&$clicks, &$logs) {
|
||||
echo "clicks=$clicks logs=$logs";
|
||||
});
|
||||
|
||||
if (getenv('SEBSTR_KDP_TEST_KDP') === '1') {
|
||||
require dirname(__DIR__) . '/plugin.php';
|
||||
}
|
||||
if (getenv('SEBSTR_KDP_TEST_SELLER') === '1') {
|
||||
require dirname(__DIR__, 2) . '/sebstr_amazon/plugin.php';
|
||||
}
|
||||
|
||||
$_SERVER['REMOTE_ADDR'] = getenv('SEBSTR_KDP_TEST_PEER');
|
||||
$country = getenv('SEBSTR_KDP_TEST_COUNTRY');
|
||||
if ($country !== '') {
|
||||
$_SERVER['HTTP_CF_IPCOUNTRY'] = $country;
|
||||
}
|
||||
yourls_redirect_shorturl(getenv('SEBSTR_KDP_TEST_URL'), 'existing-keyword');
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
exit;
|
||||
}
|
||||
|
||||
require dirname(__DIR__) . '/routing.php';
|
||||
|
||||
use function Sebstr\AmazonKdp\visitor_country;
|
||||
|
||||
function check(bool $ok, string $message): void
|
||||
{
|
||||
if (!$ok) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
}
|
||||
|
||||
check(visitor_country('2a06:98c7::1', 'GB') === 'GB', 'IPv6 prefix upper boundary');
|
||||
check(visitor_country('2a06:98c8::1', 'GB') === null, 'IPv6 outside prefix');
|
||||
check(visitor_country('104.23.255.255', 'NL') === 'NL', 'IPv4 prefix upper boundary');
|
||||
check(visitor_country('104.28.0.1', 'NL') === null, 'IPv4 outside prefix');
|
||||
check(visitor_country('::ffff:104.16.0.1', 'NL') === 'NL', 'IPv4-mapped Cloudflare address');
|
||||
|
||||
$uk = 'https://www.amazon.co.uk/dp/TESTBOOK01';
|
||||
$us = 'https://www.amazon.com/dp/TESTBOOK01';
|
||||
$de = 'https://www.amazon.de/dp/TESTBOOK01';
|
||||
$nl = 'https://www.amazon.nl/dp/TESTBOOK01';
|
||||
$left = 'https://www.amazon.com.au/gp/product/1234567890';
|
||||
$seller = $uk . '?smid=EXPLICIT';
|
||||
$legacy = $uk . '/ref=cx_skuctr_share?smid=EXPLICIT';
|
||||
$sellerTag = '/ref=cx_skuctr_share?smid={seller-id}';
|
||||
$unrelated = 'https://www.amazon.com/dp/B012345678';
|
||||
$unrelatedUk = 'https://www.amazon.co.uk/dp/B012345678';
|
||||
$nonAmazon = 'https://example.com/dp/TESTBOOK01';
|
||||
$impostor = 'https://amazon.com.example.org/dp/TESTBOOK01';
|
||||
|
||||
$cases = [
|
||||
['UK original', $uk, 'GB', $uk, 302],
|
||||
['US from same short link', $uk, 'US', $us, 302],
|
||||
['NL from same short link', $uk, 'NL', $nl, 302],
|
||||
['Germany from same short link', $uk, 'DE', $de, 302],
|
||||
['from Germany', $de, 'GB', $uk, 302],
|
||||
['from Netherlands', $nl, 'US', $us, 302],
|
||||
['Belgium', $us, 'BE', 'https://www.amazon.com.be/dp/TESTBOOK01', 302],
|
||||
['Ireland', $us, 'IE', 'https://www.amazon.ie/dp/TESTBOOK01', 302],
|
||||
['Brazil', $uk, 'BR', 'https://www.amazon.com.br/dp/TESTBOOK01', 302],
|
||||
['Mexico', $uk, 'MX', 'https://www.amazon.com.mx/dp/TESTBOOK01', 302],
|
||||
['unsupported', $uk, 'CH', $uk, 302],
|
||||
['missing', $uk, '', $uk, 302],
|
||||
['invalid', $uk, 'NL,US', $uk, 302],
|
||||
['spoofed', $uk, 'NL', $uk, 302, true, true, '203.0.113.2'],
|
||||
['queries and fragment', $uk . '?tag=foo&%74ag=bar&th=1&k=two%20words#detail', 'US',
|
||||
$us . '?tag=foo&th=1&k=two%20words#detail', 302],
|
||||
['exact fallback', $uk . '?th=1&th=2', 'XX', $uk . '?th=1&th=2', 302],
|
||||
['left-handed', $left, 'DE', 'https://www.amazon.de/dp/1234567890', 302],
|
||||
['seller parameter', $seller, 'US', $seller . '&ref=cx_skuctr_share?smid={seller-id}', 301],
|
||||
['legacy seller path alone', $legacy, 'US', $legacy, 301, true, false],
|
||||
['encoded seller selection', $us . '?%73mid=EXPLICIT', 'DE', $us . '?%73mid=EXPLICIT', 301],
|
||||
['seller via ref query', $us . '?ref=cx_skuctr_share%3Fsmid%3DEXPLICIT', 'DE',
|
||||
$us . '?ref=cx_skuctr_share%3Fsmid%3DEXPLICIT', 301],
|
||||
['other Amazon product', $unrelated, 'DE', $unrelated, 301],
|
||||
['seller plugin handles unregistered products', $unrelatedUk, 'DE', $unrelatedUk . $sellerTag, 301],
|
||||
['non-Amazon', $nonAmazon, 'DE', $nonAmazon, 301],
|
||||
['impostor host', $impostor, 'DE', $impostor, 301],
|
||||
['malformed ASIN', $us . 'X', 'DE', $us . 'X', 301],
|
||||
['KDP alone', $uk, 'US', $us, 302, true, false],
|
||||
['seller alone', $uk, 'US', $uk . $sellerTag, 301, false, true],
|
||||
];
|
||||
|
||||
foreach ($cases as $case) {
|
||||
[$label, $url, $country, $expected, $status] = $case;
|
||||
$environment = [
|
||||
'REDIRECT_STATUS' => '200',
|
||||
'GATEWAY_INTERFACE' => 'CGI/1.1',
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
||||
'SCRIPT_FILENAME' => __DIR__ . '/request.php',
|
||||
'SEBSTR_KDP_TEST_URL' => $url,
|
||||
'SEBSTR_KDP_TEST_COUNTRY' => $country,
|
||||
'SEBSTR_KDP_TEST_PEER' => $case[7] ?? '104.16.0.1',
|
||||
'SEBSTR_KDP_TEST_KDP' => ($case[5] ?? true) ? '1' : '0',
|
||||
'SEBSTR_KDP_TEST_SELLER' => ($case[6] ?? true) ? '1' : '0',
|
||||
];
|
||||
$process = proc_open(['php-cgi', '-d', 'display_errors=stderr', '-d', 'log_errors=0'], [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, null, $environment);
|
||||
check(is_resource($process), 'Start php-cgi');
|
||||
$output = stream_get_contents($pipes[1]);
|
||||
$errors = stream_get_contents($pipes[2]);
|
||||
fclose($pipes[1]);
|
||||
fclose($pipes[2]);
|
||||
check(proc_close($process) === 0, "$label CGI error $errors $output");
|
||||
$locationPattern = str_replace(preg_quote('{seller-id}', '~'), '[A-Z0-9]+', preg_quote($expected, '~'));
|
||||
check(str_contains($output, "Status: $status ") && preg_match("~^Location: $locationPattern\\r?$~m", $output) === 1,
|
||||
"$label unexpected response $output $errors");
|
||||
check(str_contains($output, 'clicks=1 logs=1'), "$label tracking mismatch");
|
||||
check(str_contains($output, 'Cache-Control: private, no-store, max-age=0') === ($status === 302), "$label browser cache header mismatch");
|
||||
check(str_contains($output, 'Cloudflare-CDN-Cache-Control: no-store') === ($status === 302), "$label Cloudflare cache header mismatch");
|
||||
}
|
||||
|
||||
echo count($cases) . " CGI redirect cases and five IP checks passed.\n";
|
||||
|
||||
define('YOURLS_ABSPATH', dirname(__DIR__, 4) . '/');
|
||||
define('YOURLS_ADMIN', true);
|
||||
require YOURLS_ABSPATH . 'includes/functions-plugins.php';
|
||||
|
||||
function yourls_get_option($name, $default = false)
|
||||
{
|
||||
return $GLOBALS['test_options'][$name] ?? $default;
|
||||
}
|
||||
|
||||
function yourls_update_option($name, $value)
|
||||
{
|
||||
$GLOBALS['test_options'][$name] = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
function yourls_get_db($context)
|
||||
{
|
||||
return new class {
|
||||
public function add_plugin_page($slug, $title, $callback): void
|
||||
{
|
||||
$GLOBALS['test_pages'][$slug] = $callback;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function yourls_create_nonce($action)
|
||||
{
|
||||
return 'test-' . $action;
|
||||
}
|
||||
|
||||
function yourls_verify_nonce($action, $nonce)
|
||||
{
|
||||
if ($nonce !== yourls_create_nonce($action)) {
|
||||
throw new InvalidArgumentException('Invalid nonce');
|
||||
}
|
||||
}
|
||||
|
||||
require dirname(__DIR__) . '/plugin.php';
|
||||
yourls_do_action('plugins_loaded');
|
||||
$page = $GLOBALS['test_pages']['sebstr_amazon_kdp'] ?? null;
|
||||
check(is_callable($page), 'YOURLS admin page registration');
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||
$key = Sebstr\AmazonKdp\SETTINGS;
|
||||
$_POST = ['operation' => 'save', 'asin' => 'testbook01', 'title' => '<Standard>', 'nonce' => 'invalid'];
|
||||
ob_start();
|
||||
try {
|
||||
$page();
|
||||
check(false, 'Invalid nonce accepted');
|
||||
} catch (InvalidArgumentException $exception) {
|
||||
check(yourls_get_option($key, []) === [], 'Invalid nonce changed registry');
|
||||
} finally {
|
||||
ob_end_clean();
|
||||
}
|
||||
$_POST['nonce'] = yourls_create_nonce($key);
|
||||
ob_start();
|
||||
$page();
|
||||
$html = ob_get_clean();
|
||||
check(str_contains($html, '<Standard>'), 'Title is escaped');
|
||||
check(yourls_get_option($key) === ['TESTBOOK01' => '<Standard>'], 'ASIN and title saved');
|
||||
|
||||
$_POST = ['operation' => 'remove', 'asin' => 'TESTBOOK01', 'nonce' => yourls_create_nonce($key)];
|
||||
ob_start();
|
||||
$page();
|
||||
ob_end_clean();
|
||||
check(yourls_get_option($key) === [], 'ASIN removed');
|
||||
|
||||
echo "Admin registration, nonce rejection, title escaping, addition and removal passed.\n";
|
||||
Reference in New Issue
Block a user