-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualCaptchaComponent.php
More file actions
349 lines (324 loc) · 10.6 KB
/
VisualCaptchaComponent.php
File metadata and controls
349 lines (324 loc) · 10.6 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* VisualCaptcha Component
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author AllCreator <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
/**
* 名前空間の定義
* VisualCaptchaの中でSessionクラスを定義している
*/
use visualCaptcha\Captcha;
use visualCaptcha\Session;
App::uses('Component', 'Controller');
App::import('Vendor', 'Captcha', array(
'file' => 'emotionloop' . DS . 'visualcaptcha' . DS . 'src' . DS . 'visualCaptcha' . DS . 'Captcha'
));
App::import('Vendor', 'Session', array(
'file' => 'emotionloop' . DS . 'visualcaptcha' . DS . 'src' . DS . 'visualCaptcha' . DS . 'Session'
));
/**
* VisualCaptcha Component
*
* 画像認証画面へのリダイレクト、認証処理を行います。<br>
* 利用方式、対象アクション、認証要素key名称を指定してください。
*
* [利用方式](#operationtype)<br>
* [対象アクション](#operationtype)
*
*
*
* @author AllCreator <info@allcreator.net>
* @package NetCommons\VisualCaptcha\Controller\Component
*/
class VisualCaptchaComponent extends Component {
/**
* captcha operation type
*
* @var string
*/
const OPERATION_REDIRECT = 'redirect';
const OPERATION_EMBEDDING = 'embedding';
const OPERATION_NONE = 'none';
/**
* 利用方式
*
* * OPERATION_REDIRECT<br>
* 切り替わり方式<br>
* 認証が必要な画面を表示する前に、画像認証画面が自動的に表示される方式です。<br>
* 画像認証に成功した後、認証が必要な画面を表示します。<br>
* この場合、画像認証画面、認証Postを当プラグインが処理するため、、
* 利用プラグインは、VisualCaptchaComponentを設定するのみです。<br>
* 対象アクション名も指定してください。
*
* #### サンプルコード
* ```
* public $components = array(
* 'VisualCaptcha.VisualCaptcha' => array(
* 'operationType' => VisualCaptchaComponent::OPERATION_REDIRECT,
* 'targetAction' => 'answer',
* 'identifyKey' => 'Questionnaire'???
* )
* )
* ```
*
* * OPERATION_EMBEDDING<br>
* 埋め込み方式(デフォルト)<br>
* 認証が必要な画面に、画像認証パーツを埋め込む方式です。<br>
* 切り替わり方式だと画像認証画面だけが表示されることになるが、埋め込み方式は認証が必要な画面の任意の場所に埋め込めます。<br>
* この場合は、VisualCaptchaComponentを設定、viewファイルへのvisual_captcha.ctpの埋め込み、
* 正しい回答がされたかのチェックを行う必要があります。<br>
*
*
* #### サンプルコード
* ##### Controller
* ```
* public $components = array(
* 'VisualCaptcha.VisualCaptcha' => array(
* 'operationType' => VisualCaptchaComponent::OPERATION_EMBEDDING,
* 'identifyKey' => 'VisualCaptcha'
* )
* )
* ```
* ##### View
* ```
* <?php
* echo $this->element(
* 'VisualCaptcha.visual_captcha', array(
* 'identifyKey' => 'VisualCaptcha'
* )
* ); ?>
* ```
*
* @var string
*/
public $operationType = VisualCaptchaComponent::OPERATION_EMBEDDING;
/**
* call controller w/ associations
*
* @var object
*/
public $controller = null;
/**
* visual captcha redirect target controller action
*
* @var string
*/
public $targetAction = null;
/**
* assetPath /r associations
*
* @var string
*/
public $assetPath = null;
/**
* imageField Answer /r associations
*
* @var string
*/
public $imageField = null;
/**
* audioField Answer /r associations
*
* @var string
*/
public $audioField = null;
/**
* 切り替えタイプのときの切り替え先画面のURLデフォルト値
* デフォルトの画像認証画面では困る場合はこの構造データを変更してください
*
* @var array
*/
public $visualCaptchaAction = array(
'plugin' => 'visual_captcha',
'controller' => 'visual_captcha',
'action' => 'view',
);
/**
* Called before the Controller::beforeFilter().
*
* @param Controller $controller Controller with components to initialize
* @return void
* @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::initialize
*/
public function initialize(Controller $controller) {
$this->controller = $controller;
// 画像認証リソースファイルへのパス
$this->assetPath = App::pluginPath('VisualCaptcha') . 'Resource' . DS . 'visual_captcha';
// フレームID別に記録されている正解データを保持
$frameId = Hash::get($this->controller->request->query, 'frame_id');
$session = new Session('visual_captcha_' . $frameId);
$frontendData = $session->get('frontendData');
if (isset($frontendData['imageFieldName'])) {
$this->imageField = $frontendData['imageFieldName'];
} else {
$this->imageField = null;
}
if (isset($frontendData['audioFieldName'])) {
$this->audioField = $frontendData['audioFieldName'];
} else {
$this->audioField = null;
}
// セキュリティコンポーネントを使用されている場合は
// 画像認証フィールドをUnlockにしておく
if (array_key_exists('Security', $this->controller->components)) {
if ($this->imageField && $this->audioField) {
$this->controller->Security->unlockedFields = array(
$this->imageField,
$this->audioField,
'frame_id'
);
}
}
}
/**
* Called after the Controller::beforeFilter() and before the controller action
*
* @param Controller $controller Controller with components to startup
* @return void
* @throws ForbiddenException
*/
public function startup(Controller $controller) {
// 何もしないでと指示されている場合
if ($this->operationType == VisualCaptchaComponent::OPERATION_NONE) {
return;
}
// 埋め込み型の時
if ($this->operationType == VisualCaptchaComponent::OPERATION_EMBEDDING) {
// 埋め込み型のときは判断・処理は利用側のプラグインに移譲する
return;
}
// 切り替え型のとき
// リダイレクトURL準備
$this->visualCaptchaAction['frame_id'] = Current::read('Frame.id');
$this->visualCaptchaAction['block_id'] = Current::read('Block.id');
// リファラが自分自身でないことが必須(無限ループになる
if ($this->operationType == VisualCaptchaComponent::OPERATION_REDIRECT) {
if ($controller->action == $this->targetAction) {
// OK判定が出ているか出てないならばリダイレクト
if (! $controller->Session->check('VisualCaptcha.judgement')) {
// 切り替え後、認証成功時のURLを取り出す
$returnUrl = $controller->request->here(false);
$controller->Session->write('VisualCaptcha.returnUrl', $returnUrl);
$controller->redirect(NetCommonsUrl::actionUrl($this->visualCaptchaAction));
} else {
// 出ているときはリダイレクトない
// そのままガード外して目的の画面へ行かせるので、ここでOK判定を消しておく
$controller->Session->delete('VisualCaptcha.judgement');
}
}
}
}
/**
* getReturnUrl get return screen url
*
* @return string
*/
public function getReturnUrl() {
return $this->controller->Session->read('VisualCaptcha.returnUrl');
}
/**
* check input response
*
* @return bool
*/
public function check() {
$reqData = $this->controller->request->data;
$session = new Session('visual_captcha_' . (Current::read('Frame.id')));
$captcha = new Captcha($session, $this->assetPath);
$ret = false;
$errorMessage = __d('visual_captcha', 'No answer! Please try again.');
if (isset($reqData[$this->imageField])) {
// php7.4でUnitTestでWarningが発生する可能性があるため事前にチェックしておく
// @see https://github.com/desirepath41/visualCaptcha-packagist/blob/0.0.4/src/visualCaptcha/Captcha.php#L184
if ($session->get('validImageOption')) {
$ret = $captcha->validateImage($reqData[$this->imageField]);
} else {
$ret = false;
}
$errorMessage = __d('visual_captcha', 'Image was NOT valid! Please try again.');
} elseif (isset($reqData[$this->audioField])) {
// php7.4でUnitTestでWarningが発生する可能性があるため事前にチェックしておく
// @see https://github.com/desirepath41/visualCaptcha-packagist/blob/0.0.4/src/visualCaptcha/Captcha.php#L191
if ($session->get('validAudioOption')) {
$ret = $captcha->validateAudio($reqData[$this->audioField]);
} else {
$ret = false;
}
$errorMessage = __d('visual_captcha', 'Accessibility answer was NOT valid! Please try again.');
}
if ($ret === false) {
//$this->controller->NetCommons->setFlashNotification($errorMessage);
$this->controller->set('visualCaptchaErrorMessage', $errorMessage);
} else {
// 判定セッション情報はリダイレクトの処理専用
if ($this->controller->name == 'VisualCaptcha') {
$this->controller->Session->write('VisualCaptcha.judgement', 'OK');
}
}
return $ret;
}
/**
* generate visual captcha data and return it
*
* @param int $count display image count
* @return string
*/
public function generate($count = 5) {
$session = new Session('visual_captcha_' . (Current::read('Frame.id')));
$lang = Configure::read('Config.language');
$imageJsonPath = $this->assetPath . DS . $lang . DS . 'images.json';
$audioJsonPath = $this->assetPath . DS . $lang . DS . 'audios.json';
$imageJson = $this->__utilReadJSON($imageJsonPath);
$audioJson = $this->__utilReadJSON($audioJsonPath);
if (! $imageJson || ! $audioJson) {
return '';
}
$captcha = new Captcha(
$session,
$this->assetPath,
$this->__utilReadJSON($imageJsonPath),
$this->__utilReadJSON($audioJsonPath)
);
$captcha->generate($count);
$ret = $captcha->getFrontEndData();
return json_encode($ret);
}
/**
* generate visual captcha image data and return it
*
* @param int $index display image index
* @return string
*/
public function image($index) {
$session = new Session('visual_captcha_' . (Current::read('Frame.id')));
$captcha = new Captcha($session, $this->assetPath);
return $captcha->streamImage(array(), $index, 0);
}
/**
* generate audio captcha data and return it
*
* @return streaming data
*/
public function audio() {
$session = new Session('visual_captcha_' . (Current::read('Frame.id')));
$captcha = new Captcha($session, $this->assetPath);
return $captcha->streamAudio(array(), 'mp3');
}
/**
* Read input file as JSON
*
* @param string $filePath json file path
* @return object
*/
private function __utilReadJSON($filePath) {
if (!file_exists($filePath)) {
return null;
}
return json_decode(file_get_contents($filePath), true);
}
}