人人商城

account.mod.php 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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 uni_owned($uid = 0, $is_uni_fetch = true) {
  8. global $_W;
  9. $uid = intval($uid) > 0 ? intval($uid) : $_W['uid'];
  10. $uniaccounts = array();
  11. $user_accounts = uni_user_accounts($uid);
  12. if (empty($user_accounts)) {
  13. return $uniaccounts;
  14. }
  15. if (!empty($user_accounts) && !empty($is_uni_fetch)) {
  16. foreach ($user_accounts as &$row) {
  17. $row = uni_fetch($row['uniacid']);
  18. }
  19. }
  20. return $user_accounts;
  21. }
  22. function uni_user_accounts($uid = 0, $type = 'app') {
  23. global $_W;
  24. $uid = intval($uid) > 0 ? intval($uid) : $_W['uid'];
  25. if (!in_array($type, array('app', 'wxapp', 'webapp', 'phoneapp'))) {
  26. $type = 'app';
  27. }
  28. $type = $type == 'app' ? 'wechats' : $type;
  29. $cachekey = cache_system_key('user_accounts', array('type' => $type, 'uid' => $uid));
  30. $cache = cache_load($cachekey);
  31. if (!empty($cache)) {
  32. return $cache;
  33. }
  34. $select_fields = 'w.acid, w.uniacid, w.name, a.type';
  35. if (in_array($type, array('wechats', 'wxapp', 'xzapp'))) {
  36. $select_fields .= ', w.level, w.key, w.secret, w.token';
  37. }
  38. $where = '';
  39. $params = array();
  40. $user_is_founder = user_is_founder($uid);
  41. if (empty($user_is_founder) || user_is_vice_founder($uid)) {
  42. $select_fields .= ', u.role';
  43. $where .= " LEFT JOIN " . tablename('uni_account_users') . " u ON u.uniacid = w.uniacid WHERE u.uid = :uid AND u.role IN(:role1, :role2) ";
  44. $params[':uid'] = $uid;
  45. $params[':role1'] = ACCOUNT_MANAGE_NAME_OWNER;
  46. $params[':role2'] = ACCOUNT_MANAGE_NAME_VICE_FOUNDER;
  47. }
  48. $where .= !empty($where) ? " AND a.isdeleted <> 1 AND u.role IS NOT NULL" : " WHERE a.isdeleted <> 1";
  49. $sql = "SELECT " . $select_fields . " FROM " . tablename('account_' . $type) . " w LEFT JOIN " . tablename('account') . " a ON a.acid = w.acid AND a.uniacid = w.uniacid" . $where;
  50. $result = pdo_fetchall($sql, $params, 'uniacid');
  51. cache_write($cachekey, $result);
  52. return $result;
  53. }
  54. function account_owner($uniacid = 0) {
  55. global $_W;
  56. load()->model('user');
  57. $uniacid = intval($uniacid);
  58. if (empty($uniacid)) {
  59. return array();
  60. }
  61. $ownerid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), 'uid');
  62. if (empty($ownerid)) {
  63. $ownerid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'vice_founder'), 'uid');
  64. if (empty($ownerid)) {
  65. $founders = explode(',', $_W['config']['setting']['founder']);
  66. $ownerid = $founders[0];
  67. }
  68. }
  69. $owner = user_single($ownerid);
  70. if (empty($owner)) {
  71. return array();
  72. }
  73. return $owner;
  74. }
  75. function uni_accounts($uniacid = 0) {
  76. global $_W;
  77. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  78. $account_info = pdo_get('account', array('uniacid' => $uniacid));
  79. if (!empty($account_info)) {
  80. $accounts = pdo_fetchall("SELECT w.*, a.type, a.isconnect FROM " . tablename('account') . " a INNER JOIN " . tablename(uni_account_tablename($account_info['type'])) . " w USING(acid) WHERE a.uniacid = :uniacid AND a.isdeleted <> 1 ORDER BY a.acid ASC", array(':uniacid' => $uniacid), 'acid');
  81. }
  82. return !empty($accounts) ? $accounts : array();
  83. }
  84. function uni_fetch($uniacid = 0) {
  85. global $_W;
  86. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  87. $account_api = WeAccount::createByUniacid($uniacid);
  88. if (is_error($account_api)) {
  89. return $account_api;
  90. }
  91. $account_api->__toArray();
  92. return $account_api;
  93. }
  94. function uni_site_store_buy_goods($uniacid, $type = STORE_TYPE_MODULE) {
  95. $cachekey = cache_system_key('site_store_buy', array('type' => $type, 'uniacid' => $uniacid));
  96. $site_store_buy_goods = cache_load($cachekey);
  97. if (!empty($site_store_buy_goods)) {
  98. return $site_store_buy_goods;
  99. }
  100. $store_table = table('store');
  101. if ($type != STORE_TYPE_API) {
  102. $store_table->searchWithEndtime();
  103. $site_store_buy_goods = $store_table->searchAccountBuyGoods($uniacid, $type);
  104. $site_store_buy_goods = array_keys($site_store_buy_goods);
  105. } else {
  106. $site_store_buy_goods = $store_table->searchAccountBuyGoods($uniacid, $type);
  107. $setting = uni_setting_load('statistics', $uniacid);
  108. $use_number = isset($setting['statistics']['use']) ? intval($setting['statistics']['use']) : 0;
  109. $site_store_buy_goods = $site_store_buy_goods - $use_number;
  110. }
  111. cache_write($cachekey, $site_store_buy_goods);
  112. return $site_store_buy_goods;
  113. }
  114. function uni_modules_by_uniacid($uniacid) {
  115. global $_W;
  116. load()->model('user');
  117. load()->model('module');
  118. $account_info = uni_fetch($uniacid);
  119. $founders = explode(',', $_W['config']['setting']['founder']);
  120. $owner_uid = pdo_getall('uni_account_users', array('uniacid' => $uniacid, 'role' => array('owner', 'vice_founder')), array('uid', 'role'), 'role');
  121. $owner_uid = !empty($owner_uid['owner']) ? $owner_uid['owner']['uid'] : (!empty($owner_uid['vice_founder']) ? $owner_uid['vice_founder']['uid'] : 0);
  122. $cachekey = cache_system_key('unimodules', array('uniacid' => $uniacid));
  123. $modules = cache_load($cachekey);
  124. if (empty($modules)) {
  125. $enabled_modules = table('modules')->searchWithRecycle();
  126. if (!empty($owner_uid) && !in_array($owner_uid, $founders)) {
  127. $group_modules = table('account')->accountGroupModules($uniacid, $type);
  128. $goods_type = 0;
  129. switch ($account_info['type']) {
  130. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  131. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  132. $goods_type = STORE_TYPE_MODULE;
  133. break;
  134. case ACCOUNT_TYPE_APP_NORMAL:
  135. case ACCOUNT_TYPE_APP_AUTH:
  136. case ACCOUNT_TYPE_WXAPP_WORK:
  137. $goods_type = STORE_TYPE_WXAPP_MODULE;
  138. break;
  139. }
  140. if ($goods_type) {
  141. $site_store_buy_goods = uni_site_store_buy_goods($uniacid, $goods_type);
  142. if (!empty($site_store_buy_goods)) {
  143. $group_modules = array_merge($group_modules, $site_store_buy_goods);
  144. }
  145. }
  146. $user_modules = user_modules($owner_uid);
  147. if (!empty($user_modules)) {
  148. $group_modules = array_unique(array_merge($group_modules, array_keys($user_modules)));
  149. $group_modules = array_intersect(array_keys($enabled_modules), $group_modules);
  150. }
  151. } else {
  152. $group_modules = array_keys($enabled_modules);
  153. }
  154. cache_write($cachekey, $group_modules);
  155. $modules = $group_modules;
  156. }
  157. $system_modules = array(
  158. 'basic', 'news', 'music', 'service', 'userapi', 'recharge', 'images', 'video', 'voice', 'wxcard',
  159. 'custom', 'chats', 'paycenter', 'keyword', 'special', 'welcome', 'default', 'apply', 'reply', 'core', 'store',
  160. );
  161. $modules = array_merge($modules, $system_modules);
  162. $module_list = array();
  163. if (!empty($modules)) {
  164. foreach ($modules as $name) {
  165. $module_info = module_fetch($name);
  166. if ($module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  167. in_array($account_info['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH))) {
  168. continue;
  169. }
  170. if ($module_info[MODULE_SUPPORT_WEBAPP_NAME] != MODULE_SUPPORT_WEBAPP &&
  171. in_array($account_info['type'], array(ACCOUNT_TYPE_WEBAPP_NORMAL))) {
  172. continue;
  173. }
  174. if ($module_info[MODULE_SUPPORT_PHONEAPP_NAME] != MODULE_SUPPORT_PHONEAPP &&
  175. in_array($account_info['type'], array(ACCOUNT_TYPE_PHONEAPP_NORMAL))) {
  176. continue;
  177. }
  178. if ($module_info[MODULE_SUPPORT_XZAPP_NAME] != MODULE_SUPPORT_XZAPP &&
  179. in_array($account_info['type'], array(ACCOUNT_TYPE_XZAPP_NORMAL, ACCOUNT_TYPE_XZAPP_AUTH))) {
  180. continue;
  181. }
  182. if ($module_info[MODULE_SUPPORT_ALIAPP_NAME] != MODULE_SUPPORT_ALIAPP &&
  183. in_array($account_info['type'], array(ACCOUNT_TYPE_ALIAPP_NORMAL))) {
  184. continue;
  185. }
  186. if ($module_info[MODULE_SUPPORT_WXAPP_NAME] != MODULE_SUPPORT_WXAPP &&
  187. $module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  188. in_array($account_info['type'], array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH))) {
  189. continue;
  190. }
  191. if ($module_info[MODULE_SUPPORT_SYSTEMWELCOME_NAME] == MODULE_SUPPORT_SYSTEMWELCOME &&
  192. $module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  193. $module_info[MODULE_SUPPORT_WEBAPP_NAME] != MODULE_SUPPORT_WEBAPP &&
  194. $module_info[MODULE_SUPPORT_PHONEAPP_NAME] != MODULE_SUPPORT_PHONEAPP &&
  195. $module_info[MODULE_SUPPORT_ALIAPP_NAME] != MODULE_SUPPORT_ALIAPP &&
  196. $module_info[MODULE_SUPPORT_WXAPP_NAME] != MODULE_SUPPORT_WXAPP) {
  197. continue;
  198. }
  199. if (!empty($module_info)) {
  200. $module_list[$name] = $module_info;
  201. }
  202. }
  203. }
  204. $module_list['core'] = array('title' => '系统事件处理模块', 'name' => 'core', 'issystem' => 1, 'enabled' => 1, 'isdisplay' => 0);
  205. return $module_list;
  206. }
  207. function uni_modules_list($uniacid, $type = '') {
  208. global $_W;
  209. if ($type == '') {
  210. $account_info = uni_fetch($uniacid);
  211. $type = $account_info['type'];
  212. }
  213. $founders = explode(',', $_W['config']['setting']['founder']);
  214. $owner_uid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), 'uid');
  215. $modules = table('modules')->searchWithRecycle();
  216. if (!empty($owner_uid) && !in_array($owner_uid, $founders)) {
  217. $group_modules = table('account')->accountGroupModules($uniacid, $type);
  218. $goods_type = 0;
  219. switch ($type) {
  220. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  221. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  222. $goods_type = STORE_TYPE_MODULE;
  223. break;
  224. case ACCOUNT_TYPE_APP_NORMAL:
  225. case ACCOUNT_TYPE_APP_AUTH:
  226. case ACCOUNT_TYPE_WXAPP_WORK:
  227. $goods_type = STORE_TYPE_WXAPP_MODULE;
  228. break;
  229. }
  230. if ($goods_type) {
  231. $site_store_buy_goods = uni_site_store_buy_goods($uniacid, $goods_type);
  232. if (!empty($site_store_buy_goods)) {
  233. $group_modules = array_merge($group_modules, $site_store_buy_goods);
  234. }
  235. }
  236. $user_modules = user_modules($owner_uid);
  237. if (!empty($user_modules)) {
  238. $group_modules = array_merge($group_modules, array_keys($user_modules));
  239. }
  240. } else {
  241. $group_modules = array_keys($modules);
  242. }
  243. $module_list = array();
  244. if (!empty($group_modules)) {
  245. foreach ($group_modules as $module_name) {
  246. $module_info = $modules[$module_name];
  247. if (file_exists (IA_ROOT . '/addons/' . $module_info['name'] . '/icon-custom.jpg')) {
  248. $module_info['logo'] = tomedia (IA_ROOT . '/addons/' . $module_info['name'] . '/icon-custom.jpg') . "?v=" . time ();
  249. } else {
  250. $module_info['logo'] = tomedia (IA_ROOT . '/addons/' . $module_info['name'] . '/icon.jpg') . "?v=" . time ();
  251. }
  252. if ($module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  253. in_array($type, array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH))) {
  254. continue;
  255. }
  256. if ($module_info[MODULE_SUPPORT_WEBAPP_NAME] != MODULE_SUPPORT_WEBAPP &&
  257. in_array($type, array(ACCOUNT_TYPE_WEBAPP_NORMAL))) {
  258. continue;
  259. }
  260. if ($module_info[MODULE_SUPPORT_PHONEAPP_NAME] != MODULE_SUPPORT_PHONEAPP &&
  261. in_array($type, array(ACCOUNT_TYPE_PHONEAPP_NORMAL))) {
  262. continue;
  263. }
  264. if ($module_info[MODULE_SUPPORT_XZAPP_NAME] != MODULE_SUPPORT_XZAPP &&
  265. in_array($type, array(ACCOUNT_TYPE_XZAPP_NORMAL, ACCOUNT_TYPE_XZAPP_AUTH))) {
  266. continue;
  267. }
  268. if ($module_info[MODULE_SUPPORT_ALIAPP_NAME] != MODULE_SUPPORT_ALIAPP &&
  269. in_array($type, array(ACCOUNT_TYPE_ALIAPP_NORMAL))) {
  270. continue;
  271. }
  272. if ($module_info[MODULE_SUPPORT_WXAPP_NAME] != MODULE_SUPPORT_WXAPP &&
  273. $module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  274. in_array($type, array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH))) {
  275. continue;
  276. }
  277. if ($module_info[MODULE_SUPPORT_SYSTEMWELCOME_NAME] == MODULE_SUPPORT_SYSTEMWELCOME &&
  278. $module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  279. $module_info[MODULE_SUPPORT_WEBAPP_NAME] != MODULE_SUPPORT_WEBAPP &&
  280. $module_info[MODULE_SUPPORT_PHONEAPP_NAME] != MODULE_SUPPORT_PHONEAPP &&
  281. $module_info[MODULE_SUPPORT_WXAPP_NAME] != MODULE_SUPPORT_WXAPP) {
  282. continue;
  283. }
  284. if (!empty($module_info)) {
  285. $module_list[$module_name] = $module_info;
  286. }
  287. }
  288. }
  289. $module_list['core'] = array('title' => '系统事件处理模块', 'name' => 'core', 'issystem' => 1, 'enabled' => 1, 'isdisplay' => 0);
  290. return $module_list;
  291. }
  292. function uni_modules() {
  293. global $_W;
  294. return uni_modules_by_uniacid($_W['uniacid']);
  295. }
  296. function uni_modules_app_binding() {
  297. global $_W;
  298. $cachekey = cache_system_key('unimodules_binding', array('uniacid' => $_W['uniacid']));
  299. $cache = cache_load($cachekey);
  300. if (!empty($cache)) {
  301. return $cache;
  302. }
  303. load()->model('module');
  304. $result = array();
  305. $modules = uni_modules();
  306. if(!empty($modules)) {
  307. foreach($modules as $module) {
  308. if($module['type'] == 'system') {
  309. continue;
  310. }
  311. $entries = module_app_entries($module['name'], array('home', 'profile', 'shortcut', 'function', 'cover'));
  312. if(empty($entries)) {
  313. continue;
  314. }
  315. if($module['type'] == '') {
  316. $module['type'] = 'other';
  317. }
  318. $result[$module['name']] = array(
  319. 'name' => $module['name'],
  320. 'type' => $module['type'],
  321. 'title' => $module['title'],
  322. 'entries' => array(
  323. 'cover' => $entries['cover'],
  324. 'home' => $entries['home'],
  325. 'profile' => $entries['profile'],
  326. 'shortcut' => $entries['shortcut'],
  327. 'function' => $entries['function']
  328. )
  329. );
  330. unset($module);
  331. }
  332. }
  333. cache_write($cachekey, $result);
  334. return $result;
  335. }
  336. function uni_groups($groupids = array(), $show_all = false) {
  337. load()->model('module');
  338. global $_W;
  339. $cachekey = cache_system_key('uni_groups');
  340. $list = cache_load($cachekey);
  341. if (empty($list)) {
  342. $condition = ' WHERE uniacid = 0 AND uid = 0';
  343. $list = pdo_fetchall("SELECT * FROM " . tablename('uni_group') . $condition . " ORDER BY id DESC", array(), 'id');
  344. if (!empty($groupids)) {
  345. if (in_array('-1', $groupids)) {
  346. $list[-1] = array('id' => -1, 'name' => '所有服务', 'modules' => array('title' => '系统所有模块'), 'templates' => array('title' => '系统所有模板'));
  347. }
  348. if (in_array('0', $groupids)) {
  349. $list[0] = array('id' => 0, 'name' => '基础服务', 'modules' => array('title' => '系统模块'), 'templates' => array('title' => '系统模板'));
  350. }
  351. }
  352. if (!empty($list)) {
  353. foreach ($list as $k => &$row) {
  354. $modules = (array)iunserializer($row['modules']);
  355. $row['modules'] = $row['wxapp'] = $row['webapp'] = $row['phoneapp'] = $row['xzapp'] = $row['aliapp'] = array();
  356. if (!empty($modules)) {
  357. foreach ($modules as $type => $modulenames) {
  358. if (empty($modulenames) || !is_array($modulenames)) {
  359. continue;
  360. }
  361. foreach ($modulenames as $name) {
  362. $module = module_fetch($name);
  363. if (empty($module)) {
  364. continue;
  365. }
  366. switch ($type) {
  367. case 'modules':
  368. if ($module[MODULE_SUPPORT_ACCOUNT_NAME] == MODULE_SUPPORT_ACCOUNT) {
  369. $row['modules'][] = $name;
  370. }
  371. break;
  372. case 'wxapp':
  373. if ($module[MODULE_SUPPORT_WXAPP_NAME] == MODULE_SUPPORT_WXAPP) {
  374. $row['wxapp'][] = $name;
  375. }
  376. break;
  377. case 'webapp':
  378. if ($module[MODULE_SUPPORT_WEBAPP_NAME] == MODULE_SUPPORT_WEBAPP) {
  379. $row['webapp'][] = $name;
  380. }
  381. break;
  382. case 'xzapp':
  383. if ($module[MODULE_SUPPORT_XZAPP_NAME] == MODULE_SUPPORT_XZAPP) {
  384. $row['xzapp'][] = $name;
  385. }
  386. break;
  387. case 'phoneapp':
  388. if ($module[MODULE_SUPPORT_PHONEAPP_NAME] == MODULE_SUPPORT_PHONEAPP) {
  389. $row['phoneapp'][] = $name;
  390. }
  391. break;
  392. case 'aliapp':
  393. if ($module[MODULE_SUPPORT_ALIAPP_NAME] == MODULE_SUPPORT_ALIAPP) {
  394. $row['aliapp'][] = $name;
  395. }
  396. break;
  397. }
  398. }
  399. }
  400. }
  401. if (!empty($row['templates'])) {
  402. $row['templates'] = (array)iunserializer($row['templates']);
  403. if (!empty($row['templates'])) {
  404. $row['templates'] = pdo_getall('site_templates', array('id' => $row['templates']), array('id', 'name', 'title'), 'name');
  405. }
  406. }
  407. }
  408. }
  409. cache_write($cachekey, $list);
  410. }
  411. $group_list = array();
  412. if (!empty($groupids)) {
  413. foreach ($groupids as $id) {
  414. $group_list[$id] = $list[$id];
  415. }
  416. } else {
  417. if (user_is_vice_founder() && empty($show_all)) {
  418. foreach ($list as $group_key => $group) {
  419. if ($group['owner_uid'] != $_W['uid']) {
  420. unset($list[$group_key]);
  421. continue;
  422. }
  423. }
  424. }
  425. $group_list = $list;
  426. }
  427. $module_section = array('modules', 'phoneapp', 'wxapp', 'webapp', 'xzapp', 'aliapp');
  428. if (!empty($group_list)) {
  429. foreach ($group_list as $id => $group) {
  430. foreach ($module_section as $section) {
  431. if (!empty($group_list[$id][$section])) {
  432. $modules = $group_list[$id][$section];
  433. $group_list[$id][$section] = array();
  434. foreach ($modules as $modulename) {
  435. if (is_string($modulename)) {
  436. $group_list[$id][$section][$modulename] = module_fetch($modulename);
  437. }
  438. }
  439. }
  440. }
  441. }
  442. }
  443. return $group_list;
  444. }
  445. function uni_templates() {
  446. global $_W;
  447. $owneruid = pdo_fetchcolumn("SELECT uid FROM ".tablename('uni_account_users')." WHERE uniacid = :uniacid AND role = 'owner'", array(':uniacid' => $_W['uniacid']));
  448. load()->model('user');
  449. $owner = user_single(array('uid' => $owneruid));
  450. if (empty($owner) || user_is_founder($owner['uid'])) {
  451. $groupid = '-1';
  452. } else {
  453. $groupid = $owner['groupid'];
  454. }
  455. $extend = pdo_getall('uni_account_group', array('uniacid' => $_W['uniacid']), array(), 'groupid');
  456. if (!empty($extend) && $groupid != '-1') {
  457. $groupid = '-2';
  458. }
  459. if (empty($groupid)) {
  460. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " WHERE name = 'default'", array(), 'id');
  461. } elseif ($groupid == '-1') {
  462. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " ORDER BY id ASC", array(), 'id');
  463. } else {
  464. $group = pdo_fetch("SELECT id, name, package FROM ".tablename('users_group')." WHERE id = :id", array(':id' => $groupid));
  465. $packageids = iunserializer($group['package']);
  466. if (!empty($extend)) {
  467. foreach ($extend as $extend_packageid => $row) {
  468. $packageids[] = $extend_packageid;
  469. }
  470. }
  471. if(is_array($packageids)) {
  472. if (in_array('-1', $packageids)) {
  473. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " ORDER BY id ASC", array(), 'id');
  474. } else {
  475. $wechatgroup = pdo_fetchall("SELECT `templates` FROM " . tablename('uni_group') . " WHERE id IN ('".implode("','", $packageids)."') OR uniacid = '{$_W['uniacid']}'");
  476. $ms = array();
  477. $mssql = '';
  478. if (!empty($wechatgroup)) {
  479. foreach ($wechatgroup as $row) {
  480. $row['templates'] = iunserializer($row['templates']);
  481. if (!empty($row['templates'])) {
  482. foreach ($row['templates'] as $templateid) {
  483. $ms[$templateid] = $templateid;
  484. }
  485. }
  486. }
  487. $ms[] = 1;
  488. $mssql = " `id` IN ('".implode("','", $ms)."')";
  489. }
  490. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') .(!empty($mssql) ? " WHERE $mssql" : '')." ORDER BY id DESC", array(), 'id');
  491. }
  492. }
  493. }
  494. if (empty($templates)) {
  495. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " WHERE id = 1 ORDER BY id DESC", array(), 'id');
  496. }
  497. return $templates;
  498. }
  499. function uni_setting_save($name, $value) {
  500. global $_W;
  501. if (empty($name)) {
  502. return false;
  503. }
  504. if (is_array($value)) {
  505. $value = serialize($value);
  506. }
  507. $unisetting = pdo_get('uni_settings', array('uniacid' => $_W['uniacid']), array('uniacid'));
  508. if (!empty($unisetting)) {
  509. pdo_update('uni_settings', array($name => $value), array('uniacid' => $_W['uniacid']));
  510. } else {
  511. pdo_insert('uni_settings', array($name => $value, 'uniacid' => $_W['uniacid']));
  512. }
  513. cache_delete(cache_system_key('uniaccount', array('uniacid' => $_W['uniacid'])));
  514. return true;
  515. }
  516. function uni_setting_load($name = '', $uniacid = 0) {
  517. global $_W;
  518. $uniacid = empty($uniacid) ? $_W['uniacid'] : $uniacid;
  519. $cachekey = cache_system_key('unisetting', array('uniacid' => $uniacid));
  520. $unisetting = cache_load($cachekey);
  521. if (empty($unisetting)) {
  522. $unisetting = pdo_get('uni_settings', array('uniacid' => $uniacid));
  523. if (!empty($unisetting)) {
  524. $serialize = array('site_info', 'stat', 'oauth', 'passport', 'uc', 'notify',
  525. 'creditnames', 'default_message', 'creditbehaviors', 'payment',
  526. 'recharge', 'tplnotice', 'mcplugin', 'statistics', 'bind_domain');
  527. foreach ($unisetting as $key => &$row) {
  528. if (in_array($key, $serialize) && !empty($row)) {
  529. $row = (array)iunserializer($row);
  530. }
  531. }
  532. } else {
  533. $unisetting = array();
  534. }
  535. cache_write($cachekey, $unisetting);
  536. }
  537. if (empty($unisetting)) {
  538. return array();
  539. }
  540. if (empty($name)) {
  541. return $unisetting;
  542. }
  543. if (!is_array($name)) {
  544. $name = array($name);
  545. }
  546. return array_elements($name, $unisetting);
  547. }
  548. if (!function_exists('uni_setting')) {
  549. function uni_setting($uniacid = 0, $fields = '*', $force_update = false) {
  550. global $_W;
  551. load()->model('account');
  552. if ($fields == '*') {
  553. $fields = '';
  554. }
  555. return uni_setting_load($fields, $uniacid);
  556. }
  557. }
  558. function uni_account_default($uniacid = 0) {
  559. global $_W;
  560. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  561. $uni_account = pdo_fetch("SELECT * FROM ".tablename('uni_account')." a LEFT JOIN ".tablename('account')." w ON a.uniacid = w.uniacid AND a.default_acid = w.acid WHERE a.uniacid = :uniacid", array(':uniacid' => $uniacid));
  562. if (empty($uni_account)) {
  563. $uni_account = pdo_fetch("SELECT * FROM ".tablename('uni_account')." a LEFT JOIN ".tablename('account')." w ON a.uniacid = w.uniacid WHERE a.uniacid = :uniacid ORDER BY w.acid DESC", array(':uniacid' => $uniacid));
  564. }
  565. if (!empty($uni_account)) {
  566. $account = pdo_get(uni_account_tablename($uni_account['type']), array('acid' => $uni_account['acid']));
  567. if (empty($account)) {
  568. $account['uniacid'] = $uni_account['uniacid'];
  569. $account['acid'] = $uni_account['default_acid'];
  570. }
  571. $account['type'] = $uni_account['type'];
  572. $account['isconnect'] = $uni_account['isconnect'];
  573. $account['isdeleted'] = $uni_account['isdeleted'];
  574. $account['endtime'] = $uni_account['endtime'];
  575. return $account;
  576. }
  577. }
  578. function uni_account_tablename($type) {
  579. switch ($type) {
  580. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  581. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  582. return 'account_wechats';
  583. case ACCOUNT_TYPE_APP_NORMAL:
  584. case ACCOUNT_TYPE_APP_AUTH:
  585. return 'account_wxapp';
  586. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  587. return 'account_webapp';
  588. case ACCOUNT_TYPE_PHONEAPP_NORMAL:
  589. return 'account_phoneapp';
  590. case ACCOUNT_TYPE_XZAPP_NORMAL:
  591. return 'account_xzapp';
  592. case ACCOUNT_TYPE_ALIAPP_NORMAL:
  593. return 'account_aliapp';
  594. }
  595. }
  596. function uni_user_account_role($uniacid, $uid, $role) {
  597. $vice_account = array(
  598. 'uniacid' => intval($uniacid),
  599. 'uid' => intval($uid),
  600. 'role' => trim($role)
  601. );
  602. $account_user = pdo_get('uni_account_users', $vice_account, array('id'));
  603. if (!empty($account_user)) {
  604. return false;
  605. }
  606. return pdo_insert('uni_account_users', $vice_account);
  607. }
  608. function uni_user_see_more_info($user_type, $see_more = false) {
  609. global $_W;
  610. if (empty($user_type)) {
  611. return false;
  612. }
  613. if ($user_type == ACCOUNT_MANAGE_NAME_VICE_FOUNDER && !empty($see_more) || $_W['role'] != $user_type) {
  614. return true;
  615. }
  616. return false;
  617. }
  618. function uni_owner_account_nums($uid, $role) {
  619. $account_num = $wxapp_num = $webapp_num = $phoneapp_num = $xzapp_num = $aliapp_num = 0;
  620. $condition = array('uid' => $uid, 'role' => $role);
  621. $uniacocunts = pdo_getall('uni_account_users', $condition, array(), 'uniacid');
  622. if (!empty($uniacocunts)) {
  623. $all_account = pdo_fetchall('SELECT * FROM (SELECT u.uniacid, a.default_acid FROM ' . tablename('uni_account_users') . ' as u RIGHT JOIN '. tablename('uni_account').' as a ON a.uniacid = u.uniacid WHERE u.uid = :uid AND u.role = :role ) AS c LEFT JOIN '.tablename('account').' as d ON c.default_acid = d.acid WHERE d.isdeleted = 0', array(':uid' => $uid, ':role' => $role));
  624. foreach ($all_account as $account) {
  625. if ($account['type'] == ACCOUNT_TYPE_OFFCIAL_NORMAL || $account['type'] == ACCOUNT_TYPE_OFFCIAL_AUTH) {
  626. $account_num++;
  627. }
  628. if ($account['type'] == ACCOUNT_TYPE_APP_NORMAL || $account['type'] == ACCOUNT_TYPE_APP_AUTH) {
  629. $wxapp_num++;
  630. }
  631. if ($account['type'] == ACCOUNT_TYPE_WEBAPP_NORMAL) {
  632. $webapp_num++;
  633. }
  634. if ($account['type'] == ACCOUNT_TYPE_PHONEAPP_NORMAL) {
  635. $phoneapp_num++;
  636. }
  637. if ($account['type'] == ACCOUNT_TYPE_XZAPP_NORMAL) {
  638. $xzapp_num++;
  639. }
  640. if ($account['type'] == ACCOUNT_TYPE_ALIAPP_NORMAL) {
  641. $aliapp_num++;
  642. }
  643. }
  644. }
  645. $num = array(
  646. 'account_num' => $account_num,
  647. 'wxapp_num' => $wxapp_num,
  648. 'webapp_num' => $webapp_num,
  649. 'phoneapp_num' => $phoneapp_num,
  650. 'xzapp_num' => $xzapp_num,
  651. 'aliapp_num' => $aliapp_num,
  652. );
  653. return $num;
  654. }
  655. function uni_update_week_stat() {
  656. global $_W;
  657. $cachekey = cache_system_key('stat_todaylock', array('uniacid' => $_W['uniacid']));
  658. $cache = cache_load($cachekey);
  659. if(!empty($cache) && $cache['expire'] > TIMESTAMP) {
  660. return true;
  661. }
  662. $seven_days = array(
  663. date('Ymd', strtotime('-1 days')),
  664. date('Ymd', strtotime('-2 days')),
  665. date('Ymd', strtotime('-3 days')),
  666. date('Ymd', strtotime('-4 days')),
  667. date('Ymd', strtotime('-5 days')),
  668. date('Ymd', strtotime('-6 days')),
  669. date('Ymd', strtotime('-7 days')),
  670. );
  671. $week_stat_fans = pdo_getall('stat_fans', array('date' => $seven_days, 'uniacid' => $_W['uniacid']), '', 'date');
  672. $stat_update_yes = false;
  673. foreach ($seven_days as $sevens) {
  674. if (empty($week_stat_fans[$sevens]) || $week_stat_fans[$sevens]['cumulate'] <=0) {
  675. $stat_update_yes = true;
  676. break;
  677. }
  678. }
  679. if (empty($stat_update_yes)) {
  680. return true;
  681. }
  682. foreach($seven_days as $sevens) {
  683. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY) {
  684. $account_obj = WeAccount::createByUniacid();
  685. $weixin_stat = $account_obj->getFansStat();
  686. if(is_error($weixin_stat) || empty($weixin_stat)) {
  687. return error(-1, '调用微信接口错误');
  688. } else {
  689. $update_stat = array();
  690. $update_stat = array(
  691. 'uniacid' => $_W['uniacid'],
  692. 'new' => $weixin_stat[$sevens]['new'],
  693. 'cancel' => $weixin_stat[$sevens]['cancel'],
  694. 'cumulate' => $weixin_stat[$sevens]['cumulate'],
  695. 'date' => $sevens,
  696. );
  697. }
  698. } else {
  699. $update_stat = array();
  700. $update_stat['cumulate'] = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('mc_mapping_fans') . " WHERE acid = :acid AND uniacid = :uniacid AND follow = :follow AND followtime < :endtime", array(':acid' => $_W['acid'], ':uniacid' => $_W['uniacid'], ':endtime' => strtotime($sevens)+86400, ':follow' => 1));
  701. $update_stat['date'] = $sevens;
  702. $update_stat['new'] = $week_stat_fans[$sevens]['new'];
  703. $update_stat['cancel'] = $week_stat_fans[$sevens]['cancel'];
  704. $update_stat['uniacid'] = $_W['uniacid'];
  705. }
  706. if(empty($week_stat_fans[$sevens])) {
  707. pdo_insert('stat_fans', $update_stat);
  708. } elseif (empty($week_stat_fans[$sevens]['cumulate']) || $week_stat_fans[$sevens]['cumulate'] < 0) {
  709. pdo_update('stat_fans', $update_stat, array('id' => $week_stat_fans[$sevens]['id']));
  710. }
  711. }
  712. cache_write($cachekey, array('expire' => TIMESTAMP + 7200));
  713. return true;
  714. }
  715. function uni_account_rank_top($uniacid) {
  716. global $_W;
  717. if (!empty($_W['isfounder'])) {
  718. $max_rank = pdo_getcolumn('uni_account', array(), 'max(rank)');
  719. pdo_update('uni_account', array('rank' => ($max_rank + 1)), array('uniacid' => $uniacid));
  720. }else {
  721. $max_rank = pdo_getcolumn('uni_account_users', array('uid' => $_W['uid']), 'max(rank)');
  722. pdo_update('uni_account_users', array('rank' => ($max_rank['maxrank'] + 1)), array('uniacid' => $uniacid, 'uid' => $_W['uid']));
  723. }
  724. return true;
  725. }
  726. function uni_account_last_switch() {
  727. global $_W, $_GPC;
  728. $cache_key = cache_system_key('last_account', array('switch' => $_GPC['__switch']));
  729. $cache_lastaccount = (int)cache_load($cache_key);
  730. return $cache_lastaccount;
  731. }
  732. function uni_account_switch($uniacid, $redirect = '', $type = ACCOUNT_TYPE_SIGN) {
  733. global $_W;
  734. if (!in_array($type, array(ACCOUNT_TYPE_SIGN, WXAPP_TYPE_SIGN, WEBAPP_TYPE_SIGN, PHONEAPP_TYPE_SIGN, XZAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN))) {
  735. return error(-1, '账号类型不合法');
  736. }
  737. uni_account_save_switch($uniacid, $type);
  738. isetcookie('__uid', $_W['uid'], 7 * 86400);
  739. if (!empty($redirect)) {
  740. header('Location: ' . $redirect);
  741. exit;
  742. }
  743. return true;
  744. }
  745. function uni_account_save_switch($uniacid, $type = ACCOUNT_TYPE_SIGN) {
  746. global $_W, $_GPC;
  747. load()->model('visit');
  748. if (!in_array($type, array(ACCOUNT_TYPE_SIGN, WXAPP_TYPE_SIGN, WEBAPP_TYPE_SIGN, PHONEAPP_TYPE_SIGN, XZAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN))) {
  749. return error(-1, '账号类型不合法');
  750. }
  751. if (empty($_GPC['__switch'])) {
  752. $_GPC['__switch'] = random(5);
  753. }
  754. $cache_key = cache_system_key('last_account', array('switch' => $_GPC['__switch']));
  755. $cache_lastaccount = $uniacid;
  756. visit_system_update(array('uniacid' => $uniacid, 'uid' => $_W['uid']));
  757. cache_write($cache_key, $cache_lastaccount);
  758. cache_write(cache_system_key('last_account_type'), $type);
  759. isetcookie('__uniacid', $uniacid, 7 * 86400);
  760. isetcookie('__switch', $_GPC['__switch'], 7 * 86400);
  761. return true;
  762. }
  763. function account_create($uniacid, $account) {
  764. global $_W;
  765. $accountdata = array('uniacid' => $uniacid, 'type' => $account['type'], 'hash' => random(8));
  766. $user_create_account_info = permission_user_account_num();
  767. if (empty($_W['isfounder']) && empty($user_create_account_info['usergroup_account_limit'])) {
  768. $accountdata['endtime'] = strtotime('+1 month', time());
  769. pdo_insert('site_store_create_account', array('endtime' => strtotime('+1 month', time()), 'uid' => $_W['uid'], 'uniacid' => $uniacid, 'type' => $account['type']));
  770. }
  771. pdo_insert('account', $accountdata);
  772. $acid = pdo_insertid();
  773. $account['acid'] = $acid;
  774. $account['token'] = random(32);
  775. $account['encodingaeskey'] = random(43);
  776. $account['uniacid'] = $uniacid;
  777. $table = $account['type'] == ACCOUNT_TYPE_OFFCIAL_NORMAL ? 'account_wechats' : 'account_xzapp';
  778. unset($account['type']);
  779. pdo_insert($table, $account);
  780. return $acid;
  781. }
  782. function account_fetch($acid) {
  783. $account_info = pdo_get('account', array('acid' => $acid));
  784. if (empty($account_info)) {
  785. return error(-1, '公众号不存在');
  786. }
  787. return uni_fetch($account_info['uniacid']);
  788. }
  789. function uni_setmeal($uniacid = 0) {
  790. global $_W;
  791. if(!$uniacid) {
  792. $uniacid = $_W['uniacid'];
  793. }
  794. $owneruid = pdo_fetchcolumn("SELECT uid FROM ".tablename('uni_account_users')." WHERE uniacid = :uniacid AND role = 'owner'", array(':uniacid' => $uniacid));
  795. if(empty($owneruid)) {
  796. $user = array(
  797. 'uid' => -1,
  798. 'username' => '创始人',
  799. 'timelimit' => '未设置',
  800. 'groupid' => '-1',
  801. 'groupname' => '所有服务'
  802. );
  803. return $user;
  804. }
  805. load()->model('user');
  806. $groups = pdo_getall('users_group', array(), array('id', 'name'), 'id');
  807. $owner = user_single(array('uid' => $owneruid));
  808. $user = array(
  809. 'uid' => $owner['uid'],
  810. 'username' => $owner['username'],
  811. 'groupid' => $owner['groupid'],
  812. 'groupname' => $groups[$owner['groupid']]['name']
  813. );
  814. if(empty($owner['endtime'])) {
  815. $user['timelimit'] = date('Y-m-d', $owner['starttime']) . ' ~ 无限制' ;
  816. } else {
  817. if($owner['endtime'] <= TIMESTAMP) {
  818. $user['timelimit'] = '已到期';
  819. } else {
  820. $year = 0;
  821. $month = 0;
  822. $day = 0;
  823. $endtime = $owner['endtime'];
  824. $time = strtotime('+1 year');
  825. while ($endtime > $time)
  826. {
  827. $year = $year + 1;
  828. $time = strtotime("+1 year", $time);
  829. };
  830. $time = strtotime("-1 year", $time);
  831. $time = strtotime("+1 month", $time);
  832. while($endtime > $time)
  833. {
  834. $month = $month + 1;
  835. $time = strtotime("+1 month", $time);
  836. } ;
  837. $time = strtotime("-1 month", $time);
  838. $time = strtotime("+1 day", $time);
  839. while($endtime > $time)
  840. {
  841. $day = $day + 1;
  842. $time = strtotime("+1 day", $time);
  843. } ;
  844. if (empty($year)) {
  845. $timelimit = empty($month)? $day.'天' : date('Y-m-d', $owner['starttime']) . '~'. date('Y-m-d', $owner['endtime']);
  846. }else {
  847. $timelimit = date('Y-m-d', $owner['starttime']) . '~'. date('Y-m-d', $owner['endtime']);
  848. }
  849. $user['timelimit'] = $timelimit;
  850. }
  851. }
  852. return $user;
  853. }
  854. function uni_is_multi_acid($uniacid = 0) {
  855. global $_W;
  856. if(!$uniacid) {
  857. $uniacid = $_W['uniacid'];
  858. }
  859. $cachekey = cache_system_key('unicount', array('uniacid' => $uniacid));
  860. $nums = cache_load($cachekey);
  861. $nums = intval($nums);
  862. if(!$nums) {
  863. $nums = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('account_wechats') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
  864. cache_write($cachekey, $nums);
  865. }
  866. if($nums <= 1) {
  867. return false;
  868. }
  869. return true;
  870. }
  871. function account_delete($acid) {
  872. global $_W;
  873. load()->func('file');
  874. load()->model('module');
  875. load()->model('job');
  876. $jobid = 0;
  877. $account = pdo_get('uni_account', array('default_acid' => $acid));
  878. if ($account) {
  879. $uniacid = $account['uniacid'];
  880. $state = permission_account_user_role($_W['uid'], $uniacid);
  881. if (!in_array($state, array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_FOUNDER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER))) {
  882. itoast('没有该公众号操作权限!', url('account/recycle'), 'error');
  883. }
  884. if($uniacid == $_W['uniacid']) {
  885. isetcookie('__uniacid', '');
  886. }
  887. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  888. $modules = array();
  889. $rules = pdo_fetchall("SELECT id, module FROM ".tablename('rule')." WHERE uniacid = '{$uniacid}'");
  890. if (!empty($rules)) {
  891. foreach ($rules as $index => $rule) {
  892. $deleteid[] = $rule['id'];
  893. }
  894. pdo_delete('rule', "id IN ('".implode("','", $deleteid)."')");
  895. }
  896. $subaccount = pdo_fetchall("SELECT acid FROM ".tablename('account')." WHERE uniacid = :uniacid", array(':uniacid' => $uniacid));
  897. if (!empty($subaccount)) {
  898. foreach ($subaccount as $childaccount) {
  899. @unlink(IA_ROOT . '/attachment/qrcode_'.$childaccount['acid'].'.jpg');
  900. @unlink(IA_ROOT . '/attachment/headimg_'.$childaccount['acid'].'.jpg');
  901. file_remote_delete('qrcode_'.$childaccount['acid'].'.jpg');
  902. file_remote_delete('headimg_'.$childaccount['acid'].'.jpg');
  903. }
  904. if (!empty($acid)) {
  905. $jobid = job_create_delete_account($uniacid, $account['name'], $_W['uid']);
  906. }
  907. }
  908. $tables = array(
  909. 'account','account_wechats', 'account_wxapp', 'wxapp_versions', 'account_webapp', 'account_phoneapp',
  910. 'phoneapp_versions','core_paylog','core_resource',
  911. 'cover_reply', 'mc_chats_record','mc_credits_recharge','mc_credits_record',
  912. 'mc_fans_groups','mc_groups','mc_handsel','mc_mapping_fans','mc_mapping_ucenter','mc_mass_record',
  913. 'mc_member_address','mc_member_fields','mc_members','menu_event',
  914. 'qrcode','qrcode_stat', 'rule','rule_keyword','site_article','site_category','site_multi','site_nav','site_slide',
  915. 'site_styles','site_styles_vars','stat_keyword', 'stat_rule','uni_account','uni_account_modules','uni_account_users','uni_settings', 'uni_group', 'uni_verifycode','users_permission',
  916. 'mc_member_fields', 'wechat_news',
  917. );
  918. if (!empty($tables)) {
  919. foreach ($tables as $table) {
  920. $tablename = str_replace($GLOBALS['_W']['config']['db']['tablepre'], '', $table);
  921. pdo_delete($tablename, array( 'uniacid'=> $uniacid));
  922. }
  923. }
  924. } else {
  925. $account = account_fetch($acid);
  926. if (empty($account)) {
  927. itoast('子公众号不存在或是已经被删除', '', '');
  928. }
  929. $uniacid = $account['uniacid'];
  930. $state = permission_account_user_role($_W['uid'], $uniacid);
  931. if($state != ACCOUNT_MANAGE_NAME_FOUNDER && $state != ACCOUNT_MANAGE_NAME_OWNER) {
  932. itoast('没有该公众号操作权限!', url('account/recycle'), 'error');
  933. }
  934. $uniaccount = uni_fetch($account['uniacid']);
  935. if ($uniaccount['default_acid'] == $acid) {
  936. itoast('默认子公众号不能删除', '', '');
  937. }
  938. pdo_delete('account', array('acid' => $acid));
  939. pdo_delete('account_wechats', array('acid' => $acid, 'uniacid' => $uniacid));
  940. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  941. cache_delete(cache_system_key('account_auth_refreshtoken', array('acid' => $acid)));
  942. $oauth = uni_setting($uniacid, array('oauth'));
  943. if($oauth['oauth']['account'] == $acid) {
  944. $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account_wechats') . " WHERE uniacid = :id AND level = 4 AND secret != '' AND `key` != ''", array(':id' => $uniacid));
  945. pdo_update('uni_settings', array('oauth' => iserializer(array('account' => $acid, 'host' => $oauth['oauth']['host']))), array('uniacid' => $uniacid));
  946. }
  947. @unlink(IA_ROOT . '/attachment/qrcode_'.$acid.'.jpg');
  948. @unlink(IA_ROOT . '/attachment/headimg_'.$acid.'.jpg');
  949. file_remote_delete('qrcode_'.$acid.'.jpg');
  950. file_remote_delete('headimg_'.$acid.'.jpg');
  951. }
  952. return $jobid;
  953. }
  954. function account_wechatpay_proxy () {
  955. global $_W;
  956. $proxy_account = cache_load(cache_system_key('proxy_wechatpay_account'));
  957. if (empty($proxy_account)) {
  958. $proxy_account = cache_build_proxy_wechatpay_account();
  959. }
  960. unset($proxy_account['borrow'][$_W['uniacid']]);
  961. unset($proxy_account['service'][$_W['uniacid']]);
  962. return $proxy_account;
  963. }
  964. function uni_account_module_shortcut_enabled($modulename, $status = STATUS_ON) {
  965. global $_W;
  966. $module = module_fetch($modulename);
  967. if(empty($module)) {
  968. return error(1, '抱歉,你操作的模块不能被访问!');
  969. }
  970. $module_status = pdo_get('uni_account_modules', array('module' => $modulename, 'uniacid' => $_W['uniacid']), array('id', 'shortcut'));
  971. if (empty($module_status)) {
  972. $data = array(
  973. 'uniacid' => $_W['uniacid'],
  974. 'module' => $modulename,
  975. 'enabled' => STATUS_ON,
  976. 'shortcut' => $status ? STATUS_ON : STATUS_OFF,
  977. 'settings' => '',
  978. );
  979. pdo_insert('uni_account_modules', $data);
  980. } else {
  981. $data = array(
  982. 'shortcut' => $status ? STATUS_ON : STATUS_OFF,
  983. );
  984. pdo_update('uni_account_modules', $data, array('id' => $module_status['id']));
  985. }
  986. cache_build_module_info($modulename);
  987. return true;
  988. }
  989. function uni_account_member_fields($uniacid) {
  990. if (empty($uniacid)) {
  991. return array();
  992. }
  993. $account_member_fields = pdo_getall('mc_member_fields', array('uniacid' => $uniacid), array(), 'fieldid');
  994. $system_member_fields = pdo_getall('profile_fields', array(), array(), 'id');
  995. $less_field_indexes = array_diff(array_keys($system_member_fields), array_keys($account_member_fields));
  996. if (empty($less_field_indexes)) {
  997. foreach ($account_member_fields as &$field) {
  998. $field['field'] = $system_member_fields[$field['fieldid']]['field'];
  999. }
  1000. unset($field);
  1001. return $account_member_fields;
  1002. }
  1003. $account_member_add_fields = array('uniacid' => $uniacid);
  1004. foreach ($less_field_indexes as $field_index) {
  1005. $account_member_add_fields['fieldid'] = $system_member_fields[$field_index]['id'];
  1006. $account_member_add_fields['title'] = $system_member_fields[$field_index]['title'];
  1007. $account_member_add_fields['available'] = $system_member_fields[$field_index]['available'];
  1008. $account_member_add_fields['displayorder'] = $system_member_fields[$field_index]['displayorder'];
  1009. pdo_insert('mc_member_fields', $account_member_add_fields);
  1010. $insert_id = pdo_insertid();
  1011. $account_member_fields[$insert_id]['id'] = $insert_id;
  1012. $account_member_fields[$insert_id]['field'] = $system_member_fields[$field_index]['field'];
  1013. $account_member_fields[$insert_id]['fid'] = $system_member_fields[$field_index]['id'];
  1014. $account_member_fields[$insert_id] = array_merge($account_member_fields[$insert_id], $account_member_add_fields);
  1015. }
  1016. return $account_member_fields;
  1017. }
  1018. function uni_account_global_oauth() {
  1019. load()->model('setting');
  1020. $oauth = setting_load('global_oauth');
  1021. $oauth = !empty($oauth['global_oauth']) ? $oauth['global_oauth'] : array();
  1022. return $oauth;
  1023. }
  1024. function uni_search_link_account($module_name, $account_type) {
  1025. global $_W;
  1026. load()->model('miniapp');
  1027. $module_name = trim($module_name);
  1028. if (empty($module_name) || empty($account_type) || !in_array($account_type, array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH, ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH, ACCOUNT_TYPE_WEBAPP_NORMAL))) {
  1029. return array();
  1030. }
  1031. if (in_array($account_type, array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH))) {
  1032. $owned_account = uni_user_accounts($_W['uid'], 'app');
  1033. } elseif (in_array($account_type, array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH))) {
  1034. $owned_account = uni_user_accounts($_W['uid'], 'wxapp');
  1035. } elseif ($account_type == ACCOUNT_TYPE_WEBAPP_NORMAL) {
  1036. $owned_account = uni_user_accounts($_W['uid'], 'webapp');
  1037. } else {
  1038. $owned_account = array();
  1039. }
  1040. if (!empty($owned_account)) {
  1041. foreach ($owned_account as $key => $account) {
  1042. if ($account['type'] != $account_type || $account['uniacid'] == $_W['uniacid']) {
  1043. unset($owned_account[$key]);
  1044. continue;
  1045. }
  1046. $account['role'] = permission_account_user_role($_W['uid'], $account['uniacid']);
  1047. if (!in_array($account['role'], array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER, ACCOUNT_MANAGE_NAME_FOUNDER))) {
  1048. unset($owned_account[$key]);
  1049. }
  1050. }
  1051. foreach ($owned_account as $key => $account) {
  1052. $account_modules = uni_modules_by_uniacid($account['uniacid']);
  1053. if (empty($account_modules[$module_name])) {
  1054. unset($owned_account[$key]);
  1055. continue;
  1056. }
  1057. if (in_array($account_type, array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH))) {
  1058. $last_version = (array)miniapp_fetch($account['uniacid']);
  1059. if (empty($last_version['version']) || empty($last_version['version']['modules']) || current((array)array_keys($last_version['version']['modules'])) != $module_name) {
  1060. unset($owned_account[$key]);
  1061. continue;
  1062. }
  1063. $current_module = current($last_version['version']['modules']);
  1064. if (!empty($current_module['account'])) {
  1065. unset($owned_account[$key]);
  1066. continue;
  1067. }
  1068. }
  1069. if (in_array($account_type, array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH)) && $account_modules[$module_name][MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT) {
  1070. unset($owned_account[$key]);
  1071. } elseif (in_array($account_type, array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH)) && $account_modules[$module_name]['wxapp_support'] != MODULE_SUPPORT_WXAPP) {
  1072. unset($owned_account[$key]);
  1073. } elseif ($account_type == ACCOUNT_TYPE_WEBAPP_NORMAL && $account_modules[$module_name]['webapp_support'] != MODULE_SUPPORT_WEBAPP) {
  1074. unset($owned_account[$key]);
  1075. }
  1076. }
  1077. }
  1078. return $owned_account;
  1079. }
  1080. function uni_account_oauth_host() {
  1081. global $_W;
  1082. $oauth_url = $_W['siteroot'];
  1083. $unisetting = uni_setting_load();
  1084. if (!empty($unisetting['bind_domain']) && !empty($unisetting['bind_domain']['domain'])) {
  1085. $oauth_url = $unisetting['bind_domain']['domain'] . '/';
  1086. } else {
  1087. if (!empty($unisetting['oauth']['host'])) {
  1088. $oauth_url = $unisetting['oauth']['host'] . '/';
  1089. } else {
  1090. $global_unisetting = uni_account_global_oauth();
  1091. $oauth_url = !empty($global_unisetting['oauth']['host']) ? $global_unisetting['oauth']['host'] . '/' : $oauth_url;
  1092. }
  1093. }
  1094. return $oauth_url;
  1095. }
  1096. function uni_passive_link_uniacid($uniacid, $module_name) {
  1097. global $_W;
  1098. $uniacid = intval($uniacid);
  1099. $module_name = trim($module_name);
  1100. if (empty($uniacid) || empty($module_name)) {
  1101. return false;
  1102. }
  1103. $passive_link_module = pdo_get('uni_account_modules', array('module' => $module_name, 'uniacid' => $uniacid), array('id', 'settings'));
  1104. if (!empty($passive_link_module)) {
  1105. $passive_settings = (array)iunserializer($passive_link_module['settings']);
  1106. if (!is_array($passive_settings['passive_link_uniacid']) && !empty($passive_settings['passive_link_uniacid'])) {
  1107. $passive_settings = array($passive_settings['passive_link_uniacid']);
  1108. }
  1109. if (empty($passive_settings)) {
  1110. $passive_settings = array('passive_link_uniacid' => array($_W['uniacid']));
  1111. } elseif (empty($passive_settings['passive_link_uniacid'])) {
  1112. $passive_settings['passive_link_uniacid'] = array($_W['uniacid']);
  1113. } elseif (!empty($passive_settings['passive_link_uniacid']) && !in_array($_W['uniacid'], $passive_settings['passive_link_uniacid'])) {
  1114. array_push($passive_settings['passive_link_uniacid'], array($_W['uniacid']));
  1115. }
  1116. pdo_update('uni_account_modules', array('settings' => iserializer($passive_settings)), array('id' => $passive_link_module['id']));
  1117. } else {
  1118. $passive_settings = array('passive_link_uniacid' => array($_W['uniacid']));
  1119. $passive_data = array(
  1120. 'settings' => iserializer($passive_settings),
  1121. 'uniacid' => $uniacid,
  1122. 'module' => $module_name,
  1123. 'enabled' => STATUS_ON,
  1124. );
  1125. pdo_insert('uni_account_modules', $passive_data);
  1126. }
  1127. cache_clean(cache_system_key('module_setting'));
  1128. return true;
  1129. }
  1130. function uni_unpassive_link_uniacid($uniacid, $module_name) {
  1131. global $_W;
  1132. if (empty($uniacid) || empty($module_name)) {
  1133. return false;
  1134. }
  1135. $passive_info = table('uni_account_modules')->getByUniacidAndModule($module_name, $uniacid);
  1136. if (!empty($passive_info['settings']) && is_array($passive_info['settings']['passive_link_uniacid'])) {
  1137. foreach ($passive_info['settings']['passive_link_uniacid'] as $key => $value) {
  1138. if ($_W['uniacid'] == $value) {
  1139. unset($passive_info['settings']['passive_link_uniacid'][$key]);
  1140. break;
  1141. }
  1142. }
  1143. table('uni_account_modules')->fill(array('settings' => iserializer($passive_info['settings'])))->where('module', $module_name)->where('uniacid', $uniacid)->save();
  1144. cache_delete(cache_system_key('module_setting', array('module_name' => $module_name, 'uniacid' => $uniacid)));
  1145. }
  1146. }