-
Notifications
You must be signed in to change notification settings - Fork 14
/
LocalServer.php
executable file
·151 lines (117 loc) · 3.99 KB
/
LocalServer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* DuoshuoSDK 本地服务类定义
*
* @version $Id: LocalServer.php 0 10:17 2012-7-23
* @author shen2
* @copyright Copyright (c) 2012 - , Duoshuo, Inc.
* @link http://dev.duoshuo.com
*/
class Duoshuo_LocalServer{
protected $response = array();
protected $plugin;
public function __construct($plugin){
$this->plugin = $plugin;
}
/**
* 从服务器pull评论到本地
*
* @param array $input
*/
public function sync_log($input = array()){
$syncLock = $this->plugin->getOption('sync_lock');//检查是否正在同步评论 同步完成后该值会置0
if(!isset($syncLock) || $syncLock > time()- 900){//正在或15分钟内发生过写回但没置0
$response = array(
'code' => Duoshuo_Exception::SUCCESS,
'response'=> '同步中,请稍候',
);
return;
}
$this->plugin->updateOption('sync_lock', time());
$last_sync = $this->plugin->getOption('last_sync');
$limit = 20;
$params = array(
'since_id' => $last_sync,
'limit' => $limit,
'order' => 'asc',
);
$client = $this->plugin->getClient();
$posts = array();
$affectedThreads = array();
$last_log_id = 0;
do{
$response = $client->getLogList($params);
$count = count($response['response']);
foreach($response['response'] as $log){
switch($log['action']){
case 'create':
$affected = $this->plugin->createPost($log['meta']);
break;
case 'approve':
case 'spam':
case 'delete':
$affected = $this->plugin->moderatePost($log['action'], $log['meta']);
break;
case 'delete-forever':
$affected = $this->plugin->deleteForeverPost($log['meta']);
break;
case 'update'://现在并没有update操作的逻辑
default:
$affected = array();
}
//合并
$affectedThreads = array_merge($affectedThreads, $affected);
if (strlen($log['log_id']) > strlen($last_log_id) || strcmp($log['log_id'], $last_log_id) > 0)
$last_log_id = $log['log_id'];
}
$params['since_id'] = $last_log_id;
} while ($count == $limit);//如果返回和最大请求条数一致,则再取一次
//唯一化
$aidList = array_unique($affectedThreads);
if (strlen($last_log_id) > strlen($last_sync) || strcmp($last_log_id, $last_sync) > 0)
$this->plugin->updateOption('last_sync', $last_log_id);
$this->plugin->updateOption('sync_lock', 0);
//更新静态文件
if ($this->plugin->getOption('seo_enabled'))
$this->plugin->refreshThreads($aidList);
$this->plugin->updateOption('sync_lock', 1);
//$this->response['response']
$this->response['code'] = Duoshuo_Exception::SUCCESS;
}
public function update_option($input = array()){
//duoshuo_short_name
//duoshuo_secret
//duoshuo_notice
foreach($input as $optionName => $optionValue)
if (substr($optionName, 0, 8) === 'duoshuo_'){
$this->plugin->updateOption(substr($optionName, 8), $optionValue);
}
$this->response['code'] = 0;
}
public function sendResponse(){
echo json_encode($this->response);
}
public function dispatch($input){
if (!isset($input['signature']))
throw new Duoshuo_Exception('Invalid signature.', Duoshuo_Exception::INVALID_SIGNATURE);
$signature = $input['signature'];
unset($input['signature']);
ksort($input);
$baseString = http_build_query($input, null, '&');
$expectSignature = base64_encode(hash_hmac('sha1', $baseString, $this->plugin->getOption('secret'), true));
if ($signature !== $expectSignature)
throw new Duoshuo_Exception('Invalid signature, expect: ' . $expectSignature . '. (' . $baseString . ')', Duoshuo_Exception::INVALID_SIGNATURE);
$method = $input['action'];
if (!method_exists($this, $method))
throw new Duoshuo_Exception('Unknown action.', Duoshuo_Exception::OPERATION_NOT_SUPPORTED);
$this->$method($input);
$this->sendResponse();
}
static function sendException($e){
$response = array(
'code' => $e->getCode(),
'errorMessage'=>$e->getMessage(),
);
echo json_encode($response);
}
}