<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	系统调用到的一些操作

	$RCSfile: batch.common.php,v $
	$Revision: 1.60 $
	$Date: 2006/10/31 04:58:44 $
*/

include_once('./include/main.inc.php');
include_once(S_ROOT.'./language/batch.lang.php');

$action = empty($_GET['action'])?'':$_GET['action'];

if($action == 'rightclick') {
	
	print<<<END
	<script language="JavaScript">
	if (external.menuArguments) {
		var parentwin = external.menuArguments;
		if (parentwin.document.selection) {
			var sel = parentwin.document.selection.createRange().text;
		}
		if (!sel) {
			var sel = '';
		}
		var url = parentwin.location.href;
		var title = parentwin.document.title;
		var charset = document.charset;
		if (parentwin.event.srcElement.tagName == "A") {
			url = parentwin.event.srcElement.getAttribute("HREF");
			title = parentwin.event.srcElement.innerText;
		}
		void(window.open('$siteurl/spacecp.php?action=spacelinks&op=add&openwindow=1&charset='+charset+'&title='+title+'&url='+url+'&message='+sel,'_blank','height=660,width=800,left=0,top=0,resizable=1,scrollbars=1'));
	} else {
		history.go(-1);
	}
	</script>
END;
	break;
	
} elseif($action == 'joinfavorite') {

	$itemid = empty($_GET['itemid'])?0:intval($_GET['itemid']);
	if(empty($itemid)) showxml($blang['message_favorate_none']);

	getcookie(1);
	
	if(empty($_SGLOBAL['supe_uid'])) showxml($blang['message_need_login_fav']);

	include_once('./include/common.inc.php');
	
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spaceitems')." WHERE itemid='$itemid' AND folder='1'");
	if($item = $_SGLOBAL['db']->fetch_array($query)) {
		replacetable('favorites', array('uid'=>$_SGLOBAL['supe_uid'], 'itemid'=>$itemid, 'dateline'=>$_SGLOBAL['timestamp']));
		clearspacecache($_SGLOBAL['supe_uid'], 'spacefavorite');
		showxml($blang['message_fav_ok']);
	} else {
		showxml($blang['message_favorate_none']);
	}

} elseif ($action == 'emailfriend') {

	if(submitcheck('submitemailfriend')) {
		$_POST['sendtoemail'] = str_replace(array('，',' ', ' ', '|', ';'), ',', $_POST['sendtoemail']);
		$email_to = $_POST['sendtoemail'];
		$email_from = $adminemail;
		$email_subject = $blang['mail_title'];
		$email_message = $_POST['message'];
		if(!empty($email_to) && !empty($email_message)) {
			include_once(S_ROOT.'./include/common.inc.php');
			include_once(S_ROOT.'./include/sendmail.inc.php');
		}
		echo '
		<script language="javascript">
		alert("'.$blang['message_mail_ok'].'");
		</script>';
		exit;
	}
	
	include_once(S_ROOT.'./data/system/config.cache.php');
	
	$item = array();
	$itemid = empty($_GET['itemid'])?0:intval($_GET['itemid']);
	getcookie();
	if($itemid) {
		dbconnect();
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spaceitems')." WHERE itemid='$itemid' AND folder='1'");
		$item = $_SGLOBAL['db']->fetch_array($query);
	}
	if(empty($item)) {
		$message = "{$blang[mail_c1]}\n\n$_SSCONFIG[sitename]\n{$blang[mail_c2]} $siteurl\n\n{$blang[mail_c3]}";
	} else {
		$url = geturl("uid/$item[uid]/action/viewspace/itemid/$item[itemid]");
		$message = "{$blang[mail_c4]} $_SSCONFIG[sitename] {$blang[mail_c5]} $item[subject]\n{$blang[mail_c2]}: $url\n\n{$blang[mail_c6]}";
	}

	$html = '<h5><a href="javascript:;" onclick="getbyid(\'xspace-ajax-div-emailfriend\').style.display=\'none\';">'.$blang['mail_close'].'</a>'.$blang['mail_submit'].'</h5>
	<div class="xspace-ajaxcontent">
		<form method="post" action="batch.common.php?action=emailfriend" target="phpframe_emailfriend">
			<table width="100%" summary="" cellpadding="5" cellspacing="0" border="0">
				<tr>
					<td width="60">'.$blang['mail_add'].'</td>
					<td><input type="text" name="sendtoemail" style="width: 98%;" value=""></td>
				</tr>
				<tr>
					<td valign="top">'.$blang['mail_content'].'</td>
					<td valign="top"><textarea rows="8" name="message" style="width: 98%;">'.$message.'</textarea></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td><button type="submit" name="submitemailfriend" value="true">'.$blang['mail_submit'].'</button></td>
				</tr>
			</table>
		</form>
	</div>
	<iframe id="phpframe_emailfriend" name="phpframe_emailfriend" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>';
	showxml($html);

} elseif ($action == 'grade') {

	$allow = false;
	$itemid = empty($_GET['itemid'])?0:intval($_GET['itemid']);
	if(!empty($itemid)) {
		getcookie(1);
		if(!empty($_SGLOBAL['supe_uid'])) {
			if(!empty($_SGLOBAL['group'])) {
				if($_SGLOBAL['group']['groupid'] == 1 || !empty($_SGLOBAL['group']['allowcheckitem'])) {
					include_once(S_ROOT.'./include/common.inc.php');
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spaceitems')." WHERE itemid='$itemid' AND folder='1'");
					if($item = $_SGLOBAL['db']->fetch_array($query)) {
						$allow = true;
					}
				}
			}
		}
	}

	if(!$allow) showxml('<h5><a href="javascript:;" onclick="getbyid(\'xspace-ajax-div-grade\').style.display=\'none\';">'.$blang['mail_close'].'</a>'.$blang['check_title'].'</h5><div class="xspace-ajaxcontent" style="text-align: center; padding: 3em 0;">'.$blang['message_grade_no1'].'</div>');

	$gradearr = array(
		'0' => $blang['grade0'],
		'1' => $blang['grade1'],
		'2' => $blang['grade2'],
		'3' => $blang['grade3'],
		'4' => $blang['grade4'],
		'5' => $blang['grade5'],
		'-1' => $blang['grade-1']
	);
	if(!empty($_SCONFIG['checkgrade'])) {
		$newgradearr = explode("\t", $_SCONFIG['checkgrade']);
		for($i=0; $i<5; $i++) {
			if(!empty($newgradearr[$i])) $gradearr[$i+1] = $newgradearr[$i];
		}
	}

	$gradestr = getselectstr('grade', $gradearr, $item['grade']);

	$html = '<h5><a href="javascript:;" onclick="getbyid(\'xspace-ajax-div-grade\').style.display=\'none\';">'.$blang['mail_close'].'</a>'.$blang['check_title'].'</h5>
	<div class="xspace-ajaxcontent">
		<form method="post" action="'.S_URL.'/batch.common.php?action=submitgrade&itemid='.$itemid.'" target="_self">
			<table width="100%" summary="" cellpadding="0" cellspacing="0" border="0" style="height: 100%; text-align: center;">
				<tr>
					<td style="padding: 3em 0;">'.$gradestr.'&nbsp;&nbsp;<button type="submit" name="gradesubmit" value="true">'.$blang['check_title'].'</button></td>
				</tr>
			</table></form>
	</div>';
	
	showxml($html);

} elseif ($action == 'submitgrade') {
	
	if(submitcheck('gradesubmit')) {
		$allow = false;
		$itemid = empty($_GET['itemid'])?0:intval($_GET['itemid']);
		if(!empty($itemid)) {
			getcookie(1);
			if(!empty($_SGLOBAL['supe_uid'])) {
				if(!empty($_SGLOBAL['group'])) {
					if($_SGLOBAL['group']['groupid'] == 1 || !empty($_SGLOBAL['group']['allowcheckitem'])) {
						include_once(S_ROOT.'./include/common.inc.php');
						$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('spaceitems')." WHERE itemid='$itemid' AND folder='1'");
						if($item = $_SGLOBAL['db']->fetch_array($query)) {
							$allow = true;
						}
					}
				}
			}
		}

		if($allow) {
			updatetable('spaceitems', array('grade'=>intval($_POST['grade'])), array('itemid'=>$itemid));
			messagebox('ok', $blang['message_grade_ok'], geturl("uid/$item[uid]/action/viewspace/itemid/$item[itemid]/php/1"));
		} else {
			messagebox('ok', $blang['message_grade_no1'], geturl("uid/$item[uid]/action/viewspace/itemid/$item[itemid]/php/1"));
		}
		exit;
	}

} elseif ($action == 'getblogtree') {

	dbconnect();
	$uid = intval($_GET['uid']);
	if(empty($uid)) exit;
	
	//获取所有的日志
	$marr = array();
	$blogtree = '<?xml version="1.0" encoding="'.$charset.'"?>'."\n";
	
	$showspace = true;
	$query = $_SGLOBAL['db']->query('SELECT i.*,u.spacename FROM '.tname('spaceitems').' i LEFT JOIN '.tname('userspaces').' u  ON i.uid=u.uid WHERE u.uid=\''.$uid.'\' AND i.type =\'blog\' AND i.folder=\'1\'');
	while($rowArr = $_SGLOBAL['db']->fetch_array($query)) {
		$month = sgmdate($rowArr['dateline'], 'Y'.$lang['year'].'m'.$lang['month']);
		if(sgmdate($rowArr['dateline'], 'm')<1 || 12<sgmdate($rowArr['dateline'], 'm')) continue;
		if(!isset($marr[$month])) $marr[$month] = array();
		$marr[$month][]=array(
			'itemid' => $rowArr['itemid'],
			'subject' => $rowArr['subject']
		);
		if($showspace) {
			$blogtree .= '<blog caption="'.$rowArr['spacename'].'">'."\n";
			$showspace = false;
		}
	}
	//生成目录树
	foreach($marr as $key => $value) {
		$blogtree .='	<level1  caption="'.$key.'">'."\n";
		foreach($value as $vkey => $varr) {
			$blogtree .='		<level2  caption="'.$varr['subject'].'" parent="'.$key.'" itemid="'.$varr['itemid'].'"/>'."\n";
		}
		$blogtree .='	</level1>'."\n";
	}
	$blogtree .='</blog>'."\n";

	echo $blogtree;

} elseif ($action == 'price') {
	
	$itemid = empty($_GET['itemid'])?0:intval($_GET['itemid']);
	if(empty($itemid)) messagebox('error', 'system_error');
	getcookie(1);
	if(empty($_SGLOBAL['supe_uid'])) messagebox('error', 'no_login', geturl('action/login'));
	
	$query = $_SGLOBAL['db']->query('SELECT i.*, g.* FROM '.tname('spaceitems').' i LEFT JOIN '.tname('spacegoods').' g ON i.itemid=g.itemid WHERE i.itemid=\''.$itemid.'\' AND i.subtype=\'auction\'');
	if(!$item = $_SGLOBAL['db']->fetch_array($query)) messagebox('error', 'system_error');
	if($item['uid'] == $_SGLOBAL['supe_uid']) messagebox('error', 'no_self');

	if(empty($item['term'])) $item['term'] = 7;
	$item['endtime'] = $item['starttime'] + $item['term']*24*3600;
	
	if(($item['endtime'] < $_SGLOBAL['timestamp']) && empty($item['status'])) {
		include_once(S_ROOT.'./include/common.inc.php');
		updatetable('spaceitems', array('folder'=>3), array('itemid'=>$item['itemid']));
		messagebox('error', 'no_auction');
	}

	if ($item['stocknum'] <= 0 || $_SGLOBAL['timestamp'] > $item['endtime'] || $_SGLOBAL['timestamp'] < $item['starttime']) {
		messagebox('error', 'no_auction');
	}
	
	$query = $_SGLOBAL['db']->query('SELECT price FROM '.tname('goodsprice').' WHERE itemid=\''.$itemid.'\' ORDER BY price DESC LIMIT 0,1');
	if(!$price = $_SGLOBAL['db']->result($query, 0)) {
		$price = $item['price'] + $item['markup'];
	} else {
		$price = $price + $item['markup'];
	}
	
	if(submitcheck('pricesubmit')) {
		include_once(S_ROOT.'./include/common.inc.php');
		
		$_POST['price'] = intval($_POST['price']);
		$_POST['buynum'] = intval($_POST['buynum']);
		if($_POST['price'] < $price) messagebox('error', $blang['message_price1'].' '.$price.' '.$blang['price_unit']);
		if($_POST['buynum'] < 1) messagebox('error', $blang['message_input_price']);
		if($_POST['buynum'] > $item['stocknum']) messagebox('error', $blang['message_buynum1'].' '.$item['stocknum']);
		
		$insertsqlarr = array();
		$insertsqlarr['itemid'] = $itemid;
		$insertsqlarr['price'] = intval($_POST['price']);
		$insertsqlarr['buynum'] = intval($_POST['buynum']);
		$insertsqlarr['uid'] = $_SGLOBAL['supe_uid'];
		$insertsqlarr['username'] = $_SGLOBAL['supe_username'];
		$insertsqlarr['dateline'] = $_SGLOBAL['timestamp'];
		inserttable('goodsprice', $insertsqlarr);
		messagebox('ok', 'succeed', geturl('action/viewspace/itemid/'.$itemid.'/php/1'));
	}
	
	$showtext = "
	<form method=\"post\" name=\"thevalueform\" id=\"thevalueform\" action=\"$siteurl/batch.common.php?action=price&itemid=$itemid\">
		<p>$blang[give_price]:<input name=\"price\" type=\"text\" id=\"price\" size=\"30\" value=\"$price\" /></p>
		<p>$blang[buy_num]:<input name=\"buynum\" type=\"text\" id=\"buynum\" size=\"30\" value=\"1\" /></p>
		<p><center><input type=\"submit\" name=\"pricesubmit\" value=\"$blang[give_price]\" class=\"submit\"></center></p>
	</form>";
	
	messagebox('ok', $showtext);

} elseif ($action == 'auction') {
	
	$code = empty($_GET['code'])?'':$_GET['code'];
	$priceid = intval(authcode($code, 'DECODE'));
	if(empty($priceid)) messagebox('error', 'system_error');
	getcookie(1);
	if(empty($_SGLOBAL['supe_uid'])) messagebox('error', 'no_login', geturl('action/login'));
	
	$query = $_SGLOBAL['db']->query('SELECT p.*, i.*, ii.* FROM '.tname('goodsprice').' p INNER JOIN '.tname('spaceitems').' i ON i.itemid=p.itemid AND i.uid=\''.$_SGLOBAL['supe_uid'].'\' LEFT JOIN '.tname('spacegoods').' ii ON ii.itemid=p.itemid WHERE p.priceid=\''.$priceid.'\' LIMIT 1');
	if(!$price = $_SGLOBAL['db']->fetch_array($query)) messagebox('error', 'system_error');

	if($price['uid'] != $_SGLOBAL['supe_uid']) messagebox('error', 'no_permission');
	if($price['buynum'] > $price['stocknum']) messagebox('error', $blang['message_no_num']);

	$_SGLOBAL['db']->query('UPDATE '.tname('goodsprice').' SET status = 1 WHERE priceid=\''.$priceid.'\'');
	$_SGLOBAL['db']->query('UPDATE '.tname('spacegoods').' SET status = 1, stocknum=stocknum-'.$price['buynum'].' WHERE itemid=\''.$price['itemid'].'\'');

	messagebox('ok', 'succeed', geturl('action/viewspace/itemid/'.$price['itemid'].'/php/1'));
	
} elseif ($action == 'joinfriend') {

	$uid = empty($_GET['uid'])?0:intval($_GET['uid']);
	if(empty($uid)) showxml($blang['message_friend_no_choice']);
	getcookie(1);
	if(empty($_SGLOBAL['supe_uid'])) showxml($blang['message_need_login_friend']);
	if($uid == $_SGLOBAL['supe_uid']) showxml($blang['message_no_self_friend']);
	
	include_once(S_ROOT.'./include/common.inc.php');
	replacetable('friends', array('uid'=>$_SGLOBAL['supe_uid'], 'frienduid'=>$uid, 'dateline'=>$_SGLOBAL['timestamp'], 'grade'=>'1'));
	clearspacecache($_SGLOBAL['supe_uid'], 'spacefriend');
	showxml($blang['message_friend_ok']);

} elseif ($action == 'adclick') {
	
	$id = empty($_GET['id'])?0:intval($_GET['id']);
	dbconnect();
	$_SGLOBAL['db']->query('UPDATE '.tname('ads').' SET clicks=clicks+1 WHERE adid=\''.$id.'\'');

} elseif ($action == 'viewnews') {

	if(!empty($_GET['op']) && !empty($_GET['itemid']) && !empty($_GET['catid'])) {
		$itemid = intval($_GET['itemid']);
		$catid = intval($_GET['catid']);
		$newitemid = 0;
		if($itemid && $catid && $_GET['op'] == 'up') {
			dbconnect();
			$newitemid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query('SELECT itemid FROM '.tname('spaceitems').' WHERE itemid <'.$itemid.' AND catid=\''.$catid.'\' AND folder=\'1\' AND type=\'news\' ORDER BY itemid DESC LIMIT 1'), 0);
		} elseif($itemid && $catid) {
			dbconnect();
			$newitemid = $_SGLOBAL['db']->result($_SGLOBAL['db']->query('SELECT itemid FROM '.tname('spaceitems').' WHERE itemid >'.$itemid.' AND catid=\''.$catid.'\' AND folder=\'1\' AND type=\'news\' ORDER BY itemid LIMIT 1'), 0);
		}
		if(!empty($newitemid)) {
			header('Location: '.geturl('action/viewnews/itemid/'.$newitemid));
		} else {
			header('Location: '.geturl('action/viewnews/itemid/'.$itemid));
		}
	}

}

?>