人人商城

theme.ctrl.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. $dos = array('installed', 'prepared', 'install', 'refresh', 'uninstall', 'web', 'batch-install', 'designer', 'check', 'upgrade');
  8. $do = in_array($do, $dos) ? $do : 'installed';
  9. load()->model('extension');
  10. load()->model('cloud');
  11. if($do == 'installed') {
  12. $_W['page']['title'] = '已安装的微站风格 - 风格主题 - 扩展';
  13. $templateids = array();
  14. $where = (empty($_GPC['type']) || $_GPC['type'] == 'all') ? '' : " WHERE `type` = '{$_GPC['type']}'";
  15. $templates = pdo_fetchall("SELECT * FROM ".tablename('site_templates') . $where);
  16. foreach($templates as $tpl) {
  17. $templateids[] = $tpl['name'];
  18. }
  19. $temtypes = ext_template_type();
  20. template('extension/theme');
  21. }
  22. if($do == 'prepared') {
  23. $_W['page']['title'] = '安装微站风格 - 风格主题 - 扩展';
  24. $templateids = array();
  25. $templates = pdo_fetchall("SELECT * FROM ".tablename('site_templates'));
  26. foreach($templates as $tpl) {
  27. $templateids[] = $tpl['name'];
  28. }
  29. $uninstallTemplates = array();
  30. $path = IA_ROOT . '/app/themes/';
  31. if (is_dir($path)) {
  32. if ($handle = opendir($path)) {
  33. while (false !== ($modulepath = readdir($handle))) {
  34. $manifest = ext_template_manifest($modulepath, false);
  35. if(!empty($manifest) && !in_array($manifest['name'], $templateids)) {
  36. $uninstallTemplates[$manifest['name']] = $manifest;
  37. $uninstallTemplates_title[$manifest['name']] = $manifest['title'];
  38. $templateids[] = $manifest['name'];
  39. }
  40. }
  41. }
  42. }
  43. $prepare_templates = json_encode(array_keys($uninstallTemplates));
  44. $prepare_templates_title = json_encode($uninstallTemplates_title);
  45. template('extension/theme');
  46. }
  47. if($do == 'batch-install') {
  48. if($_W['ispost']) {
  49. $id = $_GPC['templateid'];
  50. $m = ext_template_manifest($id);
  51. if (empty($m)) {
  52. exit('error');
  53. }
  54. if (pdo_fetchcolumn("SELECT id FROM ".tablename('site_templates')." WHERE name = '{$m['name']}'")) {
  55. exit('error');
  56. }
  57. unset($m['settings']);
  58. if (pdo_insert('site_templates', $m)) {
  59. exit('success');
  60. } else {
  61. exit('error');
  62. }
  63. } else {
  64. exit('error');
  65. }
  66. }
  67. if($do == 'install') {
  68. if(empty($_W['isfounder'])) {
  69. message('您没有安装模块的权限', '', 'error');
  70. }
  71. $id = $_GPC['templateid'];
  72. if (pdo_fetchcolumn("SELECT id FROM ".tablename('site_templates')." WHERE name = :name", array(':name' => $id))) {
  73. message('模板已经安装或是唯一标识已存在!', '', 'error');
  74. }
  75. $manifest = ext_template_manifest($id, false);
  76. if (!empty($manifest)) {
  77. $r = cloud_t_prepare($id);
  78. if(is_error($r)) {
  79. message($r['message'], url('extension/theme/prepared'), 'error');
  80. }
  81. }
  82. if (empty($manifest)) {
  83. $r = cloud_prepare();
  84. if(is_error($r)) {
  85. message($r['message'], url('cloud/profile'), 'error');
  86. }
  87. $info = cloud_t_info($id);
  88. if (!is_error($info)) {
  89. if (empty($_GPC['flag'])) {
  90. header('location: ' . url('cloud/process', array('t' => $id)));
  91. exit;
  92. } else {
  93. $packet = cloud_t_build($id);
  94. $manifest = ext_template_manifest_parse($packet['manifest']);
  95. $manifest['version'] = $packet['version'];
  96. }
  97. } else {
  98. message($info['message'], '', 'error');
  99. }
  100. }
  101. unset($manifest['settings']);
  102. $groups = uni_groups();
  103. if(!$_W['ispost'] || empty($_GPC['flag'])) {
  104. template('extension/select-groups');
  105. exit;
  106. }
  107. $post_groups = $_GPC['group'];
  108. $tid = intval($_GPC['tid']);
  109. $id = $_GPC['templateid'];
  110. if (empty($manifest)) {
  111. message('模板安装配置文件不存在或是格式不正确!', '', 'error');
  112. }
  113. if ($manifest['name'] != $id) {
  114. message('安装模板与文件标识不符,请重新安装', '', 'error');
  115. }
  116. if (pdo_fetchcolumn("SELECT id FROM ".tablename('site_templates')." WHERE name = '{$manifest['name']}'")) {
  117. message('模板已经安装或是唯一标识已存在!', '', 'error');
  118. }
  119. if (pdo_insert('site_templates', $manifest)) {
  120. $tid = pdo_insertid();
  121. } else {
  122. message('模板安装失败, 请联系模板开发者!');
  123. }
  124. if($id && $post_groups) {
  125. if (!pdo_fetchcolumn("SELECT id FROM ".tablename('site_templates')." WHERE id = {$tid}")) {
  126. message('指定模板不存在!', '', 'error');
  127. }
  128. foreach($post_groups as $post_group) {
  129. $item = pdo_fetch("SELECT id,name,templates FROM ".tablename('uni_group') . " WHERE id = :id", array(':id' => intval($post_group)));
  130. if(empty($item)) {
  131. continue;
  132. }
  133. $item['templates'] = iunserializer($item['templates']);
  134. if(in_array($tid, $item['templates'])) {
  135. continue;
  136. }
  137. $item['templates'][] = $tid;
  138. $item['templates'] = iserializer($item['templates']);
  139. pdo_update('uni_group', $item, array('id' => $post_group));
  140. }
  141. }
  142. message('模板安装成功, 请按照【公众号服务套餐】【用户组】来分配权限!', url('extension/theme'), 'success');
  143. }
  144. if($do == 'uninstall') {
  145. $name = pdo_fetchcolumn('SELECT name FROM ' . tablename('site_templates') . ' WHERE id = :id', array(':id' => intval($_GPC['id'])));
  146. if($name == 'default') {
  147. message('默认模板不能卸载', '', 'error');
  148. }
  149. if (pdo_delete('site_templates', array('id' => intval($_GPC['id'])))) {
  150. pdo_delete('site_styles',array('templateid' => intval($_GPC['id'])));
  151. pdo_delete('site_styles_vars',array('templateid' => intval($_GPC['id'])));
  152. message('模板移除成功, 你可以重新安装, 或者直接移除文件来安全删除!', referer(), 'success');
  153. } else {
  154. message('模板移除失败, 请联系模板开发者!');
  155. }
  156. }
  157. if($do == 'upgrade') {
  158. $check = intval($_GPC['check']);
  159. $batch = intval($_GPC['batch']);
  160. if($check == 1) {
  161. isetcookie('batch', 1);
  162. $batch = 1;
  163. $r = cloud_prepare();
  164. if(is_error($r)) {
  165. exit('cloud service is unavailable');
  166. }
  167. $templates = pdo_fetchall('SELECT id,name,version FROM ' . tablename('site_templates'), array(), 'name');
  168. $upgrade = array();
  169. $mods = array();
  170. $ret = cloud_t_query();
  171. if(!is_error($ret)) {
  172. foreach($ret as $k => $v) {
  173. if(!$templates[$k]) continue;
  174. if(ver_compare($templates[$k]['version'], $v['version']) == -1) {
  175. $upgrade[] = $k;
  176. }
  177. }
  178. } else {
  179. message('从云平台获取模板信息失败,请稍后重试', referer(), 'error');
  180. }
  181. if(empty($upgrade)) {
  182. message('您的模板已经是最新版本', referer(), 'success');
  183. }
  184. $upgrade_str = iserializer($upgrade);
  185. cache_write('upgrade:template', $upgrade_str);
  186. }
  187. if($batch == 1) {
  188. $wait_upgrade = (array)iunserializer(cache_read('upgrade:template'));
  189. if(empty($wait_upgrade)) {
  190. isetcookie('batch', 0, -10000);
  191. message('您的模板已经是最新版本', url('extension/theme'), 'success');
  192. }
  193. $id = array_shift($wait_upgrade);
  194. } else {
  195. $id = $_GPC['templateid'];
  196. }
  197. $theme = pdo_fetch("SELECT id, name, title FROM " . tablename('site_templates') . " WHERE name = :name", array(':name' => $id));
  198. if (empty($theme)) {
  199. if($batch == 1) {
  200. cache_write('upgrade:template', iserializer($wait_upgrade));
  201. message($theme['title'] . ' 模板已经被卸载或是不存在。系统将进入下一个模板的更新。<br>请勿关闭浏览器', url('extension/theme/upgrade', array('batch' => 1)), 'success');
  202. }
  203. message('模板已经被卸载或是不存在!', '', 'error');
  204. }
  205. $r = cloud_prepare();
  206. if(is_error($r)) {
  207. message($r['message'], url('cloud/profile'), 'error');
  208. }
  209. $info = cloud_t_info($id);
  210. if (is_error($info)) {
  211. message($info['message'], referer(), 'error');
  212. }
  213. $upgrade_info = cloud_t_upgradeinfo($id);
  214. if (is_error($upgrade_info)) {
  215. message($upgrade_info['message'], referer(), 'error');
  216. }
  217. if ($_W['isajax']) {
  218. if ($upgrade_info['free']) {
  219. foreach ($upgrade_info['branches'] as &$branch) {
  220. $branch['upgrade_price'] = 0;
  221. }
  222. }
  223. message($upgrade_info, '', 'ajax');
  224. }
  225. if (!is_error($info)) {
  226. if (empty($_GPC['flag'])) {
  227. if (intval($_GPC['branch']) > $upgrade_info['version']['branch_id']) {
  228. header('location: ' . url('cloud/redirect/buybranch', array('m' => $id, 'branch' => intval($_GPC['branch']), 'type' => 'theme', 'is_upgrade' => 1)));
  229. exit;
  230. }
  231. load()->func('file');
  232. rmdirs(IA_ROOT . '/app/themes/' . $id, true);
  233. header('Location: ' . url('cloud/process', array('t' => $id, 'is_upgrade' => 1)));
  234. exit;
  235. } else {
  236. $packet = cloud_t_build($id);
  237. $manifest = ext_template_manifest_parse($packet['manifest']);
  238. }
  239. }
  240. if (empty($manifest)) {
  241. if($batch == 1) {
  242. cache_write('upgrade:template', iserializer($wait_upgrade));
  243. message($theme['title'] . ' 模块安装配置文件不存在或是格式不正确。系统将进入下一个模板的更新。<br>请勿关闭浏览器', url('extension/theme/upgrade', array('batch' => 1)), 'success');
  244. }
  245. message('模块安装配置文件不存在或是格式不正确!', '', 'error');
  246. }
  247. if(ver_compare($theme['version'], $packet['version']) != -1) {
  248. if($batch == 1) {
  249. cache_write('upgrade:template', iserializer($wait_upgrade));
  250. message($theme['title'] . ' 模板版本不低于要更新的版本。系统将进入下一个模板的更新。<br>请勿关闭浏览器', url('extension/theme/upgrade', array('batch' => 1)), 'success');
  251. }
  252. message('已安装的模板版本不低于要更新的版本, 操作无效.');
  253. }
  254. pdo_update('site_templates', array('version' => $packet['version']), array('id' => $theme['id']));
  255. if($batch == 1) {
  256. cache_write('upgrade:template', iserializer($wait_upgrade));
  257. message($theme['title'] . ' 模板更新成功。系统将进入下一个模板的更新。<br>请勿关闭浏览器', url('extension/theme/upgrade', array('batch' => 1)), 'success');
  258. }
  259. message('模板更新成功!', url('extension/theme'), 'success');
  260. }
  261. if($do == 'web') {
  262. $_W['page']['title'] = '管理后台风格 - 风格主题 - 扩展';
  263. load()->model('setting');
  264. if(checksubmit('submit')) {
  265. $data = array(
  266. 'template' => $_GPC['template'],
  267. );
  268. setting_save($data, 'basic');
  269. message('更新设置成功!', 'refresh');
  270. }
  271. $path = IA_ROOT . '/web/themes/';
  272. if(is_dir($path)) {
  273. if ($handle = opendir($path)) {
  274. while (false !== ($templatepath = readdir($handle))) {
  275. if ($templatepath != '.' && $templatepath != '..') {
  276. if(is_dir($path.$templatepath)){
  277. $template[] = $templatepath;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. template('extension/web');
  284. }
  285. if ($do == 'designer') {
  286. if (empty($_W['isfounder'])) {
  287. message('您没有设计新模板的权限', '', 'error');
  288. }
  289. $_W['page']['title'] = '设计微站风格 - 风格主题 - 扩展';
  290. $available['download'] = class_exists('ZipArchive');
  291. $available['create'] = is_writable(IA_ROOT . '/app/themes');
  292. $versions = array('0.52', '0.6');
  293. $temtypes = ext_template_type();
  294. if (checksubmit('submit') && $available[$_GPC['method']]) {
  295. $t['template']['name'] = trim($_GPC['template']['name']);
  296. if(empty($t['template']['name']) || preg_match('/\*\/|\/\*|eval|\$\_/i', $t['template']['name'])) {
  297. message('请输入有效的模板名称. ');
  298. }
  299. $t['template']['identifie'] = trim($_GPC['template']['identifie']);
  300. if(empty($t['template']['identifie']) || !preg_match('/^[a-z][a-z\d_]+$/i', $t['template']['identifie'])) {
  301. message('必须输入模板标识符(仅支持字母和数字, 且只能以字母开头). ');
  302. }
  303. $t['template']['type'] = array_key_exists($_GPC['template']['type'], $temtypes) ? $_GPC['template']['type'] : 'other';
  304. $t['template']['description'] = trim($_GPC['template']['description']);
  305. if(empty($t['template']['description']) || preg_match('/\*\/|\/\*|eval|\$\_/i', $t['template']['description'])) {
  306. message('请输入有效的模板介绍. ');
  307. }
  308. $t['template']['author'] = trim($_GPC['template']['author']);
  309. if(empty($t['template']['author']) || preg_match('/\*\/|\/\*|eval|\$\_/i', $t['template']['author'])) {
  310. message('请输入有效的模板作者');
  311. }
  312. $t['template']['url'] = trim($_GPC['template']['url']);
  313. if(empty($t['template']['url']) || preg_match('/\*\/|\/\*|eval|\$\_/i', $t['template']['url'])) {
  314. message('请输入有效的模板发布页');
  315. }
  316. $t['template']['sections'] = trim($_GPC['template']['sections']);
  317. if (is_array($_GPC['versions'])) {
  318. foreach ($_GPC['versions'] as $value) {
  319. if (in_array($value, $versions)) {
  320. $t['versions'][] = $value;
  321. }
  322. }
  323. } else {
  324. message('请设置版本的兼容性');
  325. }
  326. $t['settings'] = array();
  327. if(!empty($_GPC['settings']['variables'])) {
  328. foreach($_GPC['settings']['variables'] as $key => $value) {
  329. $temp = array();
  330. if(!empty($_GPC['settings']['variables'][$key]) && preg_match('/^[a-z\d]+$/i', $_GPC['settings']['variables'][$key])) {
  331. if (!empty($_GPC['settings']['description'][$key])) {
  332. $temp['variable'] = $_GPC['settings']['variables'][$key];
  333. $temp['value'] = $_GPC['settings']['values'][$key];
  334. $temp['desc'] = $_GPC['settings']['description'][$key];
  335. $t['settings'][] = $temp;
  336. }
  337. }
  338. }
  339. }
  340. if($_FILES['preview'] && $_FILES['preview']['error'] == '0' && !empty($_FILES['preview']['tmp_name'])) {
  341. $t['preview'] = $_FILES['preview']['tmp_name'];
  342. }
  343. $manifest = manifest($t);
  344. load()->func('file');
  345. if ($_GPC['method'] == 'create') {
  346. $tpldir = IA_ROOT . '/app/themes/' . strtolower($t['template']['identifie']);
  347. if (is_dir($tpldir)) {
  348. message('模板目录' . $tpldir . '已存在,请更换模板标识还删除已存在模板');
  349. }
  350. mkdirs($tpldir);
  351. file_put_contents("{$tpldir}/manifest.xml", $manifest);
  352. if (!empty($t['preview'])) {
  353. file_move($t['preview'], "{$tpldir}/preview.jpg");
  354. }
  355. message('模板生成成功,请访问' . $tpldir . '目录进行查看', referer(), 'success');
  356. exit();
  357. }
  358. if ($_GPC['method'] == 'download') {
  359. $zipfile = IA_ROOT . '/data/temp.zip';
  360. $zip = new ZipArchive();
  361. $zip->open($zipfile, ZipArchive::CREATE);
  362. $zip->addFromString('manifest.xml', $manifest);
  363. if (!empty($t['preview'])) {
  364. $zip->addFile($t['preview'], "preview.jpg");
  365. }
  366. $zip->close();
  367. header('content-type: application/zip');
  368. header('content-disposition: attachment; filename="' . $t['template']['identifie'] . '.zip"');
  369. readfile($zipfile);
  370. @unlink($t['preview']);
  371. @unlink($zipfile);
  372. }
  373. }
  374. template('extension/desitemp');
  375. }
  376. if($do == 'check') {
  377. if($_W['isajax']) {
  378. $foo = $_GPC['foo'];
  379. $r = cloud_prepare();
  380. if(is_error($r)) {
  381. exit('cloud service is unavailable');
  382. }
  383. if ($foo == 'upgrade') {
  384. $mods = array();
  385. $ret = cloud_t_query();
  386. if (!is_error($ret)) {
  387. foreach($ret as $k => $v) {
  388. $mods[$k] = array(
  389. 'from' => 'cloud',
  390. 'version' => $v['version'],
  391. 'branches' => $v['branches'],
  392. 'site_branch' => $v['branches'][$v['branch']],
  393. );
  394. }
  395. $mods['pirate_apps'] = array_values($v['pirate_apps']);
  396. }
  397. if(!empty($mods)) {
  398. exit(json_encode($mods));
  399. }
  400. } else {
  401. $templateids = array();
  402. $templates = pdo_fetchall("SELECT `name` FROM " . tablename('site_templates') . ' ORDER BY `id` ASC');
  403. if(!empty($templates)) {
  404. foreach($templates as $m) {
  405. $templateids[] = $m['name'];
  406. }
  407. }
  408. $ret = cloud_t_query();
  409. if(!is_error($ret)) {
  410. $cloudUninstallThemes = array();
  411. foreach($ret as $k => $v) {
  412. if(!in_array(strtolower($k), $templateids)) {
  413. $v['name'] = $k;
  414. $cloudUninstallThemes[] = $v;
  415. $templateids[] = $k;
  416. }
  417. }
  418. exit(json_encode($cloudUninstallThemes));
  419. }
  420. }
  421. }
  422. exit();
  423. }
  424. function manifest($t) {
  425. $versions = implode(',', $t['versions']);
  426. $item = '';
  427. if(!empty($t['settings'])) {
  428. foreach($t['settings'] as $key => $value) {
  429. $item .= "\r\n\t\t<item variable=\"{$value['variable']}\" content=\"{$value['value']}\" description=\"{$value['desc']}\"/>";
  430. }
  431. }
  432. $tpl = <<<TPL
  433. <?xml version="1.0" encoding="utf-8"?>
  434. <manifest versionCode="{$versions}">
  435. <identifie><![CDATA[{$t['template']['identifie']}]]></identifie>
  436. <title><![CDATA[{$t['template']['name']}]]></title>
  437. <type><![CDATA[{$t['template']['type']}]]></type>
  438. <description><![CDATA[{$t['template']['description']}]]></description>
  439. <author><![CDATA[{$t['template']['author']}]]></author>
  440. <url><![CDATA[{$t['template']['url']}]]></url>
  441. <sections><![CDATA[{$t['template']['sections']}]]></sections>
  442. <settings>{$item}
  443. </settings>
  444. </manifest>
  445. TPL;
  446. return ltrim($tpl);
  447. }