<commit_before><?php

namespace pff\modules;
use pff\Abs\AModule;
use pff\Iface\IBeforeSystemHook;

/**
 *
 * @author paolo.fagni<at><EMAIL>
 */
class MultiLanguage extends AModule implements IBeforeSystemHook {

    /**
     * Contains the user selected language if specified in URL or the default specified in the configuration
     *
     * @var string
     */
    private $_selectedLanguage;

    /**
     * If true saves the preferred language on cookies!
     *
     * @var bool
     */
    private $_saveOnCookies;

    /**
     * If true saves the preferred language on session!
     *
     * @var bool
     */
    private $_saveOnSession;

    /**
     * The name of the cookie to save
     *
     * @var string
     */
    private $_cookieName;

    /**
     * The key of the session array to save
     *
     * @var string
     */
    private $_sessionKeyName;

    /**
     * Contains the specified default language, this could be overrider by $pffConfig['default_language']
     *
     * @var string
     */
    private $_defaultLang;

    private $_accepted_languages;

    public function __construct($confFile = 'multilanguage/module.conf.yaml') {
        $this->_accepted_languages = array();
        $moduleconfig = $this->readConfig($confFile);
        $this->_loadConfig($moduleconfig);
    }

    /**
     * Loads the configuration file
     */
    private function _loadConfig($parsedConfig) {
        $this->_defaultLang     = $parsedConfig['moduleConf']['default_language'];
        $this->_saveOnCookies   = $parsedConfig['moduleConf']['save_on_cookies'];
        $this->_cookieName      = $parsedConfig['moduleConf']['cookie_name'];
        $this->_saveOnSession   = $parsedConfig['moduleConf']['save_on_session'];
        $this->_sessionKeyName  = $parsedConfig['moduleConf']['session_key_name'];
        if(isset($parsedConfig['moduleConf']['accepted_languages'])) {
            foreach ($parsedConfig['moduleConf']['accepted_languages'] as $l){
                $this->_accepted_languages[] = $l;
            }
        }
        else {
            $this->_accepted_languages = null;
        }
    }

    /**
     * Executed before the system startup
     *
     * @return mixed
     */
    public function doBeforeSystem() {
        $url = $this->_app->getUrl();
        $url = $this->processUrl($url);
        if (is_null($this->_selectedLanguage)) { // No language code has been found in URL request
            $this->chooseLanguage();
        }
        $this->_app->setUrl($url);

    }

    /**
     * Processes the url to find a language
     *
     * @param $url
     * @return string
     */
    public function processUrl($url) {
        $splittedUrl = explode('/', $url);
        $langCodes = $this->getCodes();

        if (isset($langCodes[$splittedUrl[0]])) {
            $this->_selectedLanguage = array_shift($splittedUrl);
            $this->saveLanguage();

            $processedUrl = implode('/', $splittedUrl);
        }
        elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && isset($langCodes[substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)])) {
            $this->_selectedLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
            if($this->_accepted_languages !== null) {
                $this->_selectedLanguage = $this->_defaultLang;
            }
            elseif (is_array($this->_accepted_languages)){
                if (!in_array($this->_selectedLanguage, $this->_accepted_languages)) {
                    $this->_selectedLanguage = $this->_defaultLang;
                }
            }
            $this->saveLanguage();

            $processedUrl = $url;
        }
        else {
            $this->_selectedLanguage = $this->_defaultLang;
            $this->saveLanguage();

            $processedUrl = $url;
        }
        return $processedUrl;
    }

    /**
     * Saves the choosen language preference
     */
    private function saveLanguage() {
        if ($this->_saveOnCookies) {
            $this->getRequiredModules('cookies')->setCookie($this->_cookieName, $this->_selectedLanguage, 30 * 24);<commit_msg>Multilanguage module now checks for invalid language also in the URL<commit_after><?php

namespace pff\modules;
use pff\Abs\AModule;
use pff\Iface\IBeforeSystemHook;

/**
 *
 * @author paolo.fagni<at>gmail.com
 */
class MultiLanguage extends AModule implements IBeforeSystemHook {

    /**
     * Contains the user selected language if specified in URL or the default specified in the configuration
     *
     * @var string
     */
    private $_selectedLanguage;

    /**
     * If true saves the preferred language on cookies!
     *
     * @var bool
     */
    private $_saveOnCookies;

    /**
     * If true saves the preferred language on session!
     *
     * @var bool
     */
    private $_saveOnSession;

    /**
     * The name of the cookie to save
     *
     * @var string
     */
    private $_cookieName;

    /**
     * The key of the session array to save
     *
     * @var string
     */
    private $_sessionKeyName;

    /**
     * Contains the specified default language, this could be overrider by $pffConfig['default_language']
     *
     * @var string
     */
    private $_defaultLang;

    private $_accepted_languages;

    public function __construct($confFile = 'multilanguage/module.conf.yaml') {
        $this->_accepted_languages = array();
        $moduleconfig = $this->readConfig($confFile);
        $this->_loadConfig($moduleconfig);
    }

    /**
     * Loads the configuration file
     */
    private function _loadConfig($parsedConfig) {
        $this->_defaultLang     = $parsedConfig['moduleConf']['default_language'];
        $this->_saveOnCookies   = $parsedConfig['moduleConf']['save_on_cookies'];
        $this->_cookieName      = $parsedConfig['moduleConf']['cookie_name'];
        $this->_saveOnSession   = $parsedConfig['moduleConf']['save_on_session'];
        $this->_sessionKeyName  = $parsedConfig['moduleConf']['session_key_name'];
        if(isset($parsedConfig['moduleConf']['accepted_languages'])) {
            foreach ($parsedConfig['moduleConf']['accepted_languages'] as $l){
                $this->_accepted_languages[] = $l;
            }
        }
        else {
            $this->_accepted_languages = null;
        }
    }

    /**
     * Executed before the system startup
     *
     * @return mixed
     */
    public function doBeforeSystem() {
        $url = $this->_app->getUrl();
        $url = $this->processUrl($url);
        if (is_null($this->_selectedLanguage)) { // No language code has been found in URL request
            $this->chooseLanguage();
        }
        $this->_app->setUrl($url);

    }

    /**
     * Processes the url to find a language
     *
     * @param $url
     * @return string
     */
    public function processUrl($url) {
        $splittedUrl = explode('/', $url);
        $langCodes = $this->getCodes();

        if (isset($langCodes[$splittedUrl[0]])) {
            if($this->_accepted_languages !== null && is_array($this->_accepted_languages)) {
                if( in_array($splittedUrl[0], $this->_accepted_languages)){
                    $this->_selectedLanguage = array_shift($splittedUrl);
                }
                else {
                    $this->_selectedLanguage = $this->_defaultLang;
                }
            }
            else {
                $this->_selectedLanguage = array_shift($splittedUrl);
            }
            $this->saveLanguage();

            $processedUrl = implode('/', $splittedUrl);
        }
        elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && isset($langCodes[substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)])) {
            $this->_selectedLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
            if($this->_accepted_languages === null) {
                $this->_selectedLanguage = $this->_defaultLang;
            }
            elseif (is_array($this->_accepted_languages)){
                if (!in_array($this->_selectedLanguage, $this->_accepted_languages)) {
                    $this->_selectedLanguage = $this->_defaultLang;
                }
            }
            $this->saveLanguage();

            $processedUrl = $url;
        }
        else {
            $this->_selectedLanguage = $this->_defaultLang;
            $this->saveLanguage();

            $processedUrl = $url;
        }
        return $processedUrl;
    }

    /**
     * Saves the choosen language preference
     */
    private function saveLanguage() {
        if ($this->_saveOnCookies) {
            $this->getRequiredModules('cookies')->setCookie($this->_cookieName, $this->_selectedLanguage, 30 * 24);
{'fraction_non_alphanumeric': 0.13228001383923424, 'fraction_numerical': 0.00219121208626456, 'mean_word_length': 2.2106627175120326, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3303098', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2396, 'n_words': 487}