Location: PHPKode > projects > Uchome and Discuz > source/cp_magic.php
<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: cp_task.php 11506 2009-03-06 09:19:17Z liguode $
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

$op = empty($_GET['op']) ? "view" : $_GET['op'];
$mid = empty($_GET['mid']) ? '' : trim($_GET['mid']);

if(!checkperm('allowmagic')) {
	ckspacelog();
	showmessage('magic_groupid_not_allowed');//ÄúËùÔÚµÄÓû§×é±»½ûֹʹÓõÀ¾ß
}

//»ñµÃµÀ¾ß
$magic = $mid ? magic_get($mid) : array();

//Ìá½»¹ºÂò
if (submitcheck("buysubmit")) {//¹ºÂò
	
	if(!$mid) {
		showmessage('unknown_magic');
	}

	//»ñµÃµÀ¾ßÐÅÏ¢
	$results = magic_buy_get($magic);
	extract($results);

	//¹ºÂòµÀ¾ß
	$charge = magic_buy_post($magic, $magicstore, $coupon);

	if($magic['experience']) {
		showmessage('magicbuy_success_with_experence', $_POST['refer'], 0, array($charge, $magic['experience'] * intval($_POST['buynum'])));
	} else {
		showmessage('magicbuy_success', $_POST['refer'], 0, array($charge));
	}

} elseif (submitcheck("presentsubmit")) {//ÔùËÍ
	
	if(!$mid) {
		showmessage('unknown_magic');
	}

	if($mid == 'license') {
		showmessage("magic_can_not_be_presented");//´ËµÀ¾ß²»ÄÜתÈÃ
	}

	//ºÃÓÑ
	$fuid = 0;
	$_POST['fusername'] = trim($_POST['fusername']);
	if(empty($_POST['fusername'])) {
		showmessage("bad_friend_username_given");//ºÃÓÑÃû×ÖÎÞЧ
	}
	$_POST['fusername'] = getstr($_POST['fusername'], 15);
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname("friend")." WHERE uid = '$_SGLOBAL[supe_uid]' AND fusername='$_POST[fusername]'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if(!$value) {
		showmessage("bad_friend_username_given");//ºÃÓÑÃû×ÖÎÞЧ
	}
	$fuid = $value['fuid'];
	$fusername = $value['fusername'];

	//ÔùÓèÕߵıØÐëÓµÓиõÀ¾ß¼°×ªÔö¿¨
	$usermagics = array();
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname("usermagic")." WHERE uid='$_SGLOBAL[supe_uid]' AND mid IN('license', '$mid')");
	while($value = $_SGLOBAL['db']->fetch_array($query)) {
		$usermagics[$value['mid']] = $value;
	}
	if(!$usermagics['license'] || !$usermagics['license']['count']) {
		showmessage('has_no_more_present_magic');
	}
	if(!$usermagics[$mid] || !$usermagics[$mid]['count']) {
		showmessage('has_no_more_magic', '', '',  array($magic['name'], 'a_buy_'.$mid, "cp.php?ac=magic&op=buy&mid=$mid"));
	}

	//ÔùÓèÕß
	$_SGLOBAL['db']->query('UPDATE '.tname('usermagic')." SET count = count - 1 WHERE uid = '$_SGLOBAL[supe_uid]' AND mid IN ('license', '$mid')");

	//ÊÜÔùÕß
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('usermagic')." WHERE uid='$fuid' AND mid='$mid'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	$count = $value ? $value['count'] + 1 : 1;
	inserttable('usermagic',
		array('uid'=>$fuid,
			'username'=>$fusername,
			'mid'=>$mid,
			'count'=>$count), 0, true);

	//ÊÕÈ루ÔùËÍ£©ÈÕÖ¾
	inserttable('magicinlog',
		array('uid'=>$fuid,
			'username'=>$fusername,
			'mid'=>$mid,
			'count'=>1,
			'type'=>2,
			'fromid'=>$_SGLOBAL['supe_uid'],
			'credit'=>0,
			'dateline'=>$_SGLOBAL['timestamp']));

	//֪ͨ±»ÔùËÍÕß
	notification_add($fuid, 'magic', cplang('magic_present_note', array($magic['name'], "cp.php?ac=magic&view=me&mid=$mid")));
	showmessage("magicpresent_success", $_POST['refer'], '', array($fusername));
}

