create.ctrl.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('module');
  8. load()->model('miniapp');
  9. load()->model('phoneapp');
  10. load()->model('user');
  11. $dos = array('display', 'save_account', 'check_params', 'get_user_info', 'load_groups');
  12. $do = in_array($do, $dos) ? $do : 'display';
  13. $sign = safe_gpc_string($_GPC['sign']);
  14. if (empty($account_all_type_sign[$sign])) {
  15. $error_msg = '所需创建的账号类型不存在, 请重试.';
  16. empty($_W['isajax']) ? message($error_msg, '', 'error') : iajax(-1, $error_msg);
  17. }
  18. if ($do == 'load_groups') {
  19. $group_keys = array();
  20. if (user_is_vice_founder($_W['uid'])) {
  21. $founder_own_table = table('users_founder_own_uni_groups');
  22. $founder_own_uni_groups = $founder_own_table->getOwnUniGroupsByFounderUid($_W['uid']);
  23. $group_keys = array_keys((array)$founder_own_uni_groups);
  24. }
  25. $unigroups = uni_groups($group_keys);
  26. foreach ($unigroups as $key => $group) {
  27. if (empty($group[$sign])) {
  28. unset($unigroups[$key]); }
  29. }
  30. iajax(0, $unigroups);
  31. }
  32. if ($do == 'get_user_info') {
  33. if (!user_is_founder($_W['uid'])) {
  34. iajax(-1, '非法请求数据!');
  35. }
  36. $uid = intval($_GPC['uid'][0]);
  37. $sign = trim($_GPC['sign']);
  38. if (empty($account_all_type_sign[$sign])) {
  39. iajax(-1, '参数有误');
  40. }
  41. $user = user_single(array('uid' => $uid));
  42. if (empty($user)) {
  43. iajax(-1, '用户不存在或是已经被删除', '');
  44. }
  45. $info = array(
  46. 'uid' => $user['uid'],
  47. 'username' => $user['username'],
  48. 'group' => user_group_detail_info($user['groupid']),
  49. 'endtime' => user_end_time($user['uid']),
  50. 'modules' => array(),
  51. );
  52. $info['package'] = empty($info['group']['package']) ? array() : iunserializer($info['group']['package']);
  53. $user_modules = user_modules($user['uid']);
  54. if (!empty($user_modules)) {
  55. foreach ($user_modules as $module) {
  56. if ($module['issystem'] != 1 && $module[$sign.'_support'] == MODULE_SUPPORT_ACCOUNT) {
  57. $info['modules'][] = $module;
  58. }
  59. }
  60. }
  61. iajax(0, $info);
  62. }
  63. $sign_title = $account_all_type_sign[$sign]['title'];
  64. $create_account_type = $account_all_type_sign[$sign]['contain_type'][0];
  65. $user_account_num = permission_user_account_num($_W['uid']);
  66. if (empty($_W['isfounder']) && $user_account_num["{$sign}_limit"] <= 0) {
  67. $error_msg = $sign_title . '创建数量已达上限!';
  68. empty($_W['isajax']) ? message($error_msg, '', 'error') : iajax(-1, $error_msg);
  69. }
  70. if ($do == 'display') {
  71. $modules = user_modules($_W['uid']);
  72. foreach ($modules as $k => $module) {
  73. if ($module['issystem'] == 1 || $module[$sign.'_support'] != MODULE_SUPPORT_ACCOUNT) {
  74. unset($modules[$k]); } else {
  75. $modules[$k]['support'] = $sign . '_support';
  76. }
  77. }
  78. if (in_array($sign, array(ACCOUNT_TYPE_SIGN, XZAPP_TYPE_SIGN))) {
  79. $templates = pdo_fetchall("SELECT * FROM ".tablename('site_templates'));
  80. }
  81. }
  82. if ($do == 'save_account' || $do == 'check_params') {
  83. $post = array();
  84. $post['step'] = safe_gpc_string(trim($_GPC['step']));
  85. $post['name'] = safe_gpc_string(trim($_GPC['name']));
  86. $post['description'] = safe_gpc_string($_GPC['description']);
  87. $post['owner_uid'] = intval($_GPC['owner_uid']);
  88. $post['version'] = safe_gpc_string(trim($_GPC['version']));
  89. if (empty($post['step']) || $post['step'] == 'base_info') {
  90. if (empty($post['name'])) {
  91. iajax(-1, $sign_title . '名称不能为空');
  92. }
  93. $account_table = table('account');
  94. $check_uniacname = $account_table->searchWithTitle($post['name'])->searchWithType($create_account_type)->searchAccountList();
  95. if (!empty($check_uniacname)) {
  96. iajax(-1, "该{$sign_title}名称已经存在");
  97. }
  98. }
  99. if (empty($post['step']) || $post['step'] == 'account_modules') {
  100. if (user_is_founder($_W['uid'])) { if (!empty($post['owner_uid']) && !user_is_founder($post['owner_uid'], true)) {
  101. $create_account_info = permission_user_account_num($post['owner_uid']);
  102. if ($create_account_info[$sign . '_limit'] <= 0) {
  103. iajax(-1, "您所设置的主管理员所在的用户组可添加的公众号数量已达上限,请选择其他人做主管理员!");
  104. }
  105. }
  106. }
  107. }
  108. if (empty($post['step'])) {
  109. if (in_array($sign, array(PHONEAPP_TYPE_SIGN, WXAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))) {
  110. if (!preg_match('/^[0-9]{1,2}\.[0-9]{1,2}(\.[0-9]{1,2})?$/', $post['version'])) {
  111. iajax(-1, '版本号错误,只能是数字、点,数字最多2位,例如 1.1.1 或1.2');
  112. }
  113. }
  114. }
  115. if ($do == 'check_params') {
  116. iajax(0);
  117. }
  118. }
  119. if ($do == 'save_account') {
  120. if (in_array($sign, array(ACCOUNT_TYPE_SIGN, XZAPP_TYPE_SIGN, WEBAPP_TYPE_SIGN, PHONEAPP_TYPE_SIGN))) {
  121. pdo_insert('uni_account', array(
  122. 'groupid' => 0,
  123. 'default_acid' => 0,
  124. 'name' => $post['name'],
  125. 'description' => $post['description'],
  126. 'title_initial' => get_first_pinyin($post['name']),
  127. ));
  128. $uniacid = pdo_insertid();
  129. if (empty($uniacid)) {
  130. iajax(-1, "添加{$sign_title}失败, 请重试");
  131. }
  132. $account_data = array('name' => $post['name'], 'type' => $create_account_type);
  133. if ($sign == ACCOUNT_TYPE_SIGN) {
  134. $account_data['account'] = safe_gpc_string(trim($_GPC['account']));
  135. }
  136. if ($sign == ACCOUNT_TYPE_SIGN || $sign == XZAPP_TYPE_SIGN) {
  137. $account_data['original'] = safe_gpc_string(trim($_GPC['original']));
  138. $account_data['level'] = intval($_GPC['level']);
  139. $account_data['key'] = safe_gpc_string(trim($_GPC['key']));
  140. $account_data['secret'] = safe_gpc_string(trim($_GPC['secret']));
  141. }
  142. $acid = account_create($uniacid, $account_data);
  143. if(empty($acid)) {
  144. iajax(-1, "添加{$sign_title}信息失败");
  145. }
  146. pdo_update('uni_account', array('default_acid' => $acid), array('uniacid' => $uniacid));
  147. if (!empty($_GPC['headimg'])) {
  148. $headimg = safe_gpc_path($_GPC['headimg']);
  149. if (file_is_image($headimg)) {
  150. copy($headimg, IA_ROOT . '/attachment/headimg_'.$acid.'.jpg');
  151. }
  152. }
  153. if (!empty($_GPC['qrcode'])) {
  154. $qrcode = safe_gpc_path($_GPC['qrcode']);
  155. if (file_is_image($qrcode)) {
  156. copy($qrcode, IA_ROOT . '/attachment/qrcode_'.$acid.'.jpg');
  157. }
  158. }
  159. if (empty($_W['isfounder'])) {
  160. uni_user_account_role($uniacid, $_W['uid'], ACCOUNT_MANAGE_NAME_OWNER);
  161. cache_build_account_modules($uniacid);
  162. }
  163. if (user_is_vice_founder()) {
  164. uni_user_account_role($uniacid, $_W['uid'], ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  165. }
  166. if (!empty($_W['user']['owner_uid'])) {
  167. uni_user_account_role($uniacid, $_W['user']['owner_uid'], ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  168. }
  169. if (in_array($sign, array(ACCOUNT_TYPE_SIGN, XZAPP_TYPE_SIGN))) {
  170. pdo_insert('mc_groups', array('uniacid' => $uniacid, 'title' => '默认会员组', 'isdefault' => 1));
  171. $fields = pdo_getall('profile_fields');
  172. if (is_array($fields)) {
  173. foreach($fields as $field) {
  174. pdo_insert('mc_member_fields', array(
  175. 'uniacid' => $uniacid,
  176. 'fieldid' => $field['id'],
  177. 'title' => $field['title'],
  178. 'available' => $field['available'],
  179. 'displayorder' => $field['displayorder'],
  180. ));
  181. }
  182. }
  183. }
  184. if ($sign == ACCOUNT_TYPE_SIGN) {
  185. $oauth = uni_setting($uniacid, array('oauth'));
  186. if ($acid && empty($oauth['oauth']['account']) && !empty($account_data['key']) && !empty($account_data['secret']) && $account_data['level'] == ACCOUNT_SERVICE_VERIFY) {
  187. pdo_update('uni_settings',
  188. array('oauth' => iserializer(array('account' => $acid, 'host' => $oauth['oauth']['host']))),
  189. array('uniacid' => $uniacid)
  190. );
  191. }
  192. $template = pdo_fetch('SELECT id,title FROM ' . tablename('site_templates') . " WHERE name = 'default'");
  193. pdo_insert('site_styles', array(
  194. 'uniacid' => $uniacid,
  195. 'templateid' => $template['id'],
  196. 'name' => $template['title'] . '_' . random(4),
  197. ));
  198. $styleid = pdo_insertid();
  199. pdo_insert('site_multi', array(
  200. 'uniacid' => $uniacid,
  201. 'title' => $post['name'],
  202. 'styleid' => $styleid,
  203. ));
  204. $multi_id = pdo_insertid();
  205. }
  206. pdo_insert('uni_settings', array(
  207. 'creditnames' => iserializer(array('credit1' => array('title' => '积分', 'enabled' => 1), 'credit2' => array('title' => '余额', 'enabled' => 1))),
  208. 'creditbehaviors' => iserializer(array('activity' => 'credit1', 'currency' => 'credit2')),
  209. 'uniacid' => $uniacid,
  210. 'default_site' => empty($multi_id) ? 0 : $multi_id,
  211. 'sync' => iserializer(array('switch' => 0, 'acid' => '')),
  212. ));
  213. }
  214. if (in_array($sign, array(WXAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))) {
  215. $miniapp_data = array(
  216. 'name' => $post['name'],
  217. 'type' => $create_account_type,
  218. 'description' => $post['description'],
  219. 'headimg' => !empty($_GPC['headimg']) && file_is_image($_GPC['headimg']) ? $_GPC['headimg'] : '',
  220. 'qrcode' => !empty($_GPC['qrcode']) && file_is_image($_GPC['qrcode']) ? $_GPC['qrcode'] : '',
  221. );
  222. if ($sign == WXAPP_TYPE_SIGN) {
  223. $miniapp_data['original'] = safe_gpc_string($_GPC['original']);
  224. $miniapp_data['level'] = 1;
  225. }
  226. if (isset($_GPC['key']) && !empty($_GPC['key'])) {
  227. $miniapp_data['key'] = safe_gpc_string($_GPC['key']);
  228. }
  229. if (isset($_GPC['appid']) && !empty($_GPC['appid'])) {
  230. if ($sign == WXAPP_TYPE_SIGN || $sign == ALIAPP_TYPE_SIGN) {
  231. $miniapp_data['key'] = safe_gpc_string($_GPC['appid']);
  232. } else {
  233. $miniapp_data['appid'] = safe_gpc_string($_GPC['appid']);
  234. }
  235. }
  236. if (isset($_GPC['secret']) && !empty($_GPC['secret'])) {
  237. $miniapp_data['secret'] = safe_gpc_string($_GPC['secret']);
  238. }
  239. $uniacid = miniapp_create($miniapp_data);
  240. if (is_error($uniacid) || empty($uniacid)) {
  241. iajax(-1, empty($uniacid) ? '添加失败' : $uniacid['message']);
  242. }
  243. $acid = pdo_getcolumn('account', array('uniacid' => $uniacid, 'type' => $create_account_type), 'acid');
  244. }
  245. if (user_is_founder($_W['uid'])) {
  246. if (!empty($post['owner_uid'])) {
  247. $owner = pdo_get('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'));
  248. if (!empty($owner)) {
  249. pdo_update('uni_account_users', array('uid' => $post['owner_uid']), array('uniacid' => $uniacid, 'role' => 'owner'));
  250. } else {
  251. uni_user_account_role($uniacid, $post['owner_uid'], ACCOUNT_MANAGE_NAME_OWNER);
  252. }
  253. $user_vice_id = pdo_getcolumn('users', array('uid' => $post['owner_uid']), 'owner_uid');
  254. if ($_W['user']['founder_groupid'] != ACCOUNT_MANAGE_GROUP_VICE_FOUNDER && !empty($user_vice_id)) {
  255. uni_user_account_role($uniacid, $user_vice_id, ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  256. }
  257. }
  258. if (!empty($_GPC['endtime'])) {
  259. $account_end_time = strtotime($_GPC['endtime']);
  260. if (!empty($post['owner_uid'])) {
  261. $user_end_time = strtotime(user_end_time($post['owner_uid']));
  262. if ($user_end_time > 0 && $account_end_time > $user_end_time) {
  263. $account_end_time = $user_end_time;
  264. }
  265. }
  266. } else {
  267. $account_end_time = 0;
  268. }
  269. pdo_update('account', array('endtime' => $account_end_time), array('uniacid' => $uniacid));
  270. if (!empty($_GPC['groups'])) {
  271. foreach ($_GPC['groups'] as $group_id) {
  272. $group_id = intval($group_id);
  273. if (!empty($group_id)) {
  274. pdo_insert('uni_account_group', array('uniacid' => $uniacid, 'groupid' => $group_id));
  275. }
  276. }
  277. }
  278. if (!empty($_GPC['modules']) || !empty($_GPC['templates'])) {
  279. $templates = safe_gpc_array($_GPC['templates']);
  280. $modules = safe_gpc_array($_GPC['modules']);
  281. $data = array(
  282. 'modules' => array('modules' => array(), 'wxapp' => array(), 'webapp' => array(), 'xzapp' => array(), 'phoneapp' => array()),
  283. 'templates' => iserializer($templates),
  284. 'uniacid' => $uniacid,
  285. 'name' => '',
  286. );
  287. $group_sign = $sign == 'account' ? 'modules' : $sign;
  288. $data['modules'][$group_sign] = $modules;
  289. $data['modules'] = iserializer($data['modules']);
  290. pdo_insert('uni_group', $data);
  291. }
  292. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  293. cache_delete(cache_system_key('unimodules', array('uniacid' => $uniacid, 'enabled' => 1)));
  294. cache_delete(cache_system_key('unimodules', array('uniacid' => $uniacid, 'enabled' => '')));
  295. cache_delete(cache_system_key('proxy_wechatpay_account'));
  296. $cash_index = $sign == 'account' ? 'app' : $sign;
  297. cache_delete(cache_system_key('user_accounts', array('type' => $cash_index, 'uid' => $_W['uid'])));
  298. if (!empty($post['owner_uid'])) {
  299. cache_delete(cache_system_key('user_accounts', array('type' => $cash_index, 'uid' => $post['owner_uid'])));
  300. cache_build_account_modules($uniacid, $post['owner_uid']);
  301. }
  302. }
  303. $next_url = '';
  304. if ($sign == ACCOUNT_TYPE_SIGN) {
  305. $next_url = url('account/post-step', array('uniacid' => $uniacid, 'acid' => $acid, 'step' => 4));
  306. } elseif ($sign == XZAPP_TYPE_SIGN) {
  307. $next_url = url('xzapp/post-step', array('uniacid' => $uniacid, 'acid' => $acid, 'step' => 4));
  308. } elseif (in_array($sign, array(PHONEAPP_TYPE_SIGN, WXAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))) {
  309. $next_url = '';
  310. } else {
  311. $next_url = url('account/display/switch', array('uniacid' => $uniacid, 'acid' => $acid, 'type' => $create_account_type));
  312. iajax(0, '创建成功', $next_url);
  313. }
  314. if (!empty($next_url)) {
  315. iajax(0, '', $next_url);
  316. }
  317. if (in_array($sign, array(PHONEAPP_TYPE_SIGN, WXAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))) {
  318. $version = array(
  319. 'uniacid' => $uniacid,
  320. 'description' => safe_gpc_string($_GPC['version_description']),
  321. 'version' => $post['version'],
  322. 'modules' => '',
  323. 'createtime' => TIMESTAMP,
  324. );
  325. $module = module_fetch(safe_gpc_string($_GPC['version_module']));
  326. if (!empty($module)) {
  327. $version['modules'] = serialize(array($module['name'] => array(
  328. 'name' => $module['name'],
  329. 'version' => $module['version'],
  330. )));
  331. }
  332. if ($sign == WXAPP_TYPE_SIGN) {
  333. $version['design_method'] = WXAPP_MODULE;
  334. $version['quickmenu'] = '';
  335. $version['createtime'] = TIMESTAMP;
  336. $version['template'] = 0;
  337. $version['type'] = 0; $version['multiid'] = 0;
  338. }
  339. pdo_insert('wxapp_versions', $version);
  340. $version_id = pdo_insertid();
  341. if (empty($version_id)) {
  342. iajax(-1, '版本创建失败');
  343. } else {
  344. cache_delete(cache_system_key('user_accounts', array('type' => $sign, 'uid' => $_W['uid'])));
  345. iajax(0, '创建成功', url('account/display/switch', array('uniacid' => $uniacid, 'version_id' => $version_id, 'type' => $create_account_type)));
  346. }
  347. }
  348. }
  349. template('account/create');