diff --git a/includes/functions.php b/includes/functions.php index 055f436..ca30db1 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,105 +1,133 @@ . function license($code) { return Licenses::instance()->get($code); } function request_uri() { return URL_protocol() . URL_domain() . ROOT . $_SERVER['REQUEST_URI']; } /** * Generate a stupid MySQL COALESCE() clause to select the most appropriate i18n database column * * @param string $name Base virtual name of the column * @param mixed $i18n Associative array of languages and column names, or a generic column name like 'column_%s' * @return string */ function i18n_coalesce( $name, $i18n ) { // current language (e.g. 'en') $lang = latest_language()->getISO(); // default language of the website $default = 'it'; // allow a generic string if( is_string( $i18n ) ) { $generic = $i18n; $i18n = []; $i18n[ $lang ] = sprintf( $generic, $lang ); $i18n[ $default ] = sprintf( $generic, $default ); } // base priority column name $base_priority = $i18n[ $default ]; // try to catch the more specific column, then the default if( $lang !== $default && isset( $i18n[ $lang ] ) ) { $more_priority = $i18n[ $lang ]; return "COALESCE( `$more_priority`, `$base_priority` ) AS $name"; } // just pick the default return "`$base_priority` AS $name"; } /** * Eventually redirect the request to this permalink * * It's allowed the 'lang' parameter to be present or not. * * @param string $permalink URL of the incoming request (absolute or relative) * @return string */ function force_permalink( $permalink ) { // incoming URL $from = BASE_URL . $_SERVER['REQUEST_URI']; // force absolute $permalink = site_page( $permalink, true ); // if it's the same request, do nothing if( $from === $permalink ) { return; } // do not send a 301 Moved Permanently if the language was missing (maybe the user was just redirected to his language) $status = isset( $_GET['l'] ) ? 301 // 301 Moved Permanently : 300; // 300 Multiple Choices // redirect to the correct location http_redirect( $permalink, $status ); } /** * Force an URL to have the language argument it it requested the page with it * * @return string */ function keep_url_in_language( $url ) { $code = latest_language()->getCode(); $url = http_build_get_query( $url, [ 'l' => latest_language()->getISO(), ] ); return $url; } + +/** + * Replace parameters in GET string + * + * @param string $url The entire URL (no relative URLs they confuse parse_url) + * @param array $parameters Associative array, with key = paramter, value = value or null to remove it + * @return string the new query part of the URL with "?" already placed in front, or an empty string + */ +function replace_url_args( $url, $parameters ) { + $parsed = parse_url($url); + if( isset( $parsed["query"] ) ) { + parse_str( $parsed["query"], $query ); + } else { + $query = []; + } + foreach( $parameters as $k => $v ) { + if( $v === null ) { + unset( $query[ $k ] ); + } else { + $query[ $k ] = $v; + } + } + if ( count($query) > 0 ) { + return '?' . http_build_query($query); + } else { + return ''; + } +}