<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	个人空间主页文件

	$RCSfile: space.php,v $
	$Revision: 1.100 $
	$Date: 2007/06/05 13:19:37 $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

$uid = $_SGET['uid'] = empty($_SGET['uid'])?0:intval($_SGET['uid']);
if(empty($uid)) messagebox('error', 'not_found', S_URL);

dbconnect();

//模块布局
$leftblock = $mainblock = $rightblock = $detailblock = '';

//导航
$guidearr = array();


//判断用户是否登录
foreach ($_COOKIE as $key => $value)
{
    if ($key == 'YZONE_blog_auth')
    {
        $authcookie = $value;
    }
}

if (isset($authcookie))
{
    $str = md5($uid) . strtotime(date('Y-m-d'));
    $str = md5($str);

    if ($authcookie == $str)
    {
        $cookietime = 2592000;
        $ss_auth_key = md5($_SCONFIG['sitekey'].$_SERVER['HTTP_USER_AGENT']);
        include_once(S_ROOT.'./class/db_mysql.class.php');
        //链接数据库
        dbconnect();

        $query = $_SGLOBAL['db'] -> query("SELECT * FROM ".tname('members')." WHERE uid='$uid'");
        if($member = $_SGLOBAL['db'] -> fetch_array($query))
        {
            ssetcookie('sid', '', -86400 * 365);
            ssetcookie('cookietime', $cookietime, 31536000);
            ssetcookie('auth', authcode("$member[password]\t$member[secques]\t$member[uid]", 'ENCODE', $ss_auth_key), $cookietime);
        } else
        {
            ssetcookie('cookietime', $cookietime, 31536000);
            ssetcookie('loginuser', $get['username'], $cookietime);
            ssetcookie('activationauth', authcode($get['username'], 'ENCODE', $ss_auth_key), $cookietime);
        }
    }
}

//获取空间信息
$space = getuserspace($uid);
if(empty($space)) {
	dbconnect(1);

	$query = $_SGLOBAL['db_bbs']->query("SELECT m.uid, m.groupid, m.username, m.regdate, m.lastvisit, m.posts, mf.avatar FROM ".tname('members', 1)." m LEFT JOIN ".tname('memberfields', 1)." mf ON mf.uid=m.uid WHERE m.uid='$uid'");

	if($space = $_SGLOBAL['db']->fetch_array($query)) {

		//判断是否有权限自动拥有空间/没有跳转到个人资料
		if(!empty($blackgroupids) && in_array($space['groupid'], $blackgroupids)) sheader(B_URL.'/space.php?uid='.$uid);
		
		include_once(S_ROOT.'./include/common.inc.php');
		
		//判断是否本人
		@include_once(S_ROOT.'./data/system/group.cache.php');
		
		// added by lwx at 2009-6-8 14:17
		$space['username'] = dz_apply_filters('get_fixed_username', $space);
		// end of hook

		$space['spacename'] = $space['username'].$lang['personal_space'];
		$space['othernum'] = 10;
		
		$space['group'] = array();
		if(!empty($_SGLOBAL['grouparr'][$space['groupid']])) $space['group'] = $_SGLOBAL['grouparr'][$space['groupid']];
		
		getcookie();
		
		$space['showcp'] = 0;
		if($uid == $_SGLOBAL['supe_uid']) {
			if(!empty($space['group']['allowspace'])) $space['showcp'] = 1;
		}

		// added by lwx at 2009-9-9 11:05
		if ($space['showcp'] == 1) {
			sheader('/register');
		}
		// end of hook
		
		$space['photo'] = $space['avatar'];
		if(empty($space['photo'])) {
			$space['photo'] = S_URL.'/images/base/space_noface.gif';
		} elseif(!strexists($space['photo'], '://')) {
			$space['photo'] = B_URL.'/'.$space['photo'];
		}

		// added by lwx at 2009-7-16 14:58
		$space['photo'] = dz_apply_filters('get_user_avatar_url', $space['uid'], $uid, 'middle');
		// end of hook

		$space['sightml'] = '';
		
		//页面标题
		$title = $space['spacename'].' - '.$_SCONFIG['sitename'].' '.$_SCONFIG['seotitle'];

		include_once(S_ROOT.'./include/space_nospace.inc.php');
		
		ob_out();
		exit();
	} else {

		// added by lwx at 2009-9-15 9:28
		// 未同步用户

		include_once(S_ROOT.'./uc_client/client.php');

		if ($userdata = uc_get_user($uid, true)) {
			@list($uid, $username, $email, $secques, $nickname) = $userdata;
			$uid = intval($uid);
			$password = md5(time());

			$query = $_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('userspaces').' WHERE uid=\''.$uid.'\'');
			$havespace = (int) $_SGLOBAL['db']->result($query, 0);

			$insertsqlarr = array(
				'uid' => $uid,
				'groupid' => 10,
				'username' => addslashes($username),
				'password' => $password,
				'secques' => addslashes($secques),
				'timeoffset' => 9999,
				'dateformat' => 0,
				'havespace' => $havespace,
				'newpm' => 0,
				'nickname' => addslashes($nickname),
			);
			replacetable('members', $insertsqlarr);
			
			$synuserscript = uc_user_synuser($uid, $username, $password, $email, $nickname);

			// 判断是否有账号激活信息
			global $cookiepre;
			$activationauth = empty($_COOKIE[$cookiepre.'activationauth']) ? '' : $_COOKIE[$cookiepre.'activationauth'];
			$theusername = authcode($activationauth, 'DECODE');

			if ($theusername == $username) {
				// 设置登录cookie
				$cookietime = 0;
				$cookievalue = authcode("$password\t$secques\t$uid", 'ENCODE');
				ssetcookie('sid', '', $cookietime);
				ssetcookie('auth', $cookievalue, $cookietime);
				setcookie('_refer', '');
				
				$message = '您的博客还未开通，现转到博客开通页面。';
				$url = '/register';
			
			} else {
				$message = '跳转中……';
				$url = "/$uid?refresh=yes";
			}
			messagebox('ok', $message.$synuserscript, $url);

		} else {
			messagebox('error', 'not_found');//不存在的用户
		}

		// end of hook
	}
} else {
	$musicmsgs = @unserialize($space['music']);
	$musicload = empty($_SGET['notloaded'])?1:0;
	
	if(!empty($musicmsgs['mp3list']) && $musicload) {
		// 非跨页播放
		if(empty($musicmsgs['config']['passpage'])){
			//if(empty($_SGET['noframe'])) {
			//	echo '<script language="javascript" type="text/javascript">parent.window.location.href="'.S_URL.'/iframe.php?uid='.$uid.'&passpage=1";</script>';
			//}
		}else {
			$_SCONFIG['htmlspace'] = 0;
			$_SGET['noframe'] = !empty($_GET['noframe']) ? $_GET['noframe'] : $_SGET['noframe'];
			if(empty($_SGET['noframe'])) {
				echo '<script language="javascript" type="text/javascript">parent.window.location.href="'.S_URL.'/iframe.php?uid='.$uid.'";</script>';
				exit();
			}
		}
	}
	
	//空间锁定
	if($space['islock']) {
		$_SCONFIG['htmlspace'] = 0;
		getcookie(1);
		if($uid != $_SGLOBAL['supe_uid'] && $_SGLOBAL['member']['groupid'] != 1) messagebox('error', 'space_lock');
	} else {
		// removed by lwx at 2009-6-17 11:23
		//getcookie(0);
		// added by lwx at 2009-6-17 11:23
		getcookie(1);
		// end of hook
	}

	$isupdate = freshcookie('u'.$uid);

	//个人
	$spaceself = 0;
	if($uid == $_SGLOBAL['supe_uid']) {
		$spaceself = 1;
		$space['showcp'] = 1;
		$_SGET['php'] = 1;
	} else {
		$space['showcp'] = 0;
		
		//留下脚印
		if($isupdate && $_SGLOBAL['supe_uid']) {
			$_SGLOBAL['db']->query("REPLACE INTO ".tname('visitors')." (uid, visitoruid, dateline) VALUES ('$uid', '$_SGLOBAL[supe_uid]', '$_SGLOBAL[timestamp]')");

			// added by lwx at 2009-7-13 14:33
			//if (is_internal_ip()) {
				
				// 清除缓存
				require_once(S_ROOT.'./function/common.func.php');
				$_SGLOBAL['db']->query( "DELETE FROM ".getspacecachetablename( $uid )." WHERE uid='{$uid}' AND cacheid=4 ", "SILENT" );

				//dd($isupdate , $_SGLOBAL['supe_uid']);
			//}
			
		}
	}
	
	//CSS预览
	if(!empty($_SGET['css'])) $_SCONFIG['htmlspace'] = 0;
	
	//html静态
	if(!empty($_SCONFIG['htmlspace'])) {
		$_SHTML['uid'] = $uid;
		$_SHTML['action'] = 'space';
		$_SHTML['noframe'] = $_SGET['noframe'];
		$_SGLOBAL['htmlfile'] = gethtmlfile($_SHTML);
		ehtml('get', $_SCONFIG['htmlspacetime']);
		$_SCONFIG['debug'] = 0;
		$space['showcp'] = 0;//不显示快捷管理
	}

	//更新空间查看数
	if($isupdate || !$_SCONFIG['updateview']) updatespaceviewnum($uid);

	include_once(S_ROOT.'./include/common.inc.php');
	
	//页面标题
	$title = $space['spacename'].' - '.$_SCONFIG['sitename'].' '.$_SCONFIG['seotitle'].' - Powered by X-Space';
	$keywords = $space['username'].','.$space['province'].','.$space['city'];
	$description = $space['announcement'];
	// removed by lwx at 2009-6-22 13:30
	//$turl = empty($musicmsgs['config']['passpage'])?geturl("uid/$uid"):geturl("uid/$uid/notloaded/1");
	// added by lwx at 2009-6-22 13:30
	$turl = S_URL."/$uid/";
	if ( !empty($musicmsgs['config']['passpage']) ) {
		$turl .= '?noframe=1';
	}
	// end of hook
	$thepagename = '<a href="'.$turl.'">'.$lang['front_page_space'].'</a>';
	
	//页面特效
	$effect = '';
	if(!empty($space['flash'])) {
		@include_once(S_ROOT.'./data/system/effect.cache.php');
		if(isset($_SGLOBAL['effect'][$space['flash']])) {
			$flashurl = $_SGLOBAL['effect'][$space['flash']]['file'];
			$flashurl = str_replace('SITEURL', S_URL, $flashurl);
		} else {
			$flashurl = $space['flash'];
		}
		$flashurl = shtmlspecialchars($flashurl);//非html化
		$effect .= '<!--[if IE]><object id="flashplay" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="700" height="400" style="left: 50%; margin-left:-350px; position: absolute; top: -5px">
			<param name="movie" value="'.$flashurl.'" />
			<param name="quality" value="high" />
			<param name="wmode" value="transparent" />
			<embed src="'.$flashurl.'" id="theflash" style="left: 50%; margin-left:-350px; position: absolute; top: -5px" align="right" width="700" height="400" type="application/x-shockwave-flash" wmode="transparent" quality="high"></embed>
			</object><![endif]-->';
	}
	
	//页面公告
	if (strpos('str_'.$_SCONFIG['allowannounce'], 'front')) {
		@include_once(S_ROOT.'./data/system/announcement.cache.php');
		$sitemsg = '';
		if (empty($_SGLOBAL['announcement'])) {
			$_SGLOBAL['announcement'] = array();
		}
		foreach ($_SGLOBAL['announcement'] as $key => $listvalue) {
			$sitemsg .= '<p><a href=\"'.geturl('action/announcement/id/'.$listvalue['id']).'\" target=\"_blank\">' .cutstr($listvalue['subject'], 26, 1).'<\/a><\/p>';
		}
		if(!empty($sitemsg)) {
			$effect .= '<script type="text/javascript" language="javascript">
			<!--
			var siteMsg = "'.$sitemsg.'";
			getMsg();
			//-->
			</script>';
		}
	}

	//模块布局
	//空间模式
	
	// added by lwx at 2009-6-9 15:28
	if($space['spacemode'] == 'diy') {
		if (empty($space['choiceblockleft']) && empty($space['choiceblockmain']) && empty($space['choiceblockright'])) {
			$space['spacemode'] == 'blog';
		}
	}
	// end of hook

	$leftblock = 'search,archive,comment,favorite';
	if($space['spacemode'] == 'diy') {
		//自定义
		$leftblock = $space['choiceblockleft'];
		$mainblock = $space['choiceblockmain'];
		$rightblock = $space['choiceblockright'];
		if($space['blogmod']) $detailblock = 'newblog,newimage,group';
	} elseif($space['spacemode'] == 'bbs') {
		//论坛模式
		$leftblock = 'bbsreply,bbsfavorite,bbsforum';
		$mainblock = 'mythread,myblog,group,newitem';
		$detailblock = 'mythread,group';
	} elseif ($space['spacemode'] == 'blog') {
		//日志模式
		$mainblock = 'newblog,group,newitem,mythread';
		//是否选择只显示标题
		if(empty($space['blogmod'])) {
			$detailblock = 'group';
		} else {
			$detailblock = 'newblog,group';
		}
	} elseif ($space['spacemode'] == 'image') {
		//图片模式
		$mainblock = 'newimage,group,newitem,mythread';
		$detailblock = 'newimage,group';
	} elseif ($space['spacemode'] == 'video') {
		//影音模式
		$mainblock = 'newvideo,group,newitem,mythread';
		$detailblock = 'newvideo,group';
	} elseif ($space['spacemode'] == 'goods') {
		//商品模式
		$mainblock = 'newgoods,group,newitem,mythread';
		$detailblock = 'newgoods,group';
	} elseif ($space['spacemode'] == 'file') {
		//文件模式
		$mainblock = 'newfile,group,newitem,mythread';
		$detailblock = 'newfile,group';
	} elseif ($space['spacemode'] == 'link') {
		//书签模式
		$mainblock = 'newlink,group,newitem,mythread';
		$detailblock = 'newlink,group';
	} elseif ($space['spacemode'] == 'group') {
		//圈子模式
		$mainblock = 'group,newitem,mythread';
		$detailblock = 'group';
	} elseif ($space['spacemode'] == 'fav') {
		//收藏模式
		$mainblock = 'favorite,group,newitem,mythread';
		$detailblock = 'favorite,group';
	} elseif ($space['spacemode'] == 'track') {
		//足迹模式
		$mainblock = 'track,group,newitem,mythread';
		$detailblock = 'track,group';
	} elseif ($space['spacemode'] == 'all') {
		//综合模式
		$leftblock = 'search,archive,guestbook,comment,favorite,bbsreply,bbsforum,bbsfavorite';
		$mainblock = 'newblog,newimage,newvideo,newgoods,newfile,newlink,group,mythread,myblog';
	//默认模式
	} else {
		$leftblock = 'search,comment';
		$mainblock = 'group,newitem';
	}

	$space['showside'] = 1;
	if($space['spacemode'] != 'diy') {
		$leftblock = 'photo,action,calendar,'.(empty($leftblock)?'':$leftblock.',').'visitor,track,friend,userlink,music,information,rss';
	}


	$showtplfile = '';
	include_once(S_ROOT.'./include/space_template.inc.php');
	
	ob_out();
	
	//html静态
	if(!empty($_SCONFIG['htmlspace'])) {
		ehtml('make');
	}
}

?>
