1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-03 21:27:25 +02:00

Merge pull request #4067 from Deltik/fix-4054

Compliant query string ampersands in e_url
This commit is contained in:
Nick L
2020-01-14 10:17:02 +01:00
committed by GitHub
4 changed files with 1097 additions and 1104 deletions

View File

@@ -3403,17 +3403,17 @@ class e107
* @param bool $options['legacy'] When true legacy urls will be generated regardless of mod-rewrite status.
* @return string
*/
public static function url($plugin='', $key=null, $row=array(), $options = array())
public static function url($plugin = '', $key = null, $row = array(), $options = array())
{
/* backward compat - core keys. ie. news/xxx/xxx user/xxx/xxx etc, */
$legacy = array('news','page','search','user','download','gallery');
$legacy = array('news', 'page', 'search', 'user', 'download', 'gallery');
if(strpos($plugin,'/')!==false)
if (strpos($plugin, '/') !== false)
{
$tmp = explode("/",$plugin,2);
$tmp = explode("/", $plugin, 2);
if(in_array($tmp[0], $legacy))
if (in_array($tmp[0], $legacy))
{
return self::getUrl()->create($plugin, $key, $row);
}
@@ -3424,10 +3424,10 @@ class e107
$key = $tmp[1];
}
if(!$tmp = self::getRegistry('core/e107/addons/e_url'))
if (!$tmp = self::getRegistry('core/e107/addons/e_url'))
{
$tmp = self::getUrlConfig();
self::setRegistry('core/e107/addons/e_url',$tmp);
self::setRegistry('core/e107/addons/e_url', $tmp);
}
$tp = self::getParser();
@@ -3437,7 +3437,7 @@ class e107
$rootNamespace = self::getPref('url_main_module');
if(is_string($options)) // backwards compat.
if (is_string($options)) // backwards compat.
{
$options = array(
'mode' => $options,
@@ -3451,20 +3451,24 @@ class e107
'query' => array(),
);
if(isset($options['fragment']) && $options['fragment'] !== '')
if (isset($options['fragment']) && $options['fragment'] !== '')
{
$options['fragment'] = '#' . $options['fragment'];
}
if(!empty($tmp[$plugin][$key]['sef']))
if (!empty($plugin) && empty($tmp[$plugin][$key]['sef']))
{
if(!empty($tmp[$plugin][$key]['alias']))
self::getMessage()->addDebug("e_url.php in <b>" . e_PLUGIN . $plugin . "</b> is missing the key: <b>" . $key . "</b>. Or, you may need to <a href='" . e_ADMIN . "db.php?mode=plugin_scan'>scan your plugin directories</a> to register e_url.php");
return false;
}
if (!empty($tmp[$plugin][$key]['alias']))
{
$alias = (!empty($pref[e_LAN][$plugin][$key])) ? $pref[e_LAN][$plugin][$key] : $tmp[$plugin][$key]['alias'];
if(!empty($rootNamespace) && $rootNamespace === $plugin)
if (!empty($rootNamespace) && $rootNamespace === $plugin)
{
$replaceAlias = array('{alias}\/','{alias}/');
$replaceAlias = array('{alias}\/', '{alias}/');
$tmp[$plugin][$key]['sef'] = str_replace($replaceAlias, '', $tmp[$plugin][$key]['sef']);
}
else
@@ -3475,51 +3479,42 @@ class e107
}
preg_match_all('#{([a-z_]*)}#', $tmp[$plugin][$key]['sef'],$matches);
preg_match_all('#{([a-z_]*)}#', $tmp[$plugin][$key]['sef'], $matches);
$active = true;
foreach($matches[1] as $k=>$v) // check if a field value is missing, if so, revent to legacy url.
foreach ($matches[1] as $k => $v) // check if a field value is missing, if so, revert to legacy url.
{
if(!isset($row[$v]))
if (!isset($row[$v]))
{
self::getMessage()->addDebug("Missing value for ".$v." in ".$plugin."/e_url.php - '".$key."'");
self::getMessage()->addDebug("Missing value for " . $v . " in " . $plugin . "/e_url.php - '" . $key . "'");
$active = false;
break;
}
}
if(empty($sefActive[$plugin])) // SEF disabled.
if (empty($sefActive[$plugin])) // SEF disabled.
{
self::getDebug()->log('SEF URL for <b>'.$plugin.'</b> disabled.');
self::getDebug()->log('SEF URL for <b>' . $plugin . '</b> disabled.');
$active = false;
}
if(deftrue('e_MOD_REWRITE') && ($active == true) && empty($options['legacy'])) // Search-Engine-Friendly URLs active.
if (deftrue('e_MOD_REWRITE') && ($active == true) && empty($options['legacy'])) // Search-Engine-Friendly URLs active.
{
$rawUrl = $tp->simpleParse($tmp[$plugin][$key]['sef'], $row);
if($options['mode'] === 'full')
if ($options['mode'] === 'full')
{
$sefUrl = SITEURL.$rawUrl;
$sefUrl = SITEURL . $rawUrl;
}
elseif($options['mode'] === 'raw')
elseif ($options['mode'] === 'raw')
{
$sefUrl = $rawUrl;
}
else
{
$sefUrl = e_HTTP.$rawUrl;
$sefUrl = e_HTTP . $rawUrl;
}
// Append the query.
if (is_array($options['query']) && !empty($options['query'])) {
$sefUrl .= (strpos($sefUrl, '?') !== FALSE ? '&' : '?') . self::httpBuildQuery($options['query']);
}
return $sefUrl . $options['fragment'];
}
else // Legacy URL.
{
@@ -3527,15 +3522,15 @@ class e107
$srch = array();
$repl = array();
foreach($matches[0] as $k=>$val)
foreach ($matches[0] as $k => $val)
{
$srch[] = '$'.($k+1);
$srch[] = '$' . ($k + 1);
$repl[] = $val;
}
$template = isset($tmp[$plugin][$key]['legacy']) ? $tmp[$plugin][$key]['legacy'] : $tmp[$plugin][$key]['redirect'];
$urlTemplate = str_replace($srch,$repl, $template);
$urlTemplate = str_replace($srch, $repl, $template);
$urlTemplate = $tp->replaceConstants($urlTemplate, $options['mode']);
$legacyUrl = $tp->simpleParse($urlTemplate, $row);
@@ -3544,9 +3539,9 @@ class e107
// Avoid duplicate query keys. eg. URL has ?id=x and $options['query']['id'] exists.
// @see forum/e_url.php - topic/redirect and forum/view_shortcodes.php sc_post_url()
list($legacyUrl,$tmp) = explode("?",$legacyUrl);
list($legacyUrl, $tmp) = explode("?", $legacyUrl);
if(!empty($tmp))
if (!empty($tmp))
{
if (strpos($tmp, '=') === false)
{
@@ -3558,11 +3553,11 @@ class e107
else
{
parse_str($tmp,$qry);
parse_str($tmp, $qry);
foreach($qry as $k=>$v)
foreach ($qry as $k => $v)
{
if(!isset($options['query'][$k])) // $options['query'] overrides any in the original URL.
if (!isset($options['query'][$k])) // $options['query'] overrides any in the original URL.
{
$options['query'][$k] = $v;
}
@@ -3570,35 +3565,16 @@ class e107
}
}
$sefUrl = $legacyUrl;
}
// Append the query.
if (is_array($options['query']) && !empty($options['query']))
{
$legacyUrl .= (strpos($legacyUrl, '?') !== FALSE ? '&' : '?') . self::httpBuildQuery($options['query']);
$sefUrl .= (strpos($sefUrl, '?') !== FALSE ? '&amp;' : '?') . self::httpBuildQuery($options['query']);
}
return $legacyUrl . $options['fragment'];
}
}
if(!empty($plugin))
{
self::getMessage()->addDebug("e_url.php in <b>".e_PLUGIN.$plugin."</b> is missing the key: <b>".$key."</b>. Or, you may need to <a href='".e_ADMIN."db.php?mode=plugin_scan'>scan your plugin directories</a> to register e_url.php");
}
return false;
/*
elseif(varset($tmp[$plugin][$key]['redirect']))
{
return self::getParser()->replaceConstants($tmp[$plugin][$key]['redirect'],'full');
}
return;
*/
return $sefUrl . $options['fragment'];
}
@@ -3643,7 +3619,7 @@ class e107
* rawurlencode() (instead of urlencode()) all query parameters.
* @param array $query The query parameter array to be processed, e.g. $_GET.
* @param string $parent Internal use only. Used to build the $query array key for nested items.
* @return array A rawurlencoded string which can be used as or appended to the URL query string.
* @return string A rawurlencoded string which can be used as or appended to the URL query string.
*/
public static function httpBuildQuery(array $query, $parent = '')
{
@@ -3670,7 +3646,7 @@ class e107
}
}
return implode('&', $params);
return implode('&amp;', $params);
}

