private function initSite(&$path) { includePackage('Cache'); includePackage('Config'); $siteConfig = new ConfigGroup(); $saveCache = true; // Load main configuration file $kurogoConfig = ConfigFile::factory('kurogo', 'project', ConfigFile::OPTION_IGNORE_MODE | ConfigFile::OPTION_IGNORE_LOCAL); $siteConfig->addConfig($kurogoConfig); define('CONFIG_MODE', $siteConfig->getVar('CONFIG_MODE', 'kurogo')); Kurogo::log(LOG_DEBUG, "Setting config mode to " . (CONFIG_MODE ? CONFIG_MODE : '<empty>'), 'config'); define('CONFIG_IGNORE_LOCAL', $siteConfig->getVar('CONFIG_IGNORE_LOCAL', 'kurogo')); if ($cacheClass = $siteConfig->getOptionalVar('CACHE_CLASS', '', 'cache')) { $this->cacher = KurogoMemoryCache::factory($cacheClass, $siteConfig->getOptionalSection('cache')); } //multi site currently only works with a url base of root "/" if ($siteConfig->getOptionalVar('MULTI_SITE', false, 'kurogo')) { // in scripts you can pass the site name to Kurogo::initialize() if (PHP_SAPI == 'cli') { $site = strlen($path) > 0 ? $path : $siteConfig->getVar('DEFAULT_SITE'); $siteDir = implode(DIRECTORY_SEPARATOR, array(ROOT_DIR, 'site', $site)); if (!file_exists(realpath($siteDir))) { die("FATAL ERROR: Site Directory {$siteDir} not found for site {$path}"); } } else { $paths = explode("/", $path); // this is url $sites = array(); $siteDir = ''; if (count($paths) > 1) { $site = $paths[1]; if ($sites = $siteConfig->getOptionalVar('ACTIVE_SITES', array(), 'kurogo')) { //see if the site is in the list of available sites if (in_array($site, $sites)) { $testPath = implode(DIRECTORY_SEPARATOR, array(ROOT_DIR, 'site', $site)); if (($siteDir = realpath($testPath)) && file_exists($siteDir)) { $urlBase = '/' . $site . '/'; // this is a url } } } elseif (self::isValidSiteName($site)) { $testPath = implode(DIRECTORY_SEPARATOR, array(ROOT_DIR, 'site', $site)); if (($siteDir = realpath($testPath)) && file_exists($siteDir)) { $urlBase = '/' . $site . '/'; // this is a url } } } if (!$siteDir) { $site = $siteConfig->getVar('DEFAULT_SITE'); array_splice($paths, 1, 1, array($site, $paths[1])); $url = implode("/", $paths); Kurogo::redirectToURL($url, Kurogo::REDIRECT_PERMANENT); } } define('SITE_NAME', $site); } else { //make sure active site is set if (!($site = $siteConfig->getVar('ACTIVE_SITE'))) { die("FATAL ERROR: ACTIVE_SITE not set"); } // make sure site_dir is set and is a valid path // Do not call realpath_exists here because until SITE_DIR define is set // it will not allow files and directories outside ROOT_DIR if (!($siteDir = $siteConfig->getVar('SITE_DIR')) || !(($siteDir = realpath($siteDir)) && file_exists($siteDir))) { die("FATAL ERROR: Site Directory " . $siteConfig->getVar('SITE_DIR') . " not found for site " . $site); } define('SITE_NAME', $site); if (PHP_SAPI != 'cli') { // // Get URL base // if ($urlBase = $siteConfig->getOptionalVar('URL_BASE', '', 'kurogo')) { $urlBase = rtrim($urlBase, '/') . '/'; } elseif ($urlBase = Kurogo::getCache('URL_BASE')) { //@TODO this won't work yet because the cache hasn't initialized $urlBase = rtrim($urlBase, '/') . '/'; $saveCache = false; } else { //extract the path parts from the url $pathParts = array_values(array_filter(explode("/", $_SERVER['REQUEST_URI']))); $testPath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR; $urlBase = '/'; //once the path equals the WEBROOT_DIR we've found the base. This only works with symlinks if (realpath($testPath) != WEBROOT_DIR) { foreach ($pathParts as $dir) { $test = $testPath . $dir . DIRECTORY_SEPARATOR; if (file_exists(realpath($test))) { $testPath = $test; $urlBase .= $dir . '/'; if (realpath($test) == WEBROOT_DIR) { break; } } } } } } } if (PHP_SAPI == 'cli') { define('URL_BASE', null); } else { if (!isset($urlBase)) { throw new KurogoConfigurationException("URL base not set. Please report the configuration to see why this happened"); } define('URL_BASE', $urlBase); if ($saveCache) { Kurogo::setCache('URL_BASE', $urlBase); } Kurogo::log(LOG_DEBUG, "Setting site to {$site} with a base of {$urlBase}", 'kurogo'); // Strips out the leading part of the url for sites where // the base is not located at the document root, ie.. /mobile or /m // Also strips off the leading slash (needed by device debug below) if (isset($path)) { // Strip the URL_BASE off the path $baseLen = strlen(URL_BASE); if ($baseLen && strpos($path, URL_BASE) === 0) { $path = substr($path, $baseLen); } } } // Set up defines relative to SITE_DIR define('SITE_DIR', $siteDir); //already been realpath'd define('SITE_LIB_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'lib'); define('SITE_APP_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'app'); define('SITE_MODULES_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'modules'); define('DATA_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'data'); define('CACHE_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'cache'); define('LOG_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'logs'); define('SITE_CONFIG_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'config'); define('SITE_DISABLED_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'config_disabled'); //load in the site config file (required); $config = ConfigFile::factory('site', 'site'); $siteConfig->addConfig($config); // attempt to load site key $siteKey = $siteConfig->getOptionalVar('SITE_KEY', md5($siteDir)); define('SITE_KEY', $siteKey); if ($siteConfig->getOptionalVar('SITE_DISABLED')) { die("FATAL ERROR: Site disabled"); } // Set up theme define if (!($theme = $siteConfig->getVar('ACTIVE_THEME'))) { die("FATAL ERROR: ACTIVE_THEME not set"); } Kurogo::log(LOG_DEBUG, "Setting theme to {$theme}", 'kurogo'); define('THEME_DIR', SITE_DIR . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $theme); $this->siteConfig = $siteConfig; }
public function initialize(&$path = null) { includePackage('Cache'); includePackage('Config'); require LIB_DIR . '/compat.php'; require LIB_DIR . '/exceptions.php'; // add autoloader spl_autoload_register(array($this, "siteLibAutoloader")); // // Set up host define for server name and port // $host = self::arrayVal($_SERVER, 'SERVER_NAME', null); // SERVER_NAME never contains the port, while HTTP_HOST may (but we're not using HTTP_HOST for security reasons) if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] !== '80') { $host .= ":{$_SERVER['SERVER_PORT']}"; } // It's possible (under apache at least) for SERVER_NAME to contain a comma separated list. if (strpos($host, ',') !== false) { self::log(LOG_DEBUG, "Got multiple hostnames in SERVER_NAME: {$host}", 'kurogo'); $host_explode = explode(',', $host); // Only sane choice is to use the first one. $host = $host_explode[0]; } define('SERVER_HOST', $host); self::log(LOG_DEBUG, "Setting server host to {$host}", "kurogo"); define('IS_SECURE', self::isRequestSecure()); define('HTTP_PROTOCOL', IS_SECURE ? 'https' : 'http'); $this->baseConfigStore = new ConfigFileStore(); // Load main configuration file. $kurogoConfig = $this->loadKurogoConfig(); // get CONFIG_MODE from environment if available. $configMode = Kurogo::arrayVal($_SERVER, 'CONFIG_MODE', null); if ($configMode = $kurogoConfig->getOptionalVar('CONFIG_MODE', $configMode, 'kurogo')) { $this->setConfigMode($configMode); } if ($cacheClass = $kurogoConfig->getOptionalVar('CACHE_CLASS', '', 'cache')) { $this->cacher = KurogoMemoryCache::factory($cacheClass, $kurogoConfig->getOptionalSection('cache')); } // get SITES_DIR from environment if available. $_sitesDir = Kurogo::arrayVal($_SERVER, 'SITES_DIR', ROOT_BASE_DIR . DIRECTORY_SEPARATOR . 'sites'); $_sitesDir = $kurogoConfig->getOptionalVar('SITES_DIR', $_sitesDir, 'kurogo'); if (!($sitesDir = realpath($_sitesDir))) { throw new KurogoConfigurationException("SITES_DIR {$_sitesDir} does not exist"); } define('SITES_DIR', $sitesDir); define('SITES_KEY', md5(SITES_DIR)); // // Initialize Site // $this->initSite($path); $this->setCharset($this->getOptionalSiteVar('DEFAULT_CHARSET', 'UTF-8')); ini_set('default_charset', $this->charset()); ini_set('display_errors', $this->getSiteVar('DISPLAY_ERRORS')); if (!ini_get('error_log')) { ini_set('error_log', LOG_DIR . DIRECTORY_SEPARATOR . 'php_error.log'); } define('KUROGO_IS_API', preg_match("#^" . API_URL_PREFIX . "/#", $path)); // // Install exception handlers // if ($this->getSiteVar('PRODUCTION_ERROR_HANDLER_ENABLED')) { set_exception_handler("exceptionHandlerForProduction"); } else { set_exception_handler("exceptionHandlerForDevelopment"); } //get timezone from config and set $timezone = $this->getSiteVar('LOCAL_TIMEZONE'); date_default_timezone_set($timezone); $this->timezone = new DateTimeZone($timezone); self::log(LOG_DEBUG, "Setting timezone to {$timezone}", "kurogo"); if ($locale = $this->getOptionalSiteVar('LOCALE')) { $this->setLocale($locale); } else { $this->locale = $this->getSystemLocale(); } if ($languages = $this->getOptionalSiteVar('LANGUAGES')) { $this->setLanguages($languages); } else { $this->setLanguages(array('en_US')); } // // everything after this point only applies to http requests // if (PHP_SAPI == 'cli') { define('FULL_URL_BASE', ''); return; } define('FULL_URL_BASE', 'http' . (IS_SECURE ? 's' : '') . '://' . $this->_getHost() . URL_BASE); define('COOKIE_PATH', URL_BASE); // make sure host is all lower case if ($this->_getHost() != strtolower($this->_getHost())) { $url = 'http' . (IS_SECURE ? 's' : '') . '://' . strtolower($this->_getHost()) . $path; self::log(LOG_INFO, "Redirecting to lowercase url {$url}", 'kurogo'); Kurogo::redirectToURL($url, Kurogo::REDIRECT_PERMANENT); } // // Initialize global device classifier // $device = null; $deviceCacheTimeout = self::getSiteVar('DEVICE_DETECTION_COOKIE_LIFESPAN', 'cookies'); $urlPrefix = URL_BASE; $urlDeviceDebugPrefix = '/'; $override = null; if (isset($_GET['resetdevice'])) { DeviceClassifier::clearDeviceCookie(); } if (isset($_GET['setdevice'])) { $device = $_GET['setdevice']; $override = 1; $deviceCacheTimeout = 0; } // Check for device classification in url and strip it if present if ($this->getSiteVar('DEVICE_DEBUG')) { if (preg_match(';^device/([^/]+)/(.*)$;', $path, $matches)) { $device = $matches[1]; // layout forced by url $path = $matches[2]; $urlPrefix .= "device/{$device}/"; $urlDeviceDebugPrefix .= "device/{$device}/"; $deviceCacheTimeout = null; } elseif (isset($_GET['_device']) && preg_match(';^device/([^/]+)/$;', $_GET['_device'], $matches)) { $device = $matches[1]; $urlPrefix .= "device/{$device}/"; $urlDeviceDebugPrefix .= "device/{$device}/"; $deviceCacheTimeout = null; } } define('URL_DEVICE_DEBUG_PREFIX', $urlDeviceDebugPrefix); define('URL_PREFIX', $urlPrefix); self::log(LOG_DEBUG, "Setting URL_PREFIX to " . URL_PREFIX, "kurogo"); define('FULL_URL_PREFIX', 'http' . (IS_SECURE ? 's' : '') . '://' . $this->_getHost() . URL_PREFIX); $this->checkCurrentVersion(); $this->deviceClassifier = new DeviceClassifier($device, $deviceCacheTimeout, $override); //preserved for compatibility $GLOBALS['deviceClassifier'] = $this->deviceClassifier; $this->initTheme(); }
public function cacher() { if (!isset($this->cacher)) { if ($cacheClass = Kurogo::arrayVal($this->initArgs, 'CACHE_CLASS')) { $this->cacher = KurogoMemoryCache::factory($cacheClass, $this->initArgs); } else { $this->cacher = false; } } return $this->cacher; }