ThemeController.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 主题管理
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class ThemeController extends CommonController
  11. {
  12. private $html_path;
  13. private $static_path;
  14. /**
  15. * [_initialize 前置操作-继承公共前置方法]
  16. * @author Devil
  17. * @blog http://gong.gg/
  18. * @version 0.0.1
  19. * @datetime 2016-12-03T12:39:08+0800
  20. */
  21. public function _initialize()
  22. {
  23. // 调用父类前置方法
  24. parent::_initialize();
  25. // 登录校验
  26. $this->Is_Login();
  27. // 权限校验
  28. $this->Is_Power();
  29. // 静态目录和html目录
  30. $this->html_path = 'Application'.DS.'Home'.DS.'View'.DS;
  31. $this->static_path = 'Public'.DS.'Home'.DS;
  32. }
  33. /**
  34. * [Index 列表]
  35. * @author Devil
  36. * @blog http://gong.gg/
  37. * @version 0.0.1
  38. * @datetime 2016-12-06T21:31:53+0800
  39. */
  40. public function Index()
  41. {
  42. // 导航参数
  43. $view_type = I('view_type', 'home');
  44. $this->assign('view_type', $view_type);
  45. // 模板
  46. switch($view_type)
  47. {
  48. // 模板安装
  49. case 'upload':
  50. $this->display('Upload');
  51. break;
  52. // 当前模板
  53. default:
  54. // 模板列表
  55. $this->assign('data', $this->GetThemeList());
  56. // 默认主题
  57. $theme = S('cache_common_default_theme_data');
  58. $this->assign('theme', empty($theme) ? 'Default' : $theme);
  59. $this->display('Index');
  60. }
  61. }
  62. /**
  63. * [GetThemeList 获取模板列表]
  64. * @author Devil
  65. * @blog http://gong.gg/
  66. * @version 0.0.1
  67. * @datetime 2017-05-10T10:24:40+0800
  68. * @return [array] [模板列表]
  69. */
  70. private function GetThemeList()
  71. {
  72. $result = array();
  73. $dir = 'Application'.DS.'Home'.DS.'View'.DS;
  74. if(is_dir($dir))
  75. {
  76. if($dh = opendir($dir))
  77. {
  78. $default_preview = 'Public'.DS.'Common'.DS.'Images'.DS.'default-preview.jpg';
  79. while(($temp_file = readdir($dh)) !== false)
  80. {
  81. $config = $dir.$temp_file.DS.'config.json';
  82. if(!file_exists($config))
  83. {
  84. continue;
  85. }
  86. // 读取配置文件
  87. $data = json_decode(file_get_contents($config), true);
  88. if(!empty($data) && is_array($data))
  89. {
  90. if(empty($data['name']) || empty($data['ver']) || empty($data['author']))
  91. {
  92. continue;
  93. }
  94. $result[] = array(
  95. 'theme' => $temp_file,
  96. 'name' => I('data.name', '', '',$data),
  97. 'ver' => str_replace(array(',',','), ', ', I('data.ver', '', '',$data)),
  98. 'author' => I('data.author', '', '',$data),
  99. 'home' => I('data.home', '', '',$data),
  100. 'preview' => file_exists($dir.$temp_file.DS.'preview.jpg') ? $dir.$temp_file.DS.'preview.jpg' : $default_preview,
  101. );
  102. }
  103. }
  104. closedir($dh);
  105. }
  106. }
  107. return $result;
  108. }
  109. /**
  110. * [Save 数据保存]
  111. * @author Devil
  112. * @blog http://gong.gg/
  113. * @version 0.0.1
  114. * @datetime 2017-02-05T20:12:30+0800
  115. */
  116. public function Save()
  117. {
  118. // 配置更新
  119. $this->MyConfigSave();
  120. }
  121. /**
  122. * [Delete 删除]
  123. * @author Devil
  124. * @blog http://gong.gg/
  125. * @version 0.0.1
  126. * @datetime 2016-12-09T21:13:47+0800
  127. */
  128. public function Delete()
  129. {
  130. // 是否ajax
  131. if(!IS_AJAX)
  132. {
  133. $this->error(L('common_unauthorized_access'));
  134. }
  135. // 主题
  136. $id = I('id');
  137. // 默认主题
  138. $theme = S('cache_common_default_theme_data');
  139. $theme = empty($theme) ? 'Default' : $theme;
  140. // 不能删除正在使用的主题
  141. if($theme == $id)
  142. {
  143. $this->ajaxReturn(L('theme_delete_error'), -2);
  144. }
  145. // 开始删除主题
  146. if(DelDirFile($this->html_path.$id, true) && DelDirFile($this->static_path.$id, true))
  147. {
  148. $this->ajaxReturn(L('common_operation_delete_success'));
  149. } else {
  150. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  151. }
  152. }
  153. /**
  154. * [Upload 模板上传安装]
  155. * @author Devil
  156. * @blog http://gong.gg/
  157. * @version 0.0.1
  158. * @datetime 2017-05-10T16:27:09+0800
  159. */
  160. public function Upload()
  161. {
  162. // 是否ajax
  163. if(!IS_AJAX)
  164. {
  165. $this->error(L('common_unauthorized_access'));
  166. }
  167. // 文件上传校验
  168. $error = FileUploadError('theme');
  169. if($error !== true)
  170. {
  171. $this->ajaxReturn($error, -1);
  172. }
  173. // 文件格式化校验
  174. $type = array('application/zip', 'application/octet-stream');
  175. if(!in_array($_FILES['theme']['type'], $type))
  176. {
  177. $this->ajaxReturn(L('theme_upload_error'), -2);
  178. }
  179. // 开始解压文件
  180. $resource = zip_open($_FILES['theme']['tmp_name']);
  181. while(($temp_resource = zip_read($resource)) !== false)
  182. {
  183. if(zip_entry_open($resource, $temp_resource))
  184. {
  185. // 当前压缩包中项目名称
  186. $file = zip_entry_name($temp_resource);
  187. // 排除临时文件和临时目录
  188. if(strpos($file, '/.') === false && strpos($file, '__') === false)
  189. {
  190. // 拼接路径
  191. if(strpos($file, '_Html') !== false)
  192. {
  193. $file = $this->html_path.$file;
  194. } else if(strpos($file, '_Static') !== false)
  195. {
  196. $file = $this->static_path.$file;
  197. } else {
  198. continue;
  199. }
  200. $file = str_replace(array('_Static/', '_Html/'), '', $file);
  201. // 截取文件路径
  202. $file_path = substr($file, 0, strrpos($file, '/'));
  203. // 路径不存在则创建
  204. if(!is_dir($file_path))
  205. {
  206. mkdir($file_path, 0777, true);
  207. }
  208. // 如果不是目录则写入文件
  209. if(!is_dir($file))
  210. {
  211. // 读取这个文件
  212. $file_size = zip_entry_filesize($temp_resource);
  213. $file_content = zip_entry_read($temp_resource, $file_size);
  214. file_put_contents($file, $file_content);
  215. }
  216. // 关闭目录项
  217. zip_entry_close($temp_resource);
  218. }
  219. }
  220. }
  221. $this->ajaxReturn(L('common_operation_success'));
  222. }
  223. }
  224. ?>