View File

@@ -140,6 +140,7 @@ class e_plugin
{
$this->_installed = array();
$this->_addons = array();
e107::setRegistry('core/e107/addons/e_url');
$this->_init(true);
$this->_initIDs();

View File

@@ -1,5 +1,5 @@
<?php
/**
/**
* e107 website system
*
* Copyright (C) 2008-2018 e107 Inc (e107.org)
@@ -9,8 +9,8 @@
*/
class e107Test extends \Codeception\Test\Unit
{
class e107Test extends \Codeception\Test\Unit
{
/** @var e107 */
private $e107;
@@ -49,7 +49,7 @@
}
/*
/*
public function testInitInstall()
{
$res = null;
@@ -295,7 +295,7 @@
$this->assertTrue($res);
}
/*
/*
public function testGetSession()
{
$res = null;
@@ -685,7 +685,7 @@
$res = null;
$this->assertTrue($res);
}
*/
*/
/**
* This test checks getTemplate() use on loading between the core download plugin template and the _blank theme download template
*/
@@ -717,7 +717,7 @@
}
/*
/*
public function testTemplateWrapper()
{
$res = null;
@@ -783,7 +783,7 @@
$res = null;
$this->assertTrue($res);
}
*/
*/
public function testUrl()
{
$obj = $this->e107;
@@ -791,12 +791,27 @@
$result = $obj::url('news','index', array(), array('mode'=>'full'));
$this->assertEquals("https://localhost/e107/news", $result);
// var_dump(SITEURL);
// $this->assertTrue($res);
}
/*
/**
* @see https://github.com/e107inc/e107/issues/4054
*/
public function testUrlOptionQueryHasCompliantAmpersand()
{
$e107 = $this->e107;
$e107::getPlugin()->install('forum');
$url = $e107::url('forum', 'topic', [], array(
'query' => array(
'f' => 'post',
'id' => 123
),
));
$this->assertEquals(
e_PLUGIN_ABS. 'forum/forum_viewtopic.php?f=post&amp;id=123',
$url, "Generated href does not match expectation"
);
}
/*
public function testRedirect()
{
$res = null;
@@ -838,7 +853,7 @@
$res = null;
$this->assertTrue($res);
}
*/
*/
public function testBase64DecodeOnAjaxURL()
{
@@ -866,7 +881,7 @@
// $res = null;
// $this->assertTrue($res);
}
/*
/*
public function testSet_base_path()
{
$res = null;
@@ -962,7 +977,7 @@
$res = null;
$this->assertTrue($res);
}
*/
*/
public function testIsInstalled()
{
$obj = $this->e107;
@@ -977,7 +992,7 @@
// var_dump($result);
$this->assertTrue($result);
}
/*
/*
public function testIni_set()
{
$res = null;
@@ -1015,5 +1030,5 @@
}
*/
}
*/
}

View File

@@ -227,6 +227,7 @@
public function testGetFields()
{
e107::getPlugin()->uninstall('forum');
$result = $this->ep->clearCache()->load('forum')->getFields(true);
// print_r($result);