UOJContext.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. class UOJContext {
  3. public static $data = array();
  4. public static function pageConfig() {
  5. if (!isset(self::$data['type'])) {
  6. return array(
  7. 'PageNav' => 'main-nav'
  8. );
  9. } elseif (self::$data['type'] == 'blog') {
  10. return array(
  11. 'PageNav' => 'blog-nav',
  12. 'PageMainTitle' => UOJContext::$data['user']['username'] . ' 的博客',
  13. 'PageMainTitleOnSmall' => '博客',
  14. );
  15. }
  16. }
  17. public static function isAjax() {
  18. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  19. }
  20. public static function contentLength() {
  21. if (!isset($_SERVER['CONTENT_LENGTH'])) {
  22. return null;
  23. }
  24. return (int)$_SERVER['CONTENT_LENGTH'];
  25. }
  26. public static function documentRoot() {
  27. return $_SERVER['DOCUMENT_ROOT'];
  28. }
  29. public static function storagePath() {
  30. return $_SERVER['DOCUMENT_ROOT'].'/app/storage';
  31. }
  32. public static function remoteAddr() {
  33. return $_SERVER['REMOTE_ADDR'];
  34. }
  35. public static function requestURI() {
  36. return $_SERVER['REQUEST_URI'];
  37. }
  38. public static function requestPath() {
  39. $uri = $_SERVER['REQUEST_URI'];
  40. $p = strpos($uri, '?');
  41. if ($p === false) {
  42. return $uri;
  43. } else {
  44. return substr($uri, 0, $p);
  45. }
  46. }
  47. public static function requestMethod() {
  48. return $_SERVER['REQUEST_METHOD'];
  49. }
  50. public static function httpHost() {
  51. if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  52. return $_SERVER['HTTP_X_FORWARDED_HOST'];
  53. } elseif (isset($_SERVER['HTTP_HOST'])) {
  54. return $_SERVER['HTTP_HOST'];
  55. } else {
  56. return $_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT']);
  57. }
  58. }
  59. public static function cookieDomain() {
  60. $domain = UOJConfig::$data['web']['domain'];
  61. if ($domain === null) {
  62. $domain = UOJConfig::$data['web']['main']['host'];
  63. }
  64. $domain = array_shift(explode(':', $domain));
  65. if (validateIP($domain) || $domain === 'localhost') {
  66. $domain = '';
  67. } else {
  68. $domain = '.'.$domain;
  69. }
  70. return $domain;
  71. }
  72. public static function setupBlogMain() {
  73. self::$data['type'] = 'blog';
  74. }
  75. public static function setupBlog() {
  76. $username = blog_name_decode($_GET['blog_username']);
  77. if (!validateUsername($username) || !(self::$data['user'] = queryUser($username))) {
  78. become404Page();
  79. }
  80. if ($_GET['blog_username'] !== blog_name_encode(self::$data['user']['username'])) {
  81. permanentlyRedirectTo(HTML::blog_url(self::$data['user']['username'], '/'));
  82. }
  83. self::$data['type'] = 'blog';
  84. }
  85. public static function __callStatic($name, array $args) {
  86. switch (self::$data['type']) {
  87. case 'blog':
  88. switch ($name) {
  89. case 'user':
  90. return self::$data['user'];
  91. case 'userid':
  92. return self::$data['user']['username'];
  93. case 'hasBlogPermission':
  94. return Auth::check() && (isSuperUser(Auth::user()) || Auth::id() == self::$data['user']['username']);
  95. case 'isHis':
  96. if (!isset($args[0])) {
  97. return false;
  98. }
  99. $blog = $args[0];
  100. return $blog['poster'] == self::$data['user']['username'];
  101. case 'isHisBlog':
  102. if (!isset($args[0])) {
  103. return false;
  104. }
  105. $blog = $args[0];
  106. return $blog['poster'] == self::$data['user']['username'] && $blog['type'] == 'B' && $blog['is_draft'] == false;
  107. case 'isHisSlide':
  108. if (!isset($args[0])) {
  109. return false;
  110. }
  111. $blog = $args[0];
  112. return $blog['poster'] == self::$data['user']['username'] && $blog['type'] == 'S' && $blog['is_draft'] == false;
  113. }
  114. break;
  115. }
  116. }
  117. }