class_credit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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: class_credit.php 32967 2013-03-28 10:57:48Z zhengqingpeng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class credit {
  12. var $checklowerlimit = true;
  13. var $coef = 1;
  14. var $extrasql = array();
  15. function credit() {}
  16. function &instance() {
  17. static $object;
  18. if(empty($object)) {
  19. $object = new credit();
  20. }
  21. return $object;
  22. }
  23. function execrule($action, $uid = 0, $needle = '', $coef = 1, $update = 1, $fid = 0) {
  24. global $_G;
  25. $this->coef = $coef;
  26. $uid = intval($uid ? $uid : $_G['uid']);
  27. $fid = $fid ? $fid : (isset($_G['fid']) && $_G['fid'] ? $_G['fid'] : 0);
  28. $rule = $this->getrule($action, $fid);
  29. $updatecredit = false;
  30. $enabled = false;
  31. if($rule) {
  32. for ($i = 1; $i<=8; $i++) {
  33. if(!empty($rule['extcredits'.$i])) {
  34. $enabled = true; break;
  35. }
  36. }
  37. }
  38. if($enabled) {
  39. $rulelog = array();
  40. $fids = $rule['fids'] ? explode(',', $rule['fids']) : array();
  41. $fid = in_array($fid, $fids) ? $fid : 0;
  42. $rulelog = $this->getrulelog($rule['rid'], $uid, $fid);
  43. if($rulelog && $rule['norepeat']) {
  44. $rulelog = array_merge($rulelog, $this->getchecklogbyclid($rulelog['clid'], $uid));
  45. $rulelog['norepeat'] = $rule['norepeat'];
  46. }
  47. if($rule['rewardnum'] && $rule['rewardnum'] < $coef) {
  48. $coef = $rule['rewardnum'];
  49. }
  50. if(empty($rulelog)) {
  51. $logarr = array(
  52. 'uid' => $uid,
  53. 'rid' => $rule['rid'],
  54. 'fid' => $fid,
  55. 'total' => $coef,
  56. 'cyclenum' => $coef,
  57. 'dateline' => $_G['timestamp']
  58. );
  59. if(in_array($rule['cycletype'], array(2,3))) {
  60. $logarr['starttime'] = $_G['timestamp'];
  61. }
  62. $logarr = $this->addlogarr($logarr, $rule, false);
  63. if($update) {
  64. $clid = C::t('common_credit_rule_log')->insert($logarr, 1);
  65. if($rule['norepeat']) {
  66. $rulelog['isnew'] = 1;
  67. $rulelog['clid'] = $clid;
  68. $rulelog['uid'] = $uid;
  69. $rulelog['norepeat'] = $rule['norepeat'];
  70. $this->updatecheating($rulelog, $needle, true);
  71. }
  72. }
  73. $updatecredit = true;
  74. } else {
  75. $newcycle = false;
  76. $logarr = array();
  77. switch($rule['cycletype']) {
  78. case 0:
  79. break;
  80. case 1:
  81. case 4:
  82. if($rule['cycletype'] == 1) {
  83. $today = strtotime(dgmdate($_G['timestamp'], 'Y-m-d'));
  84. if($rulelog['dateline'] < $today && $rule['rewardnum']) {
  85. $rulelog['cyclenum'] = 0;
  86. $newcycle = true;
  87. }
  88. }
  89. if(empty($rule['rewardnum']) || $rulelog['cyclenum'] < $rule['rewardnum']) {
  90. if($rule['norepeat']) {
  91. $repeat = $this->checkcheating($rulelog, $needle, $rule['norepeat']);
  92. if($repeat && !$newcycle) {
  93. return false;
  94. }
  95. }
  96. if($rule['rewardnum']) {
  97. $remain = $rule['rewardnum'] - $rulelog['cyclenum'];
  98. if($remain < $coef) {
  99. $coef = $remain;
  100. }
  101. }
  102. $cyclenunm = $newcycle ? $coef : "cyclenum+'$coef'";
  103. $logarr = array(
  104. 'cyclenum' => "cyclenum=$cyclenunm",
  105. 'total' => "total=total+'$coef'",
  106. 'dateline' => "dateline='$_G[timestamp]'"
  107. );
  108. $updatecredit = true;
  109. }
  110. break;
  111. case 2:
  112. case 3:
  113. $nextcycle = 0;
  114. if($rulelog['starttime']) {
  115. if($rule['cycletype'] == 2) {
  116. $start = strtotime(dgmdate($rulelog['starttime'], 'Y-m-d H:00:00'));
  117. $nextcycle = $start+$rule['cycletime']*3600;
  118. } else {
  119. $nextcycle = $rulelog['starttime']+$rule['cycletime']*60;
  120. }
  121. }
  122. if($_G['timestamp'] <= $nextcycle && $rulelog['cyclenum'] < $rule['rewardnum']) {
  123. if($rule['norepeat']) {
  124. $repeat = $this->checkcheating($rulelog, $needle, $rule['norepeat']);
  125. if($repeat && !$newcycle) {
  126. return false;
  127. }
  128. }
  129. if($rule['rewardnum']) {
  130. $remain = $rule['rewardnum'] - $rulelog['cyclenum'];
  131. if($remain < $coef) {
  132. $coef = $remain;
  133. }
  134. }
  135. $logarr = array(
  136. 'cyclenum' => "cyclenum=cyclenum+'$coef'",
  137. 'total' => "total=total+'$coef'",
  138. 'dateline' => "dateline='$_G[timestamp]'"
  139. );
  140. $updatecredit = true;
  141. } elseif($_G['timestamp'] >= $nextcycle) {
  142. $newcycle = true;
  143. $logarr = array(
  144. 'cyclenum' => "cyclenum=$coef",
  145. 'total' => "total=total+'$coef'",
  146. 'dateline' => "dateline='$_G[timestamp]'",
  147. 'starttime' => "starttime='$_G[timestamp]'",
  148. );
  149. $updatecredit = true;
  150. }
  151. break;
  152. }
  153. if($update) {
  154. if($rule['norepeat'] && $needle) {
  155. $this->updatecheating($rulelog, $needle, $newcycle);
  156. }
  157. if($logarr) {
  158. $logarr = $this->addlogarr($logarr, $rule, true);
  159. C::t('common_credit_rule_log')->increase($rulelog['clid'], $logarr);
  160. }
  161. }
  162. }
  163. }
  164. if($update && ($updatecredit || $this->extrasql)) {
  165. if(!$updatecredit) {
  166. for($i = 1; $i <= 8; $i++) {
  167. if(isset($_G['setting']['extcredits'][$i])) {
  168. $rule['extcredits'.$i] = 0;
  169. }
  170. }
  171. }
  172. $this->updatecreditbyrule($rule, $uid, $coef, $fid);
  173. }
  174. $rule['updatecredit'] = $updatecredit;
  175. return $rule;
  176. }
  177. function lowerlimit($rule, $uid = 0, $coef = 1, $fid = 0) {
  178. global $_G;
  179. $uid = $uid ? $uid : intval($_G['uid']);
  180. if($this->checklowerlimit && $uid && $_G['setting']['creditspolicy']['lowerlimit']) {
  181. $member = C::t('common_member_count')->fetch($uid);
  182. $fid = $fid ? $fid : (isset($_G['fid']) && $_G['fid'] ? $_G['fid'] : 0);
  183. $rule = is_array($rule) ? $rule : $this->getrule($rule, $fid);
  184. for($i = 1; $i <= 8; $i++) {
  185. if($_G['setting']['extcredits'][$i] && $rule['extcredits'.$i]) {
  186. $limit = (float)$_G['setting']['creditspolicy']['lowerlimit'][$i];
  187. $extcredit = (float)$rule['extcredits'.$i] * $coef;
  188. if($extcredit < 0 && ($member['extcredits'.$i] + $extcredit) < $limit) {
  189. return $i;
  190. }
  191. }
  192. }
  193. }
  194. return true;
  195. }
  196. function updatecreditbyrule($rule, $uids = 0, $coef = 1, $fid = 0) {
  197. global $_G;
  198. $this->coef = intval($coef);
  199. $fid = $fid ? $fid : (isset($_G['fid']) && $_G['fid'] ? $_G['fid'] : 0);
  200. $uids = $uids ? $uids : intval($_G['uid']);
  201. $rule = is_array($rule) ? $rule : $this->getrule($rule, $fid);
  202. $creditarr = array();
  203. $updatecredit = false;
  204. for($i = 1; $i <= 8; $i++) {
  205. if(isset($_G['setting']['extcredits'][$i])) {
  206. $creditarr['extcredits'.$i] = intval($rule['extcredits'.$i]) * $this->coef;
  207. if(defined('IN_MOBILE') && $creditarr['extcredits'.$i] > 0) {
  208. $creditarr['extcredits'.$i] += $_G['setting']['creditspolicymobile'];
  209. }
  210. $updatecredit = true;
  211. }
  212. }
  213. if($updatecredit || $this->extrasql) {
  214. $this->updatemembercount($creditarr, $uids, is_array($uids) ? false : true, $this->coef > 0 ? urldecode($rule['rulenameuni']) : '');
  215. }
  216. }
  217. function updatemembercount($creditarr, $uids = 0, $checkgroup = true, $ruletxt = '') {
  218. global $_G;
  219. if(!$uids) $uids = intval($_G['uid']);
  220. $uids = is_array($uids) ? $uids : array($uids);
  221. if($uids && ($creditarr || $this->extrasql)) {
  222. if($this->extrasql) $creditarr = array_merge($creditarr, $this->extrasql);
  223. $sql = array();
  224. $allowkey = array('extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8', 'friends', 'posts', 'threads', 'oltime', 'digestposts', 'doings', 'blogs', 'albums', 'sharings', 'attachsize', 'views', 'todayattachs', 'todayattachsize');
  225. $creditnotice = $_G['setting']['creditnotice'] && $_G['uid'] && $uids == array($_G['uid']);
  226. if($creditnotice) {
  227. if(!isset($_G['cookiecredits'])) {
  228. $_G['cookiecredits'] = !empty($_COOKIE['creditnotice']) ? explode('D', $_COOKIE['creditnotice']) : array_fill(0, 9, 0);
  229. for($i = 1; $i <= 8; $i++) {
  230. $_G['cookiecreditsbase'][$i] = getuserprofile('extcredits'.$i);
  231. }
  232. }
  233. if($ruletxt) {
  234. $_G['cookiecreditsrule'][$ruletxt] = $ruletxt;
  235. }
  236. }
  237. foreach($creditarr as $key => $value) {
  238. if(!empty($key) && $value && in_array($key, $allowkey)) {
  239. $sql[$key] = $value;
  240. if($creditnotice && substr($key, 0, 10) == 'extcredits') {
  241. $i = substr($key, 10);
  242. $_G['cookiecredits'][$i] += $value;
  243. }
  244. }
  245. }
  246. if($creditnotice) {
  247. dsetcookie('creditnotice', implode('D', $_G['cookiecredits']).'D'.$_G['uid']);
  248. dsetcookie('creditbase', '0D'.implode('D', $_G['cookiecreditsbase']));
  249. if(!empty($_G['cookiecreditsrule'])) {
  250. dsetcookie('creditrule', strip_tags(implode("\t", $_G['cookiecreditsrule'])));
  251. }
  252. }
  253. if($sql) {
  254. C::t('common_member_count')->increase($uids, $sql);
  255. }
  256. if($checkgroup && count($uids) == 1) $this->checkusergroup($uids[0]);
  257. $this->extrasql = array();
  258. }
  259. }
  260. function countcredit($uid, $update = true) {
  261. global $_G;
  262. $credits = 0;
  263. if($uid && !empty($_G['setting']['creditsformula'])) {
  264. $member = C::t('common_member_count')->fetch($uid);
  265. eval("\$credits = round(".$_G['setting']['creditsformula'].");");
  266. if($uid == $_G['uid']) {
  267. if($update && $_G['member']['credits'] != $credits) {
  268. C::t('common_member')->update_credits($uid, $credits);
  269. $_G['member']['credits'] = $credits;
  270. }
  271. } elseif($update) {
  272. C::t('common_member')->update_credits($uid, $credits);
  273. }
  274. }
  275. return $credits;
  276. }
  277. function checkusergroup($uid) {
  278. global $_G;
  279. $uid = intval($uid ? $uid : $_G['uid']);
  280. $groupid = 0;
  281. if(!$uid) return $groupid;
  282. if($uid != $_G['uid']) {
  283. $member = getuserbyuid($uid);
  284. } else {
  285. $member = $_G['member'];
  286. }
  287. if(empty($member)) return $groupid;
  288. $credits = $this->countcredit($uid, false);
  289. $updatearray = array();
  290. $groupid = $member['groupid'];
  291. $group = C::t('common_usergroup')->fetch($member['groupid']);
  292. if($member['credits'] != $credits) {
  293. $updatearray['credits'] = $credits;
  294. $member['credits'] = $credits;
  295. }
  296. $member['credits'] = $member['credits'] == '' ? 0 : $member['credits'] ;
  297. $sendnotify = false;
  298. if(empty($group) || $group['type'] == 'member' && !($member['credits'] >= $group['creditshigher'] && $member['credits'] < $group['creditslower'])) {
  299. $newgroup = C::t('common_usergroup')->fetch_by_credits($member['credits']);
  300. if(!empty($newgroup)) {
  301. if($member['groupid'] != $newgroup['groupid']) {
  302. $updatearray['groupid'] = $groupid = $newgroup['groupid'];
  303. $sendnotify = true;
  304. }
  305. }
  306. }
  307. if($updatearray) {
  308. C::t('common_member')->update($uid, $updatearray);
  309. }
  310. if($sendnotify) {
  311. notification_add($uid, 'system', 'user_usergroup', array('usergroup' => '<a href="home.php?mod=spacecp&ac=credit&op=usergroup">'.$newgroup['grouptitle'].'</a>', 'from_id' => 0, 'from_idtype' => 'changeusergroup'), 1);
  312. }
  313. return $groupid;
  314. }
  315. function deletelogbyfid($rid, $fid) {
  316. $fid = intval($fid);
  317. if($rid && $fid) {
  318. $lids = C::t('common_credit_rule_log')->fetch_ids_by_rid_fid($rid, $fid);
  319. if($lids) {
  320. C::t('common_credit_rule_log')->delete($lids);
  321. C::t('common_credit_rule_log_field')->delete_clid($lids);
  322. }
  323. }
  324. }
  325. function updatecheating($rulelog, $needle, $newcycle) {
  326. if($needle) {
  327. $logarr = array();
  328. switch($rulelog['norepeat']) {
  329. case 0:
  330. break;
  331. case 1:
  332. $info = empty($rulelog['info'])||$newcycle ? $needle : $rulelog['info'].','.$needle;
  333. $logarr['info'] = addslashes($info);
  334. break;
  335. case 2:
  336. $user = empty($rulelog['user'])||$newcycle ? $needle : $rulelog['user'].','.$needle;
  337. $logarr['user'] = addslashes($user);
  338. break;
  339. case 3:
  340. $app = empty($rulelog['app'])||$newcycle ? $needle : $rulelog['app'].','.$needle;
  341. $logarr['app'] = addslashes($app);
  342. break;
  343. }
  344. if($rulelog['isnew']) {
  345. $logarr['clid'] = $rulelog['clid'];
  346. $logarr['uid'] = $rulelog['uid'];
  347. C::t('common_credit_rule_log_field')->insert($logarr);
  348. } elseif($logarr) {
  349. C::t('common_credit_rule_log_field')->update($rulelog['uid'], $rulelog['clid'],$logarr);
  350. }
  351. }
  352. }
  353. function addlogarr($logarr, $rule, $issql = 0) {
  354. global $_G;
  355. for($i = 1; $i <= 8; $i++) {
  356. if($_G['setting']['extcredits'][$i]) {
  357. $extcredit = intval($rule['extcredits'.$i]) * $this->coef;
  358. if($issql) {
  359. $logarr['extcredits'.$i] = 'extcredits'.$i."='$extcredit'";
  360. } else {
  361. $logarr['extcredits'.$i] = $extcredit;
  362. }
  363. }
  364. }
  365. return $logarr;
  366. }
  367. function getrule($action, $fid = 0) {
  368. global $_G;
  369. if(empty($action)) {
  370. return false;
  371. }
  372. $fid = $fid ? $fid : (isset($_G['fid']) && $_G['fid'] ? $_G['fid'] : 0);
  373. if($_G['forum'] && $_G['forum']['status'] == 3) {
  374. $group_creditspolicy = $_G['grouplevels'][$_G['forum']['level']]['creditspolicy'];
  375. if(is_array($group_creditspolicy) && empty($group_creditspolicy[$action])) {
  376. return false;
  377. } else {
  378. $fid = 0;
  379. }
  380. }
  381. loadcache('creditrule');
  382. $rule = false;
  383. if(is_array($_G['cache']['creditrule'][$action])) {
  384. $rule = $_G['cache']['creditrule'][$action];
  385. $rulenameuni = $rule['rulenameuni'];
  386. if($rule['fids'] && $fid) {
  387. $fid = intval($fid);
  388. $fids = explode(',', $rule['fids']);
  389. if(in_array($fid, $fids)) {
  390. $forumfield = C::t('forum_forumfield')->fetch($fid);
  391. $policy = dunserialize($forumfield['creditspolicy']);
  392. if(isset($policy[$action])) {
  393. $rule = $policy[$action];
  394. $rule['rulenameuni'] = $rulenameuni;
  395. }
  396. }
  397. }
  398. for($i = 1; $i <= 8; $i++) {
  399. if(empty($_G['setting']['extcredits'][$i])) {
  400. unset($rule['extcredits'.$i]);
  401. continue;
  402. }
  403. $rule['extcredits'.$i] = intval($rule['extcredits'.$i]);
  404. }
  405. }
  406. return $rule;
  407. }
  408. function getrulelog($rid, $uid = 0, $fid = 0) {
  409. global $_G;
  410. $log = array();
  411. $uid = $uid ? $uid : $_G['uid'];
  412. if($rid && $uid) {
  413. $log = C::t('common_credit_rule_log')->fetch_rule_log($rid, $uid, $fid);
  414. }
  415. return $log;
  416. }
  417. function checkcheating($rulelog, $needle, $checktype) {
  418. $repeat = false;
  419. switch($checktype) {
  420. case 0:
  421. break;
  422. case 1:
  423. $infoarr = explode(',', $rulelog['info']);
  424. if(!empty($rulelog['info']) && in_array($needle, $infoarr)) {
  425. $repeat = true;
  426. }
  427. break;
  428. case 2:
  429. $userarr = explode(',', $rulelog['user']);
  430. if(!empty($rulelog['user']) && in_array($needle, $userarr)) {
  431. $repeat = true;
  432. }
  433. break;
  434. case 3:
  435. $apparr = explode(',', $rulelog['app']);
  436. if(!empty($rulelog['app']) && in_array($needle, $apparr)) {
  437. $repeat = true;
  438. }
  439. break;
  440. }
  441. return $repeat;
  442. }
  443. function getchecklogbyclid($clid, $uid = 0) {
  444. global $_G;
  445. $uid = $uid ? $uid : $_G['uid'];
  446. return C::t('common_credit_rule_log_field')->fetch($uid, $clid);
  447. }
  448. }
  449. ?>