PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/module/home/home_magic.php

https://github.com/jinbo51/DiscuzX
PHP | 514 lines | 414 code | 94 blank | 6 comment | 110 complexity | 6369b0a57d2eb0c0984895578f3dd3a2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2009 Comsenz Inc111.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: home_magic.php 31927 2012-10-25 02:54:23Z zhengqingpeng $
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. if(!$_G['uid']) {
  11. showmessage('not_loggedin', NULL, array(), array('login' => 1));
  12. }
  13. if(!$_G['setting']['creditstransextra'][3]) {
  14. showmessage('credits_transaction_disabled');
  15. } elseif(!$_G['setting']['magicstatus']) {
  16. showmessage('magics_close');
  17. }
  18. require_once libfile('function/magic');
  19. loadcache('magics');
  20. $_G['mnid'] = 'mn_common';
  21. $magiclist = array();
  22. $_G['tpp'] = 12;
  23. $page = max(1, intval($_GET['page']));
  24. $action = $_GET['action'];
  25. $operation = $_GET['operation'];
  26. $start_limit = ($page - 1) * $_G['tpp'];
  27. $comma = $typeadd = $filteradd = $forumperm = $targetgroupperm = '';
  28. $magicarray = is_array($_G['cache']['magics']) ? $_G['cache']['magics'] : array();
  29. if(!$_G['uid'] && ($operation || $action == 'mybox')) {
  30. showmessage('not_loggedin', NULL, array(), array('login' => 1));
  31. }
  32. if(!$_G['group']['allowmagics']) {
  33. showmessage('magics_nopermission');
  34. }
  35. $totalweight = getmagicweight($_G['uid'], $magicarray);
  36. $allowweight = $_G['group']['maxmagicsweight'] - $totalweight;
  37. $location = 0;
  38. if(empty($action) && !empty($_GET['mid'])) {
  39. $_GET['magicid'] = C::t('common_member_magic')->fetch_magicid_by_identifier($_G['uid'], $_GET['mid']);
  40. if(!$_GET['magicid']) {
  41. $magic = C::t('common_magic')->fetch_by_identifier($_GET['mid']);
  42. if(!$magic['price'] && $magic['num']) {
  43. getmagic($magic['magicid'], 1, $magic['weight'], $totalweight, $_G['uid'], $_G['group']['maxmagicsweight']);
  44. updatemagiclog($magic['magicid'], '1', 1, $magic['price'].'|'.$magic['credit'], $_G['uid']);
  45. C::t('common_magic')->update_salevolume($magic['magicid'], 1);
  46. updatemembercount($_G['uid'], array($magic['credit'] => -0), true, 'BMC', $magic['magicid']);
  47. $_GET['magicid'] = $magic['magicid'];
  48. }
  49. }
  50. if($_GET['magicid']) {
  51. $action = 'mybox';
  52. $operation = 'use';
  53. } else {
  54. $action = 'shop';
  55. $operation = 'buy';
  56. $location = 1;
  57. }
  58. }
  59. $action = empty($action) ? 'shop' : $action;
  60. $actives[$action] = ' class="a"';
  61. if($action == 'shop') {
  62. $operation = empty($operation) ? 'index' : $operation;
  63. if(in_array($operation, array('index', 'hot'))) {
  64. $subactives[$operation] = 'class="a"';
  65. $filteradd = '';
  66. if($operation == 'index') {
  67. $navtitle = lang('core', 'title_magics_shop');
  68. } else {
  69. $navtitle = lang('core', 'title_magics_hot');
  70. }
  71. $magiccount = C::t('common_magic')->count_page($operation);
  72. $multipage = multi($magiccount, $_G['tpp'], $page, "home.php?mod=magic&action=shop&operation=$operation");
  73. foreach(C::t('common_magic')->fetch_all_page($operation, $start_limit, $_G['tpp']) as $magic) {
  74. $magic['discountprice'] = $_G['group']['magicsdiscount'] ? intval($magic['price'] * ($_G['group']['magicsdiscount'] / 10)) : intval($magic['price']);
  75. $eidentifier = explode(':', $magic['identifier']);
  76. if(count($eidentifier) > 1) {
  77. $magic['pic'] = 'source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.gif';
  78. } else {
  79. $magic['pic'] = STATICURL.'image/magic/'.strtolower($magic['identifier']).'.gif';
  80. }
  81. $magiclist[] = $magic;
  82. }
  83. $magiccredits = array();
  84. foreach($magicarray as $magic) {
  85. $magiccredits[$magic['credit']] = $magic['credit'];
  86. }
  87. } elseif($operation == 'buy') {
  88. $magic = C::t('common_magic')->fetch_by_identifier($_GET['mid']);
  89. if(!$magic || !$magic['available']) {
  90. showmessage('magics_nonexistence');
  91. }
  92. $magicperm = dunserialize($magic['magicperm']);
  93. $querystring = array();
  94. foreach($_GET as $k => $v) {
  95. $querystring[] = $k.'='.rawurlencode($v);
  96. }
  97. $querystring = implode('&', $querystring);
  98. $eidentifier = explode(':', $magic['identifier']);
  99. if(count($eidentifier) > 1) {
  100. $magicfile = './source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.php';
  101. $magicclass = 'magic_'.$eidentifier[1];
  102. } else {
  103. $magicfile = './source/class/magic/magic_'.$magic['identifier'].'.php';
  104. $magicclass = 'magic_'.$magic['identifier'];
  105. }
  106. if(!@include_once DISCUZ_ROOT.$magicfile) {
  107. showmessage('magics_filename_nonexistence', '', array('file' => $magicfile));
  108. }
  109. $magicclass = new $magicclass;
  110. $magicclass->magic = $magic;
  111. $magicclass->parameters = $magicperm;
  112. if(method_exists($magicclass, 'buy')) {
  113. $magicclass->buy();
  114. }
  115. $magic['discountprice'] = $_G['group']['magicsdiscount'] ? intval($magic['price'] * ($_G['group']['magicsdiscount'] / 10)) : intval($magic['price']);
  116. if(count($eidentifier) > 1) {
  117. $magic['pic'] = 'source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.gif';
  118. } else {
  119. $magic['pic'] = STATICURL.'image/magic/'.strtolower($magic['identifier']).'.gif';
  120. }
  121. $magic['credit'] = $magic['credit'] ? $magic['credit'] : $_G['setting']['creditstransextra'][3];
  122. $useperoid = magic_peroid($magic, $_G['uid']);
  123. if(!submitcheck('operatesubmit')) {
  124. $useperm = (strstr($magicperm['usergroups'], "\t$_G[groupid]\t") || !$magicperm['usergroups']) ? '1' : '0';
  125. if($magicperm['targetgroups']) {
  126. loadcache('usergroups');
  127. foreach(explode("\t", $magicperm['targetgroups']) as $_G['groupid']) {
  128. if(isset($_G['cache']['usergroups'][$_G['groupid']])) {
  129. $targetgroupperm .= $comma.$_G['cache']['usergroups'][$_G['groupid']]['grouptitle'];
  130. $comma = '&nbsp;';
  131. }
  132. }
  133. }
  134. if($magicperm['forum']) {
  135. loadcache('forums');
  136. foreach(explode("\t", $magicperm['forum']) as $fid) {
  137. if(isset($_G['cache']['forums'][$fid])) {
  138. $forumperm .= $comma.'<a href="forum.php?mod=forumdisplay&fid='.$fid.'" target="_blank">'.$_G['cache']['forums'][$fid]['name'].'</a>';
  139. $comma = '&nbsp;';
  140. }
  141. }
  142. }
  143. include template('home/space_magic_shop_opreation');
  144. dexit();
  145. } else {
  146. $magicnum = intval($_GET['magicnum']);
  147. $magic['weight'] = $magic['weight'] * $magicnum;
  148. $totalprice = $magic['discountprice'] * $magicnum;
  149. if(getuserprofile('extcredits'.$magic['credit']) < $totalprice) {
  150. if($_G['setting']['ec_ratio'] && $_G['setting']['creditstrans'][0] == $magic['credit']) {
  151. showmessage('magics_credits_no_enough_and_charge', '', array('credit' => $_G['setting']['extcredits'][$magic['credit']]['title']));
  152. } else {
  153. showmessage('magics_credits_no_enough', '', array('credit' => $_G['setting']['extcredits'][$magic['credit']]['title']));
  154. }
  155. } elseif($magic['num'] < $magicnum) {
  156. showmessage('magics_num_no_enough');
  157. } elseif(!$magicnum || $magicnum < 0) {
  158. showmessage('magics_num_invalid');
  159. }
  160. getmagic($magic['magicid'], $magicnum, $magic['weight'], $totalweight, $_G['uid'], $_G['group']['maxmagicsweight']);
  161. updatemagiclog($magic['magicid'], '1', $magicnum, $magic['price'].'|'.$magic['credit'], $_G['uid']);
  162. C::t('common_magic')->update_salevolume($magic['magicid'], $magicnum);
  163. updatemembercount($_G['uid'], array($magic['credit'] => -$totalprice), true, 'BMC', $magic['magicid']);
  164. showmessage('magics_buy_succeed', 'home.php?mod=magic&action=mybox', array('magicname' => $magic['name'], 'num' => $magicnum, 'credit' => $totalprice.' '.$_G['setting']['extcredits'][$magic['credit']]['unit'].$_G['setting']['extcredits'][$magic['credit']]['title']));
  165. }
  166. } elseif($operation == 'give') {
  167. if($_G['group']['allowmagics'] < 2) {
  168. showmessage('magics_nopermission');
  169. }
  170. $magic = C::t('common_magic')->fetch_by_identifier($_GET['mid']);
  171. if(!$magic || !$magic['available']) {
  172. showmessage('magics_nonexistence');
  173. }
  174. $magic['discountprice'] = $_G['group']['magicsdiscount'] ? intval($magic['price'] * ($_G['group']['magicsdiscount'] / 10)) : intval($magic['price']);
  175. $eidentifier = explode(':', $magic['identifier']);
  176. if(count($eidentifier) > 1) {
  177. $magic['pic'] = 'source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.gif';
  178. } else {
  179. $magic['pic'] = STATICURL.'image/magic/'.strtolower($magic['identifier']).'.gif';
  180. }
  181. if(!submitcheck('operatesubmit')) {
  182. include libfile('function/friend');
  183. $buddyarray = friend_list($_G['uid'], 20);
  184. include template('home/space_magic_shop_opreation');
  185. dexit();
  186. } else {
  187. $magicnum = intval($_GET['magicnum']);
  188. $totalprice = $magic['price'] * $magicnum;
  189. if(getuserprofile('extcredits'.$magic['credit']) < $totalprice) {
  190. if($_G['setting']['ec_ratio'] && $_G['setting']['creditstrans'][0] == $magic['credit']) {
  191. showmessage('magics_credits_no_enough_and_charge', '', array('credit' => $_G['setting']['extcredits'][$magic['credit']]['title']));
  192. } else {
  193. showmessage('magics_credits_no_enough', '', array('credit' => $_G['setting']['extcredits'][$magic['credit']]['title']));
  194. }
  195. } elseif($magic['num'] < $magicnum) {
  196. showmessage('magics_num_no_enough');
  197. } elseif(!$magicnum || $magicnum < 0) {
  198. showmessage('magics_num_invalid');
  199. }
  200. $toname = dhtmlspecialchars(trim($_GET['tousername']));
  201. if(!$toname) {
  202. showmessage('magics_username_nonexistence');
  203. }
  204. $givemessage = dhtmlspecialchars(trim($_GET['givemessage']));
  205. givemagic($toname, $magic['magicid'], $magicnum, $magic['num'], $totalprice, $givemessage, $magicarray);
  206. C::t('common_magic')->update_salevolume($magic['magicid'], $magicnum);
  207. updatemembercount($_G['uid'], array($magic['credit'] => -$totalprice), true, 'BMC', $magicid);
  208. showmessage('magics_buygive_succeed', 'home.php?mod=magic&action=shop', array('magicname' => $magic['name'], 'toname' => $toname, 'num' => $magicnum, 'credit' => $_G['setting']['extcredits'][$magic['credit']]['title'].' '.$totalprice.' '.$_G['setting']['extcredits'][$magic['credit']]['unit']), array('locationtime' => true));
  209. }
  210. } else {
  211. showmessage('undefined_action');
  212. }
  213. } elseif($action == 'mybox') {
  214. if(empty($operation)) {
  215. $pid = !empty($_GET['pid']) ? intval($_GET['pid']) : 0;
  216. $magiccount = C::t('common_member_magic')->count_by_uid($_G['uid']);
  217. $multipage = multi($magiccount, $_G['tpp'], $page, "home.php?mod=magic&action=mybox&pid=$pid$typeadd");
  218. $query = C::t('common_member_magic')->fetch_all($_G['uid'], null, $start_limit, $_G['tpp']);
  219. foreach($query as $value) {
  220. $magicids[] = $value['magicid'];
  221. }
  222. $magicm = C::t('common_magic')->fetch_all($magicids);
  223. foreach($query as $curmagicid => $mymagic) {
  224. $mymagic = $mymagic + $magicm[$mymagic['magicid']];
  225. $eidentifier = explode(':', $mymagic['identifier']);
  226. if(count($eidentifier) > 1) {
  227. $mymagic['pic'] = 'source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.gif';
  228. } else {
  229. $mymagic['pic'] = STATICURL.'image/magic/'.strtolower($mymagic['identifier']).'.gif';
  230. }
  231. $mymagic['weight'] = intval($mymagic['weight'] * $mymagic['num']);
  232. $mymagic['type'] = $mymagic['type'];
  233. $mymagiclist[] = $mymagic;
  234. }
  235. $navtitle = lang('core', 'title_magics_user');
  236. } else {
  237. $magicid = intval($_GET['magicid']);
  238. $membermagic = C::t('common_member_magic')->fetch($_G['uid'], $magicid);
  239. $magic = $membermagic + C::t('common_magic')->fetch($magicid);
  240. if(!$membermagic) {
  241. showmessage('magics_nonexistence');
  242. } elseif(!$magic['num']) {
  243. C::t('common_member_magic')->delete($_G['uid'], $magic['magicid']);
  244. showmessage('magics_nonexistence');
  245. }
  246. $magicperm = dunserialize($magic['magicperm']);
  247. $eidentifier = explode(':', $magic['identifier']);
  248. if(count($eidentifier) > 1) {
  249. $magic['pic'] = 'source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.gif';
  250. } else {
  251. $magic['pic'] = STATICURL.'image/magic/'.strtolower($magic['identifier']).'.gif';
  252. }
  253. if($operation == 'use') {
  254. $useperm = (strstr($magicperm['usergroups'], "\t$_G[groupid]\t") || empty($magicperm['usergroups'])) ? '1' : '0';
  255. if(!$useperm) {
  256. showmessage('magics_use_nopermission');
  257. }
  258. if($magic['num'] <= 0) {
  259. C::t('common_member_magic')->delete($_G['uid'], $magic['magicid']);
  260. showmessage('magics_nopermission');
  261. }
  262. $magic['weight'] = intval($magicarray[$magic['magicid']]['weight'] * $magic['num']);
  263. if(count($eidentifier) > 1) {
  264. $magicfile = './source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.php';
  265. $magicclass = 'magic_'.$eidentifier[1];
  266. } else {
  267. $magicfile = './source/class/magic/magic_'.$magic['identifier'].'.php';
  268. $magicclass = 'magic_'.$magic['identifier'];
  269. }
  270. if(!@include_once DISCUZ_ROOT.$magicfile) {
  271. showmessage('magics_filename_nonexistence', '', array('file' => $magicfile));
  272. }
  273. $magicclass = new $magicclass;
  274. $magicclass->magic = $magic;
  275. $magicclass->parameters = $magicperm;
  276. $useperoid = magic_peroid($magic, $_G['uid']);
  277. if(submitcheck('usesubmit')) {
  278. if($useperoid !== true && $useperoid <= 0) {
  279. showmessage('magics_outofperoid_'.$magic['useperoid'], '', array('usenum' => $magic['usenum']));
  280. }
  281. if(method_exists($magicclass, 'usesubmit')) {
  282. $magicclass->usesubmit();
  283. }
  284. dexit();
  285. }
  286. include template('home/space_magic_mybox_opreation');
  287. dexit();
  288. } elseif($operation == 'sell') {
  289. $magic['price'] = $_G['group']['magicsdiscount'] ? intval($magic['price'] * ($_G['group']['magicsdiscount'] / 10)) : intval($magic['price']);
  290. $discountprice = floor($magic['price'] * $_G['setting']['magicdiscount'] / 100);
  291. if(!submitcheck('operatesubmit')) {
  292. include template('home/space_magic_mybox_opreation');
  293. dexit();
  294. } else {
  295. $magicnum = intval($_GET['magicnum']);
  296. if(!$magicnum || $magicnum < 0) {
  297. showmessage('magics_num_invalid');
  298. } elseif($magicnum > $magic['num']) {
  299. showmessage('magics_amount_no_enough');
  300. }
  301. usemagic($magic['magicid'], $magic['num'], $magicnum);
  302. updatemagiclog($magic['magicid'], '2', $magicnum, '0', 0, 'sell');
  303. $totalprice = $discountprice * $magicnum;
  304. updatemembercount($_G['uid'], array($magic['credit'] => $totalprice));
  305. showmessage('magics_sell_succeed', 'home.php?mod=magic&action=mybox', array('magicname' => $magic['name'], 'num' => $magicnum, 'credit' => $totalprice.' '.$_G['setting']['extcredits'][$magic['credit']]['unit'].$_G['setting']['extcredits'][$magic['credit']]['title']));
  306. }
  307. } elseif($operation == 'drop') {
  308. if(!submitcheck('operatesubmit')) {
  309. include template('home/space_magic_mybox_opreation');
  310. dexit();
  311. } else {
  312. $magicnum = intval($_GET['magicnum']);
  313. if(!$magicnum || $magicnum < 0) {
  314. showmessage('magics_num_invalid');
  315. } elseif($magicnum > $magic['num']) {
  316. showmessage('magics_amount_no_enough');
  317. }
  318. usemagic($magic['magicid'], $magic['num'], $magicnum);
  319. updatemagiclog($magic['magicid'], '2', $magicnum, '0', 0, 'drop');
  320. showmessage('magics_drop_succeed', 'home.php?mod=magic&action=mybox', array('magicname' => $magic['name'], 'num' => $magicnum), array('locationtime' => true));
  321. }
  322. } elseif($operation == 'give') {
  323. if($_G['group']['allowmagics'] < 2) {
  324. showmessage('magics_nopermission');
  325. }
  326. if(!submitcheck('operatesubmit')) {
  327. include libfile('function/friend');
  328. $buddyarray = friend_list($_G['uid'], 20);
  329. include template('home/space_magic_mybox_opreation');
  330. dexit();
  331. } else {
  332. $magicnum = intval($_GET['magicnum']);
  333. $toname = dhtmlspecialchars(trim($_GET['tousername']));
  334. if(!$toname) {
  335. showmessage('magics_username_nonexistence');
  336. } elseif($magic['num'] < $magicnum) {
  337. showmessage('magics_num_invalid');
  338. }
  339. $givemessage = dhtmlspecialchars(trim($_GET['givemessage']));
  340. givemagic($toname, $magic['magicid'], $magicnum, $magic['num'], '0', $givemessage, $magicarray);
  341. }
  342. } else {
  343. showmessage('undefined_action');
  344. }
  345. }
  346. } elseif($action == 'log') {
  347. $subactives[$operation] = 'class="a"';
  348. $loglist = array();
  349. if($operation == 'uselog') {
  350. $count = C::t('common_magiclog')->count_by_uid_action($_G['uid'], 2);
  351. if($count) {
  352. $multipage = multi($count, $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=uselog');
  353. $logs = C::t('common_magiclog')->fetch_all_by_uid_action($_G['uid'], 2, $start_limit, $_G['tpp']);
  354. $luids=array();
  355. foreach($luids as $log) {
  356. $luids[$log['uid']] = $log['uid'];
  357. }
  358. $members = C::t('common_magiclog')->fetch_all($luids);
  359. foreach($logs as $log) {
  360. $log['username'] = $members[$log['uid']]['username'];
  361. $log['dateline'] = dgmdate($log['dateline'], 'u');
  362. $log['name'] = $magicarray[$log['magicid']]['name'];
  363. $loglist[] = $log;
  364. }
  365. }
  366. } elseif($operation == 'buylog') {
  367. $count = C::t('common_magiclog')->count_by_uid_action($_G['uid'], 1);
  368. if($count) {
  369. $multipage = multi($count, $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=buylog');
  370. foreach(C::t('common_magiclog')->fetch_all_by_uid_action($_G['uid'], 1, $start_limit, $_G['tpp']) as $log) {
  371. $log['credit'] = $log['credit'] ? $log['credit'] : $_G['setting']['creditstransextra'][3];
  372. $log['dateline'] = dgmdate($log['dateline'], 'u');
  373. $log['name'] = $magicarray[$log['magicid']]['name'];
  374. $loglist[] = $log;
  375. }
  376. }
  377. } elseif($operation == 'givelog') {
  378. $count = C::t('common_magiclog')->count_by_uid_action($_G['uid'], 3);
  379. if($count) {
  380. $multipage = multi($count, $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=givelog');
  381. $uids = null;
  382. $query = C::t('common_magiclog')->fetch_all_by_uid_action($_G['uid'], 3, $start_limit, $_G['tpp']);
  383. foreach($query as $log) {
  384. $uids[] = $log['targetuid'];
  385. }
  386. if($uids != null) {
  387. $memberdata = C::t('common_member')->fetch_all_username_by_uid($uids);
  388. }
  389. foreach($query as $log) {
  390. $log['username'] = $memberdata[$log['targetuid']];
  391. $log['dateline'] = dgmdate($log['dateline'], 'u');
  392. $log['name'] = $magicarray[$log['magicid']]['name'];
  393. $loglist[] = $log;
  394. }
  395. }
  396. } elseif($operation == 'receivelog') {
  397. $count = C::t('common_magiclog')->count_by_targetuid_action($_G['uid'], 3);
  398. if($count) {
  399. $multipage = multi($count, $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=receivelog');
  400. $logs = C::t('common_magiclog')->fetch_all_by_targetuid_action($_G['uid'], 3, $start_limit, $_G['tpp']);
  401. $luids = array();
  402. foreach($logs as $log) {
  403. $luids[$log['uid']] = $log['uid'];
  404. }
  405. $members = C::t('common_member')->fetch_all_username_by_uid($luids);
  406. foreach($logs as $log) {
  407. $log['username'] = $members[$log['uid']];
  408. $log['dateline'] = dgmdate($log['dateline'], 'u');
  409. $log['name'] = $magicarray[$log['magicid']]['name'];
  410. $loglist[] = $log;
  411. }
  412. }
  413. }
  414. $navtitle = lang('core', 'title_magics_log');
  415. } else {
  416. showmessage('undefined_action');
  417. }
  418. include template('home/space_magic');
  419. ?>