<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');

require_once(R_P.'require/showimg.php');

InitGP(array('see','type'),null,1);
$u = (int)GetGP('u');
!$u && $u = $winduid;
$thisbase = $basename."q=share&";

$where = '';
if ($see == 'friend') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	if ($friends = getFriends($winduid,0,0,false,1)) {
		$uids = array_keys($friends);
		$where .= 'uid IN('.pwImplode($uids).') AND ifhidden=0';
	} else {
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_share');
		footer();
	}
	$username = $windid;
	$thisbase .= "see=$see&";
} elseif ($see == 'favor') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	$username = $windid;
	$where .= 'uid='.pwEscape($winduid).' AND ifhidden=1';
	$thisbase .= "see=$see&";
} elseif ($see == 'recommend') {
	define('AJAX',1);
	
	if (empty($_POST['step'])) {
		InitGP(array('id'), null, 2);
		$a = 'recommend';
		$rt = $db->get_one("SELECT id,type,content,username FROM pw_share WHERE id=" . pwEscape($id));

		if (empty($rt)) {
			Showmsg('data_error');
		}

		$temp = unserialize($rt['content']);

		$rt['link']	= $temp['link'];
		if ($rt['type']=='user') {
			$title = $temp['user']['username']."($rt[link])";
		} elseif ($rt['type']=='photo') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$title= $belong.$temp['photo']['username']."($rt[link])";
		} elseif ($rt['type']=='album') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$title = $belong.$temp['album']['username']."($rt[link])";
		} elseif ($rt['type']=='group') {
			$title = $temp['group']['name']."($rt[link])";
		} elseif ($rt['type']=='diary') {
			$title = $temp['diary']['subject']."($rt[link])";
		} else {
			$title = $rt['link'];
		}
		$descrip = $temp['descrip'];
		
		$atc_name = getLangInfo('o',$rt['type'],false,1);
		require_once PrintEot('m_ajax');
		ajax_footer();

	} else {

		InitGP(array('sendtoname'));
		InitGP(array('subject','atc_content'),'P');
		require_once(R_P.'require/msg.php');

		$userdb = '';
		if ($sendtoname) {
			$userdb = $db->get_one("SELECT username FROM pw_members WHERE username=".pwEscape($sendtoname));
		}

		if (!$userdb) {
			$errorname = $sendtoname;
			Showmsg('user_not_exists');
		}
		if (!$subject || !$atc_content) {
			Showmsg('tofriend_msgerror');
		}
		$msgdb = array(
			'toUser'	=> $userdb['username'],
			'fromUid'	=> $winduid,
			'fromUser'	=> $windid,
			'subject'	=> stripslashes($subject),
			'content'	=> stripslashes($atc_content),
		);
		pwSendMsg($msgdb);
		Showmsg('operate_success');
	}
} else {
	if ($u!=$winduid) {
		$friend = getOneFriend($u);
		if ($friend) {
			$where .= 'uid='.pwEscape($u).' AND ifhidden=0';
			$faceimg = $friend['face'];
			$username = $friend['username'];
		} else {
			Showmsg('mode_o_not_friend');
		}
		$thisbase .= "u=$u&";
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$where .= 'uid='.pwEscape($winduid).' AND ifhidden=0';
		$username = $windid;
	}
}
if ($type && in_array($type,array('web','user','photo','album','group','video','music','flash','diary'))) {
	$where .= ($where=='' ? 'type='.pwEscape($type) : ' AND type='.pwEscape($type));
}

$shares = array();
$count = $db->get_value("SELECT count(*) as count FROM pw_share WHERE $where");
if ($count) {
	$perpage = 10;
	$page = (int)GetGP('page');
	$page<1 && $page = 1;
	$start = ($page-1)*$perpage;
	$numofpage = ceil($count/$perpage);

	$rs = $db->query("SELECT * FROM pw_share WHERE $where ORDER BY id DESC ".pwLimit($start,$perpage));
	while ($rt = $db->fetch_array($rs)) {
		$rt['postdate'] = get_date($rt['postdate'],'m-d H:s');
		$temp = unserialize($rt['content']);
		if ($temp['video']) {
			$rt['host']	= $temp['video']['host'];
			$rt['hash'] = $temp['video']['hash'];
		}
		$rt['link']	= $temp['link'];
		if ($rt['type']=='user') {
			$temp['link']	= $temp['user']['username'];
			$rt['image']	= $temp['user']['image'];
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".$temp['user']['username']."</a>";
		} elseif ($rt['type']=='photo') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$rt['image']	= $temp['photo']['image'];
			$temp_uid	= $temp['photo']['uid'];
			$rt['title']= $belong."<a href=\"mode.php?m=o&q=user&u=$temp_uid\" target=\"_blank\">".$temp['photo']['username']."</a>";
		} elseif ($rt['type']=='album') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$rt['image']	= $temp['album']['image'];
			$temp_uid	= $temp['album']['uid'];
			$rt['title']= $belong."<a href=\"mode.php?m=o&q=user&u=$temp_uid\" target=\"_blank\">".$temp['album']['username']."</a>";
		} elseif ($rt['type']=='group') {
			$rt['image']	= $temp['group']['image'];
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".$temp['group']['name']."</a>";
		} elseif ($rt['type']=='diary') {
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".$temp['diary']['subject']."</a>";
		} else {
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".substrs($rt['link'],40)."</a>";
		}
		$rt['descrip']	= $temp['descrip'];
		if ($rt['ifhidden']) {
			$rt['type_name']	= getLangInfo('o',$rt['type'].'_self',false,1);
		} else {
			$rt['type_name']	= getLangInfo('o',$rt['type'],false,1);
		}
		
		if ($rt['uid']!=$winduid && $friends[$rt['uid']]) {
			$rt['face'] = $friends[$rt['uid']]['face'];
		}
		unset($rt['content']);
		$shares[] = $rt;
	}
	$pages = numofpage($count,$page,$numofpage,$thisbase);
}
require_once(R_P.'require/header.php');
require_once PrintEot('m_share');
footer();
?>