<?php

/* 搜索页 */
/* 162100源码 - 162100.com - download.162100.com */


@ require ('inc/set/set.php');
@ require ('inc/set/set_sql.php');
@ require ('inc/function/confirm_power.php');
//身份验证
define('POWER', confirm_power());
$userface = user_class(POWER, $session[3]);

$article_url = $web['url_html_article'] == 1 ? '"article.".$area_mode[$area_id_root].".".$row["id"].".1.html"' : '"article.php?mode=".$area_mode[$area_id_root]."&id=".$row["id"].""';

//处理关键字
function process_input(){
  $str = preg_replace('/[[:punct:]]/i', ' ', @ $_GET['keyword']);
  $str = preg_replace('/\pP/u', ' ', $str);
  $str=trim($str);
  //$str=str_replace(' ','|',$str);
  return $str;
}

function toUnicode($word) {
  $l = strlen($word);
  $bin_str = '';
  for ($i = 0; $i < $l; $i++) {
    $bin_str .= decbin(ord(substr($word, $i, 1)));
  }
  $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);  
  return '&#'.bindec($bin_str).';';  
}

//加亮标题中的关键字
function red_keyword_title($str){
  global $keyword;
  return @preg_replace('/('.$keyword.')/i','<span class="redword">$1</span>',$str);
}

//加亮内容中的关键字
function red_keyword_text($str){
  global $keyword;
  if(preg_match('/('.str_replace(' ','|',$keyword).')/i',$str,$matches)){
    $str=cutstr(strstr((string)$str,(string)$matches[1]),300);
    return @preg_replace('/('.$keyword.')/i','<span class="redword">$1</span>',$str);
  } else {
    return '……';
  }
}
function get_br($area_id) {
  return str_replace('_', '　', preg_replace('/[\d\-]+/', '', $area_id));
}

function get_li_mode($area_index) {
  global $web;
  if (!isset($area_index) || empty($area_index)) {
    return $web['sitemode'];
  } elseif (strstr($area_index, $web['sitemode'])) {
    return $web['sitemode'];
  } else {
    $sitemode = false;
    foreach (@explode('|', $area_index) as $mode) {
      if (file_exists('inc/css/'.$mode.'/index.php') && file_exists('inc/css/'.$mode.'/list.php') && file_exists('inc/css/'.$mode.'/article.php')) {
        $sitemode = $mode == $web['sitemode'] ? $web['sitemode'] : $mode;
        break;
      }
    }
    return $sitemode ? $sitemode : $web['sitemode'];
  }
}


$keyword = process_input();

$key = preg_replace('/[\xe0-\xef][\x80-\xbf]{2}/e', '"".toUnicode("$0").""', $keyword);
//$key = str_replace('|', '%', $keyword);


//$title = '搜索';
@ require ('inc/function/get_page.php');




$arr_area_title = array();
if (isset($_GET['area_id']) && preg_match('/^(\d+\-\d+)(\_\d+)*$/', $_GET['area_id'], $area_m)) {
  $area_id_root = $area_m[1];
  $arearoot_eval = 'if($area_id_root == $row["area_id"]) { $area_mode[$row["area_id"]] = get_li_mode($row["area_index"]); }';
  $base_title = $_GET['area_id'];
  $eval_title = 'area_id="'.$_GET['area_id'].'"';
  
  while (strpos($base_title, '_') !== false) {
    $base_title = preg_replace('/\_\d+$/', '', $base_title);
    $eval_title .= ' OR area_id="'.$base_title.'"';
  }
  $eval_title .= ' OR area_id REGEXP "^'.preg_quote($_GET['area_id'], '/').'\_[0-9]+$"';
  //$eval_title = ' WHERE area_id REGEXP "^'.$_GET['area_id'].'(_[0-9]+)?$"';
  $search_eval = '" ".$where_area_id." AND B.area_check=\"0\" AND A.id=B.id AND (MATCH (A.title) AGAINST (\'\"'.$key.'\"\' IN BOOLEAN MODE))"';
  
} else {
  $arearoot_eval = '$area_mode[$row["area_id"]] = get_li_mode($row["area_index"]);';
  $eval_title = ' area_id REGEXP "^[0-9]+\-[0-9]+$"';
  $search_eval = '" (MATCH (A.title) AGAINST (\'\"'.$key.'\"\' IN BOOLEAN MODE)) AND B.id=A.id AND B.area_check=\"0\""';
}

@ require ('inc/function/cutstr.php');

$n = 0;
$text = $err = $title = $select = '';

if (!isset($sql['db_err'])) {
  db_conn();
}
if ($sql['db_err'] != '') {
  die($sql['db_err']);
}

$result = @mysql_query('SELECT area_id,area_name,area_index FROM '.$sql['pref'].''.$sql['data']['承载版区分类的表名'].' WHERE '.$eval_title.'', $db);
if (@mysql_num_rows($result) > 0) {
  while ($row = @mysql_fetch_assoc($result)) {
    eval($arearoot_eval);
    if ($row['area_id'] == @ $_GET['area_id']) {
      $select = ' selected="selected"';
    } else {
      $select = '';
    }
    $arr_area_title[$row['area_id']] = '<option value="'.$row['area_id'].'"'.$select.'>'.get_br($row['area_id']).$row['area_name'].'</option>';
    unset($row);
  }
}
@mysql_free_result($result);
@uksort($arr_area_title, 'strnatcmp');
//print_r($arr_area_title);die;

if (isset($arr_area_title[@$_GET['area_id']]) && count($arr_area_title) == 1) {
  $where_area_id = 'B.area_id="'.$_GET['area_id'].'"';
} else {
  //$where_area_id = 'B.area_id LIKE "'.@$_GET['area_id'].'\_%"';
  $where_area_id = 'B.area_id REGEXP "^'.preg_quote($_GET['area_id'], '/').'(\_[0-9]+)*$"';
}
if (empty($arr_area_title)) {
  die('&#26631;&#31614;&#38169;&#35823;&#65281;&#35831;&#20174;<a href="./">&#39318;&#39029;</a>&#37325;&#26032;&#24320;&#22987;');
}

if ($keyword == '') { //检验关键字
  $err .= '请输入字、句等有效的查询字符！';
} elseif (($strlength = strlen($keyword)) && ($strlength > 100 || $strlength < 3)) {
  $err .= '关键字长度请大于等于3且少于100字符！';
} else {

  $result = @mysql_query('SELECT COUNT(A.id) AS total FROM '.$sql['pref'].''.$sql['data']['承载关键词的表名'].' AS A,'.$sql['pref'].''.$sql['data']['承载主题文章的表名'].' AS B WHERE'.eval('return '.$search_eval.';').'', $db);  
  $row = @mysql_fetch_assoc($result);
  $n = abs($row['total']);
  @mysql_free_result($result);
  unset($row);


  if ($n > 0) {
    $p = get_page($n); //页数
    $seek = $web['pagesize'] * ($p-1);
    $result = @mysql_query('SELECT B.id,B.title,B.text,B.author_ip,B.date,B.area_id FROM '.$sql['pref'].''.$sql['data']['承载关键词的表名'].' AS A,'.$sql['pref'].''.$sql['data']['承载主题文章的表名'].' AS B WHERE'.eval('return '.$search_eval.';').' ORDER BY A.id DESC LIMIT '.$seek.','.$web['pagesize'].'', $db); // ORDER BY topdate	
		
    while ($row = @mysql_fetch_assoc($result)) {
      $area_id_root = get_arearoot($row['area_id']);
      $row['title'] = preg_replace('/(\|.*)?/', '', $row['title']);
      $text .= '
  <div class="li">
  <a class="li_a" href="'.eval('return '.$article_url.';').'" target="_blank">'.red_keyword_title($row['title']).'</a>
  <div class="text">'.red_keyword_text($row['text']).'</div>
  <div class="li_ greenword">
  发布日期（'.$row['date'].'）
  作者（'.(strstr((string)$row['author_ip'], '.') ? $row['author_ip'] : '<a href="user.php?get=card&username='.urlencode($row['author_ip']).'" target="_blank" title="点击查看发布人名片">'.$row['author_ip'].'<img src="inc/images/card.gif" /></a>').'）
  </div>
  </div>';
      unset($row);
    }
    @mysql_free_result($result);

  } else {
    $err .= '<div class="text_err">记录为空！</div>';
  }
}


$title .= $keyword;
@ require ('inc/function/add_online.php'); //在线

@mysql_close();

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title, $web['code_author']; ?></title>
<link id="my_style" rel="stylesheet" type="text/css" href="inc/css/<?php echo $web['sitemode']; ?>/style.css">
<script language="javascript" src="inc/js/main.js" type="text/javascript"></script>
</head>
<body>
<div id="session" class="session">
<button type="button" onclick="window.open('write.php?mode=<?php echo $web['sitemode']; ?>','_blank');" title="发布文章"><b><div class="send_write"></div></b></button>
  <?php @ include ('inc/require/head.txt'); ?> &nbsp;
</div>
<?php
if ($web['siteclose'] > 1) {
  @include ('inc/require/siteclose_why.txt');
}
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="logo">
  <tr valign="top">
    <td width="200"><a href="<?php echo $web['path']; ?>"><img src="<?php echo $web['sitelogo']; ?>" /></a></a></td>
    <td>&nbsp;</td>
  </tr>
</table>
<div id="banner"> <a href="./">首页</a> <a class="column_is">搜索</a> <a href="list_company.php">商号</a> <a href="list_member.php">成员</a> <a href="list_online.php">在线</a> <a href="help.php">帮助</a>
    <?php @include ('inc/css/'.$web['sitemode'].'/banner.txt'); ?>
</div>
<div class="column">
  <h5><a href="./">首页</a> &gt; 搜索</h5>
  <div class="column_in">
    <form action="search.php?action=search" method="get">

<table width="510" border="0" cellspacing="0" cellpadding="0" style="position:relative; height:32px; float:left;">
  <tr>
    <td><input class="large redword" type="text" id="keyword" name="keyword" onfocus="if(this.value=='关键词'){this.value=''}" style="width:500px;" value="<?php echo $keyword ? $_GET['keyword'] : '关键词'; ?>" />
      <table style="height:100%; position:absolute; top:0; right:6px; margin-left:auto; margin-right:8px; text-align:right; float:right;" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="right"><select name="area_id">
          <option value="">全部版区分类</option>
          <?php echo @implode('', (array)$arr_area_title); ?>
        </select></td>
          </tr>
        </table></td>
  </tr>
</table>

      <table width="200" border="0" cellspacing="0" cellpadding="0" style="height:32px; float:left;">
  <tr>
    <td><button class="large" type="submit" name="submit"><b>搜索题目</b></button></td>
  </tr>
</table>
      <input type="hidden" name="action" value="search" />
    </form> <div style="clear:both;"></div>
  </div>
</div>
    <?php @include ('inc/css/'.$web['sitemode'].'/ad_article_top.txt'); ?>
<table border="0" cellpadding="0" cellspacing="0" class="body">
  <tr valign="top">
  <td class="left">
    <div class="body">
      <div class="list_title"><a class="list_title_in">结果列表<span>（<?php echo $n; ?>）</span></a></div>
      <div class="body_in">
        <?php
echo $text, $err;
?>
    <?php
echo @ get_page_foot($n, $web['pagesize'], $p, '?action=search&keyword='.urlencode($keyword).'&area_id='.$_GET['area_id'].'&p=');
?>
      </div>
    </div>
  </td>
  <td class="right">
    <div class="hot">
      <div class="title">热门标签</div><div class="text">
      <?php @include ('inc/require/hot_mark.txt'); ?></div>
    </div>
    <div class="ad_side"><?php @include ('inc/css/'.$web['sitemode'].'/ad_article_side.txt'); ?></div>
  </td>
  </tr>
</table>
    <?php @include ('inc/css/'.$web['sitemode'].'/ad_article_bottom.txt'); ?>
<div id="foot">
  <?php @include ('inc/css/'.$web['sitemode'].'/foot.txt'); ?>
</div>

</body>
</html>