forked from seosamba/anycaptcha
/
Anycaptcha.php
152 lines (131 loc) · 4.98 KB
/
Anycaptcha.php
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
<?php
set_include_path(implode(PATH_SEPARATOR, array(
dirname(__FILE__) . '/system/classes/forms',
dirname(__FILE__) . '/system/classes/dbtables',
get_include_path(),
)));
/**
* Anycaptcha plugin
*
*/
class Anycaptcha implements RCMS_Core_PluginInterface {
const ACTION_POSTFIX = 'Action';
private $_options = array();
private $_seotoasterData = array();
private $_session = null;
private $_view = null;
private $_websiteUrl = null;
private $_request = null;
private $_redirector = null;
private $_dbTalbeSettings = null;
public function __construct($options, $seotoasterData) {
$this->_options = $options;
$this->_seotoasterData = $seotoasterData;
$this->_websiteUrl = $this->_seotoasterData['websiteUrl'];
$this->_request = new Zend_Controller_Request_Http();
$this->_redirector = new Zend_Controller_Action_Helper_Redirector();
$this->_session = new Zend_Session_Namespace($this->_websiteUrl);
$this->_dbTalbeSettings = new CaptchaSettings();
$this->_view = new Zend_View(array(
'scriptPath' => dirname(__FILE__) . '/views'
));
if($this->_seotoasterData['url']) {
$this->_session->pluginPageUrl = $this->_websiteUrl . $this->_seotoasterData['url'] . '.html';
}
$this->_view->websiteUrl = $this->_websiteUrl;
}
public function run($requestedParams = array()) {
if(!isset($this->_session->captchaError)) {
$this->_session->captchaError = array();
}
$optionResult = $this->_dispatchOptions();
if($optionResult !== null) {
return $optionResult;
}
$this->_dispatch($requestedParams);
$captchaId = RCMS_Tools_Tools::generateCaptcha();
$this->_session->captchaId = $captchaId;
$this->_view->captchaId = $captchaId;
$this->_view->captchaSettings = $this->_dbTalbeSettings->fetchAllConfig();
$this->_view->savedParams = $this->_session->savedParams;
$this->_session->savedParams = array();
return $this->_view->render('captcha.phtml');
}
private function _dispatchOptions() {
if(!empty($this->_options)) {
foreach ($this->_options as $option) {
$optionMakerName = '_makeOption' . ucfirst($option);
if(in_array($optionMakerName, get_class_methods(__CLASS__))) {
return $this->$optionMakerName();
}
return '<strong>Invalid option</strong>';
}
}
}
private function _makeOptionProxy() {
return $this->_websiteUrl . 'plugin/anycaptcha/run/handler';
}
private function _makeOptionMessage() {
$this->_view->messages = $this->_session->captchaError;
$this->_session->captchaError = array();
return $this->_view->render('messages.phtml');
}
private function _dispatch($params) {
$action = ($params['run']) ? $params['run'] . self::ACTION_POSTFIX : null;
if($action !== null) {
if(in_array($action, get_class_methods(__CLASS__))) {
$this->$action();
exit;
}
}
}
/**
*
* @todo modify this meth to use zend + altered post params
*/
public function handlerAction() {
$params = $this->_request->getParams();
$captchaSettings = $this->_dbTalbeSettings->fetchAllConfig();
$captchaWord = $params['captcha'];
$realAction = $params['realAction'];
$captcha = new Zend_Session_Namespace('Zend_Form_Captcha_' . $this->_session->captchaId);
$captchaIterator = $captcha->getIterator();
if($captchaWord == $captchaIterator['word']) {
unset($params['captcha']);
unset ($params['realAction']);
$httpClient = new Zend_Http_Client($realAction);
$httpClient->setRawData(http_build_query($params));
$response = $httpClient->request(Zend_Http_Client::POST);
if($response->getStatus() != 200) {
$this->_session->captchaError[] = 'Faild to send form';
if(isset($captchaSettings['redirectFail'])) {
$this->_redirector->gotoUrlAndExit($captchaSettings['redirectFail']);
}
$this->_session->savedParams = $params;
$this->_redirector->gotoUrlAndExit(($this->_session->pluginPageUrl) ? $this->_session->pluginPageUrl : $this->_websiteUrl);
}
else {
if(isset($captchaSettings['redirectSuccess'])) {
$this->_redirector->gotoUrlAndExit($captchaSettings['redirectSuccess']);
}
$this->_session->captchaError[] = 'Form sent.';
$this->_redirector->gotoUrlAndExit(($this->_session->pluginPageUrl) ? $this->_session->pluginPageUrl : $this->_websiteUrl);
}
}
$this->_session->captchaError[] = $captchaSettings['errorText'];
$this->_session->savedParams = $params;
$this->_redirector->gotoUrlAndExit(($this->_session->pluginPageUrl) ? $this->_session->pluginPageUrl : $this->_websiteUrl);
}
public function settingsAction() {
if(!$this->_request->isPost()) {
$settingsForm = new SettingsForm($this->_dbTalbeSettings->fetchAllConfig());
$this->_view->settingsForm = $settingsForm;
}
elseif ($this->_request->isXmlHttpRequest()) {
$data = $this->_request->getParams();
$result = $this->_dbTalbeSettings->updateSettings($data);
exit;
}
echo $this->_view->render('settings.phtml');
}
}