template.ctrl.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('extension');
  8. load()->model('cloud');
  9. $dos = array('installed', 'not_install', 'uninstall', 'install', 'upgrade', 'check_upgrade', 'get_upgrade_info');
  10. $do = in_array($do, $dos) ? $do : 'installed';
  11. if ($do == 'get_upgrade_info') {
  12. $template_name = $_GPC['name'];
  13. if (!empty($template_name)) {
  14. $template_info = pdo_get('site_templates', array('name' => $template_name));
  15. if (!empty($template_info)) {
  16. $cloud_t_upgrade_info = cloud_t_upgradeinfo($template_name); if (is_error($cloud_t_upgrade_info)) {
  17. iajax(1, $cloud_t_upgrade_info['message'], '');
  18. }
  19. $template_upgrade_info = array(
  20. 'name' => $cloud_t_upgrade_info['name'],
  21. 'title' => $template_info['title'],
  22. 'version' => $cloud_t_upgrade_info['version'],
  23. 'branches' => $cloud_t_upgrade_info['branches'],
  24. 'site_branch' => $cloud_t_upgrade_info['branches'][$cloud_t_upgrade_info['version']['branch_id']],
  25. );
  26. iajax(0, $template_upgrade_info, '');
  27. } else {
  28. iajax(1, '模板不存在', '');
  29. }
  30. }
  31. }
  32. if ($do == 'check_upgrade') {
  33. $template_list = $_GPC['template'];
  34. if (empty($template_list) || !is_array($template_list)) {
  35. iajax(1, '');
  36. }
  37. $cloud_template_list = cloud_t_query();
  38. if (is_error($cloud_template_list)) {
  39. $cloud_template_list = array();
  40. }
  41. foreach ($template_list as &$template) {
  42. $manifest = ext_template_manifest($template['name'], false);
  43. if (!empty($manifest)&& is_array($manifest)) {
  44. if (version_compare($template['version'], $manifest['application']['version']) == '-1') {
  45. $template['upgrade'] = 1;
  46. } else {
  47. $template['upgrade'] = 0;
  48. }
  49. $template['from'] = 'local';
  50. } else {
  51. if (in_array($template['name'], array_keys($cloud_template_list))) {
  52. $template['from'] = 'cloud';
  53. $site_branch = $cloud_template_list[$template['name']]['branch']; $cloud_branch_version = $cloud_template_list[$template['name']]['branches'][$site_branch]['version']; $best_branch = current($cloud_template_list[$template['name']]['branches']);
  54. if (version_compare($template['version'], $cloud_branch_version) == -1 || ($cloud_template_list[$template['name']]['branch'] < $best_branch['id'])) {
  55. $template['upgrade'] = 1;
  56. } else {
  57. $template['upgrade'] = 0;
  58. }
  59. }
  60. }
  61. }
  62. iajax(0, $template_list, '');
  63. }
  64. if ($do == 'installed') {
  65. $pindex = max(1, $_GPC['page']);
  66. $pagesize = 20;
  67. $param = empty($_GPC['type']) ? array() : array('type' => $_GPC['type']);
  68. if (!empty($_GPC['keyword'])) {
  69. $param['title LIKE'] = "%". trim($_GPC['keyword'])."%";
  70. }
  71. if (!$_W['isfounder'] || user_is_vice_founder()) {
  72. $group_info = pdo_get('users_founder_group', array('id' => $_W['user']['groupid']));
  73. $group_info['package'] = unserialize($group_info['package']);
  74. $uni_groups = pdo_getall('uni_group', array('uniacid' => 0, 'id' => $group_info['package']), array(), '', array('id DESC'));
  75. $templates = array();
  76. foreach ($uni_groups as $key => $group) {
  77. $ids = unserialize($group['templates']);
  78. foreach ($ids as $val) {
  79. $templates[] = $val;
  80. }
  81. }
  82. $param['id'] = $templates;
  83. }
  84. $template_list = pdo_getslice('site_templates', $param, array($pindex, $pagesize), $total, array(), 'name');
  85. $pager = pagination($total, $pindex, $pagesize);
  86. $temtypes = ext_template_type();
  87. }
  88. if ($do == 'not_install') {
  89. $installed_template = pdo_getall("site_templates", array(), array(), 'name');
  90. $uninstall_template = array();
  91. /*qwj
  92. $cloud_template = cloud_t_query();
  93. if(!is_error($cloud_template)) {
  94. $cloudUninstallThemes = array();
  95. foreach($cloud_template as $name => $template_info) {
  96. if (empty($template_info) || !is_array($template_info)) {
  97. continue;
  98. }
  99. if(!in_array(strtolower($name), array_keys($installed_template))) {
  100. if (!empty($_GPC['keyword']) && !strexists($template_info['title'], trim($_GPC['keyword']))) {
  101. continue;
  102. }
  103. $uninstall_template[$name] = array(
  104. 'name' => $template_info['name'],
  105. 'title' => $template_info['title'],
  106. 'logo' => $template_info['logo'],
  107. 'from' => 'cloud'
  108. );
  109. }
  110. }
  111. } */
  112. $path = IA_ROOT . '/app/themes';
  113. if (is_dir($path)) {
  114. $dir_tree = glob($path . '/*');
  115. if (!empty($dir_tree)) {
  116. foreach ($dir_tree as $modulepath) {
  117. $modulepath = str_replace(IA_ROOT. "/app/themes/", '', $modulepath);
  118. $manifest = ext_template_manifest($modulepath, false);
  119. if (!empty($_GPC['title']) && !strexists($manifest['title'], trim($_GPC['title']))) {
  120. continue;
  121. }
  122. if(!empty($manifest) && !in_array($manifest['name'], array_keys($installed_template))) {
  123. $uninstall_template[$manifest['name']] = $manifest;
  124. }
  125. }
  126. }
  127. }
  128. $total = count($uninstall_template);
  129. if (!empty($uninstall_template) && is_array($uninstall_template)) {
  130. $pindex = max(1, $_GPC['page']);
  131. $uninstall_template = array_slice($uninstall_template, ($pindex - 1) * 20, 20);
  132. }
  133. $pager = pagination($total, $pindex, 20);
  134. }
  135. if ($do == 'uninstall') {
  136. $template = pdo_getcolumn('site_templates', array('id' => intval($_GPC['id'])), 'name');
  137. if($template == 'default') {
  138. itoast('默认模板不能卸载', url('system/template/not_install'), 'error');
  139. }
  140. if (pdo_delete('site_templates', array('id' => intval($_GPC['id'])))) {
  141. pdo_delete('site_styles',array('templateid' => intval($_GPC['id'])));
  142. pdo_delete('site_styles_vars',array('templateid' => intval($_GPC['id'])));
  143. itoast('模板移除成功, 你可以重新安装, 或者直接移除文件来安全删除!', referer(), 'success');
  144. } else {
  145. itoast('模板移除失败, 请联系模板开发者!', url('system/template/not_install'), 'error');
  146. }
  147. }
  148. if ($do == 'install') {
  149. if(empty($_W['isfounder'])) {
  150. itoast('您没有安装模块的权限', url('system/template/not_install'), 'error');
  151. }
  152. $template_name = $_GPC['templateid'];
  153. if (pdo_get('site_templates', array('name' => $template_name))) {
  154. itoast('模板已经安装或是唯一标识已存在!', url('system/template/not_install'), 'error');
  155. }
  156. $manifest = ext_template_manifest($template_name, false);
  157. if (!empty($manifest)) {
  158. $prepare_result = cloud_t_prepare($template_name);
  159. if(is_error($prepare_result)) {
  160. itoast($prepare_result['message'], url('system/template/not_install'), 'error');
  161. }
  162. }
  163. if (empty($manifest)) {
  164. $cloud_result = cloud_prepare();
  165. if(is_error($cloud_result)) {
  166. itoast($cloud_result['message'], url('cloud/profile'), 'error');
  167. }
  168. $template_info = cloud_t_info($template_name);
  169. if (!is_error($template_info)) {
  170. if (empty($_GPC['flag'])) {
  171. header('location: ' . url('cloud/process', array('t' => $template_name)));
  172. exit;
  173. } else {
  174. $packet = cloud_t_build($template_name);
  175. $manifest = ext_template_manifest_parse($packet['manifest']);
  176. $manifest['version'] = $packet['version'];
  177. }
  178. } else {
  179. itoast($template_info['message'], '', 'error');
  180. }
  181. }
  182. unset($manifest['settings']);
  183. $module_group = uni_groups();
  184. if(!$_W['ispost'] || empty($_GPC['flag'])) {
  185. template('system/module-group');
  186. exit;
  187. }
  188. $post_groups = $_GPC['group'];
  189. $tid = intval($_GPC['tid']);
  190. $template_name = $_GPC['templateid'];
  191. if (empty($manifest)) {
  192. itoast('模板安装配置文件不存在或是格式不正确!', '', 'error');
  193. }
  194. if ($manifest['name'] != $template_name) {
  195. itoast('安装模板与文件标识不符,请重新安装', '', 'error');
  196. }
  197. if (pdo_get('site_templates', array('name' => $manifest['name']))) {
  198. itoast('模板已经安装或是唯一标识已存在!', url('system/template/not_install'), 'error');
  199. }
  200. if (pdo_insert('site_templates', $manifest)) {
  201. $tid = pdo_insertid();
  202. } else {
  203. itoast('模板安装失败, 请联系模板开发者!', '', 'error');
  204. }
  205. if($template_name && $post_groups) {
  206. if (!pdo_get('site_templates', array('id' => $tid))) {
  207. itoast('指定模板不存在!', '', 'error');
  208. }
  209. foreach($post_groups as $post_group) {
  210. $group = pdo_get('uni_group', array('id' => $post_group));
  211. if(empty($group)) {
  212. continue;
  213. }
  214. $group['templates'] = iunserializer($group['templates']);
  215. if(in_array($tid, $group['templates'])) {
  216. continue;
  217. }
  218. $group['templates'][] = $tid;
  219. $group['templates'] = iserializer($group['templates']);
  220. pdo_update('uni_group', $group, array('id' => $post_group));
  221. }
  222. }
  223. itoast('模板安装成功, 请按照【公众号服务套餐】【用户组】来分配权限!', url('system/template'), 'success');
  224. }
  225. if($do == 'upgrade') {
  226. $template_name = $_GPC['templateid'];
  227. $template = pdo_get("site_templates", array('name' => $template_name));
  228. if (empty($template)) {
  229. itoast('模板已经被卸载或是不存在!', url('system/template'), 'error');
  230. }
  231. if (!is_error($info)) {
  232. if (!empty($_GPC['flag'])) {
  233. $packet = cloud_t_build($template_name);
  234. $manifest = ext_template_manifest_parse($packet['manifest']);
  235. }
  236. }
  237. if (empty($manifest)) {
  238. itoast('模块安装配置文件不存在或是格式不正确!', '', 'error');
  239. }
  240. if(version_compare($template['version'], $packet['version']) != -1) {
  241. itoast('已安装的模板版本不低于要更新的版本, 操作无效.', '', 'error');
  242. }
  243. pdo_update('site_templates', array('version' => $packet['version']), array('id' => $template['id']));
  244. itoast('模板更新成功!', url('system/template'), 'success');
  245. }
  246. template('system/template');