📄 正在查看:admin/control/theme_control.class.php
大小:3,805 字节 · 修改:2014-01-23 01:42:52 · 行数:150
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 theme_control extends admin_control {
10 // 主题设置
11 public function index() {
12 // hook admin_theme_control_index_before.php
13
14 $cfg = $this->runtime->xget('cfg');
15 $k = &$cfg['theme'];
16 $themes = self::get_theme_all();
17
18 // 启用的主题放在第一
19 if(isset($themes[$k])) {
20 $tmp = array();
21 $tmp[$k] = $themes[$k];
22 unset($themes[$k]);
23 $themes = $tmp + $themes;
24 }
25
26 $this->assign('themes', $themes);
27 $this->assign('theme', $cfg['theme']);
28
29 // hook admin_theme_control_index_after.php
30
31 $this->display();
32 }
33
34 // 启用主题
35 public function enable() {
36 $theme = R('theme', 'P');
37 $this->check_theme($theme);
38
39 $this->kv->xset('theme', $theme, 'cfg');
40 $this->kv->save_changed();
41 $this->runtime->delete('cfg');
42 $this->clear_cache();
43 E(0, '启用成功!');
44 }
45
46 // 删除主题
47 public function delete() {
48 $theme = R('theme', 'P');
49 $this->check_theme($theme);
50
51 if(_rmdir(APP_PATH.'view/'.$theme)) {
52 E(0, '删除完成!');
53 }else{
54 E(1, '删除出错!');
55 }
56 }
57
58 // 在线安装插件
59 public function install_theme() {
60 $dir = R('dir');
61
62 if(empty($dir)) $this->install_tips('主题目录名不能为空!');
63 if(preg_match('/\W/', $dir)) $this->install_tips('主题目录名不正确!');
64 $install_dir = APP_PATH.'view/'.$dir;
65 if(is_dir($install_dir)) $this->install_tips('此主题已经安装过了!');
66
67 if(function_exists('set_time_limit')) {
68 set_time_limit(600); // 10分钟
69 $timeout = 300;
70 }else{
71 $timeout = 20;
72 }
73
74 $url = 'http://www.twcms.com/app/download.php?theme='.$dir;
75 try{
76 $s = fetch_url($url, $timeout);
77 }catch(Exception $e) {
78 $this->install_tips('下载主题出错!');
79 }
80 if(empty($s) || substr($s, 0, 2) != 'PK') {
81 $this->install_tips('下载主题失败!');
82 }
83 $zipfile = $install_dir.'.zip';
84 try{
85 file_put_contents($zipfile, $s);
86 }catch(Exception $e) {
87 $this->install_tips('主题写入出错,写入权限不对?');
88 }
89 try{
90 kp_zip::unzip($zipfile, $install_dir);
91 }catch(Exception $e) {
92 $this->install_tips('解压主题文件出错!');
93 }
94 unlink($zipfile);
95 $this->install_tips('下载并解压完成!', 0);
96 }
97
98 // 在线安装提示
99 private function install_tips($s, $err = 1) {
100 echo '$(".ajaxtips b").html("'.$s.'");';
101 echo 'var err = '.$err.';';
102 exit;
103 }
104
105 // 检查是否为合法的主题名
106 private function check_theme($dir) {
107 if(empty($dir)) {
108 E(1, '主题目录名不能为空!');
109 }elseif(preg_match('/\W/', $dir)) {
110 E(1, '主题目录名不正确!');
111 }elseif(!is_dir(APP_PATH.'view/'.$dir)) {
112 E(1, '主题目录名不存在!');
113 }
114 }
115
116 // 读取所有主题
117 private function get_theme_all() {
118 $dir = APP_PATH.'view/';
119 $files = _scandir($dir);
120 $themes = array();
121 foreach($files as $file) {
122 if(preg_match('/\W/', $file)) continue;
123 $path = $dir.'/'.$file;
124 $info = $path.'/info.ini';
125 if(filetype($path) == 'dir' && is_file($info) && $lines = file($info)) {
126 $themes[$file] = self::get_theme_info($lines);
127 }
128 }
129 return $themes;
130 }
131
132 // 读取主题信息
133 private function get_theme_info($lines) {
134 $res = array();
135 foreach($lines as $str) {
136 $arr = explode('=', trim($str));
137 $k = trim($arr[0]);
138 $v = isset($arr[1]) ? trim($arr[1]) : '';
139 if($k == 'brief') {
140 $res[$k] = strip_tags($v, '<br>');
141 }elseif(in_array($k, array('name', 'version', 'update', 'author', 'authorurl'))) {
142 $res[$k] = strip_tags($v);
143 }
144 }
145 return $res;
146 }
147
148 // hook admin_theme_control_after.php
149}
150