From a3130d2c2f27052710d4dbd51d0001190b19b383 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 25 Aug 2017 19:47:57 +0200 Subject: [PATCH 1/2] Make work behind a reverse proxy Without HTTP_X_FORWARDED_PORT check, might be set to false even though the user is using HTTPS, thus disabling Firefox Social block display --- application/HttpUtils.php | 28 +++++++++++++++++++++++++ index.php | 6 +++--- tests/HttpUtils/IsHttpsTest.php | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/HttpUtils/IsHttpsTest.php diff --git a/application/HttpUtils.php b/application/HttpUtils.php index 88a1efd..0083596 100644 --- a/application/HttpUtils.php +++ b/application/HttpUtils.php @@ -401,3 +401,31 @@ function getIpAddressFromProxy($server, $trustedIps) return array_pop($ips); } + +/** + * Returns true if Shaarli's currently browsed in HTTPS. + * Supports reverse proxies (if the headers are correctly set). + * + * @param array $server $_SERVER. + * + * @return bool true if HTTPS, false otherwise. + */ +function is_https($server) +{ + + if (isset($server['HTTP_X_FORWARDED_PORT'])) { + // Keep forwarded port + if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) { + $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']); + $port = trim($ports[0]); + } else { + $port = $server['HTTP_X_FORWARDED_PORT']; + } + + if ($port == '443') { + return true; + } + } + + return ! empty($server['HTTPS']); +} diff --git a/index.php b/index.php index b4c4347..de993f1 100644 --- a/index.php +++ b/index.php @@ -1063,10 +1063,10 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) // -------- Display the Tools menu if requested (import/export/bookmarklet...) if ($targetPage == Router::$PAGE_TOOLS) { - $data = array( + $data = [ 'pageabsaddr' => index_url($_SERVER), - 'sslenabled' => !empty($_SERVER['HTTPS']) - ); + 'sslenabled' => is_https($_SERVER), + ]; $pluginManager->executeHooks('render_tools', $data); foreach ($data as $key => $value) { diff --git a/tests/HttpUtils/IsHttpsTest.php b/tests/HttpUtils/IsHttpsTest.php new file mode 100644 index 0000000..097f2bc --- /dev/null +++ b/tests/HttpUtils/IsHttpsTest.php @@ -0,0 +1,36 @@ +assertTrue(is_https(['HTTPS' => true])); + $this->assertTrue(is_https(['HTTPS' => '1'])); + $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 443])); + $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443'])); + $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443,123,456,'])); + } + + /** + * Test is_https with HTTP values. + */ + public function testIsHttpsFalse() + { + $this->assertFalse(is_https([])); + $this->assertFalse(is_https(['HTTPS' => false])); + $this->assertFalse(is_https(['HTTPS' => '0'])); + $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 123])); + $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '123'])); + $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => ',123,456,'])); + } +} From 206c45bd05a79b5e6d0c51452a6ac69e85cca0b2 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 2 Sep 2017 13:50:03 +0200 Subject: [PATCH 2/2] Firefox Social title: Use document.title instead of RainTPL variable Fixes #929 --- tpl/default/js/shaarli.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tpl/default/js/shaarli.js b/tpl/default/js/shaarli.js index 4f49aff..e0b4c75 100644 --- a/tpl/default/js/shaarli.js +++ b/tpl/default/js/shaarli.js @@ -607,10 +607,11 @@ function htmlEntities(str) function activateFirefoxSocial(node) { var loc = location.href; var baseURL = loc.substring(0, loc.lastIndexOf("/") + 1); + var title = document.title; // Keeping the data separated (ie. not in the DOM) so that it's maintainable and diffable. var data = { - name: "{$shaarlititle}", + name: title, description: "The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community.", author: "Shaarli", version: "1.0.0",