PageRenderTime 34ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/require/credit.php

https://bitbucket.org/johnroyer/phpwind8.7
PHP | 348 lines | 245 code | 26 blank | 77 comment | 80 complexity | 728816d6c112bb4c4053ef7cbbf8f397 MD5 | raw file
  1. <?php
  2. !function_exists('readover') && exit('Forbidden');
  3. /**
  4. *
  5. * phpwind 積分操作統一入口
  6. * @author sky_hold@163.com
  7. * @package credit
  8. *
  9. */
  10. Class PwCredit {
  11. var $cType = array(); //積分名稱 array('money' => ??, 'rvrc' => ??, ...)
  12. var $cUnit = array(); //積分單位 array('money' => ??, 'rvrc' => ??, ...)
  13. var $Field = array();
  14. var $cLog = array();
  15. var $setUser = array();
  16. var $getUser = array();
  17. var $_logset = array();
  18. function PwCredit() {
  19. $this->cType = array(
  20. 'money' => $GLOBALS['db_moneyname'],
  21. 'rvrc' => $GLOBALS['db_rvrcname'],
  22. 'credit' => $GLOBALS['db_creditname'],
  23. 'currency' => $GLOBALS['db_currencyname']
  24. );
  25. $this->cUnit = array(
  26. 'money' => $GLOBALS['db_moneyunit'],
  27. 'rvrc' => $GLOBALS['db_rvrcunit'],
  28. 'credit' => $GLOBALS['db_creditunit'],
  29. 'currency' => $GLOBALS['db_currencyunit']
  30. );
  31. foreach ($GLOBALS['_CREDITDB'] as $key => $value) {
  32. $this->cType[$key] = $value[0];
  33. $this->cUnit[$key] = $value[1];
  34. }
  35. $this->Field = array('postnum', 'digests');
  36. $this->_logset = $GLOBALS['db_creditlog'];
  37. }
  38. /**
  39. * 獲取積分設置
  40. *
  41. * @param array $f_set 板塊積分設置
  42. * @param array $d_set 論壇積分設置
  43. * return array()
  44. */
  45. function creditset($f_set,$d_set) {
  46. if (!is_array($f_set)) $f_set = unserialize($f_set);
  47. if (!is_array($d_set)) $d_set = unserialize($d_set);
  48. foreach ($d_set as $key => $value) {
  49. foreach ($value as $k => $v) {
  50. isset($f_set[$key][$k]) && $f_set[$key][$k] !== '' && $v = $f_set[$key][$k];
  51. if (!in_array($key,array('Digest','Post','Reply'))) {
  52. $v = -$v;
  53. }
  54. $d_set[$key][$k] = $v;
  55. }
  56. }
  57. return $d_set;
  58. }
  59. function check($t) {
  60. return (isset($this->cType[$t]) || in_array($t,$this->Field)) ? true : false;
  61. }
  62. function setMdata($uid,$field,$point) {
  63. if ($this->check($field)) {
  64. $this->setUser[$uid][$field] += $point;
  65. }
  66. }
  67. /**
  68. * 獲取用戶積分
  69. *
  70. * @param int $uid 用戶UID
  71. * @param string $cType 獲取的積分類型
  72. * return mixed
  73. */
  74. function get($uid,$cType = 'ALL') {
  75. global $db;
  76. $getv = false;
  77. if (isset($this->cType[$cType])) {
  78. if (isset($this->getUser[$uid][$cType])) return $this->getUser[$uid][$cType];
  79. if (is_numeric($cType)) {
  80. if (perf::checkMemcache()){
  81. $_cacheService = Perf::gatherCache('pw_members');
  82. $userCredit = $_cacheService->getMemberCreditByUserIds(array($uid));
  83. $getv = $userCredit[$uid][$cType];
  84. } else {
  85. $getv = $db->get_value('SELECT value FROM pw_membercredit WHERE uid=' . S::sqlEscape($uid) . ' AND cid=' . S::sqlEscape($cType));
  86. }
  87. empty($getv) && $getv = 0;
  88. } else {
  89. $getv = $db->get_value("SELECT $cType FROM pw_memberdata WHERE uid=" . S::sqlEscape($uid));
  90. $cType == 'rvrc' && $getv = intval($getv/10);
  91. }
  92. $this->getUser[$uid][$cType] = $getv;
  93. }
  94. if (in_array($cType,array('ALL','COMMON','CUSTOM'))) {
  95. $getv = array();
  96. if ($cType != 'CUSTOM') {
  97. $getv = $db->get_one("SELECT money,FLOOR(rvrc/10) AS rvrc,credit,currency FROM pw_memberdata WHERE uid=" . S::sqlEscape($uid));
  98. }
  99. if ($GLOBALS['_CREDITDB'] && $cType != 'COMMON') {
  100. if (perf::checkMemcache()){
  101. $_cacheService = Perf::gatherCache('pw_members');
  102. $userCredit = $_cacheService->getMemberCreditByUserIds(array($uid));
  103. $getv = $userCredit[$uid];
  104. } else {
  105. $query = $db->query("SELECT cid,value FROM pw_membercredit WHERE uid=" . S::sqlEscape($uid));
  106. while ($rt = $db->fetch_array($query)) {
  107. $getv[$rt['cid']] = $rt['value'];
  108. }
  109. }
  110. }
  111. $this->getUser[$uid] = $getv;
  112. }
  113. return $getv;
  114. }
  115. /**
  116. * 設置用戶積分(+-)
  117. *
  118. * @param int $uid 用戶UID
  119. * @param string $cType 積分類型
  120. * @param int $point +-的值
  121. * @param bool $operate 是否實時進行數據庫操作
  122. * return bool
  123. */
  124. function set($uid, $cType, $point, $operate = true) {
  125. if (!isset($this->cType[$cType]) || empty($point)) {
  126. return false;
  127. }
  128. $arr = array(
  129. $uid => array($cType => $point)
  130. );
  131. if ($operate) {
  132. $this->runsql($arr);
  133. } else {
  134. $this->array_add($arr);
  135. }
  136. return true;
  137. }
  138. /**
  139. * 設置用戶多個積分(+-)
  140. *
  141. * @param int $uid 用戶UID
  142. * @param array $setv 積分值 array('money' => ??, 'rvrc' => ??, ...)
  143. * @param bool $operate 是否實時進行數據庫操作
  144. * return bool
  145. */
  146. function sets($uid, $setv, $operate = true) {
  147. if (empty($setv) || !is_array($setv)) {
  148. return false;
  149. }
  150. if ($operate) {
  151. $this->runsql(array($uid => $setv));
  152. } else {
  153. $this->array_add(array($uid => $setv));
  154. }
  155. return true;
  156. }
  157. /**
  158. * 設置多個用戶多個積分(+-)
  159. *
  160. * @param array $u_array 用戶UID array(1, 2, 3, ...)
  161. * @param array $setv 積分值 array('money' => ??, 'rvrc' => ??, ...)
  162. * @param bool $operate 是否實時進行數據庫操作
  163. * return bool
  164. */
  165. function setus($u_array, $setv, $operate = true) {
  166. if (empty($u_array) || !is_array($u_array) || empty($setv) || !is_array($setv)) {
  167. return false;
  168. }
  169. $arr = array();
  170. foreach ($u_array as $uid) {
  171. $arr[$uid] = $setv;
  172. }
  173. if ($operate) {
  174. $this->runsql($arr);
  175. } else {
  176. $this->array_add($arr);
  177. }
  178. return true;
  179. }
  180. /**
  181. * 對給定數據進行數據庫積分增減操作
  182. *
  183. * @param array $setArr 操作數據 array(1 => array('money' => ??, 'rvrc' => ??), 2 => array(), 3 => array(), ...)
  184. * @param bool $isAdd 是否實時進行數據庫操作
  185. */
  186. function runsql($setArr = null, $isAdd = true) {
  187. global $db,$uc_server,$uc_syncredit;
  188. $setUser = isset($setArr) ? $setArr : $this->setUser;
  189. $retv = array();
  190. if ($uc_server && $uc_syncredit) {
  191. require_once(R_P . 'uc_client/uc_client.php');
  192. $retv = uc_credit_add($setUser, $isAdd);
  193. }
  194. $cacheUids = $cacheCredits = array();
  195. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  196. foreach ($setUser as $uid => $setv) {
  197. $updateUser = $increaseUser = array();
  198. foreach ($setv as $cid => $v) {
  199. if ($this->check($cid) && ($v <> 0 || !$isAdd)) {
  200. if (isset($retv[$uid][$cid])) {
  201. if ($uc_server == 1) {
  202. continue;
  203. }
  204. $act = 'set';
  205. $v = $retv[$uid][$cid];
  206. } else {
  207. $act = $isAdd ? 'add' : 'set';
  208. }
  209. if (is_numeric($cid)) {
  210. $v = intval($v);
  211. /**
  212. $db->pw_update(
  213. "SELECT uid FROM pw_membercredit WHERE uid=" . S::sqlEscape($uid) . ' AND cid=' . S::sqlEscape($cid),
  214. "UPDATE pw_membercredit SET " . ($act == 'add' ? 'value=value+' : 'value=') . S::sqlEscape($v) . ' WHERE uid=' . S::sqlEscape($uid) . ' AND cid=' . S::sqlEscape($cid),
  215. "INSERT INTO pw_membercredit SET " . S::sqlSingle(array('uid' => $uid, 'cid' => $cid, 'value' => $v))
  216. );
  217. **/
  218. $db->pw_update(
  219. "SELECT uid FROM pw_membercredit WHERE uid=" . S::sqlEscape($uid) . ' AND cid=' . S::sqlEscape($cid),
  220. pwQuery::buildClause("UPDATE :pw_table SET " . ($act == 'add' ? 'value=value+' : 'value=') . ':value' . ' WHERE uid=:uid AND cid=:cid', array('pw_membercredit', $v, $uid, $cid)),
  221. pwQuery::insertClause('pw_membercredit', array('uid' => $uid, 'cid' => $cid, 'value' => $v))
  222. );
  223. } else {
  224. $cid == 'rvrc' && $v *= 10;
  225. if ($act == 'add') {
  226. $increaseUser[$cid] = intval($v);
  227. } else {
  228. $updateUser[$cid] = intval($v);
  229. }
  230. }
  231. }
  232. }
  233. if ($increaseUser) $userService->updateByIncrement($uid, array(), $increaseUser);
  234. if ($updateUser) $userService->update($uid, array(), $updateUser);
  235. unset($this->getUser[$uid]);
  236. $cacheUids[] = 'UID_'.$uid;
  237. $cacheCredits[] = 'UID_CREDIT_'.$uid;
  238. }
  239. // if ($cacheUids) {
  240. // $_cache = getDatastore();
  241. // $_cache->delete($cacheUids);
  242. // $_cache->delete($cacheCredits);/*積分*/
  243. // }
  244. $this->writeLog();
  245. !isset($setArr) && $this->setUser = array();
  246. }
  247. /**
  248. * 追加積分日誌設置
  249. */
  250. function appendLogSet($logset, $type = null) {
  251. if (empty($logset) || !is_array($logset)) {
  252. return false;
  253. }
  254. foreach ($logset as $key => $value) {
  255. $type && $key = $type . '_' . $key;
  256. if (!isset($this->_logset[$key])) {
  257. $this->_logset[$key] = $value;
  258. }
  259. }
  260. }
  261. /**
  262. * 驗證積分日誌是否開啟
  263. */
  264. function _checkLogSet($logtype, $creditName) {
  265. if (isset($this->_logset[$logtype][$creditName])) {
  266. return true;
  267. }
  268. list($lgt) = explode('_', $logtype);
  269. return ($lgt == 'main' || isset($this->_logset[$lgt][$creditName]));
  270. }
  271. /**
  272. * 記錄積分日誌
  273. *
  274. * @param string $logtype 日誌類型
  275. * @param array $setv 積分值 array('money' => ??, 'rvrc' => ??, ...)
  276. * @param array $log 日誌信息描述
  277. */
  278. function addLog($logtype, $setv, $log) {
  279. global $db_ifcredit,$timestamp;
  280. $credit_pop = '';
  281. $uid = $log['uid'];
  282. foreach ($setv as $key => $affect) {
  283. if (isset($this->cType[$key]) && $affect<>0 && $this->_checkLogSet($logtype, $key)) {
  284. $log['username'] = S::escapeChar($log['username']);
  285. $log['cname'] = $this->cType[$key];
  286. $log['affect'] = $affect;
  287. $log['affect'] > 0 && $log['affect'] = '+'.$log['affect'];
  288. $log['descrip'] = S::escapeChar(strip_tags(getLangInfo('creditlog',$logtype,$log)));
  289. $credit_pop .= $key.":".$log['affect'].'|';
  290. $this->cLog[] = array($log['uid'], $log['username'], $key, $affect, $timestamp, $logtype, $log['ip'], $log['descrip']);
  291. }
  292. }
  293. if ($db_ifcredit && $credit_pop) {//Credit Changes Tips
  294. $credit_pop = $logtype.'|'.$credit_pop;
  295. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  296. $userService->update($uid, array(), array('creditpop' => $credit_pop));
  297. }
  298. }
  299. function writeLog() {
  300. if (!empty($this->cLog)) {
  301. $GLOBALS['db']->update("INSERT INTO pw_creditlog (uid,username,ctype,affect,adddate,logtype,ip,descrip) VALUES ".S::sqlMulti($this->cLog,false));
  302. }
  303. $this->cLog = array();
  304. }
  305. function array_add($u_a) {
  306. if (empty($u_a)) return false;
  307. foreach ($u_a as $uid => $setv) {
  308. foreach ($setv as $key => $value) {
  309. if (isset($this->cType[$key]) && is_numeric($value) && $value <> 0) {
  310. $this->setUser[$uid][$key] += $value;
  311. isset($this->getUser[$uid][$key]) && $this->getUser[$uid][$key] += $value;
  312. }
  313. }
  314. }
  315. }
  316. function getCreditTypeByName($cName) {
  317. if (isset($this->cType[$cName])) return $cName;
  318. return array_search($cName, $this->cType);
  319. }
  320. }
  321. $credit = new PwCredit();
  322. ?>