<?php
//实时搜索
header("HTTP/1.1 200 OK");
    header("Access-Control-Allow-Origin: *");
    header('Content-Type: application/json; charset=utf-8');
    date_default_timezone_set('PRC');
error_reporting(0);
$db = Typecho_Db::get();
$select = $db->select()->from('table.contents');
$keywords = $_GET['keyword'];
$removeChar = ["https://", "http://", "/"]; 
$options = Helper::options();

$searchQuery = '%' . str_replace(' ', '%', $keywords) . '%';
            if ($this->user->hasLogin()) {
                $select->where("table.contents.password IS NULL
                 OR table.contents.password = '' OR table.contents.authorId = ?", $this->user->uid);
            } else {
                $select->where("table.contents.password IS NULL OR table.contents.password = ''");
            }

            $op = $this->db->getAdapter()->getDriver() == 'pgsql' ? 'ILIKE' : 'LIKE';
$result = array(
);
$md = new Markdown();
            $r = $db->fetchAll($select->where("table.contents.title {$op} ? OR table.contents.text {$op} ?", $searchQuery, $searchQuery)
                ->where('table.contents.type = ?', 'post')->where('status = ?','publish')->limit(50));
                if($r){
foreach($r as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
$targetSummary = General::getExcerpt($md::convert($val['text']), 10);
$expert = General::getCustomFields($val['cid'], 'excerpt');
                if($expert){
                    $targetSummary = $expert[0];
                }
$targetSummary = trim( strip_tags($targetSummary));
		$targetSummary = preg_replace('/\\s+/',' ',$targetSummary);
			if(!$targetSummary){
			    $targetSummary = 'This article currently has no abstract.';
			}
if(strpos($targetSummary, '{bs-hide') !== false || strpos($targetSummary, '[bs-hide') !== false || strpos($targetSummary, '[bshide') !== false || strpos($targetSummary, '[bslogin') !== false){
      $targetSummary = 'The article contains hidden content, please go to the inner page of the article to view it.';
      }
      
      
$result[] = array(
	 'url' => $val['permalink'],
	 'article' => $val['title'],
	 'description' => $targetSummary,
	);
	
}
$result[] = array(
	 'url' => "?s=".$_GET['keyword'],
	 'article' => "Real-time search can't find what you want?",
	 'description' => "Poke here for more results",
	);
}
else{
    $result[] = array(
	 'url' => "?s=".$_GET['keyword'],
	 'article' => "Real-time search data is empty",
	 'description' => "Real-time search data is empty.",
	);
}

        echo json_encode($result);         
                
    
?>
