-
Notifications
You must be signed in to change notification settings - Fork 5
/
Component.php
179 lines (156 loc) · 5.15 KB
/
Component.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace cleantalk\antispam;
use Cleantalk;
use CleantalkRequest;
use InvalidArgumentException;
use Yii;
use yii\base\Component as BaseComponent;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
/**
* CleanTalk API application component.
* Required set apiKey property.
*
* @version 1.0.0
* @author CleanTalk (welcome@cleantalk.org)
* @copyright (C) 2015 Сleantalk team (https://cleantalk.org)
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
*/
class Component extends BaseComponent
{
const AGENT_VERSION = 'yii2-1.0.0';
const KEY_SESSION_FORM_SUBMIT = 'ct_form_submit';
/** @var string API key */
public $apiKey;
/** @var string API URL */
public $apiUrl = 'https://moderate.cleantalk.org';
/**
* @deprecated Use setting in https://cleantalk.org/my/service?action=edit
* @var string API response lang en|ru
*/
public $responseLang;
/** @var bool enable logging */
public $enableLog = true;
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (is_null($this->apiKey)) {
throw new InvalidConfigException('CleanTalkApi configuration must have "apiKey" value');
}
}
/**
* Check if user registration allow
* @param string $email user email
* @param string $nickName user nickName
* @return array [bool, string] true, if user registration allow, false with text comment
*/
public function isAllowUser($email = '', $nickName = '')
{
$ctRequest = $this->createRequest();
$ctRequest->sender_email = $email;
$ctRequest->sender_nickname = $nickName;
$ctResult = $this->sendRequest($ctRequest, 'isAllowUser');
return [$ctResult->allow == 1, $ctResult->comment];
}
/**
* Check if user text message allow
* @param string $email user email
* @param string $nickName user nickName
* @param string $message message
* @return array [bool, string] true, if user message allow, false with text comment
*/
public function isAllowMessage($message, $email = '', $nickName = '')
{
$ctRequest = $this->createRequest();
$ctRequest->message = $message;
$ctRequest->sender_email = $email;
$ctRequest->sender_nickname = $nickName;
$ctResult = $this->sendRequest($ctRequest, 'isAllowMessage');
return [$ctResult->allow == 1, $ctResult->comment];
}
/**
* Generate form Javascript check code
* @return string
*/
public function getCheckJsCode()
{
return md5($this->apiKey . __FILE__);
}
/**
* Set begin time of submitting form
* @param string $id form id
*/
public function startFormSubmitTime($id)
{
Yii::$app->session->set(self::KEY_SESSION_FORM_SUBMIT . $id, time());
}
/**
* Get form submit time in seconds
* @param string $id form id
* @param boolean $clear clear value in session
* @return int|null
*/
public function calcFormSubmitTime($id = null, $clear = true)
{
if ($id === null) {
$id = Yii::$app->request->post('ct_formid');
}
$startTime = Yii::$app->session->get(self::KEY_SESSION_FORM_SUBMIT . $id);
if ($clear) {
Yii::$app->session->remove(self::KEY_SESSION_FORM_SUBMIT . $id);
}
return $startTime > 0 ? time() - $startTime : null;
}
/**
* Is javascript enabled
* @return int
*/
public function isJavascriptEnable()
{
return Yii::$app->request->post('ct_checkjs') == $this->getCheckJsCode() ? 1 : 0;
}
/**
* Create request for CleanTalk API.
* @return \CleantalkRequest
*/
protected function createRequest()
{
$ctRequest = new CleantalkRequest();
$ctRequest->auth_key = $this->apiKey;
$ctRequest->agent = self::AGENT_VERSION;
$ctRequest->sender_ip = Yii::$app->request->getUserIP();
$ctRequest->submit_time = $this->calcFormSubmitTime();
$ctRequest->js_on = $this->isJavascriptEnable();
$ctRequest->sender_info = Json::encode(
[
'REFFERRER' => Yii::$app->request->getReferrer(),
'USER_AGENT' => Yii::$app->request->getUserAgent(),
]
);
return $ctRequest;
}
/**
* @param \CleantalkRequest $request
* @param string $method
* @return \CleantalkResponse CleanTalk API call result
* @throws InvalidArgumentException
*/
protected function sendRequest($request, $method)
{
$ct = new Cleantalk();
$ct->server_url = $this->apiUrl;
if ($method != 'isAllowMessage' && $method != 'isAllowUser') {
throw new InvalidArgumentException('Method unknown');
}
Yii::trace('Sending request to cleantalk:' . var_export($request, true), __METHOD__);
$response = $ct->$method($request);
if ($this->enableLog) {
Yii::info(sprintf('Cleantalk response is allow=%d, inactive=%d, comment=%s', $response->allow, $response->inactive, $response->comment), __METHOD__);
}
return $response;
}
}