<?php
define("_GNUBOARD_", TRUE);

//g4m config
$g4['use_mobile']           = true;
$g4['g4m']                  = "m";
$g4['g4m_path']             = "{$g4['path']}/{$g4['g4m']}";
$g4['g4m_bbs']              = "bbs";
$g4['g4m_bbs_path']         = "{$g4['g4m_path']}/{$g4['g4m_bbs']}";
$g4['g4m_url']              = "{$g4['url']}/{$g4['g4m']}";
$g4['g4m_admin']            = "adm";
$g4['g4m_admin_path']       = $g4['g4m_path'] . "/" . $g4['g4m_admin'];
$g4['thumb']                = $g4['g4m_path'] . "/thumb.php";
$g4['thumb_dir']            = $g4['g4m_path'] . "/thumb";
$g4['g4m_site_skin']        = $g4['g4m_path'] . "/siteskin";

/*
 * 구글 GCM Key for server applications, API KEY
 * 발급은 아래 주소에서 가능.
 * https://console.developers.google.com
 * 발급방법 참고 : http://goo.gl/ngverX
 */

$g4['gcm_auth'] = '';

//g4m function
function chkMobile(){
    $mobile_browser = '0';
    if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
        $mobile_browser++;
    }
    if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
        $mobile_browser++;
    }
    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobile_agents = array(
        'w3c ', 'acs-', 'alav', 'alca', 'amoi', 'audi', 'avan', 'benq', 'bird', 'blac',
        'blaz', 'brew', 'cell', 'cldc', 'cmd-', 'dang', 'doco', 'eric', 'hipt', 'inno',
        'ipaq', 'java', 'jigs', 'kddi', 'keji', 'leno', 'lg-c', 'lg-d', 'lg-g', 'lge-',
        'maui', 'maxo', 'midp', 'mits', 'mmef', 'mobi', 'mot-', 'moto', 'mwbp', 'nec-',
        'newt', 'noki', 'oper', 'palm', 'pana', 'pant', 'phil', 'play', 'port', 'prox',
        'qwap', 'sage', 'sams', 'sany', 'sch-', 'sec-', 'send', 'seri', 'sgh-', 'shar',
        'sie-', 'siem', 'smal', 'smar', 'sony', 'sph-', 'symb', 't-mo', 'teli', 'tim-',
        'tosh', 'tsm-', 'upg1', 'upsi', 'vk-v', 'voda', 'wap-', 'wapa', 'wapi', 'wapp',
        'wapr', 'webc', 'winw', 'winw', 'xda', 'xda-');

    if (in_array($mobile_ua, $mobile_agents)) {
        $mobile_browser++;
    }
    if (strpos(strtolower($_SERVER['ALL_HTTP']), 'OperaMini') > 0) {
        $mobile_browser++;
    }
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') > 0) {
        $mobile_browser = 0;
    }


    if ($mobile_browser > 0 && $_GET['from'] != 'mobile') {
        return true;
        //모바일 기기일 경우 모바일 페이지로
        /*
        if (!strstr($_SERVER['HTTP_REFERER'], "")) {
            header("location:");
        }
         *
         */
    }else{
        return false;
    }
}

function get_new_sideview($mb_id, $name="", $email="", $homepage="") {
    global $config, $member, $g4, $bo_table, $i;
    $email = base64_encode($email);
    $str = "\n<ul class='sidemenu'>
                \n<li class='dropdown' id='sidemenu{$i}'>
                    <a class='dropdown-toggle' data-toggle='dropdown' href='#sidemenu{$i}'>
                        {$name}
                        <b class='caret'></b>
                    </a>
                \n<ul class='dropdown-menu'>";
    if($member['mb_id'] && $mb_id) { 
        $str .= "\n<li><a href='{$g4['bbs_path']}/memo_form.php?me_recv_mb_id={$mb_id}'><i class='icon-comment'></i> 쪽지보내기</a></li>\n";
    } 
    $str .= "\n<li><a href='{$g4['bbs_path']}/formmail.php?mb_id={$mb_id}&name={$name}&email={$email}'><i class='icon-envelope'></i> 이메일보내기</a></li>\n";
    if($member['mb_id'] && $mb_id) { 
        $str .= "\n<li><a href='{$g4['bbs_path']}/profile.php?mb_id={$mb_id}'><i class='icon-user'></i> 자기소개</a></li>\n";
    }
    if($homepage) { 
        $str .= "\n<li><a href='{$homepage}' target='_blank'><i class='icon-home'></i> 홈페이지</a></li>\n";
    } 
    if($mb_id) { 
        $str .= "\n<li><a href='{$g4['bbs_path']}/board.php?bo_table={$bo_table}&sca={$_GET['sca']}&sfl=mb_id,1&stx={$mb_id}'><i class='icon-search'></i> 아이디로검색</a></li>\n";
    }else{ 
        $str .= "\n<li><a href='{$g4['bbs_path']}/board.php?bo_table={$bo_table}&sca={$_GET['sca']}&sfl=wr_name,1&stx={$name}'><i class='icon-search'></i> 이름으로검색</a></li>\n";
    } 
    if($member['mb_id'] && $mb_id) { 
        $str .= "\n<li><a href='{$g4['bbs_path']}/new.php?mb_id=neojins'><i class='icon-list-alt'></i> 전체게시물</a></li>\n";
    } 
    $str .= "\n</ul>
        \n</li>
    \n</ul>";
    return $str;
}

function alert_html($msg, $url='',$debug=false){
    global $config, $member, $g4, $is_member, $is_admin;
    include_once $g4['g4m_bbs_path']."/_head.php";
    echo "<div class='page-header'>Message</div>";
    $msg = str_replace('\n', '<br/>', $msg);
    echo "<div class='alert alert-danger'>".$msg."</div>";
    if($debug){
        echo "<div class='alert alert-info'><h2>POST</h2>";
        print_r2($_POST);
        echo "</div>";
        echo "<div class='alert alert-info'><h2>GET</h2>";
        print_r2($_GET);
        echo "</div>";
    }
    echo "<div class=''><a href='javascript:;' class='btn btn-100 btn-primary history-back'>뒤로 이동</a>";
    if($url){
        
    }
    echo "</div>";
    include_once $g4['g4m_bbs_path']."/_tail.php";
    
    exit;
}

//GCM
function sendMessage($auth, $registration_id, $title, $contents, $noti_msg, $type, $url) {
    global $g4;
    $contents = cut_str($contents, 60);
    $data = array(
        'registration_ids' => array($registration_id),
        'data' => array('title' => $title, 'msg' => $contents, 'ticker' => $noti_msg,'type'=>$type,'url'=>$url)
    );

    $headers = array(
        "Content-Type:application/json",
        "Authorization:key=" . $auth
    );

    //print("<XMP>");
    //print(json_encode($data));
    //print("\n");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    $result = curl_exec($ch);

    //print_r($result);
    curl_close($ch);
}