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: * @property Settings $oModuleSettings
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: protected $manager = null;
22:
23: protected function getManager()
24: {
25: if ($this->manager === null) {
26: $this->manager = new Manager($this);
27: }
28:
29: return $this->manager;
30: }
31:
32: public function init()
33: {
34: $this->aErrors = [
35: Enums\ErrorCodes::RecaptchaVerificationError => $this->i18N('ERROR_RECAPTCHA_VERIFICATION_DID_NOT_COMPLETE'),
36: Enums\ErrorCodes::RecaptchaUnknownError => $this->i18N('ERROR_UNKNOWN_RECAPTCHA_ERROR'),
37: ];
38:
39: \Aurora\System\EventEmitter::getInstance()->onAny(
40: [
41: ['MailLoginFormWebclient::Login::before', [$this, 'onBeforeMailLoginFormWebclientLogin']],
42: ['StandardRegisterFormWebclient::Register::before', [$this, 'onBeforeStandardRegisterFormWebclientRegister']],
43: ['StandardLoginFormWebclient::Login::before', [$this, 'onBeforeStandardLoginFormWebclient'], 90],
44: ['MailSignup::Signup::before', [$this, 'onSignup'], 90],
45: ['Core::Login::after', [$this, 'onAfterLogin']]
46: ]
47: );
48:
49: $this->subscribeEvent('AddToContentSecurityPolicyDefault', array($this, 'onAddToContentSecurityPolicyDefault'));
50: }
51:
52: /**
53: * @return Module
54: */
55: public static function getInstance()
56: {
57: return parent::getInstance();
58: }
59:
60: /**
61: * @return Module
62: */
63: public static function Decorator()
64: {
65: return parent::Decorator();
66: }
67:
68: /**
69: * @return Settings
70: */
71: public function getModuleSettings()
72: {
73: return $this->oModuleSettings;
74: }
75:
76: public function onAddToContentSecurityPolicyDefault($aArgs, &$aAddDefault)
77: {
78: $aAddDefault[] = 'www.google.com www.gstatic.com';
79: }
80:
81: /**
82: * Obtains list of module settings for authenticated user.
83: * @return array
84: */
85: public function GetSettings()
86: {
87: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
88:
89: return [
90: 'PublicKey' => $this->oModuleSettings->PublicKey,
91: 'LimitCount' => $this->oModuleSettings->LimitCount,
92: 'ShowRecaptcha' => $this->getManager()->isRecaptchaEnabledForIP(),
93: ];
94: }
95:
96: public function onBeforeStandardRegisterFormWebclientRegister($aArgs, &$mResult, &$mSubscriptionResult)
97: {
98: if ($this->getManager()->isRecaptchaEnabledForIP()) {
99: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
100:
101: $mSubscriptionResult = $this->getManager()->checkIfRecaptchaError();
102: if (!empty($mSubscriptionResult)) {
103: // The result contains an error -> stop executing the Register method
104: return true;
105: }
106:
107: $this->getManager()->disableRecaptchaCheckOnLogin();
108: }
109: }
110:
111: public function onBeforeMailLoginFormWebclientLogin($aArgs, &$mResult, &$mSubscriptionResult)
112: {
113: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
114: }
115:
116: public function onBeforeStandardLoginFormWebclient($aArgs, &$mResult, &$mSubscriptionResult)
117: {
118: if ($this->getManager()->needToCheckRecaptchaOnLogin()) {
119: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
120:
121: $mSubscriptionResult = $this->getManager()->checkIfRecaptchaError();
122: if (!empty($mSubscriptionResult)) {
123: // The result contains an error -> stop executing the Login method
124: return true;
125: }
126:
127: $this->getManager()->clearAuthErrorCount();
128: }
129: }
130:
131: public function onSignup($aArgs, &$mResult, &$mSubscriptionResult)
132: {
133: if ($this->getManager()->isRecaptchaEnabledForIP()) {
134: $this->getManager()->memorizeRecaptchaWebclientPluginToken($aArgs);
135:
136: $mSubscriptionResult = $this->getManager()->checkIfRecaptchaError();
137: if (!empty($mSubscriptionResult)) {
138: // The result contains an error -> stop executing the Register method
139: return true;
140: }
141: }
142: }
143:
144: public function onAfterLogin($aArgs, &$mResult)
145: {
146: // if authentication has failed, increment auth-error counter
147: if (!(is_array($mResult) && isset($mResult[\Aurora\System\Application::AUTH_TOKEN_KEY]))) {
148: $this->getManager()->incrementAuthErrorCount();
149: }
150: }
151: }
152: