📄 正在查看:twcms/control/parseurl_control.class.php
大小:7,986 字节 · 修改:2014-04-07 07:15:32 · 行数:263
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class parseurl_control extends control{
10 public function index() {
11 // hook parseurl_control_index_before.php
12
13 if(empty($_GET)) return;
14
15 // 为了实现URL灵活性,又不想使用正则表达式,只好写这么一堆垃圾代码,写的想哭。
16 $cfg = $this->runtime->xget();
17 if(!empty($_ENV['_config']['twcms_parseurl']) && !empty($_GET['rewrite'])) {
18 $uri = $_GET['rewrite'];
19 unset($_GET['rewrite']);
20 $cate_arr = array_flip($cfg['cate_arr']);
21
22 // 分类URL未设置后缀的情况
23 if(isset($cate_arr[$uri])) {
24 $_GET['control'] = 'cate';
25 $_GET['action'] = 'index';
26 $_GET['cid'] = $cate_arr[$uri];
27 return;
28 }
29
30 // 分类URL已设置后缀的情况
31 $len = strlen($cfg['link_cate_end']);
32 if(substr($uri, -$len) == $cfg['link_cate_end']) {
33 $newurl = substr($uri, 0, -$len);
34 if(isset($cate_arr[$newurl])) {
35 $_GET['control'] = 'cate';
36 $_GET['action'] = 'index';
37 $_GET['cid'] = $cate_arr[$newurl];
38 return;
39 }
40 }
41
42 // 分类URL分页的情况
43 if(strpos($uri, $cfg['link_cate_page_pre']) !== FALSE) {
44 $len = strlen($cfg['link_cate_page_end']);
45 if(substr($uri, -$len) == $cfg['link_cate_page_end']) {
46 $newurl = substr($uri, 0, -$len);
47 $u_arr = explode($cfg['link_cate_page_pre'], $newurl);
48 if(isset($cate_arr[$u_arr[0]])) {
49 $_GET['control'] = 'cate';
50 $_GET['action'] = 'index';
51 $_GET['cid'] = $cate_arr[$u_arr[0]];
52 isset($u_arr[1]) && $_GET['page'] = $u_arr[1];
53 return;
54 }
55 }
56 }
57
58 // 标签URL
59 $len = strlen($cfg['link_tag_pre']);
60 if(substr($uri, 0, $len) == $cfg['link_tag_pre']) {
61 $len2 = strlen($cfg['link_tag_end']);
62 if(substr($uri, -$len2) == $cfg['link_tag_end']) {
63 $newurl = substr($uri, $len, -$len2);
64 $u_arr = explode('_', $newurl);
65 if(count($u_arr) > 1) {
66 $_GET['control'] = 'tag';
67 $_GET['action'] = 'index';
68 $_GET['mid'] = $u_arr[0];
69 $_GET['name'] = $u_arr[1];
70 isset($u_arr[2]) && $_GET['page'] = $u_arr[2];
71 return;
72 }
73 }
74 }
75
76 // 评论URL
77 $len = strlen($cfg['link_comment_pre']);
78 if(substr($uri, 0, $len) == $cfg['link_comment_pre']) {
79 $len2 = strlen($cfg['link_comment_end']);
80 if(substr($uri, -$len2) == $cfg['link_comment_end']) {
81 $newurl = substr($uri, $len, -$len2);
82 $u_arr = explode('_', $newurl);
83 if(count($u_arr) > 1) {
84 $_GET['control'] = 'comment';
85 $_GET['action'] = 'index';
86 $_GET['cid'] = $u_arr[0];
87 $_GET['id'] = $u_arr[1];
88 isset($u_arr[2]) && $_GET['page'] = $u_arr[2];
89 return;
90 }
91 }
92 }
93
94 // 首页分页URL (用的少)
95 $len = strlen($cfg['link_index_end']);
96 if(substr($uri, 0, 6) == 'index_' && substr($uri, -$len) == $cfg['link_index_end']) {
97 $uri = substr($uri, 6, -$len);
98 $u_arr = explode('_', $uri);
99 if(count($u_arr) > 1) {
100 $_GET['control'] = 'index';
101 $_GET['action'] = 'index';
102 $_GET['mid'] = $u_arr[0];
103 $_GET['page'] = $u_arr[1];
104 return;
105 }
106 }
107
108 // 标签排行页 (用的少)
109 if($uri == 'tag_top' && $uri == 'tag_top/') {
110 $_GET['control'] = 'tag';
111 $_GET['action'] = 'top';
112 return;
113 }
114
115 // hook parseurl_control_index_link_show_before.php
116
117 // 内容页 (最复杂的部分)
118 if($cfg['link_show_type'] == 1) { // 1. {cid}/{id} 性能最优
119 $len = strlen($cfg['link_show_end']);
120 if(empty($len) || substr($uri, -$len) == $cfg['link_show_end']) {
121 $u_arr = explode('/', $len ? substr($uri, 0, -$len) : $uri);
122 if(count($u_arr) > 1) {
123 $_GET['control'] = 'show';
124 $_GET['action'] = 'index';
125 $_GET['cid'] = $u_arr[0];
126 $_GET['id'] = $u_arr[1];
127 return;
128 }
129 }
130 }elseif($cfg['link_show_type'] == 2) { // 2. {cate_alias}/{id} 性能次优
131 $len = strlen($cfg['link_show_end']);
132 if(empty($len) || substr($uri, -$len) == $cfg['link_show_end']) {
133 $u_arr = explode('/', $len ? substr($uri, 0, -$len) : $uri);
134 if(count($u_arr) > 1 && isset($cate_arr[$u_arr[0]])) {
135 $_GET['control'] = 'show';
136 $_GET['action'] = 'index';
137 $_GET['cid'] = $cate_arr[$u_arr[0]];
138 $_GET['id'] = $u_arr[1];
139 return;
140 }
141 }
142 }elseif($cfg['link_show_type'] == 3) { // 3. {alias} 性能一般
143 $len = strlen($cfg['link_show_end']);
144 if(empty($len) || substr($uri, -$len) == $cfg['link_show_end']) {
145 $newurl = $len ? substr($uri, 0, -$len) : $uri;
146
147 // 这处有些特别,如果没有设置别名,将用 cid_id 组合
148 preg_match('#(\d+)\_(\d+)#', $newurl, $mat);
149 if(isset($mat[2])) {
150 $_GET['control'] = 'show';
151 $_GET['action'] = 'index';
152 $_GET['cid'] = $mat[1];
153 $_GET['id'] = $mat[2];
154 return;
155 }elseif(preg_match('#\w+#', $newurl)) {
156 $row = $this->only_alias->get($newurl);
157 if(!empty($row)) {
158 $_GET['control'] = 'show';
159 $_GET['action'] = 'index';
160 $_GET['cid'] = $row['cid'];
161 $_GET['id'] = $row['id'];
162 return;
163 }
164 }
165 }
166 }else{ // 4. 正则表达式解析,性能较差
167 $quote = preg_quote($cfg['link_show'], '#');
168 $quote = strtr($quote, array(
169 '\{cid\}' => '(?<cid>\d+)',
170 '\{id\}' => '(?<id>\d+)',
171 '\{alias\}' => '(?<alias>\w+)',
172 '\{cate_alias\}' => '(?<cate_alias>\w+)',
173 '\{y\}' => '\d{4}',
174 '\{m\}' => '\d{2}',
175 '\{d\}' => '\d{2}'
176 ));
177
178 try{
179 preg_match('#'.$quote.'#', $uri, $mat);
180 }catch(Exception $e) {
181 $this->message(0, '内容URL伪静态规则不正确!');
182 }
183
184 if(isset($mat['cid']) && isset($mat['id'])) { // {cid} {id} 合组
185 $_GET['control'] = 'show';
186 $_GET['action'] = 'index';
187 $_GET['cid'] = $mat['cid'];
188 $_GET['id'] = $mat['id'];
189 return;
190 }elseif(isset($mat['cate_alias']) && isset($mat['id'])) { // {cate_alias} {id} 合组
191 $_GET['control'] = 'show';
192 $_GET['action'] = 'index';
193 $_GET['cid'] = $cate_arr[$mat['cate_alias']];
194 $_GET['id'] = $mat['id'];
195 return;
196 }elseif(isset($mat['alias'])) { // {alias}
197 // 这处有些特别,如果没有设置别名,将用 cid_id 组合
198 preg_match('#(\d+)\_(\d+)#', $mat['alias'], $mat2);
199 if(isset($mat2[2])) {
200 $_GET['control'] = 'show';
201 $_GET['action'] = 'index';
202 $_GET['cid'] = $mat2[1];
203 $_GET['id'] = $mat2[2];
204 return;
205 }
206
207 $row = $this->only_alias->get($mat['alias']);
208 if(!empty($row)) {
209 $_GET['control'] = 'show';
210 $_GET['action'] = 'index';
211 $_GET['cid'] = $row['cid'];
212 $_GET['id'] = $row['id'];
213 return;
214 }
215 }
216 }
217 }
218
219 // 伪静态时,如果 $uri 有值,但没有解析到相关 $_GET 时,就提示404
220 if(empty($_GET) && !empty($uri)) {
221 core::error404();
222 }
223
224 // 上面都不符合到这里解析
225 if(!isset($_GET['control'])) {
226 if(isset($_GET['u'])) {
227 $u = $_GET['u'];
228 unset($_GET['u']);
229 }elseif(!empty($_SERVER['PATH_INFO'])) {
230 $u = $_SERVER['PATH_INFO'];
231 }else{
232 $_GET = array();
233 $u = $_SERVER["QUERY_STRING"];
234 }
235
236 //清除URL后缀
237 $url_suffix = C('url_suffix');
238 if($url_suffix) {
239 $suf_len = strlen($url_suffix);
240 if(substr($u, -($suf_len)) == $url_suffix) $u = substr($u, 0, -($suf_len));
241 }
242
243 $uarr = explode('-', $u);
244 if(count($uarr) < 2) return;
245
246 if(isset($uarr[0])) {
247 $_GET['control'] = $uarr[0];
248 array_shift($uarr);
249 }
250
251 if(isset($uarr[0])) {
252 $_GET['action'] = $uarr[0];
253 array_shift($uarr);
254 }
255
256 $num = count($uarr);
257 for($i=0; $i<$num; $i+=2){
258 isset($uarr[$i+1]) && $_GET[$uarr[$i]] = $uarr[$i+1];
259 }
260 }
261 }
262}
263