if($op == 'buy') {//¹ºÂò

	$results = magic_buy_get($magic);
	extract($results);

} elseif ($op == "present") {//ÔùËÍ

	if($mid == 'license') {
		showmessage("magic_can_not_be_presented");//´ËµÀ¾ß²»ÄÜתÈÃ
	}

	//ÔùÓèÕߵıØÐëÓµÓиõÀ¾ß¼°×ªÔö¿¨
	$usermagics = array();
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('usermagic')." WHERE uid='$_SGLOBAL[supe_uid]' AND mid IN('license', '$mid')");
	while($value = $_SGLOBAL['db']->fetch_array($query)) {
		$usermagics[$value['mid']] = $value;
	}
	if(!$usermagics['license'] || !$usermagics['license']['count']) {
		showmessage('has_no_more_present_magic');
	}
	if(!$usermagics[$mid] || !$usermagics[$mid]['count']) {
		showmessage('has_no_more_magic', '', '',  array($magic['name'], 'a_buy_'.$mid, "cp.php?ac=magic&op=buy&mid=$mid"));
	}
} elseif($op == 'showusage') {
	//ÏÔʾʹÓðïÖúͼƬ

	if(!$mid) {
		showmessage('unknown_magic');
	}
	
} elseif($op == 'receive') {//ÁìÈ¡ºì°ü

	$uid = intval($_GET['uid']);
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('magicuselog')." WHERE uid='$uid' AND mid='gift' LIMIT 1");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if($value && $value['data']) {
		$data = unserialize($value['data']);
		if($data['left'] <= 0) {
			showmessage("magic_gift_already_given_out");//ºì°üÒѾ­±»ÁìÍêÁË
		}
		$data['receiver'] = is_array($data['receiver']) ? $data['receiver'] : array();
		if(in_array($_SGLOBAL['supe_uid'], $data['receiver'])) {
			showmessage("magic_had_got_gift");//ÄúÒѾ­ÁìÈ¡¹ý´Ë´Îºì°üÁË
		}
		$credit = $data['left'] > $data['chunk'] ? $data['chunk'] : $data['left'];
		$data['receiver'][] = $_SGLOBAL['supe_uid'];
		$data['left'] = $data['left'] - $credit;
		if($data['left'] > 0) {
			updatetable('magicuselog', array('data'=>serialize($data)), array('logid'=>$value['logid']));
		} else {
			$_SGLOBAL['db']->query('DELETE FROM '.tname('magicuselog')." WHERE logid = '$value[logid]'");
		}
		$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit = credit + '$credit' WHERE uid='$_SGLOBAL[supe_uid]'");
		showmessage('magic_got_gift', '', '', array($credit));//ÄúÒѾ­ÁìÈ¡µ½ºì°üÁË£º»ñµÃ x »ý·Ö
	} else {
		showmessage('magic_has_no_gift');//¿Õ¼äÖ÷ÈËûÓÐÉèÖúì°ü
	}

} elseif($op == 'appear') {
	//È¡ÏûÒþÉí²ÝЧ¹û

	if(!$_SGLOBAL['session']['magichidden']) {
		showmessage('magic_not_hidden_yet');
	}

	if(submitcheck('appearsubmit')) {
		updatetable('session', array('magichidden'=>'0'), array('uid'=>$_SGLOBAL['supe_uid']));
		updatetable('magicuselog', array('expire'=>$_SGLOBAL['timestamp']), array('uid'=>$_SGLOBAL['supe_uid'], 'mid'=>'invisible'));
		showmessage('do_success', $_POST['refer'], 0);
	}

} elseif($op == 'retrieve') {
	//»ØÊÕºì°ü¿¨

	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('magicuselog')." WHERE uid = '$_SGLOBAL[supe_uid]' AND mid = 'gift'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	
	$leftcredit = 0;
	if(!$value) {
		showmessage('not_set_gift');//Äúµ±Ç°Ã»ÓÐÉèÖúì°ü
	} elseif($value['data']) {
		$data = unserialize($value['data']);
		$leftcredit = intval($data['left']);
	}

	if(submitcheck('retrievesubmit')) {
		$_SGLOBAL['db']->query('DELETE FROM '.tname('magicuselog')." WHERE uid = '$_SGLOBAL[supe_uid]' AND mid = 'gift'");
		$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit = credit + $leftcredit WHERE uid = '$_SGLOBAL[supe_uid]'");
		showmessage('do_success', $_POST['refer'], 0);
	}
	
} elseif($op == 'cancelsuperstar') {//È¡Ïû³¬¼¶Ã÷ÐÇ
	
	$mid = 'superstar';
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('spacefield')." WHERE uid = '$_SGLOBAL[supe_uid]'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if(!$value || !$value['magicstar']) {
		showmessage('not_superstar_yet');
	}
	
	if(submitcheck('cancelsubmit')) {
		updatetable('spacefield', array('magicstar'=>0), array('uid'=>$_SGLOBAL['supe_uid']));
		updatetable('magicuselog', array('expire'=>$_SGLOBAL['timestamp']), array('uid'=>$_SGLOBAL['supe_uid'], 'mid'=>'superstar'));
		showmessage('do_success', $_POST['refer'], 0);
	}
	
} elseif($op == 'cancelflicker') {//È¡Ïû²ÊºçìÅ
	
	$mid = 'flicker';
	$_GET['idtype'] = 'cid';
	$_GET['id'] = intval($_GET['id']);
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('comment')." WHERE cid = '$_GET[id]' AND authorid = '$_SGLOBAL[supe_uid]'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if(!$value || !$value['magicflicker']) {
		showmessage('no_flicker_yet');
	}
	
	if(submitcheck('cancelsubmit')) {
		updatetable('comment', array('magicflicker'=>0), array('cid'=>$_GET['id'], 'authorid'=>$_SGLOBAL['supe_uid']));
		showmessage('do_success', $_POST['refer'], 0);
	}
	
} elseif($op == 'cancelcolor') {//È¡Ïû²ÊÉ«µÆ
	
	$mid = 'color';
	$_GET['id'] = intval($_GET['id']);
	//idtypeµ½º¬ÓÐmagiccolor×ֶεıíÓ³Éä
	$mapping = array('blogid'=>'blogfield', 'tid'=>'thread');
	$tablename = $mapping[$_GET['idtype']];
	if(empty($tablename)) {
		showmessage('no_color_yet');
	}
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname($tablename)." WHERE $_GET[idtype] = '$_GET[id]' AND uid = '$_SGLOBAL[supe_uid]'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if(!$value || !$value['magiccolor']) {
		showmessage('no_color_yet');
	}
	
	if(submitcheck('cancelsubmit')) {
		updatetable($tablename, array('magiccolor'=>0), array($_GET['idtype']=>$_GET[id]));
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('feed')." WHERE id = '$_GET[id]' AND idtype = '$_GET[idtype]'");
		$feed = $_SGLOBAL['db']->fetch_array($query);
		if($feed) {
			$feed['body_data'] = unserialize($feed['body_data']);
			if($feed['body_data']['magic_color']) {
				unset($feed['body_data']['magic_color']);
			}
			$feed['body_data'] = serialize($feed['body_data']);
			updatetable('feed', array('body_data'=>$feed['body_data']), array('feedid'=>$feed['feedid']));
		}
		showmessage('do_success', $_POST['refer'], 0);
	}
	
} elseif($op == 'cancelframe') {//È¡ÏûÏà¿ò
	
	$mid = 'frame';
	$_GET['idtype'] = 'picid';
	$_GET['id'] = intval($_GET['id']);
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('pic')." WHERE picid = '$_GET[id]' AND uid = '$_SGLOBAL[supe_uid]'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if(!$value || !$value['magicframe']) {
		showmessage('no_frame_yet');
	}
	
	if(submitcheck('cancelsubmit')) {
		updatetable('pic', array('magicframe'=>0), array('picid'=>$_GET['id']));
		showmessage('do_success', $_POST['refer'], 0);
	}

} elseif($op == 'cancelbgimage') {//È¡ÏûÐÅÖ½

	$mid = 'bgimage';
	$_GET['idtype'] = 'blogid';
	$_GET['id'] = intval($_GET['id']);
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('blogfield')." WHERE blogid = '$_GET[id]' AND uid = '$_SGLOBAL[supe_uid]'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	if(!$value || !$value['magicpaper']) {
		showmessage('no_bgimage_yet');
	}
	
	if(submitcheck('cancelsubmit')) {
		updatetable('blogfield', array('magicpaper'=>0), array('blogid'=>$_GET['id']));
		showmessage('do_success', $_POST['refer'], 0);
	}
	
} else {//ä¯ÀÀ

	if($_GET['view'] == 'me') {//ÎҵĵÀ¾ß

		//ÓµÓеĵÀ¾ß
		$types['list'] = ' class="active"';
		$list = $ids = $magics = array();
		if($mid) {
			$magics[$mid] = $magic;
			$ids[] = $mid;
		} else {
			//ÏÔʾȫ²¿
			$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('magic')." WHERE close = '0'");
			while($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['forbiddengid'] = explode(',', $value['forbiddengid']);
				$magics[$value['mid']] = $value;
				$ids[] = $value['mid'];
			}
		}
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('usermagic')." WHERE uid='$_SGLOBAL[supe_uid]' AND mid IN (".simplode($ids).") AND count > 0");
		while($value=$_SGLOBAL['db']->fetch_array($query)) {
			$list[$value['mid']] = $value;
		}

	} elseif($_GET['view'] == 'log') {//¼Ç¼

		$_GET['type'] = in_array($_GET['type'], array('in', 'out', 'present')) ? $_GET['type'] : 'in';
		$types = array($_GET['type']=>' class="active"');

		//·ÖÒ³
		$perpage = 20;
		$page = empty($_GET['page'])?0:intval($_GET['page']);
		if($page<1) $page = 1;
		$start = ($page-1)*$perpage;
		//¼ì²é¿ªÊ¼Êý
		ckstart($start, $perpage);

		$list = array();
		if($_GET['type'] == 'in') {
			//»ñµÃ¼Ç¼
			$uids = array();//ÏÔʾÔùËÍÕß
			$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('magicinlog')." WHERE uid = '$_SGLOBAL[supe_uid]'"), 0);
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('magicinlog')." WHERE uid = '$_SGLOBAL[supe_uid]' ORDER BY dateline DESC LIMIT $start, $perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					if($value['type'] == 2) {
						$uids[] = $value['fromid'];
					}
					$list[] = $value;
				}
			}
			if($uids) {
				$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('member').' WHERE uid IN ('.simplode($uids).')');
				while($value = $_SGLOBAL['db']->fetch_array($query)) {
					realname_set($value['uid'], $value['username']);
				}
				realname_get();
			}
		} elseif($_GET['type'] == 'present') {
			//ÔùËͼǼ
			$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('magicinlog')." WHERE type = 2 AND fromid = '$_SGLOBAL[supe_uid]'"), 0);
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('magicinlog')." WHERE type = 2 AND fromid = '$_SGLOBAL[supe_uid]' ORDER BY dateline DESC LIMIT $start, $perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					realname_set($value['uid'], $value['username']);
					$list[] = $value;
				}
			}
			realname_get();
		} else {
			//ʹÓüǼ
			$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('magicuselog')." WHERE uid = '$_SGLOBAL[supe_uid]'"), 0);
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('magicuselog')." WHERE uid = '$_SGLOBAL[supe_uid]' ORDER BY dateline DESC LIMIT $start, $perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$value['data'] = unserialize($value['data']);
					$list[] = $value;
				}
			}
		}

		//·ÖÒ³
		$theurl = 'cp.php?ac=magic&view=log&type='.$_GET['type'];
		$multi = multi($count, $perpage, $page, $theurl);

	} else {//µÀ¾ßÊг¡
		$_GET['view'] = 'store';

		//ÏÔʾ˳Ðò
		$_GET['order'] = $_GET['order'] == 'hot' ? 'hot' : 'default';
		$orders = array($_GET['order']=>' class="active"');

		$magics = $ids = $list = array();
		$blacklist = array('coupon');//µÀ¾ßÉ̵êÆÁ±ÎÏÔʾµÄµÀ¾ß
		if($mid) {
			//Ö»ÏÔʾµ¥¸ö
			$magics[$mid] = $magic;
			$ids[] = $mid;
		} else {
			//ÏÔʾȫ²¿
			$orderby = $_GET['order'] == 'hot' ? '' : 'ORDER BY displayorder';
			$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('magic')." $orderby");
			while($value = $_SGLOBAL['db']->fetch_array($query)) {
				if($value['close'] || in_array($value['mid'], $blacklist)) {
					continue;
				}
				$value['forbiddengid'] = explode(',', $value['forbiddengid']);
				$magics[$value['mid']] = $value;
				$ids[] = $value['mid'];
			}
		}

		if(empty($magics)) {
			showmessage('magic_store_is_closed');//µÀ¾ßÉ̵êÒѾ­¹Ø±Õ£¨Ã»ÓÐÈκεÀ¾ß¿ª·Å£©
		}

		$oldids = array();//ÒѾ­Â¼ÈëÉ̵êµÄµÀ¾ß
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('magicstore').' WHERE mid IN ('.simplode($ids).')');
		while($value = $_SGLOBAL['db']->fetch_array($query)) {
			$list[$value['mid']] = $value;
			$oldids[] = $value['mid'];
			//¸üпâ´æÊý
			if($value['storage'] < $magics[$value['mid']]['providecount'] &&
				$value['lastprovide'] + $magics[$value['mid']]['provideperoid'] < $_SGLOBAL['timestamp']) {

				$_SGLOBAL['db']->query('UPDATE '.tname('magicstore')." SET storage = '{$magics[$value[mid]][providecount]}', lastprovide = '$_SGLOBAL[timestamp]' WHERE mid = '$value[mid]'");
				$list[$value['mid']]['storage'] = $magics[$value['mid']]['providecount'];
			}
		}

		$newids = array_diff($ids, $oldids);//»¹Î´Â¼ÈëÉ̵êµÄµÀ¾ß
		if($newids) {
			$inserts = array();
			foreach ($newids as $id) {
				$inserts[] = "('$id', '{$magics[$id][providecount]}', '$_SGLOBAL[timestamp]')";
				$list[$id] = array("mid"=>$id,
								'storage'=>$magics[$id]['providecount'],
								'lastprovide'=>$_SGLOBAL['timestamp']);
			}
			$_SGLOBAL['db']->query('INSERT INTO '.tname('magicstore').'(mid, storage, lastprovide) VALUES '.implode(',',$inserts));
		}

		//ÅÅÐò
		if($_GET['order'] == 'hot') {
			//°´ÊÛ³öÊýÅÅÐò
			function hotsort($a, $b) {
				return ($a['sellcount'] > $b['sellcount']) ? -1 : ($a['sellcount'] < $b['sellcount']);
			}
			usort($list, 'hotsort');
			$order = array();
			foreach ($list as $value) {
				$order[$value['mid']] = $value;
			}
			$list = $order;
			unset($order);
		} else {
			//ĬÈÏ
			$order = array();
			foreach ($ids as $id) {
				$order[$id] = $list[$id];
			}
			$list = $order;
			unset($order);
		}
	}

	$actives = array($_GET['view']=>' class="active"');
}

include_once template('cp_magic');

?>
Return current item: Uchome and Discuz