<?php
define('IN_OUN', true);
include_once( "./includes/command.php"); 
include_once( ROOT_PATH."includes/item_set.php"); 
include_once( ROOT_PATH."ads.php");
$prid = $id  +0;

/* 得到文章标题 */
$db_table = $pre."producttxt"; 
$sql = "SELECT prid,pcid,name  FROM ".$db_table." 
       WHERE prid='".$prid."' AND states <> 1 AND  domain_id = '$_SESSION[$un_domain_id]'  limit 1";
$rowproducttxt = $oPub->getRow($sql);
if( !$rowproducttxt ){
   $strMessage = '此产品已删除';
   echo "<SCRIPT language='javascript'>\nalert('".$strMessage."!!');top.location='products.php';</script>";
   exit;
}else{
	if($Aconf['rewrite']){
		$rowproducttxt['product_url'] = 'product-'.$prid.'.html';
	}else{
		$rowproducttxt['product_url'] = 'product.php?id='.$prid;
	}
}
/* 评论提交 */
$db_table = $pre."product_comms";
$strMessage = '';

if($_POST[act] == 'install') { 
	$descs = clean_html($_POST[descs]);
	$strMessage .= ($descs == '')?' 评论内容不能为空':'';
	$ip = real_ip();
	$strMessage .= ($ip == '')?'\n ERROR 1,系统错误，不能添加评论':'';
    /* 读取过滤的关键词 start */
    $db_table = $pre."filter";
    $sql = "SELECT * FROM ".$db_table." WHERE states=1 ORDER BY fid ASC LIMIT 1";
    $Anorm = $oPub->selectrow($sql);
	if($Anorm) {
		if($Anorm["ips"]) {
            $pos = strpos($Anorm["ips"], $ip);
            if ($pos !== false) {
                $strMessage .= '\n ERROR 2,不能添加评论！';
			}
		}

		if($Anorm["words"] && !$strMessage) {
			$Awords = explode(",",$Anorm["words"]);
            foreach ($Awords AS $val) {
				if($val) {
				    $pos = strpos($descs, $val);
                    if ($pos !== false) {
                        $strMessage .= '非法关键词,不能添加评论！';
					    break;
			        } 
				}
			}
		}
	}
    /* 读取过滤的关键词 end */  
    if($strMessage == ''){
	  $nowdatedd = gmtime();

      $sql = "SELECT dateadd  FROM  ".$pre."product_comms  WHERE ip='".$ip."' order by dateadd desc limit 1";
      $row = $oPub-> selectrow($sql);
	  $dateadd = ($row)?$nowdatedd - $row[dateadd]:31;
	  if($dateadd > 30){
          $sql = "INSERT INTO " . $pre."product_comms (prid, descs ,  ip , email ,name, dateadd , states , `domain_id` )" .
               "VALUES ('$prid','$_POST[descs]', '".real_ip()."', '$_POST[email]','$_POST[name]','".gmtime()."', '0','$_SESSION[$un_domain_id]')"; 
          $oPub-> query($sql);

		  $db_table = $pre."producttxt";
          $sql = "UPDATE " . $db_table . " SET comms =comms + 1 
	             WHERE domain_id = ".$_SESSION[$un_domain_id]." and prid = ".$prid;
           $oPub-> query($sql);
          $strMessage =  "添加成功!";
		  /* 如果是登陆用户，测记录用户文章 id coms_type = 1/2/3 文章/产品/专题*/
		  if ($_SESSION['user_id']){
              $sql = "SELECT ucid  FROM " . $pre."users_comms
			          WHERE user_id  = '".$_SESSION['user_id']."'
					  AND coms_type = 2 
					  AND arid = '".$prid."'";
			  $tmp = $oPub-> getone($sql);
              if ( !$tmp ){				  
                  $sql = "INSERT INTO " . $pre."users_comms(user_id, coms_type ,arid,dateadd,domain_id) VALUES ('".$_SESSION['user_id']."',2, '".$prid."','".gmtime()."','".$_SESSION[$un_domain_id]."')"; 
                  $oPub-> query($sql);
			  }else
			  {
                  $sql = "UPDATE ". $pre."users_comms SET dateadd = '".gmtime()."'  WHERE ucid = '$tmp'";
                  $oPub-> query($sql);
			  }
		   }
		}else
		{
          $strMessage = '30秒后才能继续添加!';
		}
	}
	unset($_POST);
}
//评论结束
if(!empty($strMessage)){  
	if($Aconf['rewrite']){ 
	 $strtmp  =  'procomms-'.$prid.'-0.html';  
	}else{
	 $strtmp  =  'procomms.php?id='.$prid; 
	} 
	echo "<SCRIPT language='javascript'>\nalert('".$strMessage."!!');top.location='".$strtmp."';</script>";
	exit;
} 

/* 当前位置导航 */ 
$sql = "SELECT name,fid FROM ".$pre."productcat 
	where  pcid = '".$rowproducttxt[pcid]."'
	AND domain_id=".$_SESSION[$un_domain_id]." 
	LIMIT 1";
$row = $oPub->getRow($sql); 
$fid = $row[fid];  
if($Aconf['rewrite']){
	$nowcatname =  '<a href="products-'.$rowproducttxt[pcid].'-0.html">'.$row[name].'</a>'; 
	$products = 'products.html';
}else{
	$nowcatname =  '<a href="products.php?id='.$rowproducttxt[pcid].'">'.$row[name].'</a>';
	$products = 'products.php';
}
$idtype = "pcid";
$strPrenave = pre_node($fid,$pre."productcat",$idtype,$products,true);
$Ahome["nowNave"]  = '<a href="./">首页</A> > <a href="'.$products.'">所有产品</a> '.$strPrenave.' > '.$nowcatname.' > <a href="'.$rowproducttxt['product_url'].'">'.$rowproducttxt[name].'</a><span style="font-weight:lighter"> 评论</span>'; 
$Aconf["header_title"] = $rowproducttxt[name].'评论|'.$row[name].'|'.$Aconf['header_title'];  


/* 评论列表 */
$db_table = $pre."product_comms";
$strWhere = ($Aconf['support'])?" states = 3 ":" states <> 1 ";
$strWhere = ' WHERE '.$strWhere.' AND prid='.$prid.' AND domain_id='.$_SESSION[$un_domain_id];
$sql = "SELECT count( * ) AS count FROM ".$db_table.$strWhere;
$row = $oPub-> selectrow($sql);
$count = $row['count'];
$rowproducttxt[count] = $row['count']; //传送留言总数

$page = new ShowPage;
$Aconf['set_pagenum'] = 30;
$page->PageSize = $Aconf['set_pagenum'];
$page->Total = $count;
$pagenew = $page->PageNum(); 
$page->LinkAry = array('id'=>$prid); 
$strOffSet = $page->OffSet();
/* 翻页 */
$rowproducttxt["showpage"] = ($row[count]  > $Aconf['set_pagenum'])?$page->ShowLink_num():''; 

$sql = "SELECT email,ip,descs,name,dateadd  FROM ".$db_table.$strWhere." ORDER BY prcid desc limit ".$strOffSet;
$AsppAll = $oPub-> select($sql);
$n = 1;
while( @list( $k, $v ) = @each( $AsppAll) ) { 
    $AsppAll[$k][dateadd] = date("y年n月j日h:i", $v[dateadd]);
    $Aip = explode(".",$v[ip]);
    $AsppAll[$k][ip] = '第'.$n.'楼 IP:'.$Aip[0].'.'.$Aip[1].'.*';
    $AsppAll[$k][nick] = empty($v[name])?'匿名':$v[name];
    $AsppAll[$k][descs] =  clean_html($v[descs]); 
    $n ++ ; 
}
$rowproducttxt[pro_comms] = $AsppAll;  
$Ahome["procomms"] = $rowproducttxt;

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$cache_id = sprintf('%X', crc32($_SESSION[$un_domain_id].$prid));
if (!$smarty->is_cached($Aconf["displayFile"], $cache_id)) {
	assign_template($Aconf); 
	$smarty->assign('home', $Ahome ); 
	$smarty->assign('user_info', $_SESSION );
	unset($Ahome); 
}
$smarty->display($Aconf["displayFile"], $cache_id);

?>
