1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\RecaptchaWebclientPlugin;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2023, Afterlogic Corp.
14: *
15: * @package Modules
16: */
17: class Module extends \Aurora\System\Module\AbstractModule
18: {
19: protected $manager = null;
20:
21: protected function getManager()
22: {
23: if ($this->manager === null) {
24: $this->manager = new Manager($this);
25: }
26:
27: return $this->manager;
28: }
29:
30: public function init()
31: {
32: $this->aErrors = [
33: Enums\ErrorCodes::RecaptchaVerificationError => $this->i18N('ERROR_RECAPTCHA_VERIFICATION_DID_NOT_COMPLETE'),
34: Enums\ErrorCodes::RecaptchaUnknownError => $this->i18N('ERROR_UNKNOWN_RECAPTCHA_ERROR'),
35: ];
36:
37: \Aurora\System\EventEmitter::getInstance()->onAny(
38: [
39: ['MailLoginFormWebclient::Login::before', [$this, 'onBeforeMailLoginFormWebclientLogin']],
40: ['StandardRegisterFormWebclient::Register::before', [$this, 'onBeforeStandardRegisterFormWebclientRegister']],
41: ['StandardLoginFormWebclient::Login::before', [$this, 'onBeforeStandardLoginFormWebclient'], 90],
42: ['MailSignup::Signup::before', [$this, 'onSignup'], 90],
43: ['Core::Login::after', [$this, 'onAfterLogin']]
44: ]
45: );
46:
47: $this->subscribeEvent('AddToContentSecurityPolicyDefault', array($this, 'onAddToContentSecurityPolicyDefault'));
48: }
49:
50: public function onAddToContentSecurityPolicyDefault($aArgs, &$aAddDefault)
51: {
52: $aAddDefault[] = 'www.google.com www.gstatic.com';
53: }
54:
55: /**
56: * Obtains list of module settings for authenticated user.
57: * @return array
58: */
59: public function GetSettings()
60: {
61: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
62:
63: return [
64: 'PublicKey' => $this->getConfig('PublicKey', ''),
65: 'LimitCount' => $this->getConfig('LimitCount', 0),
66: 'ShowRecaptcha' => $this->getManager()->isRecaptchaEnabledForIP(),
67: ];
68: }
69:
70: public function onBeforeStandardRegisterFormWebclientRegister($aArgs, &$mResult, &$mSubscriptionResult)
71: {
72: if ($this->getManager()->isRecaptchaEnabledForIP()) {
73: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
74:
75: $mSubscriptionResult = $this->getManager()->checkIfRecaptchaError();
76: if (!empty($mSubscriptionResult)) {
77: // The result contains an error -> stop executing the Register method
78: return true;
79: }
80:
81: $this->getManager()->disableRecaptchaCheckOnLogin();
82: }
83: }
84:
85: public function onBeforeMailLoginFormWebclientLogin($aArgs, &$mResult, &$mSubscriptionResult)
86: {
87: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
88: }
89:
90: public function onBeforeStandardLoginFormWebclient($aArgs, &$mResult, &$mSubscriptionResult)
91: {
92: if ($this->getManager()->needToCheckRecaptchaOnLogin()) {
93: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
94:
95: $mSubscriptionResult = $this->getManager()->checkIfRecaptchaError();
96: if (!empty($mSubscriptionResult)) {
97: // The result contains an error -> stop executing the Login method
98: return true;
99: }
100:
101: $this->getManager()->clearAuthErrorCount();
102: }
103: }
104:
105: public function onSignup($aArgs, &$mResult, &$mSubscriptionResult)
106: {
107: if ($this->getManager()->isRecaptchaEnabledForIP()) {
108: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
109:
110: $mSubscriptionResult = $this->getManager()->checkIfRecaptchaError();
111: if (!empty($mSubscriptionResult)) {
112: // The result contains an error -> stop executing the Register method
113: return true;
114: }
115: }
116: }
117:
118: public function onAfterLogin($aArgs, &$mResult)
119: {
120: // if authentication has failed, increment auth-error counter
121: if (!(is_array($mResult) && isset($mResult['AuthToken']))) {
122: $this->getManager()->incrementAuthErrorCount();
123: }
124: }
125: }
126: