public function sync2Changyan($isSetup = false)
{
global $zbp;
@set_time_limit(0);
if (function_exists('ini_get')) {
ini_set('memory_limit', '256M');
}
$nextID2CY = $this->getOption('changyan_sync2CY');
if ($nextID2CY == 0) {
$nextID2CY = 1;
}
$maxID = $zbp->db->Query($zbp->db->sql->Select('%pre%comment', 'MAX(comm_ID)', array(array('CUSTOM', "comm_Agent NOT LIKE 'Changyan_%'")), null, null, null));
$maxID = $maxID[0]['MAX(comm_ID)'];
$postIDList = $zbp->db->Query($zbp->db->sql->Select('%pre%comment', 'DISTINCT comm_LogID', array(array('CUSTOM', "comm_ID > {$nextID2CY} AND comm_ID <= {$maxID}")), null, null, null));
$postIDList2 = array();
foreach ($postIDList as $id) {
$postIDList2[] = (int) $id['comm_LogID'];
}
//flag of response
$flag = true;
$response = "";
foreach ($postIDList2 as $aPostID) {
//in case of bug of duoshuo or other plugins: postID larger than maxPostID
//echo ($aPost -> comment_post_ID)." ";//////////////////xcv
$postInfo = $zbp->GetPostByID($aPostID);
if ($postInfo->ID == 0) {
continue;
}
//build the comments to be synchronized
$topic_id = $postInfo->ID;
$topic_url = $postInfo->Url;
$topic_title = $postInfo->Title;
date_default_timezone_set('Etc/GMT-8');
$topic_time = date("Y-m-d H:i:s", $postInfo->PostTime);
$topic_parents = "";
//$postInfo[0]->post_parents;
$script = $this->getOption('changyan_script');
$appID = explode("'", $script);
//get the appID from the script
$appID = $appID[1];
//echo $topic_title."<br/>";//////////////////////xcv
if ($isSetup == true) {
} else {
}
$comments = array();
$commentsList = $zbp->GetCommentList('*', array(array('=', 'comm_IsChecking', 0), array('=', 'comm_LogID', $aPostID)));
$comments = array();
//insert comments into the commentsArray
foreach ($commentsList as $comment) {
if (strpos($comment->Agent, 'Changyan_') === 0) {
continue;
}
$user = array('userid' => $comment->Author->Name, 'nickname' => $comment->Name, 'usericon' => '', 'userurl' => $comment->HomePage);
$comments[] = array('cmtid' => $comment->ID, 'ctime' => $comment->Time(), 'content' => $comment->Content, 'replyid' => $comment->ParentID, 'user' => $user, 'ip' => $comment->IP, 'useragent' => $comment->Agent, 'channeltype' => '1', 'from' => '', 'spcount' => '', 'opcount' => '');
}
date_default_timezone_set($zbp->option['ZC_TIME_ZONE_NAME']);
//comments under a post to be synchronized
$postComments = array('title' => $topic_title, 'url' => $topic_url, 'ttime' => $topic_time, 'sourceid' => $topic_id, 'parentid' => $topic_parents, 'categoryid' => '', 'ownerid' => '', 'metadata' => '', 'comments' => $comments);
if (empty($comments)) {
continue;
}
//get the appID from the script
$script = $this->getOption('changyan_script');
$appID = explode("'", $script);
$appID = $appID[1];
$postComments = json_encode($postComments);
//hmac encode
$appKey = $this->getOption('changyan_appKey');
$appKey = trim($appKey);
$md5 = hash_hmac('sha1', $postComments, $appKey);
$postData = "appid=" . $appID . "&md5=" . $md5 . "&jsondata=" . $postComments;
//print_r($postData);////////////xcv//////////
$response = $this->postContents_curl("http://changyan.sohu.com/admin/api/import/comment", $postData);
$regex = '/success":true/';
//if "true" not found
//echo "Response is ".$response;//xcv
if (!preg_match($regex, $response)) {
$flag = false;
break;
}
}
if ($flag === true) {
//recode the latest synchronization time
$this->setOption('changyan_lastSyncTimeCY', time());
$this->setOption('changyan_sync2CY', $maxID);
die("同步成功");
} else {
die("同步失败:" . $response);
}
}