<?php

/*
 * 메타테그 플러그인
 */
if (!defined('_GNUBOARD_')) {
    exit;
}

function plugin_metatag($stript = false) {
    global $g5, $board, $config, $view;

    $cfg = get_plugin_config('metatag'); //플러그인 환경설정
    //플러그인 미설정 중지
    if (!$cfg) {
        return false;
    }
    //플러그인 미사용 중지
    if ($cfg['pc_use'] == 0) {
        return false;
    }
    if (!$stript) {
        if ($_GET['bo_table'] && $_GET['wr_id']) {
            if (validate_alphanumeric_underscore($_GET['bo_table']) && validate_alphanumeric_underscore($_GET['bo_table'])) {
                $metatag = '<meta property="og:title"           content=""/>
                    <meta property="og:site_name"       content=""/>
                    <meta property="og:type"            content="article,"/>
                    <meta property="og:url"             content=""/>
                    <meta property="og:image"           content=""/>
                    <meta property="og:video"           content=""/>
                    <meta property="og:description"     content=""/>';
                echo $metatag;
            }
        } else {
            return false;
        }
    } else {
        //첨부파일이 있으면 처리한다.
        if ($view['file']['0']['file']) {
            $img = $view['file']['0']['file'];
        }
        if (preg_match('/<iframe.*src=\"(.*)\".*><\/iframe>/isU', $view['wr_content'], $match)) {
            $video_id = $match['1'];
        }
        echo "$(function(){
            $('meta[property=\"og:title\"]').val('{$g5['title']}');
            $('meta[property=\"og:site_name\"]').val('{$config['cf_title']}');
            $('meta[property=\"og:type\"]').val('article');
            $('meta[property=\"og:video\"]').val('{$video_id}');
            $('meta[property=\"og:url\"]').val('" . G5_BBS_URL . "/board.php?bo_table={$_GET['bo_table']}&wr_id={$_GET['wr_id']}');
            $('meta[property=\"og:image\"]').val('{$img}');
        });";
    }
}
