module.mod.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. function module_types() {
  8. static $types = array(
  9. 'business' => array(
  10. 'name' => 'business',
  11. 'title' => '主要业务',
  12. 'desc' => ''
  13. ),
  14. 'customer' => array(
  15. 'name' => 'customer',
  16. 'title' => '客户关系',
  17. 'desc' => ''
  18. ),
  19. 'activity' => array(
  20. 'name' => 'activity',
  21. 'title' => '营销及活动',
  22. 'desc' => ''
  23. ),
  24. 'services' => array(
  25. 'name' => 'services',
  26. 'title' => '常用服务及工具',
  27. 'desc' => ''
  28. ),
  29. 'biz' => array(
  30. 'name' => 'biz',
  31. 'title' => '行业解决方案',
  32. 'desc' => ''
  33. ),
  34. 'enterprise' => array(
  35. 'name' => 'enterprise',
  36. 'title' => '企业应用',
  37. 'desc' => ''
  38. ),
  39. 'h5game' => array(
  40. 'name' => 'h5game',
  41. 'title' => 'H5游戏',
  42. 'desc' => ''
  43. ),
  44. 'other' => array(
  45. 'name' => 'other',
  46. 'title' => '其他',
  47. 'desc' => ''
  48. )
  49. );
  50. return $types;
  51. }
  52. function module_entries($name, $types = array(), $rid = 0, $args = null) {
  53. load()->func('communication');
  54. global $_W;
  55. $ts = array('rule', 'cover', 'menu', 'home', 'profile', 'shortcut', 'function', 'mine', 'system_welcome');
  56. if(empty($types)) {
  57. $types = $ts;
  58. } else {
  59. $types = array_intersect($types, $ts);
  60. }
  61. $bindings = pdo_getall('modules_bindings', array('module' => $name, 'entry' => $types), array(), '', 'displayorder DESC, eid ASC');
  62. $entries = array();
  63. foreach($bindings as $bind) {
  64. if(!empty($bind['call'])) {
  65. $response = ihttp_request(url('utility/bindcall', array('modulename' => $bind['module'], 'callname' => $bind['call'], 'args' => $args, 'uniacid' => $_W['uniacid'])), array(), $extra);
  66. if (is_error($response)) {
  67. continue;
  68. }
  69. $response = json_decode($response['content'], true);
  70. $ret = $response['message']['message'];
  71. if(is_array($ret)) {
  72. foreach($ret as $i => $et) {
  73. if (empty($et['url'])) {
  74. continue;
  75. }
  76. $et['url'] = $et['url'] . '&__title=' . urlencode($et['title']);
  77. $entries[$bind['entry']][] = array('eid' => 'user_' . $i, 'title' => $et['title'], 'do' => $et['do'], 'url' => $et['url'], 'from' => 'call', 'icon' => $et['icon'], 'displayorder' => $et['displayorder']);
  78. }
  79. }
  80. } else {
  81. if($bind['entry'] == 'cover') {
  82. $url = murl('entry', array('eid' => $bind['eid']));
  83. }
  84. if($bind['entry'] == 'menu') {
  85. $url = wurl("site/entry", array('eid' => $bind['eid']));
  86. }
  87. if($bind['entry'] == 'mine') {
  88. $url = $bind['url'];
  89. }
  90. if($bind['entry'] == 'rule') {
  91. $par = array('eid' => $bind['eid']);
  92. if (!empty($rid)) {
  93. $par['id'] = $rid;
  94. }
  95. $url = wurl("site/entry", $par);
  96. }
  97. if($bind['entry'] == 'home') {
  98. $url = murl("entry", array('eid' => $bind['eid']));
  99. }
  100. if($bind['entry'] == 'profile') {
  101. $url = murl("entry", array('eid' => $bind['eid']));
  102. }
  103. if($bind['entry'] == 'shortcut') {
  104. $url = murl("entry", array('eid' => $bind['eid']));
  105. }
  106. if($bind['entry'] == 'system_welcome') {
  107. $url = wurl("site/entry", array('eid' => $bind['eid']));
  108. }
  109. if(empty($bind['icon'])) {
  110. $bind['icon'] = 'fa fa-puzzle-piece';
  111. }
  112. $entries[$bind['entry']][] = array('eid' => $bind['eid'], 'title' => $bind['title'], 'do' => $bind['do'], 'url' => $url, 'from' => 'define', 'icon' => $bind['icon'], 'displayorder' => $bind['displayorder'], 'direct' => $bind['direct']);
  113. }
  114. }
  115. return $entries;
  116. }
  117. function module_app_entries($name, $types = array(), $args = null) {
  118. global $_W;
  119. $ts = array('rule', 'cover', 'menu', 'home', 'profile', 'shortcut', 'function');
  120. if(empty($types)) {
  121. $types = $ts;
  122. } else {
  123. $types = array_intersect($types, $ts);
  124. }
  125. $bindings = pdo_getall('modules_bindings', array('module' => $name, 'entry' => $types));
  126. $entries = array();
  127. foreach($bindings as $bind) {
  128. if(!empty($bind['call'])) {
  129. $extra = array();
  130. $extra['Host'] = $_SERVER['HTTP_HOST'];
  131. load()->func('communication');
  132. $urlset = parse_url($_W['siteurl']);
  133. $urlset = pathinfo($urlset['path']);
  134. $response = ihttp_request($_W['sitescheme'] . '127.0.0.1/'. $urlset['dirname'] . '/' . url('utility/bindcall', array('modulename' => $bind['module'], 'callname' => $bind['call'], 'args' => $args, 'uniacid' => $_W['uniacid'])), array('W'=>base64_encode(iserializer($_W))), $extra);
  135. if (is_error($response)) {
  136. continue;
  137. }
  138. $response = json_decode($response['content'], true);
  139. $ret = $response['message'];
  140. if(is_array($ret)) {
  141. foreach($ret as $et) {
  142. $et['url'] = $et['url'] . '&__title=' . urlencode($et['title']);
  143. $entries[$bind['entry']][] = array('title' => $et['title'], 'url' => $et['url'], 'from' => 'call');
  144. }
  145. }
  146. } else {
  147. if($bind['entry'] == 'cover') {
  148. $url = murl("entry", array('eid' => $bind['eid']));
  149. }
  150. if($bind['entry'] == 'home') {
  151. $url = murl("entry", array('eid' => $bind['eid']));
  152. }
  153. if($bind['entry'] == 'profile') {
  154. $url = murl("entry", array('eid' => $bind['eid']));
  155. }
  156. if($bind['entry'] == 'shortcut') {
  157. $url = murl("entry", array('eid' => $bind['eid']));
  158. }
  159. $entries[$bind['entry']][] = array('title' => $bind['title'], 'do' => $bind['do'], 'url' => $url, 'from' => 'define');
  160. }
  161. }
  162. return $entries;
  163. }
  164. function module_entry($eid) {
  165. $sql = "SELECT * FROM " . tablename('modules_bindings') . " WHERE `eid`=:eid";
  166. $pars = array();
  167. $pars[':eid'] = $eid;
  168. $entry = pdo_fetch($sql, $pars);
  169. if(empty($entry)) {
  170. return error(1, '模块菜单不存在');
  171. }
  172. $module = module_fetch($entry['module']);
  173. if(empty($module)) {
  174. return error(2, '模块不存在');
  175. }
  176. $querystring = array(
  177. 'do' => $entry['do'],
  178. 'm' => $entry['module'],
  179. );
  180. if (!empty($entry['state'])) {
  181. $querystring['state'] = $entry['state'];
  182. }
  183. $entry['url'] = murl('entry', $querystring);
  184. $entry['url_show'] = murl('entry', $querystring, true, true);
  185. return $entry;
  186. }
  187. function module_build_form($name, $rid, $option = array()) {
  188. $rid = intval($rid);
  189. $m = WeUtility::createModule($name);
  190. if(!empty($m)) {
  191. return $m->fieldsFormDisplay($rid, $option);
  192. }else {
  193. return null;
  194. }
  195. }
  196. function module_save_group_package($package) {
  197. global $_W;
  198. load()->model('user');
  199. load()->model('cache');
  200. if (empty($package['name'])) {
  201. return error(-1, '请输入套餐名');
  202. }
  203. if (user_is_vice_founder()) {
  204. $package['owner_uid'] = $_W['uid'];
  205. }
  206. if (!empty($package['modules'])) {
  207. $package['modules'] = iserializer($package['modules']);
  208. }
  209. if (!empty($package['templates'])) {
  210. $templates = array();
  211. foreach ($package['templates'] as $template) {
  212. $templates[] = $template['id'];
  213. }
  214. $package['templates'] = iserializer($templates);
  215. }
  216. if (!empty($package['id'])) {
  217. $name_exist = pdo_get('uni_group', array('uniacid' => 0, 'id <>' => $package['id'], 'name' => $package['name']));
  218. } else {
  219. $name_exist = pdo_get('uni_group', array('uniacid' => 0, 'name' => $package['name']));
  220. }
  221. if (!empty($name_exist)) {
  222. return error(-1, '套餐名已存在');
  223. }
  224. if (!empty($package['id'])) {
  225. pdo_update('uni_group', $package, array('id' => $package['id']));
  226. cache_build_account_modules();
  227. } else {
  228. pdo_insert('uni_group', $package);
  229. }
  230. cache_build_uni_group();
  231. return error(0, '添加成功');
  232. }
  233. function module_fetch($name) {
  234. load()->object('cloudapi');
  235. global $_W;
  236. $cachekey = cache_system_key(CACHE_KEY_MODULE_INFO, $name);
  237. $module = cache_load($cachekey);
  238. if (empty($module)) {
  239. $sql = 'SELECT * FROM '. tablename('modules') . " as a LEFT JOIN" . tablename('modules_recycle') . " as b ON a.name = b.modulename WHERE a.name = :name AND b.modulename is NULL";
  240. $module_info = pdo_fetch($sql, array(':name' => $name));
  241. if (empty($module_info)) {
  242. return array();
  243. }
  244. if (!empty($module_info['subscribes'])) {
  245. $module_info['subscribes'] = (array)unserialize ($module_info['subscribes']);
  246. }
  247. if (!empty($module_info['handles'])) {
  248. $module_info['handles'] = (array)unserialize ($module_info['handles']);
  249. }
  250. $module_info['isdisplay'] = 1;
  251. if (file_exists (IA_ROOT . '/addons/' . $module_info['name'] . '/icon-custom.jpg')) {
  252. $module_info['logo'] = tomedia (IA_ROOT . '/addons/' . $module_info['name'] . '/icon-custom.jpg') . "?v=" . time ();
  253. } else {
  254. $module_info['logo'] = tomedia (IA_ROOT . '/addons/' . $module_info['name'] . '/icon.jpg') . "?v=" . time ();
  255. }
  256. $module_info['main_module'] = pdo_getcolumn ('modules_plugin', array ('name' => $module_info['name']), 'main_module');
  257. if (!empty($module_info['main_module'])) {
  258. $main_module_info = module_fetch ($module_info['main_module']);
  259. $module_info['main_module_logo'] = $main_module_info['logo'];
  260. } else {
  261. $module_info['plugin_list'] = pdo_getall ('modules_plugin', array ('main_module' => $module_info['name']), array (), 'name');
  262. if (!empty($module_info['plugin_list'])) {
  263. $module_info['plugin_list'] = array_keys ($module_info['plugin_list']);
  264. }
  265. }
  266. if ($module_info['app_support'] != MODULE_SUPPORT_ACCOUNT && $module_info['wxapp_support'] != MODULE_SUPPORT_WXAPP && $module_info['webapp_support'] != MODULE_SUPPORT_WEBAPP && $module_info['welcome_support'] != MODULE_SUPPORT_SYSTEMWELCOME) {
  267. $module_info['app_support'] = MODULE_SUPPORT_ACCOUNT;
  268. }
  269. $module_info['is_relation'] = $module_info['app_support'] ==2 && $module_info['wxapp_support'] == 2 ? true : false;
  270. $module_ban = setting_load('module_ban');
  271. if (in_array($name, $module_ban['module_ban'])) {
  272. $module_info['is_ban'] = true;
  273. }
  274. $module_upgrade = setting_load('module_upgrade');
  275. if (in_array($name, array_keys($module_upgrade['module_upgrade']))) {
  276. $module_info['is_upgrade'] = true;
  277. }
  278. $module = $module_info;
  279. cache_write($cachekey, $module_info);
  280. }
  281. if (!empty($module) && !empty($_W['uniacid'])) {
  282. $setting_cachekey = cache_system_key(CACHE_KEY_MODULE_SETTING, $_W['uniacid'], $name);
  283. $setting = cache_load($setting_cachekey);
  284. if (empty($setting)) {
  285. $setting = pdo_get('uni_account_modules', array('module' => $name, 'uniacid' => $_W['uniacid']));
  286. if (!empty($setting)) {
  287. cache_write($setting_cachekey, $setting);
  288. }
  289. }
  290. $module['config'] = !empty($setting['settings']) ? iunserializer($setting['settings']) : array();
  291. $module['enabled'] = $module['issystem'] || !isset($setting['enabled']) ? 1 : $setting['enabled'];
  292. $module['shortcut'] = $setting['shortcut'];
  293. }
  294. return $module;
  295. }
  296. function module_get_all_unistalled($status, $cache = true, $module_type = '') {
  297. load()->func('communication');
  298. load()->model('cloud');
  299. load()->classs('cloudapi');
  300. $status = $status == 'recycle' ? 'recycle' : 'uninstalled';
  301. $cloud_api = new CloudApi();
  302. $uninstallModules = cache_load(cache_system_key('module:all_uninstall'));
  303. if (!$cache && $status == 'uninstalled') {
  304. $get_cloud_m_count = $cloud_api->get('site', 'stat', array('module_quantity' => 1), 'json');
  305. $cloud_m_count = $get_cloud_m_count['module_quantity'];
  306. } else {
  307. if(is_array($uninstallModules)){
  308. $cloud_m_count = $uninstallModules['cloud_m_count'];
  309. }
  310. }
  311. if (ACCOUNT_TYPE == ACCOUNT_TYPE_APP_NORMAL) {
  312. $account_type = 'wxapp';
  313. } elseif (ACCOUNT_TYPE == ACCOUNT_TYPE_OFFCIAL_NORMAL) {
  314. $account_type = 'app';
  315. } elseif (ACCOUNT_TYPE == ACCOUNT_TYPE_WEBAPP_NORMAL) {
  316. $account_type = 'webapp';
  317. } elseif (ACCOUNT_TYPE == ACCOUNT_TYPE_PHONEAPP_NORMAL) {
  318. $account_type = 'phoneapp';
  319. }
  320. if (!empty($module_type)) {
  321. $account_type = $module_type;
  322. }
  323. if (!is_array($uninstallModules) || empty($uninstallModules['modules'][$status][$account_type]) || intval($uninstallModules['cloud_m_count']) !== intval($cloud_m_count) || is_error($get_cloud_m_count)) {
  324. $uninstallModules = cache_build_uninstalled_module();
  325. }
  326. if (!empty($account_type)) {
  327. $uninstallModules['modules'] = (array)$uninstallModules['modules'][$status][$account_type];
  328. $uninstallModules['module_count'] = $uninstallModules[$account_type . '_count'];
  329. }
  330. return $uninstallModules;
  331. }
  332. function module_permission_fetch($name) {
  333. $module = module_fetch($name);
  334. $data = array();
  335. if($module['settings']) {
  336. $data[] = array('title' => '参数设置', 'permission' => $name.'_settings');
  337. }
  338. if($module['isrulefields']) {
  339. $data[] = array('title' => '回复规则列表', 'permission' => $name.'_rule');
  340. }
  341. $entries = module_entries($name);
  342. if(!empty($entries['home'])) {
  343. $data[] = array('title' => '微站首页导航', 'permission' => $name.'_home');
  344. }
  345. if(!empty($entries['profile'])) {
  346. $data[] = array('title' => '个人中心导航', 'permission' => $name.'_profile');
  347. }
  348. if(!empty($entries['shortcut'])) {
  349. $data[] = array('title' => '快捷菜单', 'permission' => $name.'_shortcut');
  350. }
  351. if(!empty($entries['cover'])) {
  352. foreach($entries['cover'] as $cover) {
  353. $data[] = array('title' => $cover['title'], 'permission' => $name.'_cover_'.$cover['do']);
  354. }
  355. }
  356. if(!empty($entries['menu'])) {
  357. foreach($entries['menu'] as $menu) {
  358. $data[] = array('title' => $menu['title'], 'permission' => $name.'_menu_'.$menu['do']);
  359. }
  360. }
  361. unset($entries);
  362. if(!empty($module['permissions'])) {
  363. $module['permissions'] = (array)iunserializer($module['permissions']);
  364. foreach ($module['permissions'] as $permission) {
  365. $data[] = array('title' => $permission['title'], 'permission' => $name . '_permission_' . $permission['permission']);
  366. }
  367. }
  368. return $data;
  369. }
  370. function module_uninstall($module_name, $is_clean_rule = false) {
  371. global $_W;
  372. load()->object('cloudapi');
  373. if (empty($_W['isfounder'])) {
  374. return error(1, '您没有卸载模块的权限!');
  375. }
  376. $module_name = trim($module_name);
  377. $module = pdo_get('modules', array('name' => $module_name));
  378. if (empty($module)) {
  379. return error(1, '模块已经被卸载或是不存在!');
  380. }
  381. if (!empty($module['issystem'])) {
  382. return error(1, '系统模块不能卸载!');
  383. }
  384. pdo_delete('modules_plugin', array('main_module' => $module_name));
  385. pdo_delete('uni_account_modules', array('module' => $module_name));
  386. cache_delete(cache_system_key('module:all_uninstall'));
  387. ext_module_clean($module_name, $is_clean_rule);
  388. cache_build_module_subscribe_type();
  389. cache_build_uninstalled_module();
  390. cache_build_module_info($module_name);
  391. return true;
  392. }
  393. function module_execute_uninstall_script($module_name) {
  394. global $_W;
  395. load()->object('cloudapi');
  396. load()->model('cloud');
  397. if (empty($_W['isfounder'])) {
  398. return error(1, '您没有卸载模块的权限!');
  399. }
  400. $modulepath = IA_ROOT . '/addons/' . $module_name . '/';
  401. $manifest = ext_module_manifest($module_name);
  402. if (empty($manifest)) {
  403. $result = cloud_prepare();
  404. if (is_error($result)) {
  405. return error(1, $result['message']);
  406. }
  407. $packet = cloud_m_build($module_name, 'uninstall');
  408. if ($packet['sql']) {
  409. pdo_run(base64_decode($packet['sql']));
  410. } elseif ($packet['script']) {
  411. $uninstall_file = $modulepath . TIMESTAMP . '.php';
  412. file_put_contents($uninstall_file, base64_decode($packet['script']));
  413. require($uninstall_file);
  414. unlink($uninstall_file);
  415. }
  416. } else {
  417. if (!empty($manifest['uninstall'])) {
  418. if (strexists($manifest['uninstall'], '.php')) {
  419. if (file_exists($modulepath . $manifest['uninstall'])) {
  420. require($modulepath . $manifest['uninstall']);
  421. }
  422. } else {
  423. pdo_run($manifest['uninstall']);
  424. }
  425. }
  426. }
  427. pdo_delete('modules_recycle', array('modulename' => $module_name));
  428. $cloudapi = new CloudApi();
  429. $recycle_module = $cloudapi->post('cache', 'get', array('key' => cache_system_key('recycle_module:')));
  430. $recycle_module = !empty($recycle_module['data']) ? $recycle_module['data'] : array();
  431. unset($recycle_module[$module_name]);
  432. $cloudapi->post('cache', 'set', array('key' => cache_system_key('recycle_module:'), 'value' => $recycle_module));
  433. cache_delete(cache_system_key('module:all_uninstall'));
  434. return true;
  435. }
  436. function module_get_plugin_list($module_name) {
  437. $module_info = module_fetch($module_name);
  438. if (!empty($module_info['plugin_list']) && is_array($module_info['plugin_list'])) {
  439. $plugin_list = array();
  440. foreach ($module_info['plugin_list'] as $plugin) {
  441. $plugin_info = module_fetch($plugin);
  442. if (!empty($plugin_info)) {
  443. $plugin_list[$plugin] = $plugin_info;
  444. }
  445. }
  446. return $plugin_list;
  447. } else {
  448. return array();
  449. }
  450. }
  451. function module_status($module) {
  452. load()->model('cloud');
  453. $module_status = array('upgrade' => array('upgrade' => 0), 'ban' => 0);
  454. $cloud_m_query = cloud_m_query($module);
  455. $cloud_m_query['pirate_apps'] = is_array($cloud_m_query['pirate_apps']) ? $cloud_m_query['pirate_apps'] : array();
  456. $module_status['ban'] = in_array($module, $cloud_m_query['pirate_apps']) ? 1 : 0;
  457. $cloud_m_info = cloud_m_info($module);
  458. $module_info = module_fetch($module);
  459. if (!empty($cloud_m_info) && !empty($cloud_m_info['version']['version'])) {
  460. if (version_compare($module_info['version'], $cloud_m_info['version']['version'])) {
  461. $module_status['upgrade'] = array('name' => $module_info['title'], 'version' => $cloud_m_info['version']['version'], 'upgrade' => 1);
  462. }
  463. } else {
  464. $manifest = ext_module_manifest($module);
  465. if (!empty($manifest)) {
  466. if (version_compare($module_info['version'], $manifest['application']['version'])) {
  467. $module_status['upgrade'] = array('name' => $module_info['title'], 'version' => $manifest['application']['version'], 'upgrade' => 1);
  468. }
  469. }
  470. }
  471. $cache_build_module = false;
  472. $module_ban_setting = setting_load('module_ban');
  473. $module_ban_setting = is_array($module_ban_setting['module_ban']) ? $module_ban_setting['module_ban'] : array();
  474. if (!in_array($module, $module_ban_setting) && !empty($module_status['ban'])) {
  475. $module_ban_setting[] = $module;
  476. $cache_build_module = true;
  477. setting_save($module_ban_setting, 'module_ban');
  478. }
  479. if (in_array($module, $module_ban_setting) && empty($module_status['ban'])) {
  480. $key = array_search($module, $module_ban_setting);
  481. unset($module_ban_setting[$key]);
  482. $cache_build_module = true;
  483. setting_save($module_ban_setting, 'module_ban');
  484. }
  485. $module_upgrade_setting = setting_load('module_upgrade');
  486. $module_upgrade_setting = is_array($module_upgrade_setting['module_upgrade']) ? $module_upgrade_setting['module_upgrade'] : array();
  487. if (!in_array($module, array_keys($module_upgrade_setting)) && !empty($module_status['upgrade']['upgrade'])) {
  488. $module_upgrade_setting[$module] = $module_status['upgrade'];
  489. $cache_build_module = true;
  490. setting_save($module_upgrade_setting, 'module_upgrade');
  491. }
  492. if (in_array($module, array_keys($module_upgrade_setting)) && empty($module_status['upgrade']['upgrade'])) {
  493. unset($module_upgrade_setting[$module]);
  494. $cache_build_module = true;
  495. setting_save($module_upgrade_setting, 'module_upgrade');
  496. }
  497. if ($cache_build_module) {
  498. cache_build_module_info($module);
  499. }
  500. return $module_status;
  501. }
  502. function module_filter_upgrade($module_list) {
  503. $modules = array();
  504. $installed_module = pdo_getall('modules', array('name' => $module_list), array('version', 'name'), 'name');
  505. $all_upgrade_cloud_module = cache_load(cache_system_key('all_cloud_upgrade_module:'));
  506. if (empty($all_upgrade_cloud_module)) {
  507. $all_upgrade_cloud_module = cache_build_cloud_upgrade_module();
  508. }
  509. if (!empty($module_list) && is_array($module_list) && !empty($installed_module)) {
  510. foreach ($module_list as $key => $module) {
  511. if (empty($installed_module[$module])) {
  512. continue;
  513. }
  514. $manifest = ext_module_manifest($module);
  515. if (!empty($manifest)&& is_array($manifest)) {
  516. $module = array('name' => $module);
  517. $module['from'] = 'local';
  518. if (version_compare($installed_module[$module['name']]['version'], $manifest['application']['version']) == '-1') {
  519. $module['upgrade'] = true;
  520. $module['upgrade_branch'] = true;
  521. $modules[$module['name']] = $module;
  522. }
  523. } else {
  524. if (is_array($all_upgrade_cloud_module) && !empty($all_upgrade_cloud_module[$module])) {
  525. $modules[$module] = $all_upgrade_cloud_module[$module];
  526. }
  527. }
  528. }
  529. }
  530. return $modules;
  531. }
  532. function module_upgrade_new($type = 'account') {
  533. if ($type == 'wxapp') {
  534. $module_list = user_module_by_account_type('wxapp');
  535. } else {
  536. $module_list = user_module_by_account_type('account');
  537. }
  538. $upgrade_modules = module_filter_upgrade(array_keys($module_list));
  539. if (!empty($upgrade_modules)) {
  540. foreach ($upgrade_modules as $key => &$module) {
  541. $module_fetch = module_fetch($key);
  542. $module['logo'] = $module_fetch['logo'];
  543. $module['link'] = url('module/manage-system/module_detail', array('name' => $module['name'], 'show' => 'upgrade'));
  544. }
  545. unset($module);
  546. }
  547. return $upgrade_modules;
  548. }
  549. function module_exist_in_account($module_name, $uniacid) {
  550. global $_W;
  551. $result = false;
  552. $module_name = trim($module_name);
  553. $uniacid = intval($uniacid);
  554. if (empty($module_name) || empty($uniacid)) {
  555. return $result;
  556. }
  557. $founders = explode(',', $_W['config']['setting']['founder']);
  558. $owner_uid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), 'uid');
  559. if (!empty($owner_uid) && !in_array($owner_uid, $founders)) {
  560. $packageids = pdo_getall('uni_account_group', array('uniacid' => $uniacid), array('groupid'), 'groupid');
  561. $packageids = array_keys($packageids);
  562. if (IMS_FAMILY == 'x') {
  563. $site_store_buy_goods = uni_site_store_buy_goods($uniacid);
  564. $site_store_buy_package = table('store')->searchUserBuyPackage($uniacid);
  565. $packageids = array_merge($packageids, array_keys($site_store_buy_package));
  566. } else {
  567. $site_store_buy_goods = array();
  568. }
  569. if (!in_array('-1', $packageids)) {
  570. $uni_modules = array();
  571. $uni_groups = pdo_fetchall("SELECT `modules` FROM " . tablename('uni_group') . " WHERE " . "id IN ('".implode("','", $packageids)."') OR " . " uniacid = '{$uniacid}'");
  572. if (!empty($uni_groups)) {
  573. foreach ($uni_groups as $group) {
  574. $group_module = (array)iunserializer($group['modules']);
  575. $uni_modules = array_merge($group_module, $uni_modules);
  576. }
  577. }
  578. $user_modules = user_modules($owner_uid);
  579. $modules = array_merge(array_keys($user_modules), $uni_modules, $site_store_buy_goods);
  580. $result = in_array($module_name, $modules) ? true : false;
  581. } else {
  582. $result = true;
  583. }
  584. } else {
  585. $result = true;
  586. }
  587. return $result;
  588. }
  589. function module_get_user_account_list($uid, $module_name) {
  590. $accounts_list = array();
  591. $uid = intval($uid);
  592. $module_name = trim($module_name);
  593. if (empty($uid) || empty($module_name)) {
  594. return $accounts_list;
  595. }
  596. $module_info = module_fetch($module_name);
  597. if (empty($module_info)) {
  598. return $accounts_list;
  599. }
  600. $account_users_info = table('account')->userOwnedAccount($uid);
  601. if (empty($account_users_info)) {
  602. return $accounts_list;
  603. }
  604. $accounts = array();
  605. foreach ($account_users_info as $account) {
  606. if (empty($account['uniacid'])) {
  607. continue;
  608. }
  609. $uniacid = 0;
  610. if (($account['type'] == ACCOUNT_TYPE_OFFCIAL_NORMAL || $account['type'] == ACCOUNT_TYPE_OFFCIAL_AUTH) && $module_info['app_support'] == MODULE_SUPPORT_ACCOUNT) {
  611. $uniacid = $account['uniacid'];
  612. } elseif ($account['type'] == ACCOUNT_TYPE_APP_NORMAL && $module_info['wxapp_support'] == MODULE_SUPPORT_WXAPP) {
  613. $uniacid = $account['uniacid'];
  614. }
  615. if (!empty($uniacid)) {
  616. if (module_exist_in_account($module_name, $uniacid)) {
  617. $accounts_list[$uniacid] = $account;
  618. }
  619. }
  620. }
  621. return $accounts_list;
  622. }
  623. function module_link_uniacid_fetch($uid, $module_name) {
  624. $result = array();
  625. $uid = intval($uid);
  626. $module_name = trim($module_name);
  627. if (empty($uid) || empty($module_name)) {
  628. return $result;
  629. }
  630. $accounts_list = module_get_user_account_list($uid, $module_name);
  631. if (empty($accounts_list)) {
  632. return $result;
  633. }
  634. $accounts_link_result = array();
  635. foreach ($accounts_list as $key => $account_value) {
  636. if ($account_value['type'] == ACCOUNT_TYPE_APP_NORMAL) {
  637. $account_value['versions'] = wxapp_version_all($account_value['uniacid']);
  638. if (empty($account_value['versions'])) {
  639. $accounts_link_result[$key] = $account_value;
  640. continue;
  641. }
  642. foreach ($account_value['versions'] as $version_key => $version_value) {
  643. if (empty($version_value['modules'])) {
  644. continue;
  645. }
  646. if ($version_value['modules'][0]['name'] != $module_name) {
  647. continue;
  648. }
  649. if (empty($version_value['modules'][0]['account']) || !is_array($version_value['modules'][0]['account'])) {
  650. $accounts_link_result[$key] = $account_value;
  651. continue;
  652. }
  653. if (!empty($version_value['modules'][0]['account']['uniacid'])) {
  654. $accounts_link_result[$version_value['modules'][0]['account']['uniacid']][] = array(
  655. 'uniacid' => $key,
  656. 'version' => $version_value['version'],
  657. 'version_id' => $version_value['id'],
  658. 'name' => $account_value['name'],
  659. );
  660. unset($account_value['versions'][$version_key]);
  661. }
  662. }
  663. }
  664. if ($account_value['type'] == ACCOUNT_TYPE_OFFCIAL_NORMAL || $account_value['type'] == ACCOUNT_TYPE_OFFCIAL_AUTH) {
  665. if (empty($accounts_link_result[$key])) {
  666. $accounts_link_result[$key] = $account_value;
  667. } else {
  668. $link_wxapp = $accounts_link_result[$key];
  669. $accounts_link_result[$key] = $account_value;
  670. $accounts_link_result[$key]['link_wxapp'] = $link_wxapp;
  671. }
  672. }
  673. }
  674. if (!empty($accounts_link_result)) {
  675. foreach ($accounts_link_result as $link_key => $link_value) {
  676. if (in_array($link_value['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH)) && !empty($link_value['link_wxapp']) && is_array($link_value['link_wxapp'])) {
  677. foreach ($link_value['link_wxapp'] as $value) {
  678. $result[] = array(
  679. 'app_name' => $link_value['name'],
  680. 'wxapp_name' => $value['name'] . ' ' . $value['version'],
  681. 'uniacid' => $link_value['uniacid'],
  682. 'version_id' => $value['version_id'],
  683. );
  684. }
  685. } elseif ($link_value['type'] == ACCOUNT_TYPE_APP_NORMAL && !empty($link_value['versions']) && is_array($link_value['versions'])) {
  686. foreach ($link_value['versions'] as $value) {
  687. $result[] = array(
  688. 'app_name' => '',
  689. 'wxapp_name' => $link_value['name'] . ' ' . $value['version'],
  690. 'uniacid' => $link_value['uniacid'],
  691. 'version_id' => $value['id'],
  692. );
  693. }
  694. } else {
  695. $result[] = array(
  696. 'app_name' => $link_value['name'],
  697. 'wxapp_name' => '',
  698. 'uniacid' => $link_value['uniacid'],
  699. 'version_id' => '',
  700. );
  701. }
  702. }
  703. }
  704. return $result;
  705. }
  706. function module_save_switch($module_name, $uniacid = 0, $version_id = 0) {
  707. global $_W, $_GPC;
  708. if (empty($_GPC['__switch'])) {
  709. $_GPC['__switch'] = random(5);
  710. }
  711. $cache_key = cache_system_key(CACHE_KEY_ACCOUNT_SWITCH, $_GPC['__switch']);
  712. $cache_lastaccount = cache_load($cache_key);
  713. if (empty($cache_lastaccount)) {
  714. $cache_lastaccount = array(
  715. $module_name => array(
  716. 'module_name' => $module_name,
  717. 'uniacid' => $uniacid,
  718. 'version_id' => $version_id
  719. )
  720. );
  721. } else {
  722. $cache_lastaccount[$module_name] = array(
  723. 'module_name' => $module_name,
  724. 'uniacid' => $uniacid,
  725. 'version_id' => $version_id
  726. );
  727. }
  728. cache_write($cache_key, $cache_lastaccount);
  729. isetcookie('__switch', $_GPC['__switch'], 7 * 86400);
  730. return true;
  731. }
  732. function module_last_switch($module_name) {
  733. global $_GPC;
  734. $module_name = trim($module_name);
  735. if (empty($module_name)) {
  736. return array();
  737. }
  738. $cache_key = cache_system_key(CACHE_KEY_ACCOUNT_SWITCH, $_GPC['__switch']);
  739. $cache_lastaccount = (array)cache_load($cache_key);
  740. return $cache_lastaccount[$module_name];
  741. }
  742. function module_clerk_info($module_name) {
  743. global $_W;
  744. $user_permissions = array();
  745. $module_name = trim($module_name);
  746. if (empty($module_name)) {
  747. return $user_permissions;
  748. }
  749. $params = array(
  750. ':role' => ACCOUNT_MANAGE_NAME_CLERK,
  751. ':type' => $module_name,
  752. ':uniacid' => $_W['uniacid']
  753. );
  754. $sql = "SELECT u.uid, p.permission FROM " . tablename('uni_account_users') . " u," . tablename('users_permission') . " p WHERE u.uid = p.uid AND u.uniacid = p.uniacid AND u.role = :role AND p.type = :type AND u.uniacid = :uniacid";
  755. $user_permissions = pdo_fetchall($sql, $params, 'uid');
  756. if (!empty($user_permissions)) {
  757. foreach ($user_permissions as $key => $value) {
  758. $user_permissions[$key]['user_info'] = user_single($value['uid']);
  759. }
  760. }
  761. return $user_permissions;
  762. }
  763. function module_rank_top($module_name) {
  764. global $_W;
  765. $result = table('module')->moduleSetRankTop($module_name);
  766. return empty($result) ? true : false;
  767. }