account.mod.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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'))) {
  26. $type = 'app';
  27. }
  28. $type = $type == 'app' ? 'wechats' : $type;
  29. $cachekey = cache_system_key("user_{$type}_accounts:{$uid}");
  30. $cache = cache_load($cachekey);
  31. if (!empty($cache)) {
  32. return $cache;
  33. }
  34. $field = '';
  35. $where = '';
  36. $params = array();
  37. $user_is_founder = user_is_founder($uid);
  38. if (empty($user_is_founder) || user_is_vice_founder($uid)) {
  39. $field .= ', u.role';
  40. $where .= " LEFT JOIN " . tablename('uni_account_users') . " u ON u.uniacid = w.uniacid WHERE u.uid = :uid AND u.role IN(:role1, :role2) ";
  41. $params[':uid'] = $uid;
  42. $params[':role1'] = ACCOUNT_MANAGE_NAME_OWNER;
  43. $params[':role2'] = ACCOUNT_MANAGE_NAME_VICE_FOUNDER;
  44. }
  45. $where .= !empty($where) ? " AND a.isdeleted <> 1 AND u.role IS NOT NULL" : " WHERE a.isdeleted <> 1";
  46. $sql = "SELECT w.*, a.type" . $field . " FROM " . tablename('account_' . $type) . " w LEFT JOIN " . tablename('account') . " a ON a.acid = w.acid AND a.uniacid = w.uniacid" . $where;
  47. $result = pdo_fetchall($sql, $params, 'uniacid');
  48. cache_write($cachekey, $result);
  49. return $result;
  50. }
  51. function account_owner($uniacid = 0) {
  52. global $_W;
  53. load()->model('user');
  54. $uniacid = intval($uniacid);
  55. if (empty($uniacid)) {
  56. return array();
  57. }
  58. $ownerid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), 'uid');
  59. if (empty($ownerid)) {
  60. $ownerid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'vice_founder'), 'uid');
  61. if (empty($ownerid)) {
  62. $founders = explode(',', $_W['config']['setting']['founder']);
  63. $ownerid = $founders[0];
  64. }
  65. }
  66. $owner = user_single($ownerid);
  67. if (empty($owner)) {
  68. return array();
  69. }
  70. return $owner;
  71. }
  72. function uni_accounts($uniacid = 0) {
  73. global $_W;
  74. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  75. $account_info = pdo_get('account', array('uniacid' => $uniacid));
  76. if (!empty($account_info)) {
  77. $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');
  78. }
  79. return !empty($accounts) ? $accounts : array();
  80. }
  81. function uni_fetch($uniacid = 0) {
  82. global $_W;
  83. load()->model('mc');
  84. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  85. $cachekey = "uniaccount:{$uniacid}";
  86. $cache = cache_load($cachekey);
  87. if (!empty($cache)) {
  88. return $cache;
  89. }
  90. $acid = table('account')->getAccountByUniacid($uniacid);
  91. if (empty($acid)) {
  92. return false;
  93. }
  94. $account_api = WeAccount::create($acid['acid']);
  95. if (is_error($account_api)) {
  96. return $account_api;
  97. }
  98. $account = $account_api->account;
  99. if (empty($account) || $account['isdeleted'] == 1) {
  100. return array();
  101. }
  102. $owner = account_owner($uniacid);
  103. $account['uid'] = $owner['uid'];
  104. $account['starttime'] = $owner['starttime'];
  105. if (!empty($account['endtime'])) {
  106. $account['endtime'] = $account['endtime'] == '-1' ? 0 : $account['endtime'];
  107. } else {
  108. $account['endtime'] = $owner['endtime'];
  109. }
  110. $account['groups'] = mc_groups($uniacid);
  111. $account['setting'] = uni_setting($uniacid);
  112. $account['grouplevel'] = $account['setting']['grouplevel'];
  113. $account['logo'] = tomedia('headimg_'.$account['acid']. '.jpg').'?time='.time();
  114. $account['qrcode'] = tomedia('qrcode_'.$account['acid']. '.jpg').'?time='.time();
  115. $account['switchurl'] = wurl('account/display/switch', array('uniacid' => $account['uniacid']));
  116. if (!empty($account['settings']['notify'])) {
  117. $account['sms'] = $account['setting']['notify']['sms']['balance'];
  118. } else {
  119. $account['sms'] = 0;
  120. }
  121. $account['setmeal'] = uni_setmeal($account['uniacid']);
  122. cache_write($cachekey, $account);
  123. return $account;
  124. }
  125. function uni_site_store_buy_goods($uniacid, $type = STORE_TYPE_MODULE) {
  126. $cachekey = cache_system_key($uniacid . ':site_store_buy_' . $type);
  127. $site_store_buy_goods = cache_load($cachekey);
  128. if (!empty($site_store_buy_goods)) {
  129. return $site_store_buy_goods;
  130. }
  131. $store_table = table('store');
  132. if ($type != STORE_TYPE_API) {
  133. $store_table->searchWithEndtime();
  134. $site_store_buy_goods = $store_table->searchAccountBuyGoods($uniacid, $type);
  135. $site_store_buy_goods = array_keys($site_store_buy_goods);
  136. } else {
  137. $site_store_buy_goods = $store_table->searchAccountBuyGoods($uniacid, $type);
  138. $setting = uni_setting_load('statistics', $uniacid);
  139. $use_number = isset($setting['statistics']['use']) ? intval($setting['statistics']['use']) : 0;
  140. $site_store_buy_goods = $site_store_buy_goods - $use_number;
  141. }
  142. cache_write($cachekey, $site_store_buy_goods);
  143. return $site_store_buy_goods;
  144. }
  145. function uni_modules_by_uniacid($uniacid, $enabled = true) {
  146. global $_W;
  147. load()->model('user');
  148. load()->model('module');
  149. $cachekey = cache_system_key(CACHE_KEY_ACCOUNT_MODULES, $uniacid, $enabled);
  150. $modules = cache_load($cachekey);
  151. if (empty($modules)) {
  152. $founders = explode(',', $_W['config']['setting']['founder']);
  153. $owner_uid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), 'uid');
  154. $condition = "WHERE 1";
  155. $site_store_buy_goods = array();
  156. $account_info = uni_fetch($_W['uniacid']);
  157. $goods_type = $account_info['type'] == ACCOUNT_TYPE_APP_NORMAL ? STORE_TYPE_WXAPP_MODULE : STORE_TYPE_MODULE;
  158. $site_store_buy_goods = uni_site_store_buy_goods($uniacid, $goods_type);
  159. if (!empty($owner_uid) && !in_array($owner_uid, $founders)) {
  160. $uni_modules = array();
  161. $packageids = pdo_getall('uni_account_group', array('uniacid' => $uniacid), array('groupid'), 'groupid');
  162. $packageids = array_keys($packageids);
  163. if (IMS_FAMILY == 'x') {
  164. $store = table('store');
  165. $site_store_buy_package = $store->searchUserBuyPackage($uniacid);
  166. $packageids = array_merge($packageids, array_keys($site_store_buy_package));
  167. }
  168. if (!in_array('-1', $packageids)) {
  169. $uni_groups = pdo_fetchall("SELECT `modules` FROM " . tablename('uni_group') . " WHERE " . "id IN ('".implode("','", $packageids)."') OR " . " uniacid = '{$uniacid}'");
  170. if (!empty($uni_groups)) {
  171. foreach ($uni_groups as $group) {
  172. $group_module = (array)iunserializer($group['modules']);
  173. $uni_modules = array_merge($group_module, $uni_modules);
  174. }
  175. }
  176. $user_modules = user_modules($owner_uid);
  177. $modules = array_merge(array_keys($user_modules), $uni_modules, $site_store_buy_goods);
  178. if (!empty($modules)) {
  179. $condition .= " AND a.name IN ('" . implode("','", $modules) . "')";
  180. } else {
  181. $condition .= " AND a.name = ''";
  182. }
  183. }
  184. }
  185. $condition .= $enabled ? " AND (b.enabled = 1 OR b.enabled is NULL) OR a.issystem = 1" : " OR a.issystem = 1";
  186. $sql = "SELECT a.name FROM " . tablename('modules') . " AS a LEFT JOIN " . tablename('uni_account_modules') . " AS b ON a.name = b.module AND b.uniacid = :uniacid " . $condition . " ORDER BY b.displayorder DESC, b.id DESC";
  187. $modules = pdo_fetchall($sql, array(':uniacid' => $uniacid), 'name');
  188. cache_write($cachekey, $modules);
  189. }
  190. $module_list = array();
  191. if (!empty($modules)) {
  192. foreach ($modules as $name => $module) {
  193. $module_info = module_fetch($name);
  194. if (!empty($module_info)) {
  195. $module_list[$name] = $module_info;
  196. }
  197. }
  198. }
  199. $module_list['core'] = array('title' => '系统事件处理模块', 'name' => 'core', 'issystem' => 1, 'enabled' => 1, 'isdisplay' => 0);
  200. return $module_list;
  201. }
  202. function uni_modules($enabled = true) {
  203. global $_W;
  204. return uni_modules_by_uniacid($_W['uniacid'], $enabled);
  205. }
  206. function uni_modules_app_binding() {
  207. global $_W;
  208. $cachekey = cache_system_key(CACHE_KEY_ACCOUNT_MODULES_BINDING, $_W['uniacid']);
  209. $cache = cache_load($cachekey);
  210. if (!empty($cache)) {
  211. return $cache;
  212. }
  213. load()->model('module');
  214. $result = array();
  215. $modules = uni_modules();
  216. if(!empty($modules)) {
  217. foreach($modules as $module) {
  218. if($module['type'] == 'system') {
  219. continue;
  220. }
  221. $entries = module_app_entries($module['name'], array('home', 'profile', 'shortcut', 'function', 'cover'));
  222. if(empty($entries)) {
  223. continue;
  224. }
  225. if($module['type'] == '') {
  226. $module['type'] = 'other';
  227. }
  228. $result[$module['name']] = array(
  229. 'name' => $module['name'],
  230. 'type' => $module['type'],
  231. 'title' => $module['title'],
  232. 'entries' => array(
  233. 'cover' => $entries['cover'],
  234. 'home' => $entries['home'],
  235. 'profile' => $entries['profile'],
  236. 'shortcut' => $entries['shortcut'],
  237. 'function' => $entries['function']
  238. )
  239. );
  240. unset($module);
  241. }
  242. }
  243. cache_write($cachekey, $result);
  244. return $result;
  245. }
  246. function uni_groups($groupids = array(), $show_all = false) {
  247. load()->model('module');
  248. global $_W;
  249. $cachekey = cache_system_key(CACHE_KEY_UNI_GROUP);
  250. $list = cache_load($cachekey);
  251. if (empty($list)) {
  252. $condition = ' WHERE uniacid = 0';
  253. $list = pdo_fetchall("SELECT * FROM " . tablename('uni_group') . $condition . " ORDER BY id DESC", array(), 'id');
  254. if (!empty($groupids)) {
  255. if (in_array('-1', $groupids)) {
  256. $list[-1] = array('id' => -1, 'name' => '所有服务', 'modules' => array('title' => '系统所有模块'), 'templates' => array('title' => '系统所有模板'));
  257. }
  258. if (in_array('0', $groupids)) {
  259. $list[0] = array('id' => 0, 'name' => '基础服务', 'modules' => array('title' => '系统模块'), 'templates' => array('title' => '系统模板'));
  260. }
  261. }
  262. if (!empty($list)) {
  263. foreach ($list as $k=>&$row) {
  264. $row['wxapp'] = array();
  265. if (!empty($row['modules'])) {
  266. $modules = iunserializer($row['modules']);
  267. if (is_array($modules)) {
  268. $module_list = pdo_getall('modules', array('name' => $modules), array(), 'name');
  269. $row['modules'] = array();
  270. if (!empty($module_list)) {
  271. foreach ($module_list as $key => &$module) {
  272. $module = module_fetch($key);
  273. if ($module['wxapp_support'] == MODULE_SUPPORT_WXAPP) {
  274. $row['wxapp'][$module['name']] = $module;
  275. }
  276. if ($module['webapp_support'] == MODULE_SUPPORT_WEBAPP) {
  277. $row['webapp'][$module['name']] = $module;
  278. }
  279. if ($module['phoneapp_support'] == MODULE_SUPPORT_PHONEAPP) {
  280. $row['phoneapp'][$module['name']] = $module;
  281. }
  282. if ($module['app_support'] == MODULE_SUPPORT_ACCOUNT) {
  283. if (!empty($module['main_module'])) {
  284. continue;
  285. }
  286. $row['modules'][$module['name']] = $module;
  287. if (!empty($module['plugin'])) {
  288. $group_have_plugin = array_intersect($module['plugin_list'], array_keys($module_list));
  289. if (!empty($group_have_plugin)) {
  290. foreach ($group_have_plugin as $plugin) {
  291. $row['modules'][$plugin] = module_fetch($plugin);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. if (!empty($row['templates'])) {
  301. $templates = iunserializer($row['templates']);
  302. if (is_array($templates)) {
  303. $row['templates'] = pdo_getall('site_templates', array('id' => $templates), array('id', 'name', 'title'), 'name');
  304. }
  305. }
  306. }
  307. }
  308. cache_write($cachekey, $list);
  309. }
  310. $group_list = array();
  311. if (!empty($groupids)) {
  312. foreach ($groupids as $id) {
  313. $group_list[$id] = $list[$id];
  314. }
  315. } else {
  316. if (user_is_vice_founder() && empty($show_all)) {
  317. foreach ($list as $group_key => $group) {
  318. if ($group['owner_uid'] != $_W['uid']) {
  319. unset($list[$group_key]);
  320. continue;
  321. }
  322. }
  323. }
  324. $group_list = $list;
  325. }
  326. return $group_list;
  327. }
  328. function uni_templates() {
  329. global $_W;
  330. $owneruid = pdo_fetchcolumn("SELECT uid FROM ".tablename('uni_account_users')." WHERE uniacid = :uniacid AND role = 'owner'", array(':uniacid' => $_W['uniacid']));
  331. load()->model('user');
  332. $owner = user_single(array('uid' => $owneruid));
  333. if (empty($owner) || user_is_founder($owner['uid'])) {
  334. $groupid = '-1';
  335. } else {
  336. $groupid = $owner['groupid'];
  337. }
  338. $extend = pdo_getall('uni_account_group', array('uniacid' => $_W['uniacid']), array(), 'groupid');
  339. if (!empty($extend) && $groupid != '-1') {
  340. $groupid = '-2';
  341. }
  342. if (empty($groupid)) {
  343. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " WHERE name = 'default'", array(), 'id');
  344. } elseif ($groupid == '-1') {
  345. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " ORDER BY id ASC", array(), 'id');
  346. } else {
  347. $group = pdo_fetch("SELECT id, name, package FROM ".tablename('users_group')." WHERE id = :id", array(':id' => $groupid));
  348. $packageids = iunserializer($group['package']);
  349. if (!empty($extend)) {
  350. foreach ($extend as $extend_packageid => $row) {
  351. $packageids[] = $extend_packageid;
  352. }
  353. }
  354. if(is_array($packageids)) {
  355. if (in_array('-1', $packageids)) {
  356. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " ORDER BY id ASC", array(), 'id');
  357. } else {
  358. $wechatgroup = pdo_fetchall("SELECT `templates` FROM " . tablename('uni_group') . " WHERE id IN ('".implode("','", $packageids)."') OR uniacid = '{$_W['uniacid']}'");
  359. $ms = array();
  360. $mssql = '';
  361. if (!empty($wechatgroup)) {
  362. foreach ($wechatgroup as $row) {
  363. $row['templates'] = iunserializer($row['templates']);
  364. if (!empty($row['templates'])) {
  365. foreach ($row['templates'] as $templateid) {
  366. $ms[$templateid] = $templateid;
  367. }
  368. }
  369. }
  370. $ms[] = 1;
  371. $mssql = " `id` IN ('".implode("','", $ms)."')";
  372. }
  373. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') .(!empty($mssql) ? " WHERE $mssql" : '')." ORDER BY id DESC", array(), 'id');
  374. }
  375. }
  376. }
  377. if (empty($templates)) {
  378. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " WHERE id = 1 ORDER BY id DESC", array(), 'id');
  379. }
  380. return $templates;
  381. }
  382. function uni_setting_save($name, $value) {
  383. global $_W;
  384. if (empty($name)) {
  385. return false;
  386. }
  387. if (is_array($value)) {
  388. $value = serialize($value);
  389. }
  390. $unisetting = pdo_get('uni_settings', array('uniacid' => $_W['uniacid']), array('uniacid'));
  391. if (!empty($unisetting)) {
  392. pdo_update('uni_settings', array($name => $value), array('uniacid' => $_W['uniacid']));
  393. } else {
  394. pdo_insert('uni_settings', array($name => $value, 'uniacid' => $_W['uniacid']));
  395. }
  396. $cachekey = "unisetting:{$_W['uniacid']}";
  397. $account_cachekey = "uniaccount:{$_W['uniacid']}";
  398. cache_delete($cachekey);
  399. cache_delete($account_cachekey);
  400. return true;
  401. }
  402. function uni_setting_load($name = '', $uniacid = 0) {
  403. global $_W;
  404. $uniacid = empty($uniacid) ? $_W['uniacid'] : $uniacid;
  405. $cachekey = "unisetting:{$uniacid}";
  406. $unisetting = cache_load($cachekey);
  407. if (empty($unisetting)) {
  408. $unisetting = pdo_get('uni_settings', array('uniacid' => $uniacid));
  409. if (!empty($unisetting)) {
  410. $serialize = array('site_info', 'stat', 'oauth', 'passport', 'uc', 'notify',
  411. 'creditnames', 'default_message', 'creditbehaviors', 'payment',
  412. 'recharge', 'tplnotice', 'mcplugin', 'statistics', 'bind_domain');
  413. foreach ($unisetting as $key => &$row) {
  414. if (in_array($key, $serialize) && !empty($row)) {
  415. $row = (array)iunserializer($row);
  416. }
  417. }
  418. } else {
  419. $unisetting = array();
  420. }
  421. cache_write($cachekey, $unisetting);
  422. }
  423. if (empty($unisetting)) {
  424. return array();
  425. }
  426. if (empty($name)) {
  427. return $unisetting;
  428. }
  429. if (!is_array($name)) {
  430. $name = array($name);
  431. }
  432. return array_elements($name, $unisetting);
  433. }
  434. if (!function_exists('uni_setting')) {
  435. function uni_setting($uniacid = 0, $fields = '*', $force_update = false) {
  436. global $_W;
  437. load()->model('account');
  438. if ($fields == '*') {
  439. $fields = '';
  440. }
  441. return uni_setting_load($fields, $uniacid);
  442. }
  443. }
  444. function uni_account_default($uniacid = 0) {
  445. global $_W;
  446. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  447. $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));
  448. if (empty($uni_account)) {
  449. $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));
  450. }
  451. if (!empty($uni_account)) {
  452. $account = pdo_get(uni_account_tablename($uni_account['type']), array('acid' => $uni_account['acid']));
  453. if (empty($account)) {
  454. $account['uniacid'] = $uni_account['uniacid'];
  455. $account['acid'] = $uni_account['default_acid'];
  456. }
  457. $account['type'] = $uni_account['type'];
  458. $account['isconnect'] = $uni_account['isconnect'];
  459. $account['isdeleted'] = $uni_account['isdeleted'];
  460. $account['endtime'] = $uni_account['endtime'];
  461. return $account;
  462. }
  463. }
  464. function uni_account_tablename($type) {
  465. switch ($type) {
  466. case ACCOUNT_TYPE_OFFCIAL_NORMAL:
  467. case ACCOUNT_TYPE_OFFCIAL_AUTH:
  468. return 'account_wechats';
  469. case ACCOUNT_TYPE_APP_NORMAL:
  470. return 'account_wxapp';
  471. case ACCOUNT_TYPE_WEBAPP_NORMAL:
  472. return 'account_webapp';
  473. case ACCOUNT_TYPE_PHONEAPP_NORMAL:
  474. return 'account_phoneapp';
  475. }
  476. }
  477. function uni_user_account_role($uniacid, $uid, $role) {
  478. $vice_account = array(
  479. 'uniacid' => intval($uniacid),
  480. 'uid' => intval($uid),
  481. 'role' => trim($role)
  482. );
  483. $account_user = pdo_get('uni_account_users', $vice_account, array('id'));
  484. if (!empty($account_user)) {
  485. return false;
  486. }
  487. return pdo_insert('uni_account_users', $vice_account);
  488. }
  489. function uni_user_see_more_info($user_type, $see_more = false) {
  490. global $_W;
  491. if (empty($user_type)) {
  492. return false;
  493. }
  494. if ($user_type == ACCOUNT_MANAGE_NAME_VICE_FOUNDER && !empty($see_more) || $_W['role'] != $user_type) {
  495. return true;
  496. }
  497. return false;
  498. }
  499. function uni_owner_account_nums($uid, $role) {
  500. $account_num = $wxapp_num = $webapp_num = 0;
  501. $condition = array('uid' => $uid, 'role' => $role);
  502. $uniacocunts = pdo_getall('uni_account_users', $condition, array(), 'uniacid');
  503. if (!empty($uniacocunts)) {
  504. $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));
  505. foreach ($all_account as $account) {
  506. if ($account['type'] == 1 || $account['type'] == 3) {
  507. $account_num++;
  508. }
  509. if ($account['type'] == 4) {
  510. $wxapp_num++;
  511. }
  512. if ($account['type'] == ACCOUNT_TYPE_WEBAPP_NORMAL) {
  513. $webapp_num++;
  514. }
  515. }
  516. }
  517. $num = array(
  518. 'account_num' => $account_num,
  519. 'wxapp_num' =>$wxapp_num,
  520. 'webapp_num'=>$webapp_num
  521. );
  522. return $num;
  523. }
  524. function uni_update_week_stat() {
  525. global $_W;
  526. $cachekey = "stat:todaylock:{$_W['uniacid']}";
  527. $cache = cache_load($cachekey);
  528. if(!empty($cache) && $cache['expire'] > TIMESTAMP) {
  529. return true;
  530. }
  531. $seven_days = array(
  532. date('Ymd', strtotime('-1 days')),
  533. date('Ymd', strtotime('-2 days')),
  534. date('Ymd', strtotime('-3 days')),
  535. date('Ymd', strtotime('-4 days')),
  536. date('Ymd', strtotime('-5 days')),
  537. date('Ymd', strtotime('-6 days')),
  538. date('Ymd', strtotime('-7 days')),
  539. );
  540. $week_stat_fans = pdo_getall('stat_fans', array('date' => $seven_days, 'uniacid' => $_W['uniacid']), '', 'date');
  541. $stat_update_yes = false;
  542. foreach ($seven_days as $sevens) {
  543. if (empty($week_stat_fans[$sevens]) || $week_stat_fans[$sevens]['cumulate'] <=0) {
  544. $stat_update_yes = true;
  545. break;
  546. }
  547. }
  548. if (empty($stat_update_yes)) {
  549. return true;
  550. }
  551. foreach($seven_days as $sevens) {
  552. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY) {
  553. $account_obj = WeAccount::create();
  554. $weixin_stat = $account_obj->getFansStat();
  555. if(is_error($weixin_stat) || empty($weixin_stat)) {
  556. return error(-1, '调用微信接口错误');
  557. } else {
  558. $update_stat = array();
  559. $update_stat = array(
  560. 'uniacid' => $_W['uniacid'],
  561. 'new' => $weixin_stat[$sevens]['new'],
  562. 'cancel' => $weixin_stat[$sevens]['cancel'],
  563. 'cumulate' => $weixin_stat[$sevens]['cumulate'],
  564. 'date' => $sevens,
  565. );
  566. }
  567. } else {
  568. $update_stat = array();
  569. $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));
  570. $update_stat['date'] = $sevens;
  571. $update_stat['new'] = $week_stat_fans[$sevens]['new'];
  572. $update_stat['cancel'] = $week_stat_fans[$sevens]['cancel'];
  573. $update_stat['uniacid'] = $_W['uniacid'];
  574. }
  575. if(empty($week_stat_fans[$sevens])) {
  576. pdo_insert('stat_fans', $update_stat);
  577. } elseif (empty($week_stat_fans[$sevens]['cumulate']) || $week_stat_fans[$sevens]['cumulate'] < 0) {
  578. pdo_update('stat_fans', $update_stat, array('id' => $week_stat_fans[$sevens]['id']));
  579. }
  580. }
  581. cache_write($cachekey, array('expire' => TIMESTAMP + 7200));
  582. return true;
  583. }
  584. function uni_account_rank_top($uniacid) {
  585. global $_W;
  586. if (!empty($_W['isfounder'])) {
  587. $max_rank = pdo_getcolumn('uni_account', array(), 'max(rank)');
  588. pdo_update('uni_account', array('rank' => ($max_rank + 1)), array('uniacid' => $uniacid));
  589. }else {
  590. $max_rank = pdo_getcolumn('uni_account_users', array('uid' => $_W['uid']), 'max(rank)');
  591. pdo_update('uni_account_users', array('rank' => ($max_rank['maxrank'] + 1)), array('uniacid' => $uniacid, 'uid' => $_W['uid']));
  592. }
  593. return true;
  594. }
  595. function uni_account_last_switch() {
  596. global $_W, $_GPC;
  597. $cache_key = cache_system_key(CACHE_KEY_ACCOUNT_SWITCH, $_GPC['__switch']);
  598. $cache_lastaccount = (array)cache_load($cache_key);
  599. if (strexists($_W['siteurl'], 'c=webapp')) {
  600. $uniacid = $cache_lastaccount['webapp'];
  601. } else if (strexists($_W['siteurl'], 'c=wxapp')) {
  602. $uniacid = $cache_lastaccount['wxapp'];
  603. } else if (strexists($_W['siteurl'], 'c=phoneapp')) {
  604. $uniacid = $cache_lastaccount['phoneapp'];
  605. } else {
  606. $uniacid = $cache_lastaccount['account'];
  607. }
  608. return $uniacid;
  609. }
  610. function uni_account_switch($uniacid, $redirect = '') {
  611. global $_W;
  612. uni_account_save_switch($uniacid);
  613. isetcookie('__uid', $_W['uid'], 7 * 86400);
  614. if (!empty($redirect)) {
  615. header('Location: ' . $redirect);
  616. exit;
  617. }
  618. return true;
  619. }
  620. function uni_account_save_switch($uniacid) {
  621. global $_W, $_GPC;
  622. if (empty($_GPC['__switch'])) {
  623. $_GPC['__switch'] = random(5);
  624. }
  625. $cache_key = cache_system_key(CACHE_KEY_ACCOUNT_SWITCH, $_GPC['__switch']);
  626. $cache_lastaccount = cache_load($cache_key);
  627. if (empty($cache_lastaccount)) {
  628. $cache_lastaccount = array(
  629. 'account' => $uniacid,
  630. );
  631. } else {
  632. $cache_lastaccount['account'] = $uniacid;
  633. }
  634. cache_write($cache_key, $cache_lastaccount);
  635. isetcookie('__uniacid', $uniacid, 7 * 86400);
  636. isetcookie('__switch', $_GPC['__switch'], 7 * 86400);
  637. return true;
  638. }
  639. function account_create($uniacid, $account) {
  640. $accountdata = array('uniacid' => $uniacid, 'type' => $account['type'], 'hash' => random(8));
  641. pdo_insert('account', $accountdata);
  642. $acid = pdo_insertid();
  643. $account['acid'] = $acid;
  644. $account['token'] = random(32);
  645. $account['encodingaeskey'] = random(43);
  646. $account['uniacid'] = $uniacid;
  647. unset($account['type']);
  648. pdo_insert('account_wechats', $account);
  649. return $acid;
  650. }
  651. function account_fetch($acid) {
  652. $account_info = pdo_get('account', array('acid' => $acid));
  653. if (empty($account_info)) {
  654. return error(-1, '公众号不存在');
  655. }
  656. return uni_fetch($account_info['uniacid']);
  657. }
  658. function uni_setmeal($uniacid = 0) {
  659. global $_W;
  660. if(!$uniacid) {
  661. $uniacid = $_W['uniacid'];
  662. }
  663. $owneruid = pdo_fetchcolumn("SELECT uid FROM ".tablename('uni_account_users')." WHERE uniacid = :uniacid AND role = 'owner'", array(':uniacid' => $uniacid));
  664. if(empty($owneruid)) {
  665. $user = array(
  666. 'uid' => -1,
  667. 'username' => '创始人',
  668. 'timelimit' => '未设置',
  669. 'groupid' => '-1',
  670. 'groupname' => '所有服务'
  671. );
  672. return $user;
  673. }
  674. load()->model('user');
  675. $groups = pdo_getall('users_group', array(), array('id', 'name'), 'id');
  676. $owner = user_single(array('uid' => $owneruid));
  677. $user = array(
  678. 'uid' => $owner['uid'],
  679. 'username' => $owner['username'],
  680. 'groupid' => $owner['groupid'],
  681. 'groupname' => $groups[$owner['groupid']]['name']
  682. );
  683. if(empty($owner['endtime'])) {
  684. $user['timelimit'] = date('Y-m-d', $owner['starttime']) . ' ~ 无限制' ;
  685. } else {
  686. if($owner['endtime'] <= TIMESTAMP) {
  687. $user['timelimit'] = '已到期';
  688. } else {
  689. $year = 0;
  690. $month = 0;
  691. $day = 0;
  692. $endtime = $owner['endtime'];
  693. $time = strtotime('+1 year');
  694. while ($endtime > $time)
  695. {
  696. $year = $year + 1;
  697. $time = strtotime("+1 year", $time);
  698. };
  699. $time = strtotime("-1 year", $time);
  700. $time = strtotime("+1 month", $time);
  701. while($endtime > $time)
  702. {
  703. $month = $month + 1;
  704. $time = strtotime("+1 month", $time);
  705. } ;
  706. $time = strtotime("-1 month", $time);
  707. $time = strtotime("+1 day", $time);
  708. while($endtime > $time)
  709. {
  710. $day = $day + 1;
  711. $time = strtotime("+1 day", $time);
  712. } ;
  713. if (empty($year)) {
  714. $timelimit = empty($month)? $day.'天' : date('Y-m-d', $owner['starttime']) . '~'. date('Y-m-d', $owner['endtime']);
  715. }else {
  716. $timelimit = date('Y-m-d', $owner['starttime']) . '~'. date('Y-m-d', $owner['endtime']);
  717. }
  718. $user['timelimit'] = $timelimit;
  719. }
  720. }
  721. return $user;
  722. }
  723. function uni_is_multi_acid($uniacid = 0) {
  724. global $_W;
  725. if(!$uniacid) {
  726. $uniacid = $_W['uniacid'];
  727. }
  728. $cachekey = "unicount:{$uniacid}";
  729. $nums = cache_load($cachekey);
  730. $nums = intval($nums);
  731. if(!$nums) {
  732. $nums = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('account_wechats') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
  733. cache_write($cachekey, $nums);
  734. }
  735. if($nums == 1) {
  736. return false;
  737. }
  738. return true;
  739. }
  740. function account_delete($acid) {
  741. global $_W;
  742. load()->func('file');
  743. load()->model('module');
  744. $account = pdo_get('uni_account', array('default_acid' => $acid));
  745. if ($account) {
  746. $uniacid = $account['uniacid'];
  747. $state = permission_account_user_role($_W['uid'], $uniacid);
  748. if (!in_array($state, array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_FOUNDER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER))) {
  749. itoast('没有该公众号操作权限!', url('account/recycle'), 'error');
  750. }
  751. if($uniacid == $_W['uniacid']) {
  752. isetcookie('__uniacid', '');
  753. }
  754. cache_delete("uniaccount:{$uniacid}");
  755. $modules = array();
  756. $rules = pdo_fetchall("SELECT id, module FROM ".tablename('rule')." WHERE uniacid = '{$uniacid}'");
  757. if (!empty($rules)) {
  758. foreach ($rules as $index => $rule) {
  759. $deleteid[] = $rule['id'];
  760. }
  761. pdo_delete('rule', "id IN ('".implode("','", $deleteid)."')");
  762. }
  763. $subaccount = pdo_fetchall("SELECT acid FROM ".tablename('account')." WHERE uniacid = :uniacid", array(':uniacid' => $uniacid));
  764. if (!empty($subaccount)) {
  765. foreach ($subaccount as $account) {
  766. @unlink(IA_ROOT . '/attachment/qrcode_'.$account['acid'].'.jpg');
  767. @unlink(IA_ROOT . '/attachment/headimg_'.$account['acid'].'.jpg');
  768. file_remote_delete('qrcode_'.$account['acid'].'.jpg');
  769. file_remote_delete('headimg_'.$account['acid'].'.jpg');
  770. }
  771. if (!empty($acid)) {
  772. rmdirs(IA_ROOT . '/attachment/images/' . $uniacid);
  773. @rmdir(IA_ROOT . '/attachment/images/' . $uniacid);
  774. rmdirs(IA_ROOT . '/attachment/audios/' . $uniacid);
  775. @rmdir(IA_ROOT . '/attachment/audios/' . $uniacid);
  776. }
  777. }
  778. $tables = array(
  779. 'account','account_wechats', 'account_wxapp', 'wxapp_versions', 'account_webapp', 'account_phoneapp', 'phoneapp_versions', 'core_attachment','core_paylog','core_queue','core_resource',
  780. 'wechat_attachment', 'cover_reply', 'mc_chats_record','mc_credits_recharge','mc_credits_record',
  781. 'mc_fans_groups','mc_groups','mc_handsel','mc_mapping_fans','mc_mapping_ucenter','mc_mass_record',
  782. 'mc_member_address','mc_member_fields','mc_members','menu_event',
  783. 'qrcode','qrcode_stat', 'rule','rule_keyword','site_article','site_category','site_multi','site_nav','site_slide',
  784. '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',
  785. 'mc_member_fields',
  786. );
  787. if (!empty($tables)) {
  788. foreach ($tables as $table) {
  789. $tablename = str_replace($GLOBALS['_W']['config']['db']['tablepre'], '', $table);
  790. pdo_delete($tablename, array( 'uniacid'=> $uniacid));
  791. }
  792. }
  793. } else {
  794. $account = account_fetch($acid);
  795. if (empty($account)) {
  796. itoast('子公众号不存在或是已经被删除', '', '');
  797. }
  798. $uniacid = $account['uniacid'];
  799. $state = permission_account_user_role($_W['uid'], $uniacid);
  800. if($state != ACCOUNT_MANAGE_NAME_FOUNDER && $state != ACCOUNT_MANAGE_NAME_OWNER) {
  801. itoast('没有该公众号操作权限!', url('account/recycle'), 'error');
  802. }
  803. $uniaccount = uni_fetch($account['uniacid']);
  804. if ($uniaccount['default_acid'] == $acid) {
  805. itoast('默认子公众号不能删除', '', '');
  806. }
  807. pdo_delete('account', array('acid' => $acid));
  808. pdo_delete('account_wechats', array('acid' => $acid, 'uniacid' => $uniacid));
  809. cache_delete("uniaccount:{$uniacid}");
  810. cache_delete("unisetting:{$uniacid}");
  811. cache_delete('account:auth:refreshtoken:'.$acid);
  812. $oauth = uni_setting($uniacid, array('oauth'));
  813. if($oauth['oauth']['account'] == $acid) {
  814. $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account_wechats') . " WHERE uniacid = :id AND level = 4 AND secret != '' AND `key` != ''", array(':id' => $uniacid));
  815. pdo_update('uni_settings', array('oauth' => iserializer(array('account' => $acid, 'host' => $oauth['oauth']['host']))), array('uniacid' => $uniacid));
  816. }
  817. @unlink(IA_ROOT . '/attachment/qrcode_'.$acid.'.jpg');
  818. @unlink(IA_ROOT . '/attachment/headimg_'.$acid.'.jpg');
  819. file_remote_delete('qrcode_'.$acid.'.jpg');
  820. file_remote_delete('headimg_'.$acid.'.jpg');
  821. }
  822. return true;
  823. }
  824. function account_wechatpay_proxy () {
  825. global $_W;
  826. $proxy_account = cache_load(cache_system_key('proxy_wechatpay_account:'));
  827. if (empty($proxy_account)) {
  828. $proxy_account = cache_build_proxy_wechatpay_account();
  829. }
  830. unset($proxy_account['borrow'][$_W['uniacid']]);
  831. unset($proxy_account['service'][$_W['uniacid']]);
  832. return $proxy_account;
  833. }
  834. function uni_account_module_shortcut_enabled($modulename, $uniacid = 0, $status = STATUS_ON) {
  835. global $_W;
  836. $module = module_fetch($modulename);
  837. if(empty($module)) {
  838. return error(1, '抱歉,你操作的模块不能被访问!');
  839. }
  840. $uniacid = intval($uniacid);
  841. $uniacid = !empty($uniacid) ? $uniacid : $_W['uniacid'];
  842. $module_status = pdo_get('uni_account_modules', array('module' => $modulename, 'uniacid' => $uniacid), array('id', 'shortcut'));
  843. if (empty($module_status)) {
  844. $data = array(
  845. 'uniacid' => $uniacid,
  846. 'module' => $modulename,
  847. 'enabled' => STATUS_ON,
  848. 'shortcut' => $status ? STATUS_ON : STATUS_OFF,
  849. 'settings' => '',
  850. );
  851. pdo_insert('uni_account_modules', $data);
  852. } else {
  853. $data = array(
  854. 'shortcut' => $status ? STATUS_ON : STATUS_OFF,
  855. );
  856. pdo_update('uni_account_modules', $data, array('id' => $module_status['id']));
  857. cache_build_module_info($modulename);
  858. }
  859. return true;
  860. }
  861. function uni_account_member_fields($uniacid) {
  862. if (empty($uniacid)) {
  863. return array();
  864. }
  865. $account_member_fields = pdo_getall('mc_member_fields', array('uniacid' => $uniacid), array(), 'fieldid');
  866. $system_member_fields = pdo_getall('profile_fields', array(), array(), 'id');
  867. $less_field_indexes = array_diff(array_keys($system_member_fields), array_keys($account_member_fields));
  868. if (empty($less_field_indexes)) {
  869. foreach ($account_member_fields as &$field) {
  870. $field['field'] = $system_member_fields[$field['fieldid']]['field'];
  871. }
  872. unset($field);
  873. return $account_member_fields;
  874. }
  875. $account_member_add_fields = array('uniacid' => $uniacid);
  876. foreach ($less_field_indexes as $field_index) {
  877. $account_member_add_fields['fieldid'] = $system_member_fields[$field_index]['id'];
  878. $account_member_add_fields['title'] = $system_member_fields[$field_index]['title'];
  879. $account_member_add_fields['available'] = $system_member_fields[$field_index]['available'];
  880. $account_member_add_fields['displayorder'] = $system_member_fields[$field_index]['displayorder'];
  881. pdo_insert('mc_member_fields', $account_member_add_fields);
  882. $insert_id = pdo_insertid();
  883. $account_member_fields[$insert_id]['id'] = $insert_id;
  884. $account_member_fields[$insert_id]['field'] = $system_member_fields[$field_index]['field'];
  885. $account_member_fields[$insert_id]['fid'] = $system_member_fields[$field_index]['id'];
  886. $account_member_fields[$insert_id] = array_merge($account_member_fields[$insert_id], $account_member_add_fields);
  887. }
  888. return $account_member_fields;
  889. }
  890. function uni_account_global_oauth() {
  891. load()->model('setting');
  892. $oauth = setting_load('global_oauth');
  893. $oauth = !empty($oauth['global_oauth']) ? $oauth['global_oauth'] : array();
  894. return $oauth;
  895. }
  896. function uni_search_link_account($module_name, $account_type) {
  897. global $_W;
  898. $module_name = trim($module_name);
  899. 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_WEBAPP_NORMAL))) {
  900. return array();
  901. }
  902. if (in_array($account_type, array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH))) {
  903. $owned_account = uni_user_accounts($_W['uid'], 'app');
  904. } elseif ($account_type == ACCOUNT_TYPE_APP_NORMAL) {
  905. $owned_account = uni_user_accounts($_W['uid'], 'wxapp');
  906. } elseif ($account_type == ACCOUNT_TYPE_WEBAPP_NORMAL) {
  907. $owned_account = uni_user_accounts($_W['uid'], 'webapp');
  908. } else {
  909. $owned_account = array();
  910. }
  911. if (!empty($owned_account)) {
  912. foreach ($owned_account as $key => $account) {
  913. if ($account['type'] != $account_type) {
  914. unset($owned_account[$key]);
  915. continue;
  916. }
  917. $account['role'] = permission_account_user_role($_W['uid'], $account['uniacid']);
  918. if (!in_array($account['role'], array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_FOUNDER))) {
  919. unset($owned_account[$key]);
  920. }
  921. }
  922. foreach ($owned_account as $key => $account) {
  923. $account_modules = uni_modules_by_uniacid($account['uniacid']);
  924. if (empty($account_modules[$module_name])) {
  925. unset($owned_account[$key]);
  926. continue;
  927. }
  928. if (in_array($account_type, array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH)) && $account_modules[$module_name]['app_support'] != MODULE_SUPPORT_ACCOUNT) {
  929. unset($owned_account[$key]);
  930. } elseif ($account_type == ACCOUNT_TYPE_APP_NORMAL && $account_modules[$module_name]['wxapp_support'] != MODULE_SUPPORT_WXAPP) {
  931. unset($owned_account[$key]);
  932. } elseif ($account_type == ACCOUNT_TYPE_WEBAPP_NORMAL && $account_modules[$module_name]['webapp_support'] != MODULE_SUPPORT_WEBAPP) {
  933. unset($owned_account[$key]);
  934. }
  935. }
  936. }
  937. return $owned_account;
  938. }