Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

115 lignes
4.1 KiB

  1. <?php
  2. namespace Kuxin;
  3. class Rewrite
  4. {
  5. public static $rule = [
  6. '{page}' => '(\d+)',
  7. '{chapterid}' => '(\d+)',
  8. '{siteid}' => '(\d+)',
  9. '{key}' => '([a-zA-Z0-9_\-]+)',
  10. '{searchkey}' => '([^\?\/]+?)',
  11. '{novelid}' => '(\d+)',
  12. '{author}' => '([^\?\/]+?)',
  13. '{name}' => '([^\?\/]+?)',
  14. '{authorid}' => '(\d+)',
  15. '{pinyin}' => '([\w\-]+)',
  16. '{type}' => '([\w\-]+)',
  17. '{booklistid}' => '(\d+)',
  18. '{specialid}' => '(\d+)',
  19. ];
  20. public static function format($param)
  21. {
  22. foreach ($param as &$v) {
  23. $v = '/' . ltrim($v, '/');
  24. }
  25. return $param;
  26. }
  27. public static function createRouter($param)
  28. {
  29. $data = [];
  30. foreach ($param as $k => $v) {
  31. $v = ltrim($v, '/');
  32. if ($v != '') {
  33. $rules = self::parseRule($v);
  34. if (strpos($v, '{') === false) {
  35. $position = 99;
  36. } else {
  37. $position = substr_count(explode('{', $v, 2)[0], '/') + 1;
  38. }
  39. foreach ($rules as $rule) {
  40. preg_match_all('/\{([a-z]+)\}/', $rule, $match);
  41. foreach ($match['0'] as $p) {
  42. $preg = empty(self::$rule[$p]) ? '([a-zA-Z0-9]+)' : self::$rule[$p];
  43. $rule = str_replace($p, $preg, $rule);
  44. }
  45. $rule = rtrim($rule, '/');
  46. $data[$position]["^{$rule}(\?(.*))*$"] = str_replace('.', '/', $k) . '?' . implode('&', $match[1]);
  47. }
  48. }
  49. }
  50. krsort($data);
  51. $result = [];
  52. foreach ($data as $value) {
  53. $result = array_merge($result, $value);
  54. }
  55. return $result;
  56. }
  57. public static function parseRule($rule)
  58. {
  59. preg_match_all('#\[[^\[\]]*?\{\w+\}[^\[\]]*?\]#', $rule, $match);
  60. $rules = [$rule];
  61. if ($match['0']) {
  62. if (count($match['0']) == 1) {
  63. $rules[] = str_replace($match['0']['0'], '', $rule);
  64. } elseif (count($match['0'] == 2)) {
  65. $rules[] = str_replace($match['0']['0'], '', $rule);
  66. $rules[] = str_replace($match['0']['1'], '', $rule);
  67. $rules[] = str_replace([$match['0']['0'], $match['0']['1']], '', $rule);
  68. } elseif (count($match['0'] == 3)) {
  69. $rules[] = str_replace($match['0']['0'], '', $rule);
  70. $rules[] = str_replace($match['0']['1'], '', $rule);
  71. $rules[] = str_replace($match['0']['2'], '', $rule);
  72. $rules[] = str_replace([$match['0']['0'], $match['0']['1']], '', $rule);
  73. $rules[] = str_replace([$match['0']['0'], $match['0']['2']], '', $rule);
  74. $rules[] = str_replace([$match['0']['1'], $match['0']['2']], '', $rule);
  75. $rules[] = str_replace([$match['0']['0'], $match['0']['1'], $match['0']['2']], '', $rule);
  76. }
  77. foreach ($rules as &$v) {
  78. $v = str_replace(['[', ']'], '', $v);
  79. }
  80. }
  81. return $rules;
  82. }
  83. public static function iisRule($param)
  84. {
  85. $data = "[ISAPI_Rewrite]\r\n\r\nCacheClockRate 3600\r\nRepeatLimit 32\r\n";
  86. foreach ($param as $k => $v) {
  87. $v = ltrim($v, '/');
  88. if ($v != '') {
  89. $param = [];
  90. preg_match_all('/\{([a-z]+)\}/', $v, $match);
  91. $n = 1;
  92. foreach ($match['0'] as $kk => $vv) {
  93. $preg = empty(self::$rule[$vv]) ? '([a-zA-Z0-9]+)' : self::$rule[$vv];
  94. $v = str_replace($vv, $preg, $v);
  95. ++$n;
  96. $param[] = "{$match['1'][$kk]}=$" . $n;
  97. }
  98. $data .= "RewriteRule ^(.*)/{$v}(\?(.*))*$ $1/index.php\?s=" . str_replace([
  99. '.',
  100. '_',
  101. ], '/', $k) . '&' . implode('&', $param) . '&$' . ($n + 2) . " [I]\r\n";
  102. }
  103. }
  104. return trim($data);
  105. }
  106. }