<?php
if (!defined('_GNUBOARD_')) exit;
@include_once(G5_LIB_PATH.'/thumbnail.lib.php');

// 최신글 추출
//echo latest("스킨이름", "게시판이름", 게시물수, 제목글자수, "정렬방식", "이미지만", "분류명", "작성기간설정", "회원아이디설정", "공지만/공지제외", "옵션설정";
function latest($skin_dir='', $bo_table, $rows=10, $subject_len=40, $order='', $imgs='', $ca_name='',  $days='', $mbid='', $notice='', $options='') {

    global $g5;

    if (!$skin_dir) $skin_dir = 'basic';
    
    $time_unit = 3600;  // 1시간으로 고정

    if(preg_match('#^theme/(.+)$#', $skin_dir, $match)) {
        if (G5_IS_MOBILE) {
            $latest_skin_path = G5_THEME_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            if(!is_dir($latest_skin_path))
                $latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        } else {
            $latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        }
        $skin_dir = $match[1];
    } else {
        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;
        }
    }

    $board = get_board_db($bo_table, true);
    
    $list = array();

    if( ! $board ){
        return '';
    }

    $tmp_write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름
    
    $bo_subject = get_text($board['bo_subject']);

    // $order 값에 따라 랜덤, 히트순, 날짜순(기본) 결정 - 20200116
    $order_sql = 'wr_num';
    if ($order == "hit") {
        $order_sql = 'wr_hit desc';
    } else if ($order == "date") {
        $order_sql = 'wr_datetime desc';
    } else if($order == "rand") {
        $order_sql = ' rand() ';
    } 

    $sql_common = "";
    if ($ca_name) {
        $sql_common .= " and ca_name = '$ca_name' ";
    } 
    if ($imgs) {
        $sql_common .= " and wr_file > 0 ";
    } 
    if ($days) {
        $sql_common .= " and wr_datetime > date_add(now(), interval -$days day) ";			
    }
    if ($mbid) {
        $sql_common .= " and mb_id = '$mbid' ";
    }
    if ($options) {
        $sql_common .= " and ".$options;
    }

    $notice_sql = '';
    if ($notice == 'in') { // 공지만 출력 - 20200116 with SIR(해피정)
        $arr_notice = preg_replace("/\n/",',', trim($board['bo_notice']));
        if ($arr_notice) {
            $notice_sql = " and wr_id IN({$arr_notice}) AND ";
        }
    } else if ($notice == 'out') { // 공지를 제외하고 출력 - 20200116 with SIR(해피정)
        $arr_notice = preg_replace("/\n/",',', trim($board['bo_notice'])); 
        if (!$arr_notice) {
            $arr_notice = 0; 
        }
        $notice_sql = " and wr_id Not IN({$arr_notice}) AND ";
    }

    $sql = " select * from {$tmp_write_table} where wr_is_comment = 0 {$sql_common} {$notice_sql} order by {$order_sql} limit 0, {$rows} ";
    $result = sql_query($sql);

    for ($i=0; $row = sql_fetch_array($result); $i++) {
        try {
            unset($row['wr_password']);     //패스워드 저장 안함( 아예 삭제 )
        } catch (Exception $e) {
        }
        $row['wr_email'] = '';              //이메일 저장 안함
        if (strstr($row['wr_option'], 'secret')){           // 비밀글일 경우 내용, 링크, 파일 저장 안함
            $row['wr_content'] = $row['wr_link1'] = $row['wr_link2'] = '';
            $row['file'] = array('count'=>0);
        }
        $list[$i] = get_list($row, $board, $latest_skin_url, $subject_len);
        $list[$i]['bo_table'] = $bo_table;
        $list[$i]['href'] = get_pretty_url($bo_table, $row['wr_id']);
    }

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

    return $content;
}