<?php

define('PB_PAGE', 'ajax');

require_once './include/common.inc.php';
//zzcity add
//登录开始
if (strlen($author)>0){
$customer = $DB->fetch_one("SELECT m.uid, m.username, m.password, m.email, m.adminid, m.groupid, m.publicemail, m.gender, m.regip, m.regdate, m.realgroup, m.postnum, m.commentnum, m.diggnum, m.burynum, m.currency, m.lastip, m.lastvisit, m.lastpost, m.lastcomment, m.lastupload, m.lastsearch, m.uploadnum, m.newmsg, m.friendnum, m.collectionnum, m.visitnum, m.ucuid, m.avatar, mx.qq, mx.msn, mx.site, mx.location, mx.birthday, mx.signature, mx.showsign, mx.ctsig FROM {$db_prefix}members m LEFT JOIN {$db_prefix}memberexp mx USING (uid) WHERE m.username = '$author'");
//if (!$customer || pbNewPW($customer['password']) != $p_pw)
//{
// sCookie('pb_auth', '', -1);
// unset($customer, $pb_auth);
// showMsg('auth_error');
//}
if ($customer){
$customer['uid'] = $customer['safeuid'] = (int)$customer['uid'];
$customer['groupid'] = (int)$customer['groupid'];
$customer['adminid'] = (int)$customer['adminid'];
$customer['newmsg'] && $customer['newmsg'] = '<span class="newmsg">'.$customer['newmsg'].'</span>';
$customer['groupid'] == -1 && $customer['groupid'] = (int)$customer['realgroup'];
$logStatus = 1;
}
}
//登录结束
require_once PBDIGG_ROOT.'include/validate.func.php';
require_once PBDIGG_ROOT.'include/ubb.func.php';
require_once PBDIGG_CROOT.'cache_reg.php';
$ajax_message = loadLang('ajax');
$common_message += $ajax_message;



$action='comment'; //zzcity add

$tid = (int)$tid;
$rid = (int)$rid;
$uid = (int)$customer['uid'];

//$response = array('0','',$action.'_'.$tid.'_'.$rid);
$timestamp = (time()-30*60)+(int)$replyno*mt_rand(1,5)*60;  //zzcity add

switch ($action)
{
 case 'comment':
//  if ($pb_reposttime)
//  {
//   $pb_lastcomment = $logStatus ? $customer['lastcomment'] : gCookie('pb_lastcomment');
//   if ($pb_lastcomment && $pb_reposttime && ($timestamp - $pb_lastcomment < $pb_reposttime))
//   {
//    $response[1] = $ajax_message['ajax_flood_ctrl'];
//    break;
//   }
//   sCookie('pb_lastcomment', $timestamp, 1);
//  }

//  if ((!$allowcomment || !$pb_ifcomment) && !SUPERMANAGER)
//  {
//   $response[1] = $ajax_message['ajax_comment_nopermission'];
//   break;
//  }
//  $comment = convert_encoding('UTF-8', $db_charset, stripslashes($content));
  $comment = stripslashes($content);
//  !$allowhtml && $comment = HConvert($comment);
//  if ($ck = checkComment($comment))
//  {
//   $response[1] = $ck;
//   break;
//  }
//  if (($pb_gdcheck & 8) && !ckgdcode($captcha))
//  {
//   $response[1] = $common_message['checkcode_error'];
//   break;
//  }
  $rs = $DB->fetch_one("SELECT cid, ifcheck, iflock, digg, bury, views, comments, pbrank, postdate FROM {$db_prefix}threads WHERE tid = '$tid'");
//  if (!$rs || $rs['iflock'] || !$rs['ifcheck'])
  if (!$rs)
  {
   //$response[1] = $ajax_message['ajax_action_disabled'];
   $response = '[err]'.$ajax_message['ajax_action_disabled'].'[/err]';
   break;
  }
  $cid = (int)$rs['cid'];
  $pb_creditdb = explode("\t", $pb_creditdb);
  $ifconvert = $comment == conentUBB($comment, 'c', true) ? 0 : 1;
  $ifcheck = (($pb_ccheck && !SUPERMANAGER) || ($pb_anonnews && !$uid)) ? 0 : 1;
  $DB->db_exec("INSERT INTO {$db_prefix}comments (rid, cid, tid, author, uid, content, ifcheck, ifshield, ifconvert, postdate, postip, digg, diggdate, bury, burydate) 
      VALUES (NULL, ".$rs['cid'].", '$tid', '".addslashes($customer['username'])."', '$uid', '".addslashes($comment)."', $ifcheck, 0, '$ifconvert', '$timestamp', '".$_PBENV['PB_IP']."', 0, 0, 0, 0)");
  $rid = intval($DB->db_insert_id());
  $pbrank = pbrank($rs['pbrank'], $rs['digg'], $rs['bury'], $rs['views'], ++$rs['comments'], $rs['postdate']);
  if ($logStatus)
  {
   $DB->db_exec("UPDATE {$db_prefix}members m, {$db_prefix}threads t, {$db_prefix}categories c 
       SET m.commentnum = m.commentnum + 1, m.lastcomment = '$timestamp', m.currency = m.currency + " . intval($pb_creditdb[1]) .", t.comments = t.comments + 1, t.commentdate = '$timestamp', t.pbrank = '$pbrank', c.cnum = c.cnum + 1 
       WHERE t.tid = '$tid' AND c.cid = '$cid' AND m.uid = '$uid'");
  }
  else
  {
   $DB->db_exec("UPDATE {$db_prefix}threads t, {$db_prefix}categories c 
     SET t.comments = t.comments + 1, c.cnum = c.cnum + 1, t.commentdate = '$timestamp', t.pbrank = '$pbrank' 
     WHERE t.tid = '$tid' AND c.cid = '$cid'");
  }
  $DB->db_exec("UPDATE {$db_prefix}sitestat SET comnum = comnum + 1 WHERE id = 1");
  
  //$response[0] = '1';
  $response = '[ok]rid='.$rid;
 
  //sCookie('pb_lastcomment', $timestamp);
  break;

}

//$response[1] = convert_encoding($db_charset, 'UTF-8', $response[1]);

//if (function_exists('json_encode'))
//{
// $response = json_encode($response);
//}
//else
//{
//   require_once(PBDIGG_ROOT.'include/json.class.php');
//   $json = new Services_JSON;
//   $response = $json->encode($response);
//}
echo $response;
exit;
//
//function getAjaxTPL($tpl = 'ajax')
//{
// extract($GLOBALS);
// ob_start();
// require_once pt_fetch($tpl);
// $response = preg_replace('~\d{10}\n~', '', ob_get_contents());
// ob_end_clean();
// return $response;
//}
?>
