<?php 
  //qq1900432277
  include '../core/core.php';
  $act = daddslashes($_GET['act'])??null;
  $page = daddslashes($_GET['page'])??null;
  $limit = daddslashes($_GET['limit'])??null;
  $id = daddslashes($_GET['id'])??null;
  
  if($_COOKIE['adminToken']!=md5(conf('name').conf('password'))){
    exit('{"code":0,"msg":"您未登录"}');
  }

  if($act=='sitemap'){
    $viewInfo = $DB->query("SELECT * FROM `pre_view` ORDER BY `id` DESC");
    $sitemapUrl = siteHttp().'/'.rootPath().'/';
    $str = '<?xml version="1.0" encoding="UTF-8"?>
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ';
    while($res = $viewInfo->fetch()){
      $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$res['class']."'");
      if(conf('rewrite')){
          $rewriteHref = $classInfo['path'].'/'.$res['id'].'.html';
        }else{
          $rewriteHref = 'index.php?class='.$classInfo['path'].'&amp;id='.$res['id'];
        }
       $str .= '<url><loc>'.$sitemapUrl.$rewriteHref.'</loc><lastmod>'.$res['addtime'].'</lastmod><changefreq>daily</changefreq><priority>0.8</priority></url>
       ';
    }
    $str.= '</urlset>';
    $res = file_put_contents('../sitemap.xml', $str);
    if($res){
      exit('{"code":1,"msg":"生成成功"}');
    }else{
      exit('{"code":0,"msg":"生成失败"}');
    }
  }elseif($act=='viewTuisong'){
    $mod =  daddslashes($_GET['mod'])??null;
    if($mod=='view' && $id){
      $viewInfo = $DB->getRow("SELECT * FROM `pre_view` WHERE `id`='".$id."'");
      if($viewInfo){
        $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$viewInfo['class']."'");
        if(conf('rewrite')==1){
          $url[] = siteHttp().'/'.$classInfo['path'].'/'.$viewInfo['id'].'.html';
        }else{
          $url[] = siteHttp().'/index.php?class='.$classInfo['path'].'&view='.$viewInfo['id'];
        }
        $baiduApiJson = json_decode(baiduapi($url),1);
        if($baiduApiJson['error']){
           exit('{"code":0,"msg":"失败：'.$baiduApiJson['message'].'"}');
        }else{
            exit('{"code":1,"msg":"成功提交：'.$baiduApiJson['success'].'条<br>今日剩余：'.$baiduApiJson['remain'].'"}');
        }
     }
   }elseif($mod=='class' && $id){
      $viewInfo = $DB->query("SELECT * FROM `pre_view` WHERE `class`='".$id."'");
      while($row = $viewInfo->fetch()){
        $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$row['class']."'");
        if(conf('rewrite')==1){
          $url[] = siteHttp().'/'.$classInfo['path'].'/'.$row['id'].'.html';
        }else{
          $url[] = siteHttp().'/index.php?class='.$classInfo['path'].'&view='.$row['id'];
        }
        $urls[] .= $url;
      }
      $baiduApiJson = json_decode(baiduapi($url),1);
        if($baiduApiJson['error']){
           exit('{"code":0,"msg":"失败：'.$baiduApiJson['message'].'"}');
        }else{
            exit('{"code":1,"msg":"成功提交：'.$baiduApiJson['success'].'条<br>今日剩余：'.$baiduApiJson['remain'].'"}');
        }
   }
  }elseif($act=='update'){
    $res = file_get_contents('http://api.52.run/?act=seo_up&v='.daddslashes($_GET['v']).'');
    echo $res;
  }elseif($act=='configEdit'){
    if($_POST){
      foreach($_POST as $k=>$v){
        $DB->query("UPDATE `pre_config` SET `v`='".$v."' WHERE `k`='".$k."'");
      }
      exit('{"code":1,"msg":"修改成功"}');
    }
  }elseif($act=='viewList'){
    $page = $limit*($page - 1);
    if($id){
      $viewInfo = $DB->query("SELECT id,title,class,addtime,pvAll FROM `pre_view` WHERE `id`='".$id."' ORDER BY `id` DESC limit {$page},{$limit}");
      $viewNum = $DB->getColumn("SELECT count(id) FROM `pre_view` WHERE `id`='".$id."'");
    }else{
      $viewInfo = $DB->query("SELECT id,title,class,addtime,pvAll FROM `pre_view` ORDER BY `id` DESC limit {$page},{$limit}");
      $viewNum = $DB->getColumn("SELECT count(id) FROM `pre_view`");
    }
    while($row = $viewInfo->fetch()){
      $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$row['class']."'");
      $row['class'] = $classInfo['title'];
      $infoArr[] = $row;
    }
    echo json_encode(array('code'=>0,'msg'=>'success','count'=>$viewNum,'data'=>$infoArr));
  }elseif($act=='viewSee'){
    if($id){
      $viewInfo = $DB->getRow("SELECT * FROM `pre_view` WHERE `id`='".$id."'");
      if($viewInfo){
        $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$viewInfo['class']."'");
        if(conf('rewrite')==1){
          $url = '../'.$classInfo['path'].'/'.$viewInfo['id'].'.html';
        }else{
          $url = '../index.php?class='.$classInfo['path'].'&view='.$viewInfo['id'];
        }
        exit('{"code":1,"msg":"获取成功","url":"'.$url.'"}');
      }else{
        exit('{"code":0,"msg":"ID不存在"}');
      }
    }else{
      exit('{"code":0,"msg":"ID不能为空"}');
    }
  }elseif($act=='viewDel'){
    if($id){
      $res = $DB->query("DELETE FROM `pre_view` WHERE `id`='".$id."'");
      if($res){
        exit('{"code":1,"msg":"删除成功"}');
      }else{
        exit('{"code":0,"msg":"删除失败"}');
      }
    }else{
      exit('{"code":0,"msg":"ID不能为空"}');
    }
  }elseif($act=='viewPlAdd'){
    $class = daddslashes($_POST['class']);
    $num = daddslashes($_POST['num']);
    $muban = daddslashes($_POST['muban']);
    $xdurl = daddslashes($_POST['xdurl']);
    $newurl = daddslashes($_POST['newurl']);
    if($num && $class && $muban && $xdurl && $newurl){
      $res = $DB->getRow("SELECT * FROM `pre_class` WHERE `path`='".$class."'");
      if(!$res['word'] || empty($res['word'])){
       $isWord = 1;
      }
      $classVal = daddslashes($res['id']);
      $word = explode(',',$res['word']);
      $title2_num = count($word)-1;
      if($isWord){
        $title2 = '';
        $keywords = '';
        $description = '本'.$res['title'].'已运营数年之久，受众多国内外用户支持，安全快捷稳定，我们的业务内容广泛，期待您的光临！';
      }
      $successNum = 0;
      for($i=1;$i<=$num;$i++){
        $result = $DB->query("INSERT INTO `pre_view`(`title`, `title2`, `keywords`, `description`, `xdurl`, `newurl`,`class`, `muban`, `addtime`) VALUES ('".daddslashes(randTitle().$res['title'])."','".daddslashes($word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)])."','".daddslashes($word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)].','.$word[mt_rand(0,$title2_num)])."','".daddslashes('本'.$res['title'].'已运营数年之久，受众多国内外用户支持，安全快捷稳定，我们的业务内容广泛，'.$word[mt_rand(0,$title2_num)].$word[mt_rand(0,$title2_num)].$word[mt_rand(0,$title2_num)].$res['title'].'等众多业务，期待您的光临')."','".daddslashes($xdurl)."','".daddslashes($newurl)."','".daddslashes($classVal)."','".daddslashes($muban)."','".date('Y-m-d H:i:s',time())."')");
        if($result){
          $successNum++;
        }
      }
       exit('{"code":1,"msg":"添加成功,共：'.$successNum.' 条"}');
    }else{
      exit('{"code":0,"msg":"num不能为空"}');
    }
  }elseif($act=='viewAdd'){
    $res = $DB->query("INSERT INTO `pre_view`(`title`, `title2`, `keywords`, `description`, `xdurl`, `newurl`, `head`,`foot`,`class`, `muban`, `addtime`) VALUES ('".daddslashes($_POST['title'])."','".daddslashes($_POST['title2'])."','".daddslashes($_POST['keywords'])."','".daddslashes($_POST['description'])."','".daddslashes($_POST['xdurl'])."','".daddslashes($_POST['newurl'])."','".daddslashes($_POST['head'])."','".daddslashes($_POST['foot'])."','".daddslashes($_POST['class'])."','".daddslashes($_POST['muban'])."','".date('Y-m-d H:i:s',time())."')");
    if($res){
        exit('{"code":1,"msg":"添加成功"}');
      }else{
        exit('{"code":0,"msg":"添加失败"}');
      }
  }elseif($act=='viewEdit'){
    $res = $DB->query("UPDATE `pre_view` SET `title`='".daddslashes($_POST['title'])."',`title2`='".daddslashes($_POST['title2'])."', `keywords` = '".daddslashes($_POST['keywords'])."',`description`='".daddslashes($_POST['description'])."',`xdurl`='".daddslashes($_POST['xdurl'])."',`newurl`='".daddslashes($_POST['newurl'])."',`head`='".daddslashes($_POST['head'])."',`foot`='".daddslashes($_POST['foot'])."',`class`='".daddslashes($_POST['class'])."',`muban`='".daddslashes($_POST['muban'])."' WHERE `id` = '".$_POST['id']."';");
    if($res){
        exit('{"code":1,"msg":"修改成功"}');
    }else{
      exit('{"code":0,"msg":"修改失败"}');
    }
  }elseif($act=='ClassViewContentRand'){
    $classVal = daddslashes($_POST['classVal'])??null;
    $mod = daddslashes($_POST['mod'])??null;
    if($mod=='title' && $classVal){
      $res = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$classVal."'");
      exit('{"code":1,"msg":"获取成功","text":"'.randTitle().$res['title'].'"}');
    }elseif($mod=='title2' && $classVal){
      $res = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$classVal."'");
      if(!$res['word'] || empty($res['word'])){
        exit('{"code":0,"msg":"您未设置该分类下的关键词！","text":""}');
      }
      $title2 = explode(',',$res['word']);
      $title2_num = count($title2)-1;
      $title2 = $title2[mt_rand(0,$title2_num)];
      exit('{"code":1,"msg":"获取成功","text":"'.$title2.'"}');
    }elseif($mod=='all' && $classVal){
      $res = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$classVal."'");
      if(!$res['word'] || empty($res['word'])){
       $isWord = 1;
      }
      $title2_arr = explode(',',$res['word']);
      $title2_num = count($title2_arr)-1;
      $title2 = $title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)];
      $keywords = $title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)];
      $description = '本'.$res['title'].'已运营数年之久，受众多国内外用户支持，安全快捷稳定，我们的业务内容广泛，'.$title2.$res['title'].'等众多业务，期待您的光临';
      if($isWord){
        $title2 = '';
        $keywords = '';
        $description = '本'.$res['title'].'已运营数年之久，受众多国内外用户支持，安全快捷稳定，我们的业务内容广泛，期待您的光临！';
      }
      exit('{"code":1,"msg":"获取成功","title":"'.randTitle().$res['title'].'","title2":"'.$title2.'","keywords":"'.$keywords.'","description":"'.$description.'"}');
    }elseif($mod=='keywords' && $classVal){
      $res = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$classVal."'");
      if(!$res['word'] || empty($res['word'])){
        exit('{"code":0,"msg":"您未设置该分类下的关键词！","text":""}');
      }
      $title2 = explode(',',$res['word']);
      $title2_num = count($title2)-1;
      $title2 = $title2[mt_rand(0,$title2_num)];
      exit('{"code":1,"msg":"获取成功","text":"'.$title2.'"}');
    }elseif($mod=='description' && $classVal){
      $res = $DB->getRow("SELECT * FROM `pre_class` WHERE `id`='".$classVal."'");
      if(!$res['word'] || empty($res['word'])){
        $isWord = 1;
      }
      $title2_arr = explode(',',$res['word']);
      $title2_num = count($title2_arr)-1;
      $title2 = $title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)].','.$title2_arr[mt_rand(0,$title2_num)];
      if($isWord){
        $title2 = '';
      }
      exit('{"code":1,"msg":"获取成功","text":"本'.$res['title'].'已运营数年之久，受众多国内外用户支持，安全快捷稳定，我们的业务内容广泛，'.$title2.$res['title'].'等众多业务，期待您的光临！"}');
    }
  }elseif($act=='classList'){
    $classNum = $DB->getColumn("SELECT count(id) FROM `pre_class`");
    $page = $limit*($page - 1);
    $viewInfo = $DB->query("SELECT id,path,title,word FROM `pre_class` ORDER BY `id` DESC limit {$page},{$limit}");
    while($row = $viewInfo->fetch()){
      $row['viewNum'] = $DB->getColumn("SELECT count(id) FROM `pre_view` WHERE `class`='".$row['id']."'");
      $infoArr[] = $row;
    }
    echo json_encode(array('code'=>0,'msg'=>'success','count'=>$classNum,'data'=>$infoArr));
  }elseif($act=='classDel'){
    if($id){
      if($_GET['mod']=='delAll'){
        $res = $DB->query("DELETE FROM `pre_view` WHERE `class`='".$id."'");
      }
      $res = $DB->query("DELETE FROM `pre_class` WHERE `id`='".$id."'");
      if($res){
        exit('{"code":1,"msg":"删除成功"}');
      }else{
        exit('{"code":0,"msg":"删除失败"}');
      }
    }else{
      exit('{"code":0,"msg":"ID不存在"}');
    }
  }elseif($act=='classAdd'){
    $classInfo = $DB->getRow("SELECT * FROM `pre_class` WHERE `path`='".$_POST['path']."'");
    if($classInfo){
      exit('{"code":0,"msg":"添加失败,分类路径不能重复！"}');
    }
    $res = $DB->query("INSERT INTO `pre_class`(`path`,`title`,`word`) VALUES ('".daddslashes($_POST['path'])."','".daddslashes($_POST['title'])."','".daddslashes($_POST['word'])."')");
    if($res){
        exit('{"code":1,"msg":"添加成功"}');
    }else{
      exit('{"code":0,"msg":"添加失败"}');
    }
  }elseif($act=='classEdit'){
    $res = $DB->query("UPDATE `pre_class` SET `path`='".daddslashes($_POST['path'])."',`title`='".daddslashes($_POST['title'])."', `word` = '".daddslashes($_POST['word'])."' WHERE `id` = '".$_POST['id']."';");
    if($res){
        exit('{"code":1,"msg":"修改成功"}');
    }else{
      exit('{"code":0,"msg":"修改失败"}');
    }
  }

?>