<?php
if (!defined('_GNUBOARD_')) exit;

// 최신글 추출
// $cache_time 캐시 갱신시간
function latest($skin_dir='', $bo_table, $rows=10, $subject_len=40, $cache_time=1, $options='')
{
    global $g5;
    //static $css = array();

    if (!$skin_dir) $skin_dir = 'basic';

    if(G5_IS_MOBILE) {
        $latest_skin_path = G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
        $latest_skin_url  = G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
    } else {
        $latest_skin_path = G5_SKIN_PATH.'/latest/'.$skin_dir;
        $latest_skin_url  = G5_SKIN_URL.'/latest/'.$skin_dir;
    }

    $cache_fwrite = false;
    if(G5_USE_CACHE) {
        $cache_file = G5_DATA_PATH."/cache/latest-{$bo_table}-{$skin_dir}-{$rows}-{$subject_len}.php";

        if(!file_exists($cache_file)) {
            $cache_fwrite = true;
        } else {
            if($cache_time > 0) {
                $filetime = filemtime($cache_file);
                if($filetime && $filetime < (G5_SERVER_TIME - 3600 * $cache_time)) {
                    @unlink($cache_file);
                    $cache_fwrite = true;
                }
            }

            if(!$cache_fwrite)
                include($cache_file);
			    $list = unserialize($seriallist);
        }
    }

    if(!G5_USE_CACHE || $cache_fwrite) {
        $list = array();

        $sql = " select * from {$g5['board_table']} where bo_table = '{$bo_table}' ";
        $board = sql_fetch($sql);
        $bo_subject = get_text($board['bo_subject']);

        $tmp_write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름
        $sql = " select * from {$tmp_write_table} where wr_is_comment = 0 order by wr_num limit 0, {$rows} ";
        $result = sql_query($sql);
        for ($i=0; $row = sql_fetch_array($result); $i++) {
            $list[$i] = get_list($row, $board, $latest_skin_url, $subject_len);
        }

        if($cache_fwrite) {
            $handle = fopen($cache_file, 'w');
            $cache_content = "<?php\nif (!defined('_GNUBOARD_')) exit;\n\$bo_subject=\"".$bo_subject."\";\n\$seriallist=".serialize($list)."?>";
            fwrite($handle, $cache_content);
            fclose($handle);
        }
    }

    /*
    // 같은 스킨은 .css 를 한번만 호출한다.
    if (!in_array($skin_dir, $css) && is_file($latest_skin_path.'/style.css')) {
        echo '<link rel="stylesheet" href="'.$latest_skin_url.'/style.css">';
        $css[] = $skin_dir;
    }
    */

    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}


// 최신글 추출 - redis - serialize
// $cache_time 캐시 갱신시간
function latest_redis($skin_dir='', $bo_table, $rows=10, $subject_len=40, $cache_time=60, $options='')
{
    global $g5;


    if (!$skin_dir) $skin_dir = 'basic';

    if(G5_IS_MOBILE) {
        $latest_skin_path = G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
        $latest_skin_url  = G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
    } else {
        $latest_skin_path = G5_SKIN_PATH.'/latest/'.$skin_dir;
        $latest_skin_url  = G5_SKIN_URL.'/latest/'.$skin_dir;
    }

    $cache_fwrite = false;
    if(G5_USE_CACHE) {

	    $call_key = "latest-{$bo_table}-{$skin_dir}-{$rows}-{$subject_len}";
        $redis = new Redis(); 
        $redis->connect('127.0.0.1', 6379); 
        $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); 


        if($redis->ttl($call_key) > 0) {
           $list = $redis->get($call_key);
        } else {
          $cache_fwrite = true;
        }
    }

    if(!G5_USE_CACHE || $cache_fwrite) {
        $list_temp = array();

        $sql = " select bo_table,bo_subject,bo_notice, bo_subject_len, bo_use_list_content, bo_use_sideview, bo_new, bo_hot, bo_use_list_file from {$g5['board_table']} where bo_table = '{$bo_table}' ";
        $board = sql_fetch($sql);
        $bo_subject = get_text($board['bo_subject']);

        $tmp_write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름
        $sql = " select wr_id, wr_subject, ca_name, wr_name, wr_datetime, mb_id, wr_name, wr_email, wr_datetime, wr_hit, wr_comment, wr_content, wr_1, wr_2, wr_3, wr_4, wr_5 from {$tmp_write_table} where wr_is_comment = 0 order by wr_num limit 0, {$rows} ";
        $result = sql_query($sql);
        for ($i=0; $row = sql_fetch_array($result); $i++) {
            $list_temp[$i] = get_list($row, $board, $latest_skin_url, $subject_len);
        }
        if($cache_fwrite) {
            $list = array( 
                "list" => $list_temp, 
                "bo_subject" => $bo_subject, 
            ); 
            $redis->setex($call_key, $cache_time, $list); 
        }
    }

    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}



// 최신글 추출 - redis - serialize
// $cache_time 캐시 갱신시간
function gr_latest_redis($skin_dir='', $gr_id, $gr_name, $rows=10, $subject_len=40, $cache_time=60, $options='')
{
    global $g5;


    if (!$skin_dir) $skin_dir = 'basic';

    if(G5_IS_MOBILE) {
        $latest_skin_path = G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
        $latest_skin_url  = G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
    } else {
        $latest_skin_path = G5_SKIN_PATH.'/latest/'.$skin_dir;
        $latest_skin_url  = G5_SKIN_URL.'/latest/'.$skin_dir;
    }

    $cache_fwrite = false;
    if(G5_USE_CACHE) {

	    $call_key = "gr_latest-{$gr_id}-{$skin_dir}-{$rows}-{$subject_len}";
        $redis = new Redis(); 
        $redis->connect('127.0.0.1', 6379); 
		$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); 


        if($redis->ttl($call_key) > 0) {
           $list = unserialize($redis->get($call_key));
           $list = $redis->get($call_key);

        } else {
          $cache_fwrite = true;
        }
    }

    if(!G5_USE_CACHE || $cache_fwrite) {


        $list = array();

	    $sqlgroup = " select  bo_table, bo_notice, bo_subject, bo_subject_len, bo_use_list_content, bo_new, bo_use_sideview, bo_hot, bo_use_search from {$g5[board_table]} where gr_id = '{$gr_id}' and bo_use_search=1 and bo_1 <> '1'";
	    $rsgroup = sql_query($sqlgroup);


        for ($j=0, $k=0; $row_board = sql_fetch_array($rsgroup); $j++){
          $bo_table = $row_board['bo_table'];


		   $tmp_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블

	       $sql = " select * from {$tmp_table} where wr_is_comment = 0 order by wr_num limit 0, {$rows} ";


	   	   $result = sql_query($sql);
  
		   for ($i=0; $row = sql_fetch_array($result); $i++, $k++) {

            
            $op_list[$k] = $row['wr_datetime'];
			$list[$k] = get_list($row, $row_board, $latest_skin_path, $subject_len);

			$list[$k]['bo_table'] = $row_board['bo_table'];
			$list[$k]['bo_subject'] = $row_board['bo_subject'];
            $list[$k]['gr_name'] = $gr_name;
			$list[$k]['bo_wr_subject'] = cut_str($row_board[$j]['bo_subject'] . $list[$k]['wr_subject'], $subject_len);
		   }
	    }
	    if($k>0) array_multisort($op_list, SORT_DESC, $list);
	    if($k>$rows) array_splice($list, $rows);


        if($cache_fwrite) {
            $redis->setex($call_key, $cache_time, $list); 

        }
	}
    
    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}

?>
