1: <?php
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: class Mage_Captcha_Model_Observer
35: {
36: 37: 38: 39: 40: 41:
42: public function checkForgotpassword($observer)
43: {
44: $formId = 'user_forgotpassword';
45: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
46: if ($captchaModel->isRequired()) {
47: $controller = $observer->getControllerAction();
48: if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
49: Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
50: $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
51: $controller->getResponse()->setRedirect(Mage::getUrl('*/*/forgotpassword'));
52: }
53: }
54: return $this;
55: }
56:
57: 58: 59: 60: 61: 62:
63: public function checkUserLogin($observer)
64: {
65: $formId = 'user_login';
66: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
67: $controller = $observer->getControllerAction();
68: $loginParams = $controller->getRequest()->getPost('login');
69: $login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
70: if ($captchaModel->isRequired($login)) {
71: $word = $this->_getCaptchaString($controller->getRequest(), $formId);
72: if (!$captchaModel->isCorrect($word)) {
73: Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
74: $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
75: Mage::getSingleton('customer/session')->setUsername($login);
76: $beforeUrl = Mage::getSingleton('customer/session')->getBeforeAuthUrl();
77: $url = $beforeUrl ? $beforeUrl : Mage::helper('customer')->getLoginUrl();
78: $controller->getResponse()->setRedirect($url);
79: }
80: }
81: $captchaModel->logAttempt($login);
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function checkUserCreate($observer)
92: {
93: $formId = 'user_create';
94: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
95: if ($captchaModel->isRequired()) {
96: $controller = $observer->getControllerAction();
97: if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
98: Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
99: $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
100: Mage::getSingleton('customer/session')->setCustomerFormData($controller->getRequest()->getPost());
101: $controller->getResponse()->setRedirect(Mage::getUrl('*/*/create'));
102: }
103: }
104: return $this;
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function checkGuestCheckout($observer)
114: {
115: $formId = 'guest_checkout';
116: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
117: $checkoutMethod = Mage::getSingleton('checkout/type_onepage')->getQuote()->getCheckoutMethod();
118: if ($checkoutMethod == Mage_Checkout_Model_Type_Onepage::METHOD_GUEST) {
119: if ($captchaModel->isRequired()) {
120: $controller = $observer->getControllerAction();
121: if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
122: $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
123: $result = array('error' => 1, 'message' => Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
124: $controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
125: }
126: }
127: }
128: return $this;
129: }
130:
131: 132: 133: 134: 135: 136:
137: public function checkRegisterCheckout($observer)
138: {
139: $formId = 'register_during_checkout';
140: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
141: $checkoutMethod = Mage::getSingleton('checkout/type_onepage')->getQuote()->getCheckoutMethod();
142: if ($checkoutMethod == Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER) {
143: if ($captchaModel->isRequired()) {
144: $controller = $observer->getControllerAction();
145: if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
146: $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
147: $result = array('error' => 1, 'message' => Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
148: $controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
149: }
150: }
151: }
152: return $this;
153: }
154:
155: 156: 157: 158: 159: 160:
161: public function checkUserLoginBackend($observer)
162: {
163: $formId = 'backend_login';
164: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
165: $loginParams = Mage::app()->getRequest()->getPost('login');
166: $login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
167: if ($captchaModel->isRequired($login)) {
168: if (!$captchaModel->isCorrect($this->_getCaptchaString(Mage::app()->getRequest(), $formId))) {
169: $captchaModel->logAttempt($login);
170: Mage::throwException(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
171: }
172: }
173: $captchaModel->logAttempt($login);
174: return $this;
175: }
176:
177: 178: 179: 180: 181:
182: protected function _getBackendSession()
183: {
184: return Mage::getSingleton('adminhtml/session');
185: }
186:
187: 188: 189: 190: 191: 192:
193: public function checkUserForgotPasswordBackend($observer)
194: {
195: $formId = 'backend_forgotpassword';
196: $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
197: $controller = $observer->getControllerAction();
198: $email = (string) $observer->getControllerAction()->getRequest()->getParam('email');
199: $params = $observer->getControllerAction()->getRequest()->getParams();
200:
201: if (!empty($email) && !empty($params)){
202: if ($captchaModel->isRequired()){
203: if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
204: $this->_getBackendSession()->setEmail((string) $controller->getRequest()->getPost('email'));
205: $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
206: $this->_getBackendSession()->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
207: $controller->getResponse()->setRedirect(Mage::getUrl('*/*/forgotpassword'));
208: }
209: }
210: }
211: return $this;
212: }
213:
214: 215: 216: 217: 218: 219:
220: public function resetAttemptForFrontend($observer)
221: {
222: return $this->_resetAttempt($observer->getModel()->getEmail());
223: }
224:
225: 226: 227: 228: 229: 230:
231: public function resetAttemptForBackend($observer)
232: {
233: return $this->_resetAttempt($observer->getUser()->getUsername());
234: }
235:
236: 237: 238: 239: 240:
241: public function deleteOldAttempts()
242: {
243: Mage::getResourceModel('captcha/log')->deleteOldAttempts();
244: return $this;
245: }
246:
247: 248: 249: 250: 251:
252: public function deleteExpiredImages()
253: {
254: foreach (Mage::app()->getWebsites(true) as $website){
255: $expire = time() - Mage::helper('captcha')->getConfigNode('timeout', $website->getDefaultStore())*60;
256: $imageDirectory = Mage::helper('captcha')->getImgDir($website);
257: foreach (new DirectoryIterator($imageDirectory) as $file) {
258: if ($file->isFile() && pathinfo($file->getFilename(), PATHINFO_EXTENSION) == 'png') {
259: if ($file->getMTime() < $expire) {
260: unlink($file->getPathname());
261: }
262: }
263: }
264: }
265: return $this;
266: }
267:
268: 269: 270: 271: 272: 273:
274: protected function _resetAttempt($login)
275: {
276: Mage::getResourceModel('captcha/log')->deleteUserAttempts($login);
277: return $this;
278: }
279:
280: 281: 282: 283: 284: 285: 286:
287: protected function _getCaptchaString($request, $formId)
288: {
289: $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
290: return $captchaParams[$formId];
291: }
292: }
293: