function_magic.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_magic.php 27757 2012-02-14 03:08:15Z chenmengshu $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function checkmagicperm($perms, $id) {
  12. $id = $id ? intval($id) : '';
  13. return strexists("\t".trim($perms)."\t", "\t".trim($id)."\t") || !$perms;
  14. }
  15. function getmagic($magicid, $magicnum, $weight, $totalweight, $uid, $maxmagicsweight, $force = 0) {
  16. if($weight + $totalweight > $maxmagicsweight && !$force) {
  17. showmessage('magics_weight_range_invalid', '', array('less' => $weight + $totalweight - $maxmagicsweight));
  18. } else {
  19. if(C::t('common_member_magic')->count($uid, $magicid)) {
  20. C::t('common_member_magic')->increase($uid, $magicid, array('num' => $magicnum), false, true);
  21. } else {
  22. C::t('common_member_magic')->insert(array(
  23. 'uid' => $uid,
  24. 'magicid' => $magicid,
  25. 'num' => $magicnum
  26. ));
  27. }
  28. }
  29. }
  30. function getmagicweight($uid, $magicarray) {
  31. $totalweight = 0;
  32. $query = C::t('common_member_magic')->fetch_all($uid);
  33. foreach($query as $magic) {
  34. $totalweight += $magicarray[$magic['magicid']]['weight'] * $magic['num'];
  35. }
  36. return $totalweight;
  37. }
  38. function getpostinfo($id, $type, $colsarray = '') {
  39. global $_G;
  40. $sql = $comma = '';
  41. $type = in_array($type, array('tid', 'pid', 'blogid')) && !empty($type) ? $type : 'tid';
  42. $cols = '*';
  43. if(!empty($colsarray) && is_array($colsarray)) {
  44. $cols = '';
  45. foreach($colsarray as $val) {
  46. $cols .= $comma.$val;
  47. $comma = ', ';
  48. }
  49. }
  50. switch($type) {
  51. case 'tid':
  52. $info = C::t('forum_thread')->fetch_by_tid_displayorder($id, 0);
  53. break;
  54. case 'pid':
  55. $info = C::t('forum_post')->fetch($_G['tid'], $id);
  56. if($info && $info['invisible'] == 0) {
  57. $thread = C::t('forum_thread')->fetch($_G['tid']);
  58. $thread['thread_author'] = $thread['author'];
  59. $thread['thread_authorid'] = $thread['authorid'];
  60. $thread['thread_status'] = $thread['status'];
  61. unset($thread['author']);
  62. unset($thread['authorid']);
  63. unset($thread['dateline']);
  64. unset($thread['status']);
  65. $info = array_merge($info, $thread);
  66. } else {
  67. $info = array();
  68. }
  69. break;
  70. case 'blogid':
  71. $info = C::t('home_blog')->fetch($id);
  72. if(!($info && $info['status'] == '0')) {
  73. $info = array();
  74. }
  75. break;
  76. }
  77. if(!$info) {
  78. showmessage('magics_target_nonexistence');
  79. } else {
  80. return daddslashes($info, 1);
  81. }
  82. }
  83. function getuserinfo($username) {
  84. $member = C::t('common_member')->fetch_by_username($username);
  85. if(!$member) {
  86. showmessage('magics_target_member_nonexistence');
  87. } else {
  88. return daddslashes($member, 1);
  89. }
  90. }
  91. function givemagic($username, $magicid, $magicnum, $totalnum, $totalprice, $givemessage, $magicarray) {
  92. global $_G;
  93. $member = C::t('common_member')->fetch_by_username($username);
  94. if(!$member) {
  95. showmessage('magics_target_member_nonexistence');
  96. } elseif($member['uid'] == $_G['uid']) {
  97. showmessage('magics_give_myself');
  98. }
  99. $member = array_merge(C::t('common_usergroup_field')->fetch($member['groupid']), $member);
  100. $totalweight = getmagicweight($member['uid'], $magicarray);
  101. $magicweight = $magicarray[$magicid]['weight'] * $magicnum;
  102. if($magicarray[$magicid]['weight'] && $magicweight + $totalweight > $member['maxmagicsweight']) {
  103. $num = floor(($member['maxmagicsweight'] - $totalweight) / $magicarray[$magicid]['weight']);
  104. $num = max(0, $num);
  105. showmessage('magics_give_weight_range_invalid', '', array('num' => $num));
  106. }
  107. getmagic($magicid, $magicnum, $magicweight, $totalweight, $member['uid'], $member['maxmagicsweight']);
  108. notification_add($member['uid'], 'magic', 'magics_receive', array('magicname' => $magicarray[$magicid]['name'], 'msg' => $givemessage));
  109. updatemagiclog($magicid, '3', $magicnum, $magicarray[$magicid]['price'], $member['uid']);
  110. if(empty($totalprice)) {
  111. usemagic($magicid, $totalnum, $magicnum);
  112. showmessage('magics_give_succeed', 'home.php?mod=magic&action=mybox', array('toname' => $username, 'num' => $magicnum, 'magicname' => $magicarray[$magicid]['name']));
  113. }
  114. }
  115. function magicthreadmod($tid) {
  116. foreach(C::t('forum_threadmod')->fetch_all_by_tid_magicid($tid) as $threadmod) {
  117. if(!$threadmod['magicid'] && in_array($threadmod['action'], array('CLS', 'ECL', 'STK', 'EST', 'HLT', 'EHL'))) {
  118. showmessage('magics_mod_forbidden');
  119. }
  120. }
  121. }
  122. function magicshowsetting($setname, $varname, $value, $type = 'radio', $width = '20%') {
  123. $check = array();
  124. echo '<p class="mtm mbn">'.$setname.'</p>';
  125. if($type == 'radio') {
  126. $value ? $check['true'] = 'checked="checked"' : $check['false'] = 'checked="checked"';
  127. echo "<input type=\"radio\" name=\"$varname\" class=\"pr\" value=\"1\" $check[true] /> ".lang('core', 'yes')." &nbsp; &nbsp; \n".
  128. "<input type=\"radio\" name=\"$varname\" class=\"pr\" value=\"0\" $check[false] /> ".lang('core', 'no')."\n";
  129. } elseif($type == 'text') {
  130. echo "<input type=\"text\" name=\"$varname\" class=\"px p_fre\" value=\"".dhtmlspecialchars($value)."\" size=\"12\" autocomplete=\"off\" />\n";
  131. } elseif($type == 'hidden') {
  132. echo "<input type=\"hidden\" name=\"$varname\" value=\"".dhtmlspecialchars($value)."\" />\n";
  133. } else {
  134. echo $type;
  135. }
  136. }
  137. function magicshowtips($tips) {
  138. echo '<p>'.$tips.'</p>';
  139. }
  140. function magicshowtype($type = '') {
  141. if($type != 'bottom') {
  142. echo '<p>';
  143. } else {
  144. echo '</p>';
  145. }
  146. }
  147. function usemagic($magicid, $totalnum, $num = 1) {
  148. global $_G;
  149. if($totalnum == $num) {
  150. C::t('common_member_magic')->delete($_G['uid'], $magicid);
  151. } else {
  152. C::t('common_member_magic')->increase($_G['uid'], $magicid, array('num' => -$num));
  153. }
  154. }
  155. function updatemagicthreadlog($tid, $magicid, $action = 'MAG', $expiration = 0, $extra = 0) {
  156. global $_G;
  157. $_G['username'] = !$extra ? $_G['username'] : '';
  158. $data = array(
  159. 'tid' => $tid,
  160. 'uid' => $_G['uid'],
  161. 'magicid' => $magicid,
  162. 'username' => $_G['username'],
  163. 'dateline' => $_G['timestamp'],
  164. 'expiration' => $expiration,
  165. 'action' => $action,
  166. 'status' => 1
  167. );
  168. C::t('forum_threadmod')->insert($data);
  169. }
  170. function updatemagiclog($magicid, $action, $amount, $price, $targetuid = 0, $idtype = '', $targetid = 0) {
  171. global $_G;
  172. list($price, $credit) = explode('|', $price);
  173. $data = array(
  174. 'uid' => $_G['uid'],
  175. 'magicid' => $magicid,
  176. 'action' => $action,
  177. 'dateline' => $_G['timestamp'],
  178. 'amount' => $amount,
  179. 'price' => $price,
  180. 'credit' => $credit,
  181. 'idtype' => $idtype,
  182. 'targetid' => $targetid,
  183. 'targetuid' => $targetuid
  184. );
  185. C::t('common_magiclog')->insert($data);
  186. }
  187. function magic_check_idtype($id, $idtype) {
  188. global $_G;
  189. include_once libfile('function/spacecp');
  190. $value = '';
  191. $tablename = gettablebyidtype($idtype);
  192. if($tablename) {
  193. $value = C::t($tablename)->fetch_by_id_idtype($id);
  194. if($value['uid'] != $_G['uid']) {
  195. $value = null;
  196. }
  197. }
  198. if(empty($value)) {
  199. showmessage('magicuse_bad_object');
  200. }
  201. return $value;
  202. }
  203. function magic_peroid($magic, $uid) {
  204. global $_G;
  205. if($magic['useperoid']) {
  206. $dateline = 0;
  207. if($magic['useperoid'] == 1) {
  208. $dateline = TIMESTAMP - (TIMESTAMP + $_G['setting']['timeoffset'] * 3600) % 86400 + $_G['setting']['timeoffset'] * 3600;
  209. } elseif($magic['useperoid'] == 4) {
  210. $dateline = TIMESTAMP - 86400;
  211. } elseif($magic['useperoid'] == 2) {
  212. $dateline = TIMESTAMP - 86400 * 7;
  213. } elseif($magic['useperoid'] == 3) {
  214. $dateline = TIMESTAMP - 86400 * 30;
  215. }
  216. $num = C::t('common_magiclog')->count_by_uid_magicid_action_dateline($uid, $magic['magicid'], 2, $dateline);
  217. return $magic['usenum'] - $num;
  218. } else {
  219. return true;
  220. }
  221. }
  222. ?>