<?php



namespace app\controller;

use think\Controller;

use think\Db; 

class Read extends Controller

{

public function post(){
$uid=input('uid');
$qid=input('qid');
$nr=strip_tags(input('nr'));

 if($nr==''){
 $this->error('评论内容不能为空。');
 
 }
$rs=db('comment')->insert(['uid'=>$uid,'qid'=>$qid,'nr'=>$nr,'sj'=>date('Y-m-d H:i:s'),'zt'=>0]);

if($rs){

$this->success('评论发表成功，请等待审核。');

}

}

    public function index()
    {

require_once("./api/php.php");

$q=input('get.q');

$pl=db('comment')->where('uid',$q)->limit(0,9)->order('id desc')->select();

$this->assign('pl',$pl);

 $rs3=db('news')->limit(0,9)->order('h desc')->select();

//$rs6=db('news')->limit(0,9)->order('Id desc')->select();

 $config=db('config')->where('Id',1)->find();

$clist=db('class')->where("zt=1")->order('pid,Id desc')->select();

if($config['key_true']){

$rs=db('news')->where("mid='".$q."'")->find();

// db('news')->where("mid='".$q."'")->setInc('h');

$q=$rs['uid'];

 $id=$rs['Id'];

}else{

$q=str_replace('.html','',$q);

$rs=db('news')->where("uid='".$q."'")->find();

 db('news')->where("uid='".$q."'")->setInc('h');

 $id=$rs['Id'];

$q=$rs['uid'];

}

$rs9=db('news')->where("id", ">",$id)->order("id", "asc")->find();

$rs91=db('news')->where("id", "<", $id)->order("id", "desc")->find();
  
 $this->assign('rs9',$rs9);
 $this->assign('rs91',$rs91);
 $this->assign('rs',$rs);
 
 if($rs['ct']!='1'){
 
 

$index_url="wapiknow.baidu.com/question/".$q;

//$index_url="http://wapiknow.baidu.com/question/34686946.html";
echo $index_url;

if (file_exists("cache/read/q-".$q)){

$fileTime =time()-filemtime("cache/read/q-".$q);

$ftime=floor($fileTime/60);


$cc=0;


}else{

$cc=1;


}



if ($ftime<$config['cache_read'] && $cc==0){

$str = file_get_contents("cache/read/q-".$q);  
 

}else{ 


$str=auto_C($index_url);


if($config['cache_true']){


$fp = fopen("cache/read/q-".$q,'w+');

flock($fp,LOCK_EX);//独占锁定,为防止同一时间多个用户更新这个缓存文件

fwrite($fp,$str);

flock($fp,LOCK_UN);//释放锁定 

fclose($fp);

}
}



//$str=str_replace('<img class="ikqb_img" src="http://','<img class="ikqb_img" src="/img.php?url=',$str);

//$ff=nanji_cut("<div class=\"full-content\">","<div class=\"w-detail-display-btn\">",$str);

preg_match_all("|<div class=\"full-content\">([^\"]*)</div>|", $str,$t);

 preg_match_all("|<p class=\"question-list-intro\">([^\"]*)</p>|", $str,$t2);

preg_match_all("|<p>([^\"]*(?<!微信)(?<!微博)(?<!QQ)(?<!QQ空间)(?<!答案纠错)(?<!举报))</p>|",$str,$nn);

 preg_match_all('|<img class="ikqb_img" src="([^\"]*)"|',$str,$imgx); 

//echo '$t';

//print_r($t);

//echo '<Br>$t2';

//print_r($t2);

//echo '<br>$nn';

//print_r($nn);

//echo '<Br>$imgx';

//print_r($imgx);

	foreach($imgx[1] as $vv){

								$contall=$contall.'<div align="center"><img src="'.$vv.'" width="81%"></div>';

								}
								
								     foreach($t2[1] as $v){



$contall=$contall.$v;


                                }
								
								
                        foreach($nn[1] as $v){

				if(end($nn[1])==$v){

				     break;

				}


$contall=$contall.$v;               }
if($config['key_true']){
db('news')->where("mid='".$q."'")->update(['cal'=>$contall,'ct'=>1]);
}else{
db('news')->where("uid='".$q."'")->update(['cal'=>$contall,'ct'=>1]);

}


}else{
$contall=$rs['cal'];
}



$this->assign('contall',$contall);
  $this->assign('rs3',$rs3);

    $this->assign('rs6',$rs6);
//print_r($imgx);

$this->assign('t',$t);

$this->assign('t2',$t2);

$this->assign('nn',$nn);

$this->assign('imgx',$imgx);

$this->assign('c',$c);

$this->assign('data',$data); 

$this->assign('config',$config);

$this->assign('clist',$clist);

  return view('read');



    }



}



