<?php
!function_exists('readover') && exit('Forbidden');

/**
 *
 * PHPWind 积分操作统一入口
 * author:sky_hold@163.com
 *
 */
Class PwCredit {

	var $cType = array();
	var $cUnit = array();
	var $cUser = array();
	var $Field = array();
	var $cLog  = array();

	function PwCredit() {
		$this->cType = array(
			'money'		=> $GLOBALS['db_moneyname'],
			'rvrc'		=> $GLOBALS['db_rvrcname'],
			'credit'	=> $GLOBALS['db_creditname'],
			'currency'	=> $GLOBALS['db_currencyname']
		);
		$this->cUnit = array(
			'money'		=> $GLOBALS['db_moneyunit'],
			'rvrc'		=> $GLOBALS['db_rvrcunit'],
			'credit'	=> $GLOBALS['db_creditunit'],
			'currency'	=> $GLOBALS['db_currencyunit']
		);
		foreach ($GLOBALS['_CREDITDB'] as $key => $value) {
			$this->cType[$key] = $value[0];
			$this->cUnit[$key] = $value[1];
		}
		$this->Field = array('postnum','digests');
	}

	function creditset($f_set,$d_set) {
		$f_set = unserialize($f_set);
		$d_set = unserialize($d_set);

		foreach ($d_set as $key => $value) {
			foreach ($value as $k => $v) {
				isset($f_set[$key][$k]) && $f_set[$key][$k] !== '' && $v = $f_set[$key][$k];
				if (!in_array($key,array('Digest','Post','Reply'))) {
					$v = -$v;
				}
				$d_set[$key][$k] = $v;
			}
		}
		return $d_set;
	}

	function check($t) {
		return (isset($this->cType[$t]) || in_array($t,$this->Field)) ? true : false;
	}

	function setMdata($uid,$field,$point) {
		if ($this->check($field)) {
			$this->cUser[$uid][$field] += $point;
		}
	}

	function get($uid,$cType = 'ALL') {
		global $db;
		$getv = false;
		if (isset($this->cType[$cType])) {
			if (is_numeric($cType)) {
				$getv = $db->get_value('SELECT value FROM pw_membercredit WHERE uid='.pwEscape($uid).'AND cid='.pwEscape($cType));
				empty($getv) && $getv = 0;
			} else {
				$getv = $db->get_value("SELECT $cType FROM pw_memberdata WHERE uid=".pwEscape($uid));
				$cType == 'rvrc' && $getv = intval($getv/10);
			}
		}
		if (in_array($cType,array('ALL','COMMON','CUSTOM'))) {
			$getv = array();
			if ($cType == 'COMMON' || $cType == 'ALL') {
				$getv = $db->get_one("SELECT money,FLOOR(rvrc/10) AS rvrc,credit,currency FROM pw_memberdata WHERE uid=".pwEscape($uid));
			}
			if ($GLOBALS['_CREDITDB'] && ($cType == 'CUSTOM' || $cType == 'ALL')) {
				$query = $db->query("SELECT cid,value FROM pw_membercredit WHERE uid=".pwEscape($uid));
				while ($rt = $db->fetch_array($query)) {
					$getv[$rt['cid']] = $rt['value'];
				}
			}
		}
		return $getv;
	}

	function set($uid,$cType,$point,$operate = true) {
		if (!isset($this->cType[$cType])) return false;
		if (empty($point)) return true;

		if ($operate) {
			if (is_numeric($cType)) {
				$GLOBALS['db']->pw_update(
					"SELECT value FROM pw_membercredit WHERE uid=".pwEscape($uid).' AND cid='.pwEscape($cType),
					"UPDATE pw_membercredit SET value=value+".pwEscape($point)."WHERE uid=".pwEscape($uid).' AND cid='.pwEscape($cType),
					"INSERT INTO pw_membercredit SET ".pwSqlSingle(array('uid'=>$uid,'cid'=>$cType,'value'=>$point))
				);
			} else {
				$cType == 'rvrc' && $point *= 10;
				$GLOBALS['db']->update("UPDATE pw_memberdata SET $cType=$cType+'$point' WHERE uid='$uid'",0);
			}
			$this->writeLog();
		} else {
			$this->cUser[$uid][$cType] += $point;
		}
		return true;
	}

	function sets($uid,$setv,$operate = true) {
		if (empty($setv) || !is_array($setv)) {
			return false;
		}
		$retv = '';
		foreach ($setv as $key => $value) {
			if (isset($this->cType[$key]) && is_numeric($value) && $value <> 0) {
				if ($operate) {
					if (is_numeric($key)) {
						$GLOBALS['db']->pw_update(
							"SELECT uid FROM pw_membercredit WHERE uid='$uid' AND cid='$key'",
							"UPDATE pw_membercredit SET value=value+'$value' WHERE uid='$uid' AND cid='$key'",
							"INSERT INTO pw_membercredit SET uid='$uid',cid='$key',value='$value'"
						);
					} else {
						$key == 'rvrc' && $value *= 10;
						$retv .= ($retv ? ',' : '')."$key=$key+'$value'";
					}
				} else {
					$this->cUser[$uid][$key] += $value;
				}
			}
		}
		if ($operate) {
			$retv && $GLOBALS['db']->update("UPDATE pw_memberdata SET $retv WHERE uid='$uid'",0);
			$this->writeLog();
		}
		return true;
	}

	function setus($u_array,$setv,$log = true) {
		global $db;
		if (empty($u_array) || !is_array($u_array) || empty($setv) || !is_array($setv)) {
			return false;
		}
		$uids	= pwImplode($u_array);
		$setsql = '';
		$cuskey = $setcus = $uccus = array();
		foreach ($setv as $key => $value) {
			if (isset($this->cType[$key]) && is_numeric($value) && $value <> 0) {
				if (is_numeric($key)) {
					$setcus[$key] = $value;
					$uccus[$key] = array();
					$cuskey[] = $key;
				} else {
					$key == 'rvrc' && $value *= 10;
					$setsql .= ($setsql ? ',' : '')."$key=$key+".pwEscape($value);
				}
			}
		}
		if ($setcus) {
			$cuskey = pwImplode($cuskey);
			$query = $db->query("SELECT uid,cid FROM pw_membercredit WHERE uid IN($uids) AND cid IN($cuskey)");
			while ($rt = $db->fetch_array($query)) {
				$uccus[$rt['cid']][] = $rt['uid'];
			}
			$cussql = array();

			foreach ($setcus as $key => $value) {
				$uid_insert	= array_diff($u_array,$uccus[$key]);
				$uidss = pwImplode($uccus[$key]);
				if ($uidss) {
					$db->update("UPDATE pw_membercredit SET value=value+".pwEscape($value).' WHERE cid='.pwEscape($key)."AND uid IN($uidss)",0);
				}
				if ($uid_insert) {
					foreach ($uid_insert as $k => $uid) {
						$cussql[] = array($uid,$key,$value);
					}
				}
			}
			if ($cussql) {
				$db->update("INSERT INTO pw_membercredit (uid,cid,value) VALUES ".pwSqlMulti($cussql));
			}
		}
		if ($setsql) {
			$db->update("UPDATE pw_memberdata SET $setsql WHERE uid IN($uids)",0);
		}
		$log && $this->writeLog();
	}

	function runsql($uid = null) {
		$retv = '';
		foreach ($this->cUser as $key => $value) {
			$sql = '';
			foreach ($value as $k => $v) {
				if ($this->check($k) && $v <> 0) {
					if (is_numeric($k)) {
						$GLOBALS['db']->pw_update(
							"SELECT uid FROM pw_membercredit WHERE uid=".pwEscape($key).'AND cid='.pwEscape($k),
							"UPDATE pw_membercredit SET value=value+".pwEscape($v).'WHERE uid='.pwEscape($key).'AND cid='.pwEscape($k),
							"INSERT INTO pw_membercredit SET ".pwSqlSingle(array('uid'=>$key,'cid'=>$k,'value'=>$v))
						);
					} else {
						$k == 'rvrc' && $v *= 10;
						$sql .= ($sql ? ',' : '')."$k=$k+".pwEscape($v);
					}
				}
			}
			if ($uid && $key == $uid) {
				$retv = $sql;
			} elseif ($sql) {
				$GLOBALS['db']->update("UPDATE pw_memberdata SET $sql WHERE uid=".pwEscape($key),0);
			}
		}
		$this->writeLog();
		$this->cUser = array();

		return $retv;
	}

	function addLog($logtype,$setv,$log) {
		global $db_creditlog,$timestamp;
		list($lgt) = explode('_',$logtype);

		foreach ($setv as $key => $affect) {
			if (isset($this->cType[$key]) && $affect<>0 && ($lgt == 'main' || isset($db_creditlog[$lgt][$key]))) {
				$log['username'] = Char_cv($log['username']);
				$log['cname']	 = $this->cType[$key];
				$log['affect']	 = $affect;
				$log['affect'] > 0 && $log['affect'] = '+'.$log['affect'];
				isset($log['descrip']) || $log['descrip'] = Char_cv(getLangInfo('creditlog',$logtype,$log));

				$this->cLog[] = array($log['uid'], $log['username'], $key, $affect, $timestamp, $logtype, $log['ip'], $log['descrip']);
			}
		}
	}

	function writeLog() {
		if (!empty($this->cLog)) {
			$GLOBALS['db']->update("INSERT INTO pw_creditlog (uid,username,ctype,affect,adddate,logtype,ip,descrip) VALUES ".pwSqlMulti($this->cLog,false));
		}
		$this->cLog = array();
	}
}

$credit = new PwCredit();

?>