<?php
    error_reporting(0);
    define('SYSTEM_ROOT', dirname(__FILE__) . '/');
    define('ROOT', dirname(SYSTEM_ROOT) . '/');
    include ROOT.'config.php';
    include ROOT.'/core/db.class.php';
    $DB = new PdoHelper($dbconfig);


    $act = daddslashes($_GET['act'])??null;
    $id = daddslashes($_GET['id'])??null;
    $class = daddslashes($_GET['class'])??null;
    $view = daddslashes($_GET['view'])??null;
    

    function webServer(){
        if (!isset($_SERVER['SERVER_SOFTWARE'])) {
            return '未检测到服务器类型';
        }
        $webServer = strtolower($_SERVER['SERVER_SOFTWARE']);
        if (strpos($webServer, 'apache') !== false) {
            return 'apache';
        } elseif (strpos($webServer, 'nginx') !== false) {
            return 'nginx';
        }else{
            return '未检测到服务器类型';
        }
    }

    function active($mod){
      preg_match('/n\/(.*?)\./sS',$_SERVER['DOCUMENT_URI'],$name);
      if($name[1]==$mod){
        return 'layui-this';
      }
    }

    function ad(){
        $res = file_get_contents('http://api.52.run/?act=seo_ad');
        return $res;
    }

    function rootPath($mod=false){
      if(dirname(dirname(__FILE__))==$_SERVER["DOCUMENT_ROOT"]){
        $res = substr(dirname(__FILE__),strripos(dirname(__FILE__),"/")+1);
      }elseif(dirname(dirname(dirname(__FILE__)))==$_SERVER["DOCUMENT_ROOT"]){
        $res = substr(dirname(dirname(__FILE__)),strripos(dirname(dirname(__FILE__)),"/")+1);
      }
      if($res=='core'){
        $res = '';
      }
      if($mod=='/'){
        return $res.'/';
      }else{
        return $res;
      }
    }

    function siteHttp(){
      $res = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
      return $res.$_SERVER['HTTP_HOST'];
    }

    function conf($k){
        global $DB;
        $res = $DB->getRow("SELECT * FROM `pre_config` WHERE `k`='".$k."'");
        return $res['v'];
    }

  function randTitle(){
    $arr = ['雨','尘','云','梦','爱','久','零','诚','牛','元','园','依','逸','益','怡','力','子','瓜','糖','心','沉','晨','白','糖','客','韵','音','抖','思','者','赞','苹','果','粉','丝','空','人','筷','首','免','福','南','北','西','东','苏','虎','猫','狗','鸟','乐','夏','春','东','秋','天','小','上','王','依','桐','静','玥','老','酷','财','富','九','莫','月','阳','光','海','星','宝','迪','迦','奥','特','曼'];
    $arr_num = count($arr)-1;
    $randText = $arr[mt_rand(0,$arr_num)].$arr[mt_rand(0,$arr_num)];
    return $randText;
    }

    function view_error(){
        exit('<!DOCTYPE html><html><head><meta charset=UTF-8><title>404页面错误提示-页面未找到</title><meta name=robots content="noindex,nofollow"><meta name=viewport content="width=device-width,initial-scale=1,user-scalable=no"><style>body{margin:0;padding:0;font-family:Helvetica,Arial,"PingFang SC","Microsoft YaHei","WenQuanYi Micro Hei","tohoma,sans-serif"}a,button.submit{color:#6E7173;text-decoration:none;-webkit-transition:all .1s ease-in;-moz-transition:all .1s ease-in;-o-transition:all .1s ease-in;transition:all .1s ease-in}a:hover,a:active{color:#6E7173}.body404{position:absolute;height:100%;width:100%;background:#fff;background-size:auto 100%;text-shadow:1px 1px 0 #fff}.body-about .body404{background:#fff}.site-name404{margin:0 auto;text-align:center;letter-spacing:2px;font:400 150px/1 “Helvetica Neue”,Helvetica,Arial;color:#444}.site-name404 h1{margin:0 0 10px;font-size:50px;line-height:1.2}.title404 span{font-size:15px;width:2px}.site-name404 i{font-style:normal}.title404 p{font-size:20px;line-height:1.5;margin:0;color:#444}.info404{position:absolute;top:30%;text-align:center;width:100%;margin-top:-160px}.body-about .info404{margin-top:-180px}#footer404{margin-top:30px;font-size:10px}.index404{margin-top:24px;display:inline-block;white-space:nowrap;cursor:pointer;background:#444;letter-spacing:1px;font-size:14px;-moz-user-select:-moz-none;-webkit-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;text-shadow:none;border:1px solid #ccc;line-height:36px;text-align:center;height:36px;padding:0 25px;border-radius:16px;-webkit-transition-duration:400ms;transition-duration:400ms;background-color:#fff;color:#999}.index404:hover{color:#F77B83;border-color:#F77B83;outline-style:none}.icon-about{padding:10px 0 25px}.icon-about a{font-size:20px;margin:5px;color:#fff;background-color:#000;border-radius:100%;padding:6px}</style><body><div class=body404><div class=info404><header id=header404 class=clearfix><div class=site-name404><i>404</i></div></header><section><div class=title404><p>哎呀！找不到页面了！</p><p>不要难过，建议亲核对下地址哦！</p><a href="http://'.$_SERVER['HTTP_HOST'].'" class=index404>返回首页</a></section></div></div>');
    }

    function randView(){
        global $DB;
        $viewInfo = $DB->query("SELECT * FROM `pre_view` ORDER BY RAND() LIMIT 5");
        while($res = $viewInfo->fetch()){
            $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$res['class']."'");
            if(conf('rewrite')==1){
                $isHref = siteHttp().'/'.rootPath().'/'.$classInfo['path'].'/'.$res['id'].'.html';
            }else{
                $isHref = siteHttp().'/'.rootPath().'/index.php?class='.$classInfo['path'].'&view='.$res['id'];
            }
            $content .= '<font size="1"><a style="color:black" href="'.$isHref.'">'.$res['title'].'-'.$res['title2'].'</a></font> | ';
        }
        return $content;
    }

    function view_start($class,$view){
        global $DB;
        if($class && !$view){   //分类页面
            include ROOT.'view/index/index.php';
        }elseif($class && $view){  //页面
        $DB->query("UPDATE `pre_view` SET `pvAll`=`pvAll`+1 WHERE `id` = '".$view."'");
            $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `path`='".$class."'");
            $viewInfo = $DB->getRow("SELECT * FROM `pre_view` WHERE `id`='".$view."'");
            $classPath = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$viewInfo['class']."'");
            if($classPath['path']!=$class){
                view_error();
            }
            if(file_exists(ROOT.'view/muban/'.$viewInfo['muban'].'/index.php')){
                include ROOT.'view/muban/'.$viewInfo['muban'].'/index.php';
            }else{
                include ROOT.'view/muban/default/index.php';
            }
        }else{          
            include ROOT.'view/index/index.php';
        }
    }

    function admin_error(){
        exit('<div class="layui-fluid">
  <div class="layadmin-tips">
    <i class="layui-icon" face>&#xe664;</i>
    <div class="layui-text">
      <h1>
        <span class="layui-anim layui-anim-loop layui-anim-">4</span> 
        <span class="layui-anim layui-anim-loop layui-anim-rotate">0</span> 
        <span class="layui-anim layui-anim-loop layui-anim-">4</span>
      </h1>
    </div>
  </div>
</div>');
    }

    function daddslashes($string, $force = 0, $strip = FALSE) {
        !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
        if(!MAGIC_QUOTES_GPC || $force) {
            if(is_array($string)) {
                foreach($string as $key => $val) {
                    $string[$key] = daddslashes($val, $force, $strip);
                }
            } else {
                $string = addslashes($strip ? stripslashes($string) : $string);
            }
        }
        return $string;
    }

    function rewriteHref(){
        if(conf('rewrite')){
            return '..';
        }else{
            return '.';
        }
    }

    function getMuban(){
      $dir = '../view/muban/';
      $dirArray[] = NULL;
        if (false != ($handle = opendir($dir))) {
            $i = 0;
            while (false !== ($file = readdir($handle))) {
                if($file != "." && $file != ".." && $file != ".DS_Store" && !strpos($file, ".")) {
                    $dirArray[$i] = $file;
                    $i++;
                }
            }
            closedir($handle);
        }
        return $dirArray;
  }

  function baiduapi($url){
    $api = conf('baiduApi');
    $ch = curl_init();
    $options =  array(
        CURLOPT_URL => $api,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => implode("\n", $url),
        CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
    );
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    return $result;
}
?>