📄 正在查看:admin/control/setting_control.class.php
大小:15,814 字节 · 修改:2014-04-04 08:11:12 · 行数:394
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 setting_control extends admin_control {
10 // 基本设置
11 public function index() {
12 if(empty($_POST)) {
13 $cfg = $this->kv->xget('cfg');
14 $input = array();
15 $input['webname'] = form::get_text('webname', $cfg['webname']);
16 $input['webdomain'] = form::get_text('webdomain', $cfg['webdomain']);
17 $input['webdir'] = form::get_text('webdir', $cfg['webdir']);
18 $input['webmail'] = form::get_text('webmail', $cfg['webmail']);
19 $input['tongji'] = form::get_textarea('tongji', $cfg['tongji']);
20 $input['beian'] = form::get_text('beian', $cfg['beian']);
21 $input['dis_comment'] = empty($cfg['dis_comment']) ? 0 : 1;
22
23 // hook admin_setting_control_index_after.php
24
25 $this->assign('input', $input);
26 $this->display();
27 }else{
28 _trim($_POST);
29 $this->kv->xset('webname', R('webname', 'P'), 'cfg');
30 $this->kv->xset('webdomain', R('webdomain', 'P'), 'cfg');
31 $this->kv->xset('webdir', R('webdir', 'P'), 'cfg');
32 $this->kv->xset('webmail', R('webmail', 'P'), 'cfg');
33 $this->kv->xset('tongji', R('tongji', 'P'), 'cfg');
34 $this->kv->xset('beian', R('beian', 'P'), 'cfg');
35 $this->kv->xset('dis_comment', (int)R('dis_comment', 'P'), 'cfg');
36
37 // hook admin_setting_control_index_post_after.php
38
39 $this->kv->save_changed();
40 $this->runtime->delete('cfg');
41
42 exit('{"err":0, "msg":"修改成功"}');
43 }
44 }
45
46 // SEO设置
47 public function seo() {
48 if(empty($_POST)) {
49 $cfg = $this->kv->xget('cfg');
50 $input = array();
51 $input['seo_title'] = form::get_text('seo_title', $cfg['seo_title']);
52 $input['seo_keywords'] = form::get_text('seo_keywords', $cfg['seo_keywords']);
53 $input['seo_description'] = form::get_textarea('seo_description', $cfg['seo_description']);
54
55 // hook admin_setting_control_seo_after.php
56
57 $this->assign('input', $input);
58 $this->display();
59 }else{
60 _trim($_POST);
61 $this->kv->xset('seo_title', R('seo_title', 'P'), 'cfg');
62 $this->kv->xset('seo_keywords', R('seo_keywords', 'P'), 'cfg');
63 $this->kv->xset('seo_description', R('seo_description', 'P'), 'cfg');
64
65 // hook admin_setting_control_seo_post_after.php
66
67 $this->kv->save_changed();
68 $this->runtime->delete('cfg');
69
70 exit('{"err":0, "msg":"修改成功"}');
71 }
72 }
73
74 // 链接设置
75 public function link() {
76 if(empty($_POST)) {
77 $software = R('SERVER_SOFTWARE', 'S');
78 $this->assign('software', $software);
79
80 $parseurl = $_ENV['_config']['twcms_parseurl'];
81 $cfg = $this->kv->xget('cfg');
82 $this->assign('cfg', $cfg);
83 $mk = R('mk');
84 $del = R('del');
85 $do = (int) R('do');
86 $this->assign('do', $do);
87
88 // 伪静态规则
89 $nginx = 'if (!-e $request_filename) {'."\n";
90 $nginx .= "\t".'rewrite ^'.$cfg['webdir'].'(.+) '.$cfg['webdir'].'index.php?rewrite=$1 last;'."\n";
91 $nginx .= '}';
92 $this->assign('nginx', $nginx);
93
94 $apache = '<IfModule mod_rewrite.c>'."\r\n";
95 $apache .= 'RewriteEngine On'."\r\n";
96 $apache .= 'RewriteBase '.$cfg['webdir']."\r\n";
97 $apache .= 'RewriteCond %{REQUEST_FILENAME} !-f'."\r\n";
98 $apache .= 'RewriteCond %{REQUEST_FILENAME} !-d'."\r\n";
99 $apache .= 'RewriteRule (.+) index.php?rewrite=$1 [L]'."\r\n";
100 $apache .= '</IfModule>';
101 $this->assign('apache', $apache);
102
103 // 创建.htaccess
104 $file_apache = TWCMS_PATH.'.htaccess';
105 $is_file_apache = is_file($file_apache);
106 $this->assign('is_file_apache', $is_file_apache);
107 if($mk == 'htaccess') {
108 $f = @fopen($file_apache, 'w');
109 if (!$f) {
110 exit('{"err":1, "msg":"无写入权限"}');
111 } else {
112 $bytes = fwrite($f, $apache);
113 fclose($f);
114 if($bytes > 0) {
115 exit('{"err":0, "msg":"创建 .htaccess 成功"}');
116 }else{
117 exit('{"err":1, "msg":"创建 .htaccess 失败"}');
118 }
119 }
120 }
121
122 // 删除.htaccess
123 if($del == 'htaccess') {
124 $ret = FALSE;
125 try{ $is_file_apache && $ret = unlink($file_apache); }catch(Exception $e) {}
126 if($ret) {
127 exit('{"err":0, "msg":"删除 .htaccess 成功"}');
128 }else{
129 exit('{"err":1, "msg":"删除 .htaccess 失败"}');
130 }
131 }
132
133 $iis = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
134 $iis .= '<configuration>'."\r\n";
135 $iis .= "\t".'<system.webServer>'."\r\n";
136 $iis .= "\t\t".'<rewrite>'."\r\n";
137 $iis .= "\t\t\t".'<rules>'."\r\n";
138 $iis .= "\t\t\t\t".'<rule name="TWCMS Rule '.$cfg['webdir'].'" stopProcessing="true">'."\r\n";
139 $iis .= "\t\t\t\t\t".'<match url="(.+)" ignoreCase="false" />'."\r\n";
140 $iis .= "\t\t\t\t\t".'<conditions logicalGrouping="MatchAll">'."\r\n";
141 $iis .= "\t\t\t\t\t\t".'<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />'."\r\n";
142 $iis .= "\t\t\t\t\t\t".'<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />'."\r\n";
143 $iis .= "\t\t\t\t\t".'</conditions>'."\r\n";
144 $iis .= "\t\t\t\t\t".'<action type="Rewrite" url="index.php?rewrite={R:1}" />'."\r\n";
145 $iis .= "\t\t\t\t".'</rule>'."\r\n";
146 $iis .= "\t\t\t".'</rules>'."\r\n";
147 $iis .= "\t\t".'</rewrite>'."\r\n";
148 $iis .= "\t".'</system.webServer>'."\r\n";
149 $iis .= '</configuration>';
150 $this->assign('iis', $iis);
151
152 // 创建web.config
153 $file_iis = TWCMS_PATH.'web.config';
154 $is_file_iis = is_file($file_iis);
155 $this->assign('is_file_iis', $is_file_iis);
156 if($mk == 'web_config') {
157 $f = @fopen($file_iis, 'w');
158 if (!$f) {
159 exit('{"err":1, "msg":"无写入权限"}');
160 } else {
161 $bytes = fwrite($f, $iis);
162 fclose($f);
163 if($bytes > 0) {
164 exit('{"err":0, "msg":"创建 web.config 成功"}');
165 }else{
166 exit('{"err":1, "msg":"创建 web.config 失败"}');
167 }
168 }
169 }
170
171 // 删除web.config
172 if($del == 'web_config') {
173 $ret = FALSE;
174 try{ $is_file_iis && $ret = unlink($file_iis); }catch(Exception $e) {}
175 if($ret) {
176 exit('{"err":0, "msg":"删除 web.config 成功"}');
177 }else{
178 exit('{"err":1, "msg":"删除 web.config 失败"}');
179 }
180 }
181
182 // IIS6
183 $path_file = $path_dir = '';
184 $dh = opendir(TWCMS_PATH);
185 while($file = readdir($dh)) {
186 if(preg_match('#^[\w]+$#', $file) && is_dir(TWCMS_PATH.$file)) {
187 $path_dir .= $file.'|';
188 }elseif(preg_match('#^\w[\w\.]+$#', $file) && is_file(TWCMS_PATH.$file)) {
189 $path_file .= preg_quote($file).'|';
190 }
191 }
192
193 $webdir = preg_quote($cfg['webdir']);
194 $iis6 = '[ISAPI_Rewrite]'."\r\n\r\n";
195 $iis6 .= 'RewriteRule '.$webdir.'('.trim($path_file, '|').') '.$webdir.'$1 [L]'."\r\n";
196 $iis6 .= 'RewriteRule '.$webdir.'('.trim($path_dir, '|').')/(.*) '.$webdir.'$1/$2 [L]'."\r\n";
197 $iis6 .= 'RewriteRule '.$webdir.'(.+) '.$webdir.'index\.php\?rewrite=$1 [L]';
198 $this->assign('iis6', $iis6);
199
200 // 创建httpd.ini
201 $file_iis6 = $_SERVER["DOCUMENT_ROOT"].'/httpd.ini';
202 $is_file_iis6 = is_file($file_iis6);
203 $this->assign('is_file_iis6', $is_file_iis6);
204 if($mk == 'httpd_ini') {
205 $f = @fopen($file_iis6, 'w');
206 if (!$f) {
207 exit('{"err":1, "msg":"无写入权限"}');
208 } else {
209 $bytes = fwrite($f, $iis6);
210 fclose($f);
211 if($bytes > 0) {
212 exit('{"err":0, "msg":"创建 httpd.ini 成功"}');
213 }else{
214 exit('{"err":1, "msg":"创建 httpd.ini 失败"}');
215 }
216 }
217 }
218
219 // 删除httpd.ini
220 if($del == 'httpd_ini') {
221 $ret = FALSE;
222 try{ $is_file_iis6 && $ret = unlink($file_iis6); }catch(Exception $e) {}
223 if($ret) {
224 exit('{"err":0, "msg":"删除 httpd.ini 成功"}');
225 }else{
226 exit('{"err":1, "msg":"删除 httpd.ini 失败"}');
227 }
228 }
229
230 $input = array();
231 $input['parseurl'] = form::loop('radio', 'parseurl', array('0'=>'动态', '1'=>'伪静态'), $parseurl, ' &nbsp; &nbsp;');
232 $input['link_show'] = form::get_text('link_show', $cfg['link_show'], 'inp wa');
233 $input['link_cate_end'] = form::get_text('link_cate_end', $cfg['link_cate_end'], 'inp wb');
234 $input['link_cate_page_pre'] = form::get_text('link_cate_page_pre', $cfg['link_cate_page_pre'], 'inp wb');
235 $input['link_cate_page_end'] = form::get_text('link_cate_page_end', $cfg['link_cate_page_end'], 'inp wb');
236 $input['link_tag_pre'] = form::get_text('link_tag_pre', $cfg['link_tag_pre'], 'inp wb');
237 $input['link_tag_end'] = form::get_text('link_tag_end', $cfg['link_tag_end'], 'inp wb');
238 $input['link_comment_pre'] = form::get_text('link_comment_pre', $cfg['link_comment_pre'], 'inp wb');
239 $input['link_comment_end'] = form::get_text('link_comment_end', $cfg['link_comment_end'], 'inp wb');
240 $input['link_index_end'] = form::get_text('link_index_end', $cfg['link_index_end'], 'inp wb');
241 $this->assign('input', $input);
242
243 // hook admin_setting_control_link_after.php
244
245 $this->display();
246 }else{
247 _trim($_POST);
248 // 伪静态开关
249 $parseurl = (int)R('parseurl', 'P');
250 $file = APP_PATH.'config/config.inc.php';
251 if(!_is_writable($file)) exit('{"err":1, "msg":"配置文件 twcms/config/config.inc.php 不可写"}');
252 $s = file_get_contents($file);
253 $s = preg_replace("#'twcms_parseurl'\s*=>\s*\d,#", "'twcms_parseurl' => {$parseurl},", $s);
254 if(!file_put_contents($file, $s)) exit('{"err":1, "msg":"写入 config.inc.php 失败"}');
255
256 // 关闭伪静态时,不需要更改伪静态参数
257 if($parseurl == 0) {
258 $this->runtime->truncate();
259 exit('{"err":0, "msg":"修改成功"}');
260 }
261
262 // 智能生成内容链接参数 (四种情况,性能方面依次排列)
263 $link_show = R('link_show', 'P');
264 if(substr($link_show, 0, 10) == '{cid}/{id}' && strpos($link_show, '{', 10) === FALSE) {
265 $link_show_type = 1;
266 $link_show_end = (string)substr($link_show, 10);
267 }elseif(substr($link_show, 0, 17) == '{cate_alias}/{id}' && strpos($link_show, '{', 17) === FALSE) {
268 $link_show_type = 2;
269 $link_show_end = (string)substr($link_show, 17);
270 }elseif(substr($link_show, 0, 7) == '{alias}' && strpos($link_show, '{', 7) === FALSE) {
271 $link_show_type = 3;
272 $link_show_end = (string)substr($link_show, 7);
273 }else{
274 $link_show_type = 4;
275 $link_show_end = '';
276 }
277 $this->kv->xset('link_show', $link_show, 'cfg');
278 $this->kv->xset('link_show_type', $link_show_type, 'cfg');
279 $this->kv->xset('link_show_end', $link_show_end, 'cfg');
280
281 $link_cate_page_pre = R('link_cate_page_pre', 'P');
282 $link_cate_page_end = R('link_cate_page_end', 'P');
283 $link_cate_end = R('link_cate_end', 'P');
284 $link_tag_pre = R('link_tag_pre', 'P');
285 $link_tag_end = R('link_tag_end', 'P');
286 $link_comment_pre = R('link_comment_pre', 'P');
287 $link_comment_end = R('link_comment_end', 'P');
288 $link_index_end = R('link_index_end', 'P');
289
290 // 暂时不考虑过滤 标签URL前缀 和 评论URL后缀 重复问题
291 if(empty($link_cate_page_pre)) exit('{"err":1, "msg":"分类URL前缀不能为空"}');
292 if(empty($link_cate_page_end)) exit('{"err":1, "msg":"分类URL后缀不能为空"}');
293 if(empty($link_cate_end)) exit('{"err":1, "msg":"分类URL首页后缀不能为空"}');
294 if(empty($link_tag_pre)) exit('{"err":1, "msg":"标签URL前缀不能为空"}');
295 if(empty($link_tag_end)) exit('{"err":1, "msg":"标签URL后缀不能为空"}');
296 if(empty($link_comment_pre)) exit('{"err":1, "msg":"评论URL前缀不能为空"}');
297 if(empty($link_comment_end)) exit('{"err":1, "msg":"评论URL后缀不能为空"}');
298 if(empty($link_index_end)) exit('{"err":1, "msg":"首页分页URL后缀不能为空"}');
299
300 $this->kv->xset('link_index_end', $link_index_end, 'cfg');
301 $this->kv->xset('link_cate_page_pre', $link_cate_page_pre, 'cfg');
302 $this->kv->xset('link_cate_page_end', $link_cate_page_end, 'cfg');
303 $this->kv->xset('link_cate_end', $link_cate_end, 'cfg');
304 $this->kv->xset('link_tag_pre', $link_tag_pre, 'cfg');
305 $this->kv->xset('link_tag_end', $link_tag_end, 'cfg');
306 $this->kv->xset('link_comment_pre', $link_comment_pre, 'cfg');
307 $this->kv->xset('link_comment_end', $link_comment_end, 'cfg');
308
309 // hook admin_setting_control_link_post_after.php
310
311 $this->kv->save_changed();
312 $this->runtime->truncate();
313
314 exit('{"err":0, "msg":"修改成功"}');
315 }
316 }
317
318 // 上传设置
319 public function attach() {
320 if(empty($_POST)) {
321 $cfg = $this->kv->xget('cfg');
322 $input = array();
323 $input['up_img_ext'] = form::get_text('up_img_ext', $cfg['up_img_ext'], 'inp wa');
324 $input['up_img_max_size'] = form::get_number('up_img_max_size', $cfg['up_img_max_size'], 'inp ws');
325 $input['up_file_ext'] = form::get_text('up_file_ext', $cfg['up_file_ext'], 'inp wa');
326 $input['up_file_max_size'] = form::get_number('up_file_max_size', $cfg['up_file_max_size'], 'inp ws');
327
328 // hook admin_setting_control_attach_after.php
329
330 $this->assign('input', $input);
331 $this->display();
332 }else{
333 _trim($_POST);
334 $this->kv->xset('up_img_ext', R('up_img_ext', 'P'), 'cfg');
335 $this->kv->xset('up_img_max_size', R('up_img_max_size', 'P'), 'cfg');
336 $this->kv->xset('up_file_ext', R('up_file_ext', 'P'), 'cfg');
337 $this->kv->xset('up_file_max_size', R('up_file_max_size', 'P'), 'cfg');
338
339 // hook admin_setting_control_attach_post_after.php
340
341 $this->kv->save_changed();
342 $this->runtime->delete('cfg');
343
344 exit('{"err":0, "msg":"修改成功"}');
345 }
346 }
347
348 // 图片设置
349 public function image() {
350 if(empty($_POST)) {
351 $cfg = $this->kv->xget('cfg');
352 $input = array();
353 $input['thumb_article_w'] = form::get_number('thumb_article_w', $cfg['thumb_article_w'], 'inp ws');
354 $input['thumb_article_h'] = form::get_number('thumb_article_h', $cfg['thumb_article_h'], 'inp ws');
355 $input['thumb_product_w'] = form::get_number('thumb_product_w', $cfg['thumb_product_w'], 'inp ws');
356 $input['thumb_product_h'] = form::get_number('thumb_product_h', $cfg['thumb_product_h'], 'inp ws');
357 $input['thumb_photo_w'] = form::get_number('thumb_photo_w', $cfg['thumb_photo_w'], 'inp ws');
358 $input['thumb_photo_h'] = form::get_number('thumb_photo_h', $cfg['thumb_photo_h'], 'inp ws');
359
360 $input['thumb_type'] = form::loop('radio', 'thumb_type', array('1'=>'补白', '2'=>'居中', '3'=>'上左'), $cfg['thumb_type'], ' &nbsp; &nbsp;');
361 $input['thumb_quality'] = form::get_number('thumb_quality', $cfg['thumb_quality'], 'inp ws');
362
363 $cfg['watermark_pos'] = isset($cfg['watermark_pos']) ? (int)$cfg['watermark_pos'] : 0;
364 $input['watermark_pct'] = form::get_number('watermark_pct', $cfg['watermark_pct'], 'inp ws');
365
366 // hook admin_setting_control_image_after.php
367
368 $this->assign('input', $input);
369 $this->assign('cfg', $cfg);
370 $this->display();
371 }else{
372 $this->kv->xset('thumb_article_w', (int) R('thumb_article_w', 'P'), 'cfg');
373 $this->kv->xset('thumb_article_h', (int) R('thumb_article_h', 'P'), 'cfg');
374 $this->kv->xset('thumb_product_w', (int) R('thumb_product_w', 'P'), 'cfg');
375 $this->kv->xset('thumb_product_h', (int) R('thumb_product_h', 'P'), 'cfg');
376 $this->kv->xset('thumb_photo_w', (int) R('thumb_photo_w', 'P'), 'cfg');
377 $this->kv->xset('thumb_photo_h', (int) R('thumb_photo_h', 'P'), 'cfg');
378 $this->kv->xset('thumb_type', (int) R('thumb_type', 'P'), 'cfg');
379 $this->kv->xset('thumb_quality', (int) R('thumb_quality', 'P'), 'cfg');
380 $this->kv->xset('watermark_pos', (int) R('watermark_pos', 'P'), 'cfg');
381 $this->kv->xset('watermark_pct', (int) R('watermark_pct', 'P'), 'cfg');
382
383 // hook admin_setting_control_image_post_after.php
384
385 $this->kv->save_changed();
386 $this->runtime->delete('cfg');
387
388 exit('{"err":0, "msg":"修改成功"}');
389 }
390 }
391
392 // hook admin_setting_control_after.php
393}
394