/** * Set the code of current site. * * www.xirang.com => xirang * xirang.com => xirang * xirang.com.cn => xirang * xirang.cn => xirang * xirang => xirang * 192.168.1.1 => 192.168.1.1 * * @access protected * @return void */ public function setSiteCode() { return $this->siteCode = helper::getSiteCode($this->server->http_host); }
/** * Check domain and header 301. * * @access public * @return void */ public function checkDomain() { if (RUN_MODE == 'install' or RUN_MODE == 'upgrade' or RUN_MODE == 'shell' or RUN_MODE == 'admin' or !$this->config->installed) { return true; } $http = (isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) != 'off') ? 'https://' : 'http://'; $httpHost = $this->server->http_host; $currentURI = $http . $httpHost . $this->server->request_uri; $scheme = isset($this->config->site->scheme) ? $this->config->site->scheme : 'http'; $mainDomain = isset($this->config->site->domain) ? $this->config->site->domain : ''; $mainDomain = str_replace(array('http://', 'https://'), '', $mainDomain); /* Check main domain and scheme. */ $redirectURI = $currentURI; if (strpos($redirectURI, $scheme . '://') !== 0) { $redirectURI = $scheme . substr($redirectURI, strpos($redirectURI, '://')); } if (!empty($mainDomain) and $httpHost != $mainDomain) { $redirectURI = str_replace($httpHost, $mainDomain, $redirectURI); } if ($redirectURI != $currentURI) { header301($redirectURI); } /* Check domain is allowed. */ $allowedDomains = isset($this->config->site->allowedDomain) ? $this->config->site->allowedDomain : ''; $allowedDomains = str_replace(array('http://', 'https://'), '', $allowedDomains); if (!empty($allowedDomains)) { if (strpos($allowedDomains, $httpHost) !== false) { return true; } if (!empty($mainDomain) and helper::getSiteCode($httpHost) == helper::getSiteCode($mainDomain)) { return true; } die('domain denied.'); } }
#!/usr/bin/env php <?php include '../../../tests/init.php'; // Include the init file of testing framework. include '../../router.class.php'; // Include router class. include '../../helper.class.php'; // Include helper class. title("testing the logic for site code."); $router = router::createApp('test', '../../../'); run(helper::getSiteCode('www.xirang.com')) && expect('xirang'); run(helper::getSiteCode('xirang.com')) && expect('xirang'); run(helper::getSiteCode('xirang.com.cn')) && expect('xirang'); run(helper::getSiteCode('www.xirang.cn')) && expect('xirang'); run(helper::getSiteCode('xirang')) && expect('xirang'); run(helper::getSiteCode('192.168.1.1')) && expect('192.168.1.1'); run(helper::getSiteCode('www.xirang.com.cn')) && expect('xirang'); run(appendChanzhiDomain()); run(helper::getSiteCode('xirang.n1.chanzhi.net')) && expect('xirang'); function appendChanzhiDomain() { global $router; $router->config->domainPostfix .= '|n1.chanzhi.net|'; }