From 8ae9a3b9fef9a8036821901430a435849418f4ff Mon Sep 17 00:00:00 2001 From: Ivo Petkov Date: Mon, 23 Nov 2020 00:32:59 +0200 Subject: [PATCH] Code optimizations and better validation. --- components/embedElement.php | 25 +++++++++++++++++++------ index.php | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/components/embedElement.php b/components/embedElement.php index e801fdd..c99e78c 100644 --- a/components/embedElement.php +++ b/components/embedElement.php @@ -9,7 +9,7 @@ use BearFramework\App; $app = App::get(); -$context = $app->contexts->get(__FILE__); +$context = $app->contexts->get(__DIR__); $allowedHosts = [ 'skydrive.live.com', @@ -114,7 +114,17 @@ $aspectRatioParts = explode(':', $aspectRatio); $paddingBottom = '75%'; if (sizeof($aspectRatioParts) === 2 && is_numeric($aspectRatioParts[0]) && is_numeric($aspectRatioParts[1])) { - $paddingBottom = ((float) $aspectRatioParts[1] / (float) $aspectRatioParts[0] * 100) . '%'; + $widthRatio = (float) $aspectRatioParts[0]; + $heightRatio = (float) $aspectRatioParts[1]; + if ($widthRatio > 0 && $heightRatio > 0) { + if ($heightRatio / $widthRatio > 10) { // prevent super tall element + $heightRatio = $widthRatio * 10; + } + $paddingBottomValue = ($heightRatio / $widthRatio * 100); + if ($paddingBottomValue >= 0) { + $paddingBottom = $paddingBottomValue . '%'; + } + } } $containerStyle = 'padding-bottom:' . $paddingBottom . ';'; } else { @@ -134,8 +144,11 @@ } } ?> - - - - + + + + + + + \ No newline at end of file diff --git a/index.php b/index.php index 17cbdd3..e995e71 100644 --- a/index.php +++ b/index.php @@ -14,7 +14,7 @@ $app->bearCMS->addons ->register('bearcms/embed-element-addon', function(\BearCMS\Addons\Addon $addon) use ($app) { $addon->initialize = function() use ($app) { - $context = $app->contexts->get(__FILE__); + $context = $app->contexts->get(__DIR__); $context->assets->addDir('assets');