1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-19 20:21:51 +02:00

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
This commit is contained in:
Nick Liu 2018-04-30 06:24:20 -05:00
parent fa08c915a3
commit 0d7f7dc543
No known key found for this signature in database
GPG Key ID: 1167C5F9C9897637
5 changed files with 76 additions and 26 deletions

View File

@ -0,0 +1,52 @@
<?php
require_once('SecureLinkDecorator.php');
class NginxSecureLinkMd5Decorator implements SecureLinkDecorator
{
protected $url = null;
protected $prefs = array();
const SUPPORTED_VARIABLES = array(
'$secure_link_expires',
'$uri',
'$remote_addr'
);
static function supported_variables() {
return self::SUPPORTED_VARIABLES;
}
function __construct($url, $preferences)
{
$this->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)));
}
}

View File

@ -0,0 +1,6 @@
<?php
interface SecureLinkDecorator
{
public function decorate();
}

View File

@ -2113,6 +2113,14 @@ $columnInfo = array(
function show_download_options()
{
global $pref, $ns;
require_once(__DIR__."/../handlers/NginxSecureLinkMd5Decorator.php");
$supported_secure_link_variables_html = "<ul>";
foreach(NginxSecureLinkMd5Decorator::supported_variables() as $variable)
{
$supported_secure_link_variables_html .= "<li><code>$variable</code></li>";
}
$supported_secure_link_variables_html .= "</ul>";
require_once(e_HANDLER."form_handler.php");
$frm = new e_form(true); //enable inner tabindex counter
@ -2263,6 +2271,12 @@ $columnInfo = array(
<td>
".$frm->text('download_security_expression', $pref['download_security_expression'], 1024)."
<div class='field-help'>".LAN_DL_SECURITY_NGINX_SECURELINKMD5_EXPRESSION_HELP."</div>
<small><a href='#' onclick='event.preventDefault();$(\"#supported-nginx-variables\").toggle();this.blur()'>
".LAN_DL_SECURITY_NGINX_SUPPORTED_VARIABLES_TOGGLE."
</a></small>
<div id='supported-nginx-variables' style='display:none'>
".$supported_secure_link_variables_html."
</div>
</td>
</tr>
<tr>

View File

@ -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",
"<a target='_blank' href='https://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link_md5'>NGINX secure_link_md5 expression</a>");
define("LAN_DL_SECURITY_NGINX_SECURELINKMD5_EXPRESSION_HELP", "Same expression as configured on the server");

View File

@ -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();
}