From 0d7f7dc543d6c15fbaaf86284e85c796d8f4de8c Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Mon, 30 Apr 2018 06:24:20 -0500 Subject: [PATCH] Improved: Downloads >> Preferences >> Protection - MOD: Refactored NGINX secure_link_md5 decorator into interface and class in Downloads plugin - NEW: Downloads plugin: Admin preferences UX improvement: Preferences >> Protection now shows the user a list of supported NGINX variables pulled from NginxSecureLinkMd5Decorator --- .../handlers/NginxSecureLinkMd5Decorator.php | 52 +++++++++++++++++++ .../download/handlers/SecureLinkDecorator.php | 6 +++ e107_plugins/download/includes/admin.php | 14 +++++ .../languages/English/English_admin.php | 1 + e107_plugins/download/request.php | 29 ++--------- 5 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 e107_plugins/download/handlers/NginxSecureLinkMd5Decorator.php create mode 100644 e107_plugins/download/handlers/SecureLinkDecorator.php diff --git a/e107_plugins/download/handlers/NginxSecureLinkMd5Decorator.php b/e107_plugins/download/handlers/NginxSecureLinkMd5Decorator.php new file mode 100644 index 000000000..7ce9ffcca --- /dev/null +++ b/e107_plugins/download/handlers/NginxSecureLinkMd5Decorator.php @@ -0,0 +1,52 @@ +url = $url; + $this->prefs = $preferences; + } + + public function decorate() + { + $prefs = $this->prefs; + $url = $this->url; + $expiry = intval($prefs['download_security_link_expiry']); + if ($expiry <= 0) + $expiry = PHP_INT_MAX; + else + $expiry = time() + $expiry; + $url_parts = parse_url($url); + $evaluation = str_replace( + self::supported_variables(), + array( + $expiry, + $url_parts['path'], + $_SERVER['REMOTE_ADDR'] + ), + $prefs['download_security_expression'] + ); + $query_string = $url_parts['query']; + parse_str($query_string, $query_args); + $query_args['md5'] = md5($evaluation); + if (strpos($prefs['download_security_expression'], '$secure_link_expires') !== false) + $query_args['expires'] = $expiry; + require_once(__DIR__.'/../includes/shim_http_build_url.php'); + return http_build_url($url_parts, array('query' => http_build_query($query_args))); + } +} \ No newline at end of file diff --git a/e107_plugins/download/handlers/SecureLinkDecorator.php b/e107_plugins/download/handlers/SecureLinkDecorator.php new file mode 100644 index 000000000..0dd48ab93 --- /dev/null +++ b/e107_plugins/download/handlers/SecureLinkDecorator.php @@ -0,0 +1,6 @@ +"; + foreach(NginxSecureLinkMd5Decorator::supported_variables() as $variable) + { + $supported_secure_link_variables_html .= "
  • $variable
  • "; + } + $supported_secure_link_variables_html .= ""; require_once(e_HANDLER."form_handler.php"); $frm = new e_form(true); //enable inner tabindex counter @@ -2263,6 +2271,12 @@ $columnInfo = array( ".$frm->text('download_security_expression', $pref['download_security_expression'], 1024)."
    ".LAN_DL_SECURITY_NGINX_SECURELINKMD5_EXPRESSION_HELP."
    + + ".LAN_DL_SECURITY_NGINX_SUPPORTED_VARIABLES_TOGGLE." + + diff --git a/e107_plugins/download/languages/English/English_admin.php b/e107_plugins/download/languages/English/English_admin.php index 0a63c15ea..d3a6d1d33 100644 --- a/e107_plugins/download/languages/English/English_admin.php +++ b/e107_plugins/download/languages/English/English_admin.php @@ -235,6 +235,7 @@ define("LAN_DL_SECURITY_DESCRIPTION", "Downloads can make use of server-side URL define("LAN_DL_SECURITY_MODE", "URL protection mode"); define("LAN_DL_SECURITY_MODE_NONE", "None (Default)"); define("LAN_DL_SECURITY_MODE_NGINX_SECURELINKMD5", "NGINX secure_link_md5"); +define("LAN_DL_SECURITY_NGINX_SUPPORTED_VARIABLES_TOGGLE", "Click to toggle list of supported NGINX variables"); define("LAN_DL_SECURITY_NGINX_SECURELINKMD5_EXPRESSION", "NGINX secure_link_md5 expression"); define("LAN_DL_SECURITY_NGINX_SECURELINKMD5_EXPRESSION_HELP", "Same expression as configured on the server"); diff --git a/e107_plugins/download/request.php b/e107_plugins/download/request.php index 346eae424..afe4de188 100644 --- a/e107_plugins/download/request.php +++ b/e107_plugins/download/request.php @@ -440,30 +440,7 @@ function decorate_download_location($url) $pref = e107::getPref(); if ($pref['download_security_mode'] !== 'nginx-secure_link_md5') return $url; - $expiry = intval($pref['download_security_link_expiry']); - if ($expiry <= 0) - $expiry = PHP_INT_MAX; - else - $expiry = time() + $expiry; - $url_parts = parse_url($url); - $evaluation = str_replace( - array( - '$secure_link_expires', - '$uri', - '$remote_addr' - ), - array( - $expiry, - $url_parts['path'], - $_SERVER['REMOTE_ADDR'] - ), - $pref['download_security_expression'] - ); - $query_string = $url_parts['query']; - parse_str($query_string, $query_args); - $query_args['md5'] = md5($evaluation); - if (strpos($pref['download_security_expression'], '$secure_link_expires') !== false) - $query_args['expires'] = $expiry; - require_once(__DIR__.'/includes/shim_http_build_url.php'); - return http_build_url($url_parts, array('query' => http_build_query($query_args))); + require_once(__DIR__."/handlers/NginxSecureLinkMd5Decorator.php"); + $decorator = new NginxSecureLinkMd5Decorator($url, $pref); + return $decorator->decorate(); } \ No newline at end of file