예제 #1
0
 public function initHttpEnvironment($inEnvironment = null, $inLocale = null, $inVersion = null)
 {
     $urlIni = Ini::parse(Registry::get('applicationPath') . '/configs/url.ini', true);
     Registry::set('urlIni', $urlIni);
     // environment: dev, test, or prod
     if (!empty($inEnvironment)) {
         $environment = $inEnvironment;
     } else {
         // by default
         $environment = $urlIni->defaults->environment;
         if (!empty($_SERVER['HTTP_HOST'])) {
             // let's check the environment from the http host (and set the other values)
             list($localeFromDomain, $versionFromDomain, $environmentFromDomain) = $this->getLocaleAndVersionAndEnvironmentFromDomain($_SERVER['HTTP_HOST'], $urlIni);
             if (!empty($environmentFromDomain)) {
                 $environment = $environmentFromDomain;
             }
         }
     }
     if (!empty($inLocale)) {
         $locale = $inLocale;
     } else {
         // locale selection order
         if (!empty($urlIni->localeSelectionOrder->{$environment})) {
             $localeSelectionOrder = $urlIni->localeSelectionOrder->{$environment};
         } else {
             $localeSelectionOrder = self::DEFAULT_LOCALESELECTIONORDER;
         }
         $localeSelectionOrderArray = (array) explode(',', $localeSelectionOrder);
         // 3 possibilities : according to the url, or by a cookie, or by the browser's accept language
         $locale = null;
         foreach ($localeSelectionOrderArray as $localeSelectionMethod) {
             if (!in_array($localeSelectionMethod, array('browser', 'domain', 'cookie'))) {
                 throw new \Exception('The locale selection method must be chosen from these values: browser and/or domain and/or cookie');
             }
             if (empty($locale)) {
                 switch ($localeSelectionMethod) {
                     case 'browser':
                         // we use the locale of the browser if requested to
                         if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                             // vérification de la syntaxe par une regexp
                             if (preg_match('/[a-z]+[_\\-]?[a-z]+[_\\-]?[a-z]+/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
                                 $locale = Localization::normalizeLocale($matches[0]);
                             }
                         }
                         break;
                     case 'domain':
                         // we will read the url.ini to guess the requested locale
                         // according to the domain name
                         if (!empty($_SERVER['HTTP_HOST'])) {
                             list($localeFromDomain, $versionFromDomain, $environmentFromDomain) = $this->getLocaleAndVersionAndEnvironmentFromDomain($_SERVER['HTTP_HOST'], $urlIni);
                             if (!empty($localeFromDomain)) {
                                 $locale = $localeFromDomain;
                             }
                         }
                         break;
                     case 'cookie':
                         // read the cookie to select the locale
                         if (!empty($_COOKIE['_nfLc'])) {
                             // matching of the locale with the cookie's value
                             if (preg_match('/[a-z]+[_\\-]?[a-z]+[_\\-]?[a-z]+/i', $_COOKIE['_nfLc'], $matches)) {
                                 $locale = Localization::normalizeLocale($matches[0]);
                             }
                         }
                         break;
                 }
             } else {
                 break;
             }
         }
         // if we did not find the locale with the http host or cookie or browser, let's use the default value
         if ($locale === null) {
             if (!empty($urlIni->defaults->locale)) {
                 $locale = $urlIni->defaults->locale;
             } else {
                 throw new \Exception('Locale not found from browser, cookie or url: you have to set a default locale in url.ini');
             }
         }
     }
     // version (web, mobile, cli...)
     if (empty($inVersion)) {
         if (!empty($versionFromDomain)) {
             $version = $versionFromDomain;
         } else {
             if (in_array('url', $localeSelectionOrderArray)) {
                 if (!empty($_SERVER['HTTP_HOST'])) {
                     list($localeFromDomain, $versionFromDomain, $environmentFromDomain) = $this->getLocaleAndVersionAndEnvironmentFromDomain($_SERVER['HTTP_HOST'], $urlIni);
                 }
             }
             if (!empty($versionFromDomain)) {
                 $version = $versionFromDomain;
             } else {
                 // let's take the default version then
                 if (isset($urlIni->defaults->version)) {
                     $version = $urlIni->defaults->version;
                 } else {
                     trigger_error('Cannot guess the requested version from the domain name: you have to set a default locale in url.ini');
                 }
             }
         }
     } else {
         $version = $inVersion;
     }
     // on assigne les variables d'environnement et de language en registry
     Registry::set('environment', $environment);
     Registry::set('locale', $locale);
     Registry::set('version', $version);
     // we use the requested section from the config.ini to load our config
     Config::init($locale, $environment, $version);
     Registry::set('config', Config::getInstance());
     // parse the variables from the .env file or environment
     $env = Env::init($locale, $environment, $version);
     Registry::set('env', Env::getInstance());
     // create the Settings Object
     Registry::set('settings', Settings::getInstance());
     // let's block the use of index.php
     if (isset($_SERVER['REQUEST_URI']) && in_array($_SERVER['REQUEST_URI'], array('index.php', '/index.php'))) {
         header("HTTP/1.1 301 Moved Permanently");
         header("Location: /");
         return false;
     }
     return true;
 }