Security.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Widget;
  3. use Typecho\Common;
  4. use Typecho\Response;
  5. use Typecho\Widget;
  6. if (!defined('__TYPECHO_ROOT_DIR__')) {
  7. exit;
  8. }
  9. /**
  10. * 安全选项组件
  11. *
  12. * @link typecho
  13. * @package Widget
  14. * @copyright Copyright (c) 2014 Typecho team (http://typecho.org)
  15. * @license GNU General Public License 2.0
  16. */
  17. class Security extends Base
  18. {
  19. /**
  20. * @var string
  21. */
  22. private $token;
  23. /**
  24. * @var boolean
  25. */
  26. private $enabled = true;
  27. /**
  28. * @param int $components
  29. */
  30. public function initComponents(int &$components)
  31. {
  32. $components = self::INIT_OPTIONS | self::INIT_USER;
  33. }
  34. /**
  35. * 初始化函数
  36. */
  37. public function execute()
  38. {
  39. $this->token = $this->options->secret;
  40. if ($this->user->hasLogin()) {
  41. $this->token .= '&' . $this->user->authCode . '&' . $this->user->uid;
  42. }
  43. }
  44. /**
  45. * @param bool $enabled
  46. */
  47. public function enable(bool $enabled = true)
  48. {
  49. $this->enabled = $enabled;
  50. }
  51. /**
  52. * 保护提交数据
  53. */
  54. public function protect()
  55. {
  56. if ($this->enabled && $this->request->get('_') != $this->getToken($this->request->getReferer())) {
  57. $this->response->goBack();
  58. }
  59. }
  60. /**
  61. * 获取token
  62. *
  63. * @param string|null $suffix 后缀
  64. * @return string
  65. */
  66. public function getToken(?string $suffix): string
  67. {
  68. return md5($this->token . '&' . $suffix);
  69. }
  70. /**
  71. * 获取绝对路由路径
  72. *
  73. * @param string|null $path
  74. * @return string
  75. */
  76. public function getRootUrl(?string $path): string
  77. {
  78. return Common::url($this->getTokenUrl($path), $this->options->rootUrl);
  79. }
  80. /**
  81. * 生成带token的路径
  82. *
  83. * @param $path
  84. * @param string|null $url
  85. * @return string
  86. */
  87. public function getTokenUrl($path, ?string $url = null): string
  88. {
  89. $parts = parse_url($path);
  90. $params = [];
  91. if (!empty($parts['query'])) {
  92. parse_str($parts['query'], $params);
  93. }
  94. $params['_'] = $this->getToken($url ?: $this->request->getRequestUrl());
  95. $parts['query'] = http_build_query($params);
  96. return Common::buildUrl($parts);
  97. }
  98. /**
  99. * 输出后台安全路径
  100. *
  101. * @param $path
  102. */
  103. public function adminUrl($path)
  104. {
  105. echo $this->getAdminUrl($path);
  106. }
  107. /**
  108. * 获取安全的后台路径
  109. *
  110. * @param string $path
  111. * @return string
  112. */
  113. public function getAdminUrl(string $path): string
  114. {
  115. return Common::url($this->getTokenUrl($path), $this->options->adminUrl);
  116. }
  117. /**
  118. * 输出安全的路由路径
  119. *
  120. * @param $path
  121. */
  122. public function index($path)
  123. {
  124. echo $this->getIndex($path);
  125. }
  126. /**
  127. * 获取安全的路由路径
  128. *
  129. * @param $path
  130. * @return string
  131. */
  132. public function getIndex($path): string
  133. {
  134. return Common::url($this->getTokenUrl($path), $this->options->index);
  135. }
  136. }