diff --git a/includes/functions.php b/includes/functions.php index 2ad7784..44e3a88 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,60 +1,101 @@ . 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; + } + + // redirect to the correct location + http_redirect( $permalink, 303 ); +} + +/** + * Force an URL to have the language argument it it requested the page with it + * + * @return string + */ +function keep_url_in_language( $url, $force = false ) { + + if( isset( $_GET['l'] ) || $force ) { + $url = http_build_get_query( $url, [ + 'l' => latest_language()->getCode(), + ] ); + } + + return $url; +} diff --git a/includes/load-post.php b/includes/load-post.php index 847b686..a8f6a42 100644 --- a/includes/load-post.php +++ b/includes/load-post.php @@ -1,110 +1,104 @@ . // Die if called directly defined('BOZ_PHP') or exit; // Custom Sessionuser class define('SESSIONUSER_CLASS', 'User'); define('INCLUDES', 'includes'); // Autoload classes spl_autoload_register( function($c) { $path = ABSPATH . __ . INCLUDES . __ . "class-$c.php"; if( is_file( $path ) ) { require $path; } } ); // One day I will push these 2 functions in Boz-PHP require ABSPATH . '/includes/functions.php'; // user permissions register_permissions( 'user', [] ); // admin permissions inherit_permissions( 'admin', 'user', [ 'add-event', 'edit-users', 'edit-events', ] ); define_default( 'LATEST_CONFERENCE_UID', '2019' ); define_default( 'CURRENT_CONFERENCE_UID', LATEST_CONFERENCE_UID ); define_default( 'CONTACT_PHONE_PREFIX', '+39' ); define_default( 'REPO', 'https://github.com/LinuxDayTorino/LinuxDay-Torino-website' ); define_default( 'LIBMARKDOWN_PATH', '/usr/share/php/markdown.php' ); define_default( 'JQUERY', '/javascript/jquery/jquery.min.js' ); define_default( 'LEAFLET_DIR', '/javascript/leaflet' ); define_default( 'NOINDEX', false ); // /{conference_uid}/ define_default( 'PERMALINK_CONFERENCE', '%s/' ); // /{conference_uid}/{$chapter_uid}/{event_uid} define_default( 'PERMALINK_EVENT', '%1$s/%3$s/%2$s' ); // /2016/{user_uid} define_default( 'PERMALINK_USER', '%1$s/user/%2$s' ); define_default( 'FORCE_PERMALINK', 1 ); // timezone of database dates define_default( 'DEFAULT_TIMEZONE', 'Europe/Rome' ); register_js( 'jquery', JQUERY ); register_js( 'leaflet', LEAFLET_DIR . '/leaflet.js' ); register_css( 'leaflet', LEAFLET_DIR . '/leaflet.css' ); register_js('typed', ROOT . '/2017/static/typed/typed.min.js'); // GNU Gettext configuration define( 'GETTEXT_DOMAIN', 'linuxday' ); define( 'GETTEXT_DIRECTORY', ABSPATH . __ . 'l10n' ); define( 'GETTEXT_DEFAULT_ENCODE', 'utf8' ); // register languages register_language( 'en_US', ['en', 'en-us', 'en-en'], null, null, 'English' ); register_language( 'it_IT', ['it', 'it-it'], null, null, 'Italiano' ); register_default_language( 'it_IT' ); // apply the global timezone date_default_timezone_set( DEFAULT_TIMEZONE ); -// apply a language +// apply a language for this request only if( isset( $_GET[ 'l' ] ) ) { - // for this request only - apply_language( $_GET[ 'l' ] ); -} elseif( isset( $_POST[ 'l' ] ) ) { - if( false !== apply_language( $_POST[ 'l' ] ) ) { - // also for future requests - setcookie( 'l', latest_language()->getCode() ); - } + apply_language( $_GET['l'] ); } else { - // when the cookie is empty is taken from browser language - apply_language( @ $_COOKIE[ 'l' ] ); + // as last resource, take from browser language + apply_language(); }