<?php
include "../core.php";
if ( empty( $_REQUEST['uid'] ) || !is_numeric( $_REQUEST['uid'] ) ) {
		include SAXUE_WEB_PATH . "/404.php";
} 
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
if ( empty( $_REQUEST['like'] ) || !in_array( $_REQUEST['like'], array( 'fav', 'top', 'down' ) ) ) $GLOBALS['_REQUEST']['like'] = 'all';
include SAXUE_WEB_PATH . "/header.php";
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . "/space/like.html";
$saxueTset['saxue_page_cacheid'] = floor( $_REQUEST['uid'] / 1000 ) . '/' . $_REQUEST['uid'] . '_' . $_REQUEST['like'] . '_' . $_REQUEST['page'];
$page_used_cache = false;
if ( SAXUE_USE_CACHE ) {
		$cachedtime = $saxueTpl -> get_cachedtime( $saxueTset['saxue_page_template'], $saxueTset['saxue_page_cacheid'] );
		if ( SAXUE_NOW_TIME - $cachedtime < SAXUE_CACHE_LIFETIME ) {
				$page_used_cache = true;
		}
		if ( !$page_used_cache ) {
				$saxueTpl -> update_cachedtime( $saxueTset['saxue_page_template'], $saxueTset['saxue_page_cacheid'] );
				$saxueTpl -> setcaching( 2 );
		} else {
				$saxueTpl -> setcaching( 1 );
		}
		$saxueTpl -> setcachetime( 99999999 );
} else {
		$saxueTpl -> setcaching( 0 );
}
if ( !$page_used_cache ) {
		/* 用户信息 */
		include SAXUE_ROOT_PATH . "/model/member_users.php";
		$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
		$saxueUsers = $users_handler -> get( $_REQUEST['uid'] );
		if ( !is_object( $saxueUsers ) ) {
				include SAXUE_WEB_PATH . "/404.php";
		} 
		include SAXUE_ROOT_PATH . "/model/member_userdata.php";
		$userdata_handler = &saxuememberuserdatahandler :: getinstance( "saxuememberuserdatahandler" );
		$saxueUserdata = $userdata_handler -> get( $_REQUEST['uid'] );
		$user = $saxueUsers -> getvars( "n" ) + $saxueUserdata -> getvars( "n" );
		$user['uavatar'] = saxue_geturl( 'user_avatar', $_REQUEST['uid'], 'l', $user['avatar'] );
		$user['uhome'] = saxue_geturl( 'user_home', $_REQUEST['uid'] );
		$saxueTpl -> assign_by_ref( "user", $user );
		$saxueTpl -> assign( "uid", $_REQUEST['uid'] );
		/* 用户信息 */
		saxue_getconfigs( "configs", 'member' );
		saxue_getconfigs( "doingtype", 'member' );
		include SAXUE_ROOT_PATH . "/common/function.php";
		include SAXUE_ROOT_PATH . "/model/member_doing.php";
		$doing_handler = &saxuememberdoinghandler :: getinstance( "saxuememberdoinghandler" );
		$criteria = new criteriacompo( new criteria( 'uid', $_REQUEST['uid'] ) );
		switch ( $_REQUEST['like'] ) {
				case 'fav':
						$criteria -> add( new criteria( 'doing', 2 ) );
						break;
				case 'top':
						$criteria -> add( new criteria( 'doing', 3 ) );
						break;
				case 'down':
						$criteria -> add( new criteria( 'doing', 4 ) );
						break;
				case 'all':
				default:
						$criteria -> add( new criteria( 'doing', 1, '>' ) );
						break;
		}
		$criteria -> setsort( "doid" );
		$criteria -> setorder( "DESC" );
		$criteria -> setlimit( $saxueConfigs['member']['homepnum'] );
		$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['homepnum'] );
		$doing_handler -> queryobjects( $criteria );
		$rows = $aids = array();
		$k = 0;
		while ( $v = $doing_handler -> getobject() ) {
				$rows[$k] = $v -> getvars( 'n' );
				$rows[$k]['doname'] = $saxueDoingtype[$rows[$k]['doing']];
				$rows[$k]['dodate'] = saxue_getdate( $rows[$k]['dotime'] );
				$aids[] = $rows[$k]['aid'];
				++$k;
		} 
		if ( $k > 0 ) {
				saxue_getconfigs( "cate", 'article' );
				$aids = array_unique( $aids );
				$sql = 'SELECT * FROM ' . saxue_dbprefix( "article" ) . ' WHERE aid IN(' . implode( ',', $aids ) . ')';
				$_res = $doing_handler -> db -> query( $sql );
				$articles = $tables = array();
				while ( $v = $doing_handler -> getobject( $_res ) ) {
						$k = $v -> getvar( 'aid' );
						$articles[$k] = $v -> getvars( 'n' );
						$articles[$k]['cname'] = $saxueCate['article'][$articles[$k]['cid']]['cname'];
						$articles[$k]['curl'] = $saxueCate['article'][$articles[$k]['cid']]['curl'];
						$articles[$k]['uhome'] = saxue_geturl( "user_home", $articles[$k]['uid'] );
						$tables[$articles[$k]['tbnum']][] = $k;
				}
				foreach ( $tables as $tbnum => $ids  ) {
						$_res = $doing_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
						while ( $v = $doing_handler -> getobject( $_res ) ) {
								$k = $v -> getvar( 'aid' );
								$articles[$k] = $articles[$k] + ( $v -> getvars( 'n' ) );
								$articles[$k]['intro'] = nl2br( $articles[$k]['intro'] );
								if ( $articles[$k]['isslide'] ) {
										$articles[$k]['album'] = explode( '|', $articles[$k]['album'] );
								}
								// 读取TAG
								$_tags = array();
								$_tagnum = 0;
								if ( strlen( $articles[$k]['tags'] ) > 0 ) {
										$_arr = explode( ',', $articles[$k]['tags'] );
										$_arrid = explode( ',', $articles[$k]['tagids'] );
										foreach ( $_arr as $_i => $_s ) {
												$_tags[$_tagnum]['name'] = $_s;
												$_tags[$_tagnum]['url'] = saxue_geturl( "tag_show", $_arrid[$_i] );
												++$_tagnum;
										} 
								}
								$articles[$k]['taglist'] = $_tags;
						} 
				} 
				foreach ( $rows as $k => $v ) {
						if ( isset( $articles[$v['aid']] ) ) {
								$rows[$k]['article'] = $articles[$v['aid']];
						} else {
								unset( $rows[$k] );
						}
				}
		} 
		$saxueTpl -> assign_by_ref( "rows", $rows );
		// 分页
		include SAXUE_ROOT_PATH . "/lib/util/page.php";
		if ( SAXUE_USE_CACHE ) {
				$pagecacheid = $_REQUEST['uid'] . '-' . $_REQUEST['like'];
				$_sub = floor( $_REQUEST['uid'] / 100 );
				saxue_getcachevars( $_sub, 'homelikelog' );
				if ( !is_array( $saxueHomelikelog ) ) {
						$saxueHomelikelog = array( );
				}
				if ( !isset( $saxueHomelikelog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueHomelikelog[$pagecacheid]['time'] ) {
						$saxueHomelikelog[$pagecacheid] = array( "rows" => $doing_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( $_sub, $saxueHomelikelog, 'homelikelog' );
				}
				$listrows = $saxueHomelikelog[$pagecacheid]['rows'];
		} else {
				$listrows = $doing_handler -> getcount( $criteria );
		}
		$_like = $_REQUEST['like'] == 'all' ? 'like' : 'like-' . $_REQUEST['like'];
		$jumppage = new saxuepage( $listrows, $saxueConfigs['member']['homepnum'], $_REQUEST['page'], 5 );
		$jumppage -> setlink( saxue_geturl( "user_home", $_REQUEST['uid'], $_like, '<{$page}>' ) );
		$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
		$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
		$saxueTpl -> assign( "like", $_REQUEST['like'] );
		$saxueTpl -> assign( "spacenav", 'like' );
}
include SAXUE_WEB_PATH . "/footer.php";