forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
53 lines (47 loc) · 2.76 KB
/
Action.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
<?php
class ChangyanCallback_Action extends Typecho_Widget implements Widget_Interface_Do
{
public function action()
{
if (!$this->request->isPost() ||
empty($this->request->get('data')) ||
empty($requestContent = json_decode($this->request->get('data'), true)) ||
empty($requestContent['sourceid'])
) {
throw new Typecho_Widget_Exception(_t('页面不存在'), 404);
}
$db = Typecho_Db::get();
$replyId = empty($requestContent['comments'][0]['replyid']) ? 0 : $requestContent['comments'][0]['replyid'];
$parentId = 0;
if (!empty($replyId)) {
$parentId = (int)$db->fetchObject(
$db->select('coid')->from('table.comments')->where('cid = ?', $requestContent['sourceid'])->where('cmtid = ?', $replyId)->limit(1)
)->coid;
}
$db->query(
$db->insert('table.comments')->rows(
[
'cid' => $requestContent['sourceid'],
'created' => empty($requestContent['comments'][0]['ctime']) ? time() : $requestContent['comments'][0]['ctime'] / 1000,
'author' => empty($requestContent['comments'][0]['user']['nickname']) ? '' : $requestContent['comments'][0]['user']['nickname'],
'authorId' => empty($requestContent['comments'][0]['user']['userid']) ? 0 : $requestContent['comments'][0]['user']['userid'],
'ownerId' => empty($requestContent['comments'][0]['user']['userid']) ? 0 : $requestContent['comments'][0]['user']['userid'],
'url' => empty($requestContent['comments'][0]['user']['userurl']) ? '' : $requestContent['comments'][0]['user']['userurl'],
'ip' => empty($requestContent['comments'][0]['ip']) ? '' : $requestContent['comments'][0]['ip'],
'agent' => empty($requestContent['comments'][0]['useragent']) ? '' : $requestContent['comments'][0]['useragent'],
'text' => empty($requestContent['comments'][0]['content']) ? '' : $requestContent['comments'][0]['content'],
'type' => 'comment',
'status' => 'approved',
'parent' => $parentId,
'cmtid' => empty($requestContent['comments'][0]['cmtid']) ? 0 : $requestContent['comments'][0]['cmtid'],
]
)
);
$num = $db->fetchObject(
$db->select(['COUNT(coid)' => 'num'])->from('table.comments')->where('status = ? AND cid = ?', 'approved', $requestContent['sourceid'])
)->num;
$db->query(
$db->update('table.contents')->rows(['commentsNum' => $num])->where('cid = ?', $requestContent['sourceid'])
);
}
}