From c96ade84631a2ae24d7335b50a2c8c5b1f48b242 Mon Sep 17 00:00:00 2001 From: Cesar G Date: Sat, 9 Nov 2013 23:08:56 -0800 Subject: [ticket/11508] Split parts of build_url() into reusable functions. PHPBB3-11508 --- phpBB/includes/functions.php | 100 +++++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 28 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 0cb88cd8ee..49b7293ed6 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2368,26 +2368,45 @@ function build_url($strip_vars = false) // Append SID $redirect = append_sid($page, false, false); - - // Add delimiter if not there... - if (strpos($redirect, '?') === false) - { - $redirect .= '?'; - } + $basic_parts = phpbb_get_url_parts($redirect, '&'); + $params = $basic_parts['params']; // Strip vars... - if ($strip_vars !== false && strpos($redirect, '?') !== false) + if ($strip_vars !== false) { if (!is_array($strip_vars)) { $strip_vars = array($strip_vars); } - $query = $_query = array(); + // Strip the vars off + foreach ($strip_vars as $strip) + { + if (isset($params[$strip])) + { + unset($params[$strip]); + } + } + } + + return $basic_parts['base'] . (($params) ? '?' . phpbb_glue_url_params($params) : ''); +} + +/** +* Get the base and parameters of a URL +* +* @param string $url URL to break apart +* @param string $separator Parameter separator. Defaults to & +* @return array Returns the base and parameters in the form of array('base' => string, 'params' => array(name => value)) +*/ +function phpbb_get_url_parts($url, $separator = '&') { + $params = array(); - $args = substr($redirect, strpos($redirect, '?') + 1); - $args = ($args) ? explode('&', $args) : array(); - $redirect = substr($redirect, 0, strpos($redirect, '?')); + if (strpos($url, '?') !== false) + { + $base = substr($url, 0, strpos($url, '?')); + $args = substr($url, strlen($base) + 1); + $args = ($args) ? explode($separator, $args) : array(); foreach ($args as $argument) { @@ -2400,29 +2419,54 @@ function build_url($strip_vars = false) continue; } - $query[$key] = implode('=', $arguments); + $params[$key] = implode('=', $arguments); } + } + else + { + $base = $url; + } - // Strip the vars off - foreach ($strip_vars as $strip) - { - if (isset($query[$strip])) - { - unset($query[$strip]); - } - } + return array( + 'base' => $base, + 'params' => $params, + ); +} - // Glue the remaining parts together... already urlencoded - foreach ($query as $key => $value) - { - $_query[] = $key . '=' . $value; - } - $query = implode('&', $_query); +/** +* Glue URL parameters together +* +* @param array $params URL parameters in the form of array(name => value) +* @return array Returns the glued string. +*/ +function phpbb_glue_url_params($params) { + $_params = array(); + + foreach ($params as $key => $value) + { + $_params[] = $key . '=' . $value; + } + return implode('&', $_params); +} + +/** +* Append parameters to an already built URL. +* +* @param array $new_params Parameters to add in the form of array(name => value) +* @return string Returns the new URL. +*/ +function phpbb_append_url_params($url, $new_params) { + $url_parts = phpbb_get_url_parts($url); + $params = array_merge($url_parts['params'], $new_params); - $redirect .= ($query) ? '?' . $query : ''; + // Move the sid to the end if it's set + if (isset($params['sid'])) { + $sid = $params['sid']; + unset($params['sid']); + $params['sid'] = $sid; } - return str_replace('&', '&', $redirect); + return $url_parts['base'] . '?' . phpbb_glue_url_params($params); } /** -- cgit v1.2.1 From ef4202d8590cdf3d88049329fe02864881369906 Mon Sep 17 00:00:00 2001 From: Cesar G Date: Sat, 9 Nov 2013 23:18:10 -0800 Subject: [ticket/11508] Build the jumpbox hidden fields using the _form action_ PHPBB3-11508 --- phpBB/includes/functions.php | 5 ++--- phpBB/includes/functions_content.php | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 49b7293ed6..aa09b90b35 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2437,7 +2437,7 @@ function phpbb_get_url_parts($url, $separator = '&') { * Glue URL parameters together * * @param array $params URL parameters in the form of array(name => value) -* @return array Returns the glued string. +* @return string Returns the glued string. */ function phpbb_glue_url_params($params) { $_params = array(); @@ -2452,6 +2452,7 @@ function phpbb_glue_url_params($params) { /** * Append parameters to an already built URL. * +* @param string $url URL to append parameters to * @param array $new_params Parameters to add in the form of array(name => value) * @return string Returns the new URL. */ @@ -4965,7 +4966,6 @@ function page_header($page_title = '', $display_online_list = false, $item_id = } } - $hidden_fields_for_jumpbox = phpbb_build_hidden_fields_for_query_params($request, array('f')); $notification_mark_hash = generate_link_hash('mark_all_notifications_read'); // The following assigns all _common_ variables that may be used at any point in a template. @@ -4982,7 +4982,6 @@ function page_header($page_title = '', $display_online_list = false, $item_id = 'LOGGED_IN_USER_LIST' => $online_userlist, 'RECORD_USERS' => $l_online_record, 'PRIVATE_MESSAGE_COUNT' => (!empty($user->data['user_unread_privmsg'])) ? $user->data['user_unread_privmsg'] : 0, - 'HIDDEN_FIELDS_FOR_JUMPBOX' => $hidden_fields_for_jumpbox, 'UNREAD_NOTIFICATIONS_COUNT' => ($notifications !== false) ? $notifications['unread_count'] : '', 'NOTIFICATIONS_COUNT' => ($notifications !== false) ? $notifications['unread_count'] : '', diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index b1f69c5756..387695a9e8 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -195,11 +195,13 @@ function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list } $db->sql_freeresult($result); unset($padding_store); + $url_parts = phpbb_get_url_parts($action); $template->assign_vars(array( - 'S_DISPLAY_JUMPBOX' => $display_jumpbox, - 'S_JUMPBOX_ACTION' => $action) - ); + 'S_DISPLAY_JUMPBOX' => $display_jumpbox, + 'S_JUMPBOX_ACTION' => $action, + 'HIDDEN_FIELDS_FOR_JUMPBOX' => build_hidden_fields($url_parts['params']), + )); return; } -- cgit v1.2.1 From e1c6b40ee82a79a58276d8e85de9c3593a6f3703 Mon Sep 17 00:00:00 2001 From: Cesar G Date: Mon, 11 Nov 2013 15:45:02 -0800 Subject: [ticket/11508] Move the stripping param code to separate function as well. PHPBB3-11508 --- phpBB/includes/functions.php | 56 +++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 19 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index aa09b90b35..71716ae6b3 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2368,28 +2368,13 @@ function build_url($strip_vars = false) // Append SID $redirect = append_sid($page, false, false); - $basic_parts = phpbb_get_url_parts($redirect, '&'); - $params = $basic_parts['params']; - // Strip vars... if ($strip_vars !== false) { - if (!is_array($strip_vars)) - { - $strip_vars = array($strip_vars); - } - - // Strip the vars off - foreach ($strip_vars as $strip) - { - if (isset($params[$strip])) - { - unset($params[$strip]); - } - } + $redirect = phpbb_strip_url_params($redirect, $strip_vars, '&'); } - return $basic_parts['base'] . (($params) ? '?' . phpbb_glue_url_params($params) : ''); + return $redirect; } /** @@ -2449,15 +2434,48 @@ function phpbb_glue_url_params($params) { return implode('&', $_params); } +/** +* Strip parameters from an already built URL. +* +* @param string $url URL to strip parameters from +* @param array|string $strip Parameters to strip. +* @param string $separator Parameter separator. Defaults to & +* @return string Returns the new URL. +*/ +function phpbb_strip_url_params($url, $strip, $separator = '&') { + $url_parts = phpbb_get_url_parts($url, $separator); + $params = $url_parts['params']; + + if (!is_array($strip)) + { + $strip = array($strip); + } + + if (!empty($params)) + { + // Strip the parameters off + foreach ($strip as $param) + { + if (isset($params[$param])) + { + unset($params[$param]); + } + } + } + + return $url_parts['base'] . (($params) ? '?' . phpbb_glue_url_params($params) : ''); +} + /** * Append parameters to an already built URL. * * @param string $url URL to append parameters to * @param array $new_params Parameters to add in the form of array(name => value) +* @param string $separator Parameter separator. Defaults to & * @return string Returns the new URL. */ -function phpbb_append_url_params($url, $new_params) { - $url_parts = phpbb_get_url_parts($url); +function phpbb_append_url_params($url, $new_params, $separator = '&') { + $url_parts = phpbb_get_url_parts($url, $separator); $params = array_merge($url_parts['params'], $new_params); // Move the sid to the end if it's set -- cgit v1.2.1 From 3163388f63a6d0bac77ada86e2f1561f1d7f9714 Mon Sep 17 00:00:00 2001 From: Cesar G Date: Fri, 6 Dec 2013 13:38:54 -0800 Subject: [ticket/11508] Move helper functions to path_helper class. PHPBB3-11508 --- phpBB/includes/functions.php | 123 ++--------------------------------- phpBB/includes/functions_content.php | 6 +- 2 files changed, 11 insertions(+), 118 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 71716ae6b3..9c77aa935c 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2344,8 +2344,10 @@ function reapply_sid($url) */ function build_url($strip_vars = false) { - global $config, $user, $phpEx, $phpbb_root_path; + global $config, $user, $phpbb_container; + $path_helper = $phpbb_container->get('path_helper'); + $php_ext = $path_helper->get_php_ext(); $page = $user->page['page']; // We need to be cautious here. @@ -2358,12 +2360,12 @@ function build_url($strip_vars = false) if ($url_parts === false || empty($url_parts['scheme']) || empty($url_parts['host'])) { // Remove 'app.php/' from the page, when rewrite is enabled - if ($config['enable_mod_rewrite'] && strpos($page, 'app.' . $phpEx . '/') === 0) + if ($config['enable_mod_rewrite'] && strpos($page, 'app.' . $php_ext . '/') === 0) { - $page = substr($page, strlen('app.' . $phpEx . '/')); + $page = substr($page, strlen('app.' . $php_ext . '/')); } - $page = $phpbb_root_path . $page; + $page = $path_helper->get_phpbb_root_path() . $page; } // Append SID @@ -2371,123 +2373,12 @@ function build_url($strip_vars = false) if ($strip_vars !== false) { - $redirect = phpbb_strip_url_params($redirect, $strip_vars, '&'); + $redirect = $path_helper->strip_url_params($redirect, $strip_vars, '&'); } return $redirect; } -/** -* Get the base and parameters of a URL -* -* @param string $url URL to break apart -* @param string $separator Parameter separator. Defaults to & -* @return array Returns the base and parameters in the form of array('base' => string, 'params' => array(name => value)) -*/ -function phpbb_get_url_parts($url, $separator = '&') { - $params = array(); - - if (strpos($url, '?') !== false) - { - $base = substr($url, 0, strpos($url, '?')); - $args = substr($url, strlen($base) + 1); - $args = ($args) ? explode($separator, $args) : array(); - - foreach ($args as $argument) - { - $arguments = explode('=', $argument); - $key = $arguments[0]; - unset($arguments[0]); - - if ($key === '') - { - continue; - } - - $params[$key] = implode('=', $arguments); - } - } - else - { - $base = $url; - } - - return array( - 'base' => $base, - 'params' => $params, - ); -} - -/** -* Glue URL parameters together -* -* @param array $params URL parameters in the form of array(name => value) -* @return string Returns the glued string. -*/ -function phpbb_glue_url_params($params) { - $_params = array(); - - foreach ($params as $key => $value) - { - $_params[] = $key . '=' . $value; - } - return implode('&', $_params); -} - -/** -* Strip parameters from an already built URL. -* -* @param string $url URL to strip parameters from -* @param array|string $strip Parameters to strip. -* @param string $separator Parameter separator. Defaults to & -* @return string Returns the new URL. -*/ -function phpbb_strip_url_params($url, $strip, $separator = '&') { - $url_parts = phpbb_get_url_parts($url, $separator); - $params = $url_parts['params']; - - if (!is_array($strip)) - { - $strip = array($strip); - } - - if (!empty($params)) - { - // Strip the parameters off - foreach ($strip as $param) - { - if (isset($params[$param])) - { - unset($params[$param]); - } - } - } - - return $url_parts['base'] . (($params) ? '?' . phpbb_glue_url_params($params) : ''); -} - -/** -* Append parameters to an already built URL. -* -* @param string $url URL to append parameters to -* @param array $new_params Parameters to add in the form of array(name => value) -* @param string $separator Parameter separator. Defaults to & -* @return string Returns the new URL. -*/ -function phpbb_append_url_params($url, $new_params, $separator = '&') { - $url_parts = phpbb_get_url_parts($url, $separator); - $params = array_merge($url_parts['params'], $new_params); - - // Move the sid to the end if it's set - if (isset($params['sid'])) { - $sid = $params['sid']; - unset($params['sid']); - $params['sid'] = $sid; - } - - return $url_parts['base'] . '?' . phpbb_glue_url_params($params); -} - /** * Meta refresh assignment * Adds META template variable with meta http tag. diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index 387695a9e8..ff75a5b05d 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -110,7 +110,7 @@ function gen_sort_selects(&$limit_days, &$sort_by_text, &$sort_days, &$sort_key, */ function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list = false, $force_display = false) { - global $config, $auth, $template, $user, $db; + global $config, $auth, $template, $user, $db, $phpbb_container; // We only return if the jumpbox is not forced to be displayed (in case it is needed for functionality) if (!$config['load_jumpbox'] && $force_display === false) @@ -195,7 +195,9 @@ function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list } $db->sql_freeresult($result); unset($padding_store); - $url_parts = phpbb_get_url_parts($action); + + $path_helper = $phpbb_container->get('path_helper'); + $url_parts = $path_helper->get_url_parts($action); $template->assign_vars(array( 'S_DISPLAY_JUMPBOX' => $display_jumpbox, -- cgit v1.2.1 From 8987fc95f97e2e64915fcbe809a7e14051b8a328 Mon Sep 17 00:00:00 2001 From: Cesar G Date: Fri, 7 Feb 2014 09:20:49 -0800 Subject: [ticket/11508] Change separator parameter to a simple true|false $is_amp. PHPBB3-11508 --- phpBB/includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 9c77aa935c..2062cd062c 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2373,7 +2373,7 @@ function build_url($strip_vars = false) if ($strip_vars !== false) { - $redirect = $path_helper->strip_url_params($redirect, $strip_vars, '&'); + $redirect = $path_helper->strip_url_params($redirect, $strip_vars, false); } return $redirect; -- cgit v1.2.1 From 5e0ab146d9f592c62661ccd495ce096701b8208d Mon Sep 17 00:00:00 2001 From: Cesar G Date: Thu, 27 Feb 2014 23:34:01 -0800 Subject: [ticket/11508] The question mark is expected even if there are no parameters. PHPBB3-11508 --- phpBB/includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 2062cd062c..17cc109432 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2376,7 +2376,7 @@ function build_url($strip_vars = false) $redirect = $path_helper->strip_url_params($redirect, $strip_vars, false); } - return $redirect; + return $redirect . ((strpos($redirect, '?') === false) ? '?' : ''); } /** -- cgit v1.2.1 From c2ac76e8b11c0cfb5917febe90f56216d74bc10a Mon Sep 17 00:00:00 2001 From: Cesar G Date: Tue, 22 Apr 2014 12:40:28 -0700 Subject: [ticket/11508] Use $phpbb_path_helper. PHPBB3-11508 --- phpBB/includes/functions.php | 9 ++++----- phpBB/includes/functions_content.php | 5 ++--- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 17cc109432..6e5f6896a6 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2344,10 +2344,9 @@ function reapply_sid($url) */ function build_url($strip_vars = false) { - global $config, $user, $phpbb_container; + global $config, $user, $phpbb_path_helper; - $path_helper = $phpbb_container->get('path_helper'); - $php_ext = $path_helper->get_php_ext(); + $php_ext = $phpbb_path_helper->get_php_ext(); $page = $user->page['page']; // We need to be cautious here. @@ -2365,7 +2364,7 @@ function build_url($strip_vars = false) $page = substr($page, strlen('app.' . $php_ext . '/')); } - $page = $path_helper->get_phpbb_root_path() . $page; + $page = $phpbb_path_helper->get_phpbb_root_path() . $page; } // Append SID @@ -2373,7 +2372,7 @@ function build_url($strip_vars = false) if ($strip_vars !== false) { - $redirect = $path_helper->strip_url_params($redirect, $strip_vars, false); + $redirect = $phpbb_path_helper->strip_url_params($redirect, $strip_vars, false); } return $redirect . ((strpos($redirect, '?') === false) ? '?' : ''); diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index ff75a5b05d..b8314c076c 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -110,7 +110,7 @@ function gen_sort_selects(&$limit_days, &$sort_by_text, &$sort_days, &$sort_key, */ function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list = false, $force_display = false) { - global $config, $auth, $template, $user, $db, $phpbb_container; + global $config, $auth, $template, $user, $db, $phpbb_path_helper; // We only return if the jumpbox is not forced to be displayed (in case it is needed for functionality) if (!$config['load_jumpbox'] && $force_display === false) @@ -196,8 +196,7 @@ function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list $db->sql_freeresult($result); unset($padding_store); - $path_helper = $phpbb_container->get('path_helper'); - $url_parts = $path_helper->get_url_parts($action); + $url_parts = $phpbb_path_helper->get_url_parts($action); $template->assign_vars(array( 'S_DISPLAY_JUMPBOX' => $display_jumpbox, -- cgit v1.2.1