S2OJ/web/app/models/UOJContext.php

123 lines
3.3 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
class UOJContext {
public static $data = array();
public static function pageConfig() {
if (!isset(self::$data['type'])) {
return array(
'PageNav' => 'main-nav'
);
2020-06-25 12:41:16 +00:00
} elseif (self::$data['type'] == 'blog') {
2016-07-18 16:39:37 +00:00
return array(
'PageNav' => 'blog-nav',
'PageMainTitle' => UOJContext::$data['user']['username'] . '的博客',
'PageMainTitleOnSmall' => '博客',
);
}
}
public static function isAjax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
public static function contentLength() {
if (!isset($_SERVER['CONTENT_LENGTH'])) {
return null;
}
return (int)$_SERVER['CONTENT_LENGTH'];
}
2016-07-18 16:39:37 +00:00
public static function documentRoot() {
return $_SERVER['DOCUMENT_ROOT'];
}
public static function storagePath() {
return $_SERVER['DOCUMENT_ROOT'].'/app/storage';
}
public static function remoteAddr() {
return $_SERVER['REMOTE_ADDR'];
}
public static function requestURI() {
return $_SERVER['REQUEST_URI'];
}
public static function requestPath() {
$uri = $_SERVER['REQUEST_URI'];
$p = strpos($uri, '?');
if ($p === false) {
return $uri;
} else {
return substr($uri, 0, $p);
}
}
public static function requestMethod() {
return $_SERVER['REQUEST_METHOD'];
}
public static function httpHost() {
fix(uoj/1/app/models): session issue caused by wrong cookie domain After we changed the detection of the real httpHost value, the token check will fail when register. A long time ago we just simply added "Session_Start();" at the beginning of uoj/1/app/index.php. It temporally solved the problem but caused another series of issues that we can't see outside. There is also a session_start() when executing Session::init() at importing app/libs/uoj-lib.php. So, when we add one more "Session_Start();" it will execute this one more time, just cause warning: > session_name(): Cannot change session name when session is active > PHP Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time And, the session name and session path, session domain will not be set so may cause other problems. The reason is that using UOJContext::httpHost() as web hostname when default, it will add the port at the end. When using IP the validateIP() will return false, or using domain with port the cookie domain will be set wrongly. As a result the register process throws out the "Expired" error and refuses to register, and other uses token will fail too. We made it cut out the port when setting cookie domain, and also changed the style of UOJContext::httpHost() to make it easier to read. With this problem known and solved the "Session_Start();" in index.php will also say bye-bye, and there will be no multiple session_start()s. NOTICE: If you have set all your information to yours in .config.php, and not using address with port other than 80, you may not face this problem. But the warning of php will consistently shown in the error log file. So kick the annoying warning information out if you are angry with this ;-)
2018-10-11 10:46:58 +00:00
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
return $_SERVER['HTTP_X_FORWARDED_HOST'];
2020-06-25 12:41:16 +00:00
} elseif (isset($_SERVER['HTTP_HOST'])) {
fix(uoj/1/app/models): session issue caused by wrong cookie domain After we changed the detection of the real httpHost value, the token check will fail when register. A long time ago we just simply added "Session_Start();" at the beginning of uoj/1/app/index.php. It temporally solved the problem but caused another series of issues that we can't see outside. There is also a session_start() when executing Session::init() at importing app/libs/uoj-lib.php. So, when we add one more "Session_Start();" it will execute this one more time, just cause warning: > session_name(): Cannot change session name when session is active > PHP Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time And, the session name and session path, session domain will not be set so may cause other problems. The reason is that using UOJContext::httpHost() as web hostname when default, it will add the port at the end. When using IP the validateIP() will return false, or using domain with port the cookie domain will be set wrongly. As a result the register process throws out the "Expired" error and refuses to register, and other uses token will fail too. We made it cut out the port when setting cookie domain, and also changed the style of UOJContext::httpHost() to make it easier to read. With this problem known and solved the "Session_Start();" in index.php will also say bye-bye, and there will be no multiple session_start()s. NOTICE: If you have set all your information to yours in .config.php, and not using address with port other than 80, you may not face this problem. But the warning of php will consistently shown in the error log file. So kick the annoying warning information out if you are angry with this ;-)
2018-10-11 10:46:58 +00:00
return $_SERVER['HTTP_HOST'];
} else {
return $_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT']);
}
2016-07-18 16:39:37 +00:00
}
public static function cookieDomain() {
$domain = UOJConfig::$data['web']['domain'];
if ($domain === null) {
$domain = UOJConfig::$data['web']['main']['host'];
}
2022-03-17 08:19:43 +00:00
$domain = explode(':', $domain);
$domain = array_shift($domain);
if (validateIP($domain) || $domain === 'localhost') {
2016-07-18 16:39:37 +00:00
$domain = '';
} else {
$domain = '.'.$domain;
}
return $domain;
}
public static function setupBlog() {
$username = blog_name_decode($_GET['blog_username']);
if (!validateUsername($username) || !(self::$data['user'] = queryUser($username))) {
become404Page();
}
if ($_GET['blog_username'] !== blog_name_encode(self::$data['user']['username'])) {
permanentlyRedirectTo(HTML::blog_url(self::$data['user']['username'], '/'));
}
self::$data['type'] = 'blog';
}
public static function __callStatic($name, array $args) {
switch (self::$data['type']) {
case 'blog':
switch ($name) {
case 'user':
return self::$data['user'];
case 'userid':
return self::$data['user']['username'];
case 'hasBlogPermission':
return Auth::check() && (isSuperUser(Auth::user()) || Auth::id() == self::$data['user']['username']);
case 'isHis':
if (!isset($args[0])) {
return false;
}
$blog = $args[0];
return $blog['poster'] == self::$data['user']['username'];
case 'isHisBlog':
if (!isset($args[0])) {
return false;
}
$blog = $args[0];
2022-03-17 08:38:39 +00:00
return $blog['poster'] == self::$data['user']['username'] && $blog['type'] == 'B';
2016-07-18 16:39:37 +00:00
case 'isHisSlide':
if (!isset($args[0])) {
return false;
}
$blog = $args[0];
2022-03-17 08:38:39 +00:00
return $blog['poster'] == self::$data['user']['username'] && $blog['type'] == 'S';
2016-07-18 16:39:37 +00:00
}
break;
}
}
}