PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/source/module/home/home_magic.php

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