<?php

session_start();
header("Cache-Control: no-cache, must-revalidate");

include_once(__DIR__ . "/configs.php");
$link = mysqli_connect(HOST, DB_USER, DB_PWD, DB);
$configs = getConfigs();

//取得配置 
function getConfigs()
{
    global $link;
    $sql = "SELECT * FROM  configs  WHERE 1 ";
    $result = mysqli_query($link, $sql);
    while ($r = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $data[$r['key_name']] = $r['value'];
    }
    return $data;
}

//取得推荐位
function get_recommend()
{
    global $link;

    $sql = "SELECT * FROM websites WHERE url_type =1  and  upload_img !=''   and recommend ='1' ORDER BY  sorts ASC ";
    $result = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $data[$row['id']] = $row;
    }
    return $data;
}

//取得分类
function getCates()
{
    global $link;
    $sql = "SELECT * FROM  cates WHERE 1   order by sorts asc ";
    $result = mysqli_query($link, $sql);
    while ($r = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $cates[$r['cate_id']] = $r;
    }
    return $cates;
}
function getCate($id)
{
    if(!is_numeric($id) || empty($id)) {
        return false;
    }
    global $link;
    $sql = "select *   from cates where  cate_id='".$id."'    ";
    $result = mysqli_query($link, $sql);
    return $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
 

}
// 取得显示 的数据内容 
function shows($url_type = '', $cate_id = '')
{
    global $link;

    $sql = "SELECT * FROM websites WHERE 1 ";
    if ($url_type != '') {
        $sql .= " and  url_type ='" . $url_type . "' ";
    }
    if ($cate_id != '') {
        $sql .= " and  cate_id ='" . $cate_id . "' ";
    }
    $sql .= "  ORDER BY  sorts ASC ";
    $result = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        if ($url_type != '') {
            $data[$row['cate_id']] = $row;
        } else {
            $data[] = $row;
        }
    }
    return $data;
}

//取得留言列表
function getMsgBook()
{
    global $link;
    $sql = "select * from guestbook where 1    order by id  DESC ";
    $result = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $data[] = $row;
    }
    return $data;
}
//取得留言总数
function getMsgBookNumber()
{
    global $link;
    $sql = "select count(*) as counts  from guestbook where 1    ";
    $result = mysqli_query($link, $sql);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    return $row['counts'];
}

function shows_all()
{
    global $link;

    $sql = "SELECT * FROM websites WHERE 1 and  recommend ='0' ";

    $sql .= "  ORDER BY  sorts ASC ";
    $result = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $data[$row['cate_id']][$row['id']] = $row;
    }
    return $data;
}

//取得网址总数
function getUrlNumber()
{
    global $link;
    $sql = "select count(*) as counts  from websites where 1    ";
    $result = mysqli_query($link, $sql);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    return $row['counts'];
}
//手机网页跳转
//如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true
function is_mobile()
{
    $regex_match = "/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
    $regex_match .= "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
    $regex_match .= "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
    $regex_match .= "symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
    $regex_match .= "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
    $regex_match .= ")/i";
    return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
