From 7c9cc7754f9e5c0dfc4e7721dabe1b407e8b75fd Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 16 Mar 2024 10:17:58 -0400 Subject: [PATCH 1/2] Allow for shortenviayourls in query params --- lib/Request.php | 2 +- tst/JsonApiTest.php | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/Request.php b/lib/Request.php index d123acf4..ef6c7043 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -143,7 +143,7 @@ class Request } elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) { $this->_operation = 'jsonld'; } elseif (array_key_exists('link', $this->_params) && !empty($this->_params['link'])) { - if (strpos($this->getRequestUri(), '/shortenviayourls') !== false) { + if (strpos($this->getRequestUri(), '/shortenviayourls') !== false || array_key_exists('shortenviayourls', $this->_params)) { $this->_operation = 'yourlsproxy'; } } diff --git a/tst/JsonApiTest.php b/tst/JsonApiTest.php index 45784d24..032ff4d7 100644 --- a/tst/JsonApiTest.php +++ b/tst/JsonApiTest.php @@ -285,26 +285,39 @@ class JsonApiTest extends TestCase /** * @runInSeparateProcess + * @dataProvider baseUriProvider */ - public function testShortenViaYourls() + public function testShortenViaYourls($baseUri) { $mock_yourls_service = $this->_path . DIRECTORY_SEPARATOR . 'yourls.json'; $options = parse_ini_file(CONF, true); $options['main']['basepath'] = 'https://example.com/path'; // missing slash gets added by Configuration constructor - $options['main']['urlshortener'] = 'https://example.com/path/shortenviayourls?link='; + $options['main']['urlshortener'] = 'https://example.com' . $baseUri . 'link='; $options['yourls']['apiurl'] = $mock_yourls_service; Helper::createIniFile(CONF, $options); // the real service answer is more complex, but we only look for the shorturl & statusCode file_put_contents($mock_yourls_service, '{"shorturl":"https:\/\/example.com\/1","statusCode":200}'); - $_SERVER['REQUEST_URI'] = '/path/shortenviayourls?link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar'; + $_SERVER['REQUEST_URI'] = $baseUri . 'link=https%3A%2F%2Fexample.com%2Fpath%2F%3Ffoo%23bar'; $_GET['link'] = 'https://example.com/path/?foo#bar'; + if (strpos($baseUri, '?shortenviayourls') !== false) { + $_GET['shortenviayourls'] = null; + } ob_start(); new Controller; $content = ob_get_contents(); ob_end_clean(); - $this->assertStringContainsString('id="pasteurl" href="https://example.com/1"', $content, 'outputs shortened URL correctly'); + $this->assertStringContainsString('id="pasteurl" href="https://example.com/1"', $content, "'{$baseUri}' outputs shortened URL correctly"); + } + + public function baseUriProvider() + { + return array( + array('/path/shortenviayourls?'), + array('/path/index.php/shortenviayourls?'), + array('/path?shortenviayourls&'), + ); } /** From 7a4c6c010f695d6cc2fa0fc827ef855023317c5b Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Sat, 16 Mar 2024 19:46:57 -0400 Subject: [PATCH 2/2] Update conf.sample.php --- cfg/conf.sample.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cfg/conf.sample.php b/cfg/conf.sample.php index 8c8eeb6a..7db4c889 100644 --- a/cfg/conf.sample.php +++ b/cfg/conf.sample.php @@ -254,8 +254,8 @@ dir = PATH "data" ; Only use this if you allow short URL creation without credentials. ; - Alternatively, using the parameters in this section ("signature" and ; "apiurl"), "urlshortener" needs to point to the base URL of your PrivateBin -; instance with "shortenviayourls?link=" appended. For example: -; urlshortener = "${basepath}shortenviayourls?link=" +; instance with "?shortenviayourls&link=" appended. For example: +; urlshortener = "${basepath}?shortenviayourls&link=" ; This URL will in turn call YOURLS on the server side, using the URL from ; "apiurl" and the "access signature" from the "signature" parameters below.