1 | <?php |
||
2 | |||
3 | namespace app\controllers\ajax; |
||
4 | |||
5 | use Yii; |
||
6 | use yii\base\InvalidArgumentException; |
||
7 | use yii\filters\{ContentNegotiator, VerbFilter}; |
||
8 | use yii\web\{Controller, Request, Response, BadRequestHttpException}; |
||
9 | use ReCaptcha\ReCaptcha; |
||
10 | use app\helpers\BaseHelper; |
||
11 | use app\traits\ResponseTrait; |
||
12 | |||
13 | /** |
||
14 | * Class RecaptchaAjaxController |
||
15 | * |
||
16 | * @package app\controllers |
||
17 | */ |
||
18 | class RecaptchaAjaxController extends Controller |
||
19 | { |
||
20 | use ResponseTrait; |
||
21 | |||
22 | /** |
||
23 | * @var string|array the configuration for creating the serializer that formats the response data. |
||
24 | */ |
||
25 | public $serializer = 'yii\rest\Serializer'; |
||
26 | |||
27 | /** |
||
28 | * @var ReCaptcha |
||
29 | */ |
||
30 | protected $recaptchaDriver; |
||
31 | |||
32 | /** |
||
33 | * Initialize. |
||
34 | */ |
||
35 | public function init() |
||
36 | { |
||
37 | parent::init(); |
||
38 | |||
39 | $this->recaptchaDriver = new ReCaptcha(Yii::$app->params['captcha']['secret_key']); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function behaviors() |
||
46 | { |
||
47 | return [ |
||
48 | 'contentNegotiator' => [ |
||
49 | 'class' => ContentNegotiator::class, |
||
50 | 'formats' => [ |
||
51 | 'application/json' => Response::FORMAT_JSON, |
||
52 | ], |
||
53 | ], |
||
54 | 'verbFilter' => [ |
||
55 | 'class' => VerbFilter::class, |
||
56 | 'actions' => $this->verbs(), |
||
57 | ], |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function afterAction($action, $result) |
||
65 | { |
||
66 | $result = parent::afterAction($action, $result); |
||
67 | return $this->serializeData($result); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | public function verbs() |
||
74 | { |
||
75 | return [ |
||
76 | 'validate' => ['POST'] |
||
77 | ]; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Send new file to upload it. |
||
82 | * |
||
83 | * @throws BadRequestHttpException |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | public function actionValidate() |
||
88 | { |
||
89 | try { |
||
90 | return $this->checkVerifyCode(Yii::$app->request); |
||
91 | |||
92 | } catch (InvalidArgumentException|\Exception $e) { |
||
93 | throw new BadRequestHttpException($e->getMessage(), $e->getCode()); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Provides upload or update file. |
||
99 | * |
||
100 | * @throws InvalidArgumentException |
||
101 | * |
||
102 | * @param $request |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | private function checkVerifyCode($request) |
||
107 | { |
||
108 | if (!($request instanceof Request)) { |
||
109 | throw new InvalidArgumentException('Param $request must be instanceof yii\web\Request.'); |
||
110 | } |
||
111 | |||
112 | $resp = $this->recaptchaDriver->verify($request->post('g_recaptcha_response'), BaseHelper::ip_address()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
113 | |||
114 | if ($resp->isSuccess()) { |
||
115 | return $this->getSuccessResponse(''); |
||
116 | } else { |
||
117 | return $this->getFailResponse(Yii::t('feedback', 'Error verify captcha.'), [ |
||
118 | 'errors' => $resp->getErrorCodes() |
||
119 | ]); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Serializes the specified data. |
||
125 | * The default implementation will create a serializer based on the configuration given by [[serializer]]. |
||
126 | * It then uses the serializer to serialize the given data. |
||
127 | * |
||
128 | * @param mixed $data the data to be serialized |
||
129 | * |
||
130 | * @return mixed the serialized data. |
||
131 | */ |
||
132 | private function serializeData($data) |
||
133 | { |
||
134 | return Yii::createObject($this->serializer)->serialize($data); |
||
135 | } |
||
136 | } |
||
137 |