<?php
$g4_path = '.'; // common.php 의 상대 경로
include_once ($g4_path.'/common.php');

$is_amina_setup = true;

include_once(AMINA_PATH.'/lib/admin.lib.php');

//rss관련 필드 추가
if(!amina_chk_field("rss", "wr_daum")) {
    sql_query(" alter table {$g4['board_new_table']} add wr_rss varchar(10) default '' NOT NULL ", false);
	sql_query(" alter table {$g4['board_new_table']} add wr_photo varchar(10) default '' NOT NULL AFTER wr_rss ", false);
    sql_query(" alter table {$g4['board_new_table']} add wr_video varchar(10) default '' NOT NULL AFTER wr_photo ", false);
    sql_query(" alter table {$g4['board_new_table']} add wr_main varchar(10) default '' NOT NULL AFTER wr_video ", false);
    sql_query(" alter table {$g4['board_new_table']} add wr_good varchar(10) default '' NOT NULL AFTER wr_main ", false);
    sql_query(" alter table {$g4['board_new_table']} add wr_daum varchar(10) default '' NOT NULL AFTER wr_good ", false);
}

//아미나 설정값 테이블 추가
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina']}` (
			`id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`gr_id` varchar(255) NOT NULL default '',
			`bo_type` tinyint(4) NOT NULL default '0',
			`auto_rss` tinyint(4) NOT NULL default '0',
			`video` tinyint(4) NOT NULL default '0',
			`google_map` tinyint(4) NOT NULL default '0',
			`view_code` varchar(255) NOT NULL default '',
			`poll_level` tinyint(4) NOT NULL default '0',
			`poll_point` int(11) NOT NULL default '0',
			`tag` tinyint(4) NOT NULL default '0',
			`tag_mb` text NOT NULL default '',
			`nameless_write` tinyint(4) NOT NULL default '0',
			`nameless_cmt` tinyint(4) NOT NULL default '0',
			`nameless_head` varchar(255) NOT NULL default '',
			`nameless_tail` int(11) NOT NULL default '0',
			`view_photo` tinyint(4) NOT NULL default '0',
			`cmt_photo` tinyint(4) NOT NULL default '0',
			`emo_skin` varchar(255) NOT NULL default '',
			`emo_cnt` int(11) NOT NULL default '0',
			`limit_post` tinyint(4) NOT NULL default '0',
			`limit_post_cnt` tinyint(4) NOT NULL default '0',
			`limit_cmt` tinyint(4) NOT NULL default '0',
			`limit_date` int(11) NOT NULL default '0',
			`shingo` int(11) NOT NULL default '0',
			`upimg_size` int(11) NOT NULL default '0',
			`upimg_quality` int(11) NOT NULL default '0',
			`good_point` int(11) NOT NULL default '0',
			`good_repoint` int(11) NOT NULL default '0',
			`star` tinyint(4) NOT NULL default '0',
			`star_color` varchar(255) NOT NULL default '',
			`star_point` int(11) NOT NULL default '0',
			`star_repoint` int(11) NOT NULL default '0',
			`cmt_cool` tinyint(4) NOT NULL default '0',
			`cmt_cool_txt` varchar(255) NOT NULL default '',
			`cmt_bad_txt` varchar(255) NOT NULL default '',
			`cmt_good` tinyint(4) NOT NULL default '0',
			`cmt_best` int(11) NOT NULL default '0',
			`cmt_good_txt` varchar(255) NOT NULL default '',
			`cmt_good_point` int(11) NOT NULL default '0',
			`cmt_good_repoint` int(11) NOT NULL default '0',
			`cmt_choice` tinyint(4) NOT NULL default '0',
			`cmt_choice_return` int(11) NOT NULL default '0',
			`cmt_choice_txt` varchar(255) NOT NULL default '',
			`po_view` tinyint(4) NOT NULL default '0',
			`po_view_cnt` tinyint(4) NOT NULL default '0',
			`po_view_day` tinyint(4) NOT NULL default '0',
			`po_view_time` tinyint(4) NOT NULL default '0',
			`po_view_term` smallint(6) NOT NULL default '0',
			`po_view_fee` tinyint(4) NOT NULL default '0',
			`po_view_free` tinyint(4) NOT NULL default '0',
			`po_view_level` tinyint(4) NOT NULL default '0',
			`po_down` tinyint(4) NOT NULL default '0',
			`po_down_cnt` tinyint(4) NOT NULL default '0',
			`po_down_day` tinyint(4) NOT NULL default '0',
			`po_down_time` tinyint(4) NOT NULL default '0',
			`po_down_term` smallint(6) NOT NULL default '0',
			`po_down_fee` tinyint(4) NOT NULL default '0',
			`po_down_free` tinyint(4) NOT NULL default '0',
			`po_down_level` tinyint(4) NOT NULL default '0',
			`editor` tinyint(4) NOT NULL default '0',
			`resize` tinyint(4) NOT NULL default '0',
			`uploader` tinyint(4) NOT NULL default '0',
			`move` tinyint(4) NOT NULL default '0',
			`move_bo` varchar(20) NOT NULL default '',
			`move_ca` varchar(255) NOT NULL default '',
			`move_term` smallint(6) NOT NULL default '0',
			`move_hit` smallint(6) NOT NULL default '0',
			`move_cmt` smallint(6) NOT NULL default '0',
			`move_good` tinyint(4) NOT NULL default '0',
			`move_cnt` smallint(6) NOT NULL default '0',
			`move_star` smallint(6) NOT NULL default '0',
			`move_score` decimal(4,2) NOT NULL default '0',
			`lucky_rate` smallint(6) NOT NULL default '0',
			`lucky_point` smallint(6) NOT NULL default '0',
			`wm` tinyint(4) NOT NULL default '0',
			`wm_file` varchar(255) NOT NULL default '',
			`wm_loc` varchar(20) NOT NULL default '',
			`wm_x` smallint(6) NOT NULL default '0',
			`wm_y` smallint(6) NOT NULL default '0',
			`limit_down_cmt` tinyint(4) NOT NULL default '0',
			`limit_down_good` tinyint(4) NOT NULL default '0',
			`limit_down_star` tinyint(4) NOT NULL default '0',
			`limit_down_poll` tinyint(4) NOT NULL default '0',
			`limit_good_down` tinyint(4) NOT NULL default '0',
			`limit_good_cmt` tinyint(4) NOT NULL default '0',
			`limit_good_star` tinyint(4) NOT NULL default '0',
			`limit_good_poll` tinyint(4) NOT NULL default '0',
			`limit_poll_down` tinyint(4) NOT NULL default '0',
			`limit_poll_cmt` tinyint(4) NOT NULL default '0',
			`limit_poll_good` tinyint(4) NOT NULL default '0',
			`limit_poll_star` tinyint(4) NOT NULL default '0',
			`limit_star_down` tinyint(4) NOT NULL default '0',
			`limit_star_cmt` tinyint(4) NOT NULL default '0',
			`limit_star_good` tinyint(4) NOT NULL default '0',
			`limit_star_poll` tinyint(4) NOT NULL default '0',
			`auto_sns` tinyint(4) NOT NULL default '0',
			`daum_view` tinyint(4) NOT NULL default '0',
			`autosave` tinyint(4) NOT NULL default '0',
			`mileage` tinyint(4) NOT NULL default '0',
			`syndi` tinyint(4) NOT NULL default '0',
			PRIMARY KEY  (`id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//아미나스킨 설정값 테이블 추가
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_skin']}` (
			`id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`gr_id` varchar(255) NOT NULL default '',
			`sca` varchar(255) NOT NULL default '',
			`skin_type` tinyint(4) NOT NULL default '0',
			`cate_skin` varchar(255) NOT NULL default '',
			`cate_css` varchar(255) NOT NULL default '',
			`head_skin` varchar(255) NOT NULL default '',
			`head_css` varchar(255) NOT NULL default '',
			`list_skin` varchar(255) NOT NULL default '',
			`list_css` varchar(255) NOT NULL default '',
			`list_set` varchar(255) NOT NULL default '',
			`list_rows` smallint(6) NOT NULL default '0',
			`view_skin` varchar(255) NOT NULL default '',
			`view_css` varchar(255) NOT NULL default '',
			`view_set` varchar(255) NOT NULL default '',
			`view_good` varchar(255) NOT NULL default '',
			`view_sns` varchar(255) NOT NULL default '',
			`star_skin` varchar(255) NOT NULL default '',
			`poll_skin` varchar(255) NOT NULL default '',
			`preview_skin` varchar(255) NOT NULL default '',
			`related_skin` varchar(255) NOT NULL default '',
			`cmt_skin` varchar(255) NOT NULL default '',
			`cmt_css` varchar(255) NOT NULL default '',
			`cmt_rows` smallint(6) NOT NULL default '0',
			`cmt_sns` varchar(255) NOT NULL default '',
			`sign_skin` varchar(255) NOT NULL default '',
			`sign_css` varchar(255) NOT NULL default '',
			`sign_set` varchar(255) NOT NULL default '',
			`tag_skin` varchar(255) NOT NULL default '',
			`tag_css` varchar(255) NOT NULL default '',
			`tag_set` varchar(255) NOT NULL default '',
			`linked_tab` text NOT NULL default '',
			`linked_skin` varchar(255) NOT NULL default '',
			`linked_css` varchar(255) NOT NULL default '',
			`addon_skin` varchar(255) NOT NULL default '',
			`addon_css` varchar(255) NOT NULL default '',
			`addon_set` varchar(255) NOT NULL default '',
			`info_skin` varchar(255) NOT NULL default '',
			`info_css` varchar(255) NOT NULL default '',
			`info_set` varchar(255) NOT NULL default '',
			`write_skin` varchar(255) NOT NULL default '',
			`page_skin` varchar(255) NOT NULL default '',
			`page_css` varchar(255) NOT NULL default '',
			`page_rows` smallint(6) NOT NULL default '0',
			`btn_skin` varchar(255) NOT NULL default '',
			`deco_list` varchar(255) NOT NULL default '',
			`deco_view` varchar(255) NOT NULL default '',
			`deco_write` varchar(255) NOT NULL default '',
			`attach_skin` varchar(255) NOT NULL default '',
			`attach_css` varchar(255) NOT NULL default '',
			`attach_set` varchar(255) NOT NULL default '',
			PRIMARY KEY  (`id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//별점 테이블 추가
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_star']}` (
			`bg_id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`wr_id` int(11) NOT NULL default '0',
			`mb_id` varchar(20) NOT NULL default '',
			`bg_flag` varchar(255) NOT NULL default '',
			`bg_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			`bg_star` int(11) NOT NULL default '0',
			`bg_ip` varchar(20) NOT NULL default '',
			PRIMARY KEY  (`bg_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//블라인드 테이블 추가
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_blind']}` (
			`id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`wr_id` int(11) NOT NULL default '0',
			`mb_id` varchar(20) NOT NULL default '',
			`flag` varchar(255) NOT NULL default '',
			`datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			`ip` varchar(20) NOT NULL default '',
			PRIMARY KEY  (`id`),
			UNIQUE KEY `fkey1` (`bo_table`,`wr_id`,`mb_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//포인트 테이블 추가
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_point']}` (
			`po_id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`wr_id` int(11) NOT NULL default '0',
			`mb_id` varchar(20) NOT NULL default '',
			`po_mb_id` varchar(20) NOT NULL default '',
			`po_cnt` int(11) NOT NULL default '0',
			`po_point` int(11) NOT NULL default '0',
			`po_payment` int(11) NOT NULL default '0',
			`po_flag` varchar(255) NOT NULL default '',
			`po_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			PRIMARY KEY  (`po_id`),
			KEY `mb_id` (`mb_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//태그로그 테이블(v2.1.6)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_taglog']}` (
			`id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`wr_id` int(11) NOT NULL default '0',
			`tag_id` int(11) NOT NULL default '0',
			`tag` varchar(255) NOT NULL default '',
			`regdate` datetime NOT NULL default '0000-00-00 00:00:00',
			`wr_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			PRIMARY KEY  (`id`),
			KEY tag_id (tag_id,bo_table,wr_id)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//태그로그에 글작성일 등록(v3.1.7)
sql_query(" alter table {$g4['amina_taglog']} add wr_datetime datetime default '0000-00-00 00:00:00' NOT NULL ", false);

//태그 테이블(v2.1.6)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_tag']}` (
			`id` int(11) NOT NULL auto_increment,
			`tag` varchar(255) NOT NULL default '',
			`cnt` int(11) NOT NULL default '0',
			`desc` text NOT NULL default '',
			`regdate` datetime NOT NULL default '0000-00-00 00:00:00',
			`lastdate` datetime NOT NULL default '0000-00-00 00:00:00',
			PRIMARY KEY  (`id`),
			KEY tag (tag,lastdate)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//캐쉬 테이블(v2.1.6)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_cache']}` (
			`c_id` int(11) NOT NULL AUTO_INCREMENT,
			`c_name` varchar(255) NOT NULL,
			`c_text` text NOT NULL,
			`c_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			PRIMARY KEY (`c_id`),
			UNIQUE KEY `c_name` (`c_name`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//설문 테이블(v2.1.7)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_poll']}` (
			`poll_id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`wr_id` int(11) NOT NULL default '0',
			`ca_name` varchar(255) NOT NULL default '',
			`poll_subject` varchar(255) NOT NULL default '',
			`poll_item1` varchar(255) NOT NULL default '',
			`poll_item2` varchar(255) NOT NULL default '',
			`poll_item3` varchar(255) NOT NULL default '',
			`poll_item4` varchar(255) NOT NULL default '',
			`poll_item5` varchar(255) NOT NULL default '',
			`poll_item6` varchar(255) NOT NULL default '',
			`poll_item7` varchar(255) NOT NULL default '',
			`poll_item8` varchar(255) NOT NULL default '',
			`poll_item9` varchar(255) NOT NULL default '',
			`poll_cnt` int(11) NOT NULL default '0',
			`poll_cnt1` int(11) NOT NULL default '0',
			`poll_cnt2` int(11) NOT NULL default '0',
			`poll_cnt3` int(11) NOT NULL default '0',
			`poll_cnt4` int(11) NOT NULL default '0',
			`poll_cnt5` int(11) NOT NULL default '0',
			`poll_cnt6` int(11) NOT NULL default '0',
			`poll_cnt7` int(11) NOT NULL default '0',
			`poll_cnt8` int(11) NOT NULL default '0',
			`poll_cnt9` int(11) NOT NULL default '0',
			`poll_level` tinyint(4) NOT NULL default '0',
			`poll_point` int(11) NOT NULL default '0',
			`poll_regdate` datetime NOT NULL default '0000-00-00 00:00:00',
			`poll_enddate` datetime NOT NULL default '0000-00-00 00:00:00',
			PRIMARY KEY (`poll_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//설문로그 테이블(v2.1.7)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_polllog']}` (
			`poll_id` int(11) NOT NULL auto_increment,
			`bo_table` varchar(20) NOT NULL default '',
			`wr_id` int(11) NOT NULL default '0',
			`mb_id` varchar(20) NOT NULL default '',
			`poll` tinyint(4) NOT NULL default '0',
			`poll_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			`poll_ip` varchar(20) NOT NULL default '',
			PRIMARY KEY  (`poll_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//멀티업로드(v3.0.3)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_file']}` (
            `id` int not null auto_increment,
            `bo_table` varchar(20) not null,
            `bf_no` int not null,
            `mb_id` varchar(20) not null,
            `bf_source` varchar(255) not null,
            `bf_file` varchar(255) not null,
            `bf_filesize` int not null,
            `bf_width` int not null,
            `bf_height` int not null,
            `bf_type` tinyint not null,
            `bf_datetime` datetime not null,
            `bf_ip` varchar(20) not null,
            primary key (id),
            index (bo_table, mb_id, bf_no)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//네이버 신디(v3.2.5)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['syndi_log_table']}` (
			`content_id` int(11) NOT NULL, 
			`bbs_id` varchar(50) NOT NULL, 
			`title` text NOT NULL,
			`link_alternative` varchar(250) NOT NULL, 
			`delete_date` varchar(14) NOT NULL, 
			PRIMARY KEY  (`content_id`,`bbs_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//유니크키(v3.2.5)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['uniqid_table']}` (
			`uq_id` bigint(20) unsigned NOT NULL,
			`uq_ip` varchar(255) NOT NULL,
			PRIMARY KEY (`uq_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//임시저장(v3.2.5)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['autosave_table']}` (
			`as_id` int(11) NOT NULL AUTO_INCREMENT,
			`mb_id` varchar(20) NOT NULL,
			`as_uid` bigint(20) unsigned NOT NULL,
			`as_subject` varchar(255) NOT NULL,
			`as_content` text NOT NULL,
			`as_datetime` datetime NOT NULL,
			PRIMARY KEY (`as_id`),
			UNIQUE KEY `as_uid` (`as_uid`),
			KEY `mb_id` (`mb_id`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//마일리지(v3.2.5)
$sql = " CREATE TABLE IF NOT EXISTS `{$g4['amina_mileage']}` (
			`po_id` int(11) NOT NULL auto_increment,
			`mb_id` varchar(20) NOT NULL default '',
			`from_id` varchar(20) NOT NULL default '',
			`po_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
			`po_content` varchar(255) NOT NULL default '',
			`po_point` int(11) NOT NULL default '0',
			`po_rel_table` varchar(20) NOT NULL default '',
			`po_rel_id` varchar(20) NOT NULL default '',
			`po_rel_action` varchar(255) NOT NULL default '',
			PRIMARY KEY  (`po_id`),
			KEY `index1` (`mb_id`,`po_rel_table`,`po_rel_id`,`po_rel_action`)
			) DEFAULT CHARSET=utf8; ";

sql_query($sql, false);

//삭제를 위해 퍼미션 변경
@chmod(G4_PATH.'/setup.php', 0606);
@unlink(G4_PATH.'/setup.php');

alert('DB에 아미나 스킨용 테이블과 필드를 생성하였습니다.', G4_ADMIN_URL.'/amina.admin.php?bo_table='.$bo_table);

?>