Hqpeak_TorBlocker - Version 1.0.0

Version Notes

This is a stable v1.0.0 Tor Blocker extension which is developed and tested under Magento v1.8.1.0

Download this release

Release Info

Developer HQPeak
Extension Hqpeak_TorBlocker
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/Hqpeak/TorBlocker/Block/Adminhtml/Information.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Block_Adminhtml_Information extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface
3
+ {
4
+ public function render(Varien_Data_Form_Element_Abstract $element) {
5
+ $helper = Mage::helper('hqpeaktorblocker');
6
+ $moduleId = 'Hqpeak_TorBlocker';
7
+ $name = $this->_getConfigValue($moduleId, 'name');
8
+ $version = $this->_getConfigValue($moduleId, 'version');
9
+ $description = $this->_getConfigValue($moduleId, 'description');
10
+ $extensionPage = $this->_getConfigValue($moduleId, 'extlink');
11
+
12
+ $html =
13
+ '<style>
14
+ .information {width: 600px;}
15
+ .information-label {color: #000000; font-weight: bold; width: 150px;}
16
+ .information-text { padding-bottom: 15px;}
17
+ </style>';
18
+
19
+ $html .= '
20
+ <table cellspacing="0" cellpading="0" class="information">
21
+ <tr>
22
+ <td class="information-label">'.$helper->__('Extension:').'</td>
23
+ <td class="information-text"><strong>'.$name.'</strong></td>
24
+ </tr>
25
+ <tr>
26
+ <td class="information-label">'.$helper->__('Version:').'</td>
27
+ <td class="information-text"><strong>'.$version.'</strong></td>
28
+ </tr>
29
+ <tr>
30
+ <td class="information-label">'.$helper->__('Short Description:').'</td>
31
+ <td class="information-text">'.$description.'</td>
32
+ </tr>
33
+ <tr>
34
+ <td class="information-label">'.$helper->__('Download Page:').'</td>
35
+ <td class="information-text">'.$extensionPage.'</td>
36
+ </tr>
37
+ </table>';
38
+
39
+ return $html;
40
+ }
41
+
42
+ protected function _getConfigValue($module, $config) {
43
+ $locale = Mage::app()->getLocale()->getLocaleCode();
44
+ $defaultLocale = 'en_US';
45
+ $mainConfig = Mage::getConfig();
46
+ $moduleConfig = $mainConfig->getNode('modules/'.$module.'/'.$config);
47
+
48
+ if ( (string) $moduleConfig ) {
49
+ return $moduleConfig;
50
+ }
51
+
52
+ if ( $moduleConfig->$locale ) {
53
+ return $moduleConfig->$locale;
54
+ } else {
55
+ return $moduleConfig->$defaultLocale;
56
+ }
57
+ }
58
+ }
59
+ ?>
app/code/community/Hqpeak/TorBlocker/Block/Adminhtml/System/Config/Checkbox.php ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Block_Adminhtml_System_Config_Checkbox extends Mage_Adminhtml_Block_System_Config_Form_Field
3
+ {
4
+ const CONFIG_PATH = 'settings/configuration/options';
5
+ protected $_values = null;
6
+
7
+ protected function _construct() {
8
+ parent::_construct();
9
+ $this->setTemplate('hqpeaktorblocker/system/config/checkbox.phtml');
10
+ }
11
+
12
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
13
+ $this->setNamePrefix($element->getName())->setHtmlId($element->getHtmlId());
14
+ return $this->_toHtml();
15
+ }
16
+
17
+ public function getValues() {
18
+ $values = array();
19
+ foreach (Mage::getSingleton('hqpeaktorblocker/source')->toOptionArray() as $value) {
20
+ $values[$value['value']] = $value['label'];
21
+ }
22
+ return $values;
23
+ }
24
+
25
+ public function getIsChecked($name) {
26
+ return in_array($name, $this->getCheckedValues());
27
+ }
28
+
29
+ public function getCheckedValues() {
30
+ $data = explode(",", Mage::getStoreConfig(self::CONFIG_PATH));
31
+ $this->_values = $data;
32
+ return $this->_values;
33
+ }
34
+ }
35
+ ?>
app/code/community/Hqpeak/TorBlocker/Helper/Data.php ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Helper_Data extends Mage_Core_Helper_Abstract {
3
+
4
+ }
5
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Ipaddress.php ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Ipaddress extends Mage_Core_Model_Abstract
3
+ {
4
+ protected $_table = null;
5
+ protected $_read = null;
6
+ protected $_write = null;
7
+ protected $_default_version = null;
8
+
9
+ public function _construct() {
10
+ parent::_construct();
11
+
12
+ $this->_init('hqpeaktorblocker/ipaddress');
13
+
14
+ $this->_table = Mage::getSingleton('core/resource')->getTableName('hqpeaktorblocker/ipaddress');
15
+ $this->_read = Mage::getSingleton('core/resource')->getConnection('core/read');
16
+ $this->_write = Mage::getSingleton('core/resource')->getConnection('core/write');
17
+ }
18
+
19
+ public function setTable($table) {
20
+ $this->_table = $table;
21
+ }
22
+
23
+ public function getTable() {
24
+ return $this->_table;
25
+ }
26
+
27
+ public function setRead($read) {
28
+ $this->_read = $read;
29
+ }
30
+
31
+ public function getRead() {
32
+ return $this->_read;
33
+ }
34
+
35
+ public function setWrite($write) {
36
+ $this->_write = $write;
37
+ }
38
+
39
+ public function getWrite() {
40
+ return $this->_write;
41
+ }
42
+
43
+ public function setVersion() {
44
+ if ( !Mage::getStoreConfig('settings/main/url') ) {
45
+ $this->_version = 'http://hqpeak.com/torexitlist/free/?format=json';
46
+ } else {
47
+ $this->_version = Mage::getStoreConfig('settings/main/url');
48
+ }
49
+ }
50
+
51
+ public function getVersion() {
52
+ return $this->_version;
53
+ }
54
+
55
+ public function emptyData() {
56
+ if ( $this->_table ) {
57
+ $sql = $this->_read->query("TRUNCATE TABLE $this->_table");
58
+ return true;
59
+ }
60
+
61
+ return false;
62
+ }
63
+
64
+ public function torGetIp($url) {
65
+ $timeout = 5;
66
+ $redirects = 0;
67
+ $client = new Zend_Http_Client($url, array('maxredirects' => $redirects, 'timeout' => $timeout));
68
+ $response = $client->request('POST');
69
+ $data = $response->getBody();
70
+
71
+
72
+ if ( $response->isError() )
73
+ return array();
74
+
75
+ //decode output as array
76
+ $service_data = json_decode($data, true);
77
+
78
+ //never trust the input - sanitate every ip
79
+ if ( is_array($service_data) && $size = sizeof($service_data) > 0 ) {
80
+ for ( $i=0; $i<$size; $i++ ) {
81
+ if ( !preg_match("/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}/", $service_data[$i]) )
82
+ $service_data[$i] = "0.0.0.0";
83
+ }
84
+ } else {
85
+ return array();
86
+ }
87
+
88
+ return $service_data;
89
+ }
90
+
91
+ public function torToLong($arr) {
92
+ if ( is_array($arr) ) {
93
+ $ip2long = array();
94
+
95
+ foreach ( $arr as $ip ) {
96
+ if ( !in_array(ip2long($ip), $ip2long) )
97
+ $ip2long[] = ip2long($ip);
98
+ }
99
+ } else {
100
+ die("Bad data");
101
+ }
102
+
103
+ return $ip2long;
104
+ }
105
+
106
+ public function torFillTable($ip, $write, $table) {
107
+ if ( is_array($ip) ) {
108
+ $sql = "INSERT INTO $table (ip) VALUES ";
109
+
110
+ foreach ( $ip as $dataItem ) {
111
+ $dataItem = Mage::getSingleton('core/resource')->getConnection('core/read')->quote($dataItem, Zend_Db::INT_TYPE);
112
+ $sql .= "(".$dataItem."), ";
113
+ }
114
+
115
+ $sql = rtrim($sql, ', ');
116
+ $write->query($sql);
117
+ } else {
118
+ die("Bad data");
119
+ }
120
+ }
121
+ }
122
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Observer.php ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Observer extends Varien_Event_Observer
3
+ {
4
+ protected $_uri = null;
5
+ protected $_url = null;
6
+ protected $_template = null;
7
+ protected $_request = null;
8
+ protected $_options = null;
9
+ protected $_time = null;
10
+ protected $_timediff = null;
11
+ protected $_table = null;
12
+ protected $_read = null;
13
+ protected $_write = null;
14
+
15
+ public function _construct() {
16
+ parent::_construct();
17
+
18
+ if ( !Mage::getStoreConfig('settings/main/time') ) {
19
+ $this->_setTime();
20
+ }
21
+
22
+ $this->_table = Mage::getSingleton('core/resource')->getTableName('hqpeaktorblocker/ipaddress');
23
+ $this->_read = Mage::getSingleton('core/resource')->getConnection('core/read');
24
+ $this->_write = Mage::getSingleton('core/resource')->getConnection('core/write');
25
+ $this->_timediff = Mage::getStoreConfig('settings/main/timeupdate');
26
+ }
27
+
28
+ public function serviceCheck($observer) {
29
+ $this->_readData();
30
+ $this->_updateService();
31
+ }
32
+
33
+ public function ipCheckVisit($observer) {
34
+ $this->_allowVisit('visit');
35
+ $this->_allowPost();
36
+ }
37
+
38
+ public function ipCheckUrl($observer) {
39
+ $this->_urlVisit();
40
+ }
41
+
42
+ public function ipCheckFront($observer) {
43
+ $this->_allowVisit('front');
44
+
45
+ }
46
+
47
+ public function ipCheckAdmin($observer) {
48
+ $this->_allowVisit('admin');
49
+ }
50
+
51
+ public function checkRegister($observer) {
52
+ $this->_allowVisit('register');
53
+ }
54
+
55
+ public function saveConfig($observer) {
56
+ $url = Mage::getStoreConfig('settings/main/url');
57
+ $urlCheck = Mage::getStoreConfig('settings/main/version');
58
+
59
+ if ( !Mage::getStoreConfig('settings/configuration/check') ) {
60
+ Mage::getConfig()->saveConfig('settings/main/check', 1);
61
+ }
62
+
63
+ if ( $url !== $urlCheck ) {
64
+ Mage::getConfig()->saveConfig('settings/main/version', $url);
65
+ $model->updateTable();
66
+ }
67
+ }
68
+
69
+ protected function _readData() {
70
+ $this->_uri = Mage::app()->getRequest()->getRequestUri();
71
+ $this->_url = Mage::getStoreConfig('settings/main/url');
72
+ $this->_time = Mage::getStoreConfig('settings/main/time');
73
+ $this->_template = Mage::getStoreConfig('settings/main/errortemplate');
74
+ $this->_request = Mage::getStoreConfig('settings/configuration/request');
75
+ $this->_options = Mage::getStoreConfig('settings/configuration/options');
76
+ }
77
+
78
+ protected function _updateService() {
79
+ $t = time();
80
+ $diff = $t - $this->_time;
81
+
82
+ if ( ($this->_url === 'http://hqpeak.com/torexitlist/free/?format=json' ||
83
+ preg_match('/^http(s)?:\/\/(w{3}\.)?hqpeak.com(\/.+)+\?id=[0-9a-zA-Z]{40}&format=json/', $this->_url)) &&
84
+ $diff > $this->_timediff ) {
85
+
86
+ $model = Mage::getModel('hqpeaktorblocker/ipaddress');
87
+ if ( !$model )
88
+ die('Model cannot be loaded or doesn\'t exist');
89
+
90
+
91
+ $ip_arr = $model->torGetIp($this->_url);
92
+ if ( is_array($ip_arr) && sizeof($ip_arr) > 0 ) {
93
+ if ( $this->_checkTable() ) {
94
+ $sql = $this->_read->query("TRUNCATE TABLE $this->_table");
95
+ } else {
96
+ $sql = $this->_write->query("CREATE TABLE $this->_table");
97
+ }
98
+
99
+ $ip_long = $model->torToLong($ip_arr);
100
+ $model->torFillTable($ip_long, $this->_write, $this->_table);
101
+ }
102
+
103
+ $this->_setTime();
104
+ }
105
+ }
106
+
107
+ protected function _allowVisit($allow) {
108
+ $opt_arr = explode(",", $this->_options);
109
+ $check = false;
110
+
111
+ if ( !empty($opt_arr) ) {
112
+ foreach ( $opt_arr as $option ) {
113
+ if ( $option == $allow ) {
114
+ $check = true;
115
+ break;
116
+ }
117
+ }
118
+
119
+ if ( !$check ) {
120
+ if ( $this->_ipCheck() ) {
121
+ $this->_errorMsg();
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ protected function _allowPost() {
128
+ $opt_arr = explode(",", $this->_options);
129
+ $check = false;
130
+
131
+ if ( !empty($opt_arr) ) {
132
+ foreach ( $opt_arr as $option ) {
133
+ if ( $option == "post" ) {
134
+ $check = true;
135
+ break;
136
+ }
137
+ }
138
+
139
+ if ( !$check && Mage::app()->getRequest()->isPost() ) {
140
+ if ( $this->_ipCheck() ) {
141
+ $this->_errorMsg();
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ protected function _urlVisit() {
148
+ $requests_arr = explode(",", $this->_request);
149
+ $check = false;
150
+
151
+ if ( !empty($requests_arr) ) {
152
+ foreach ( $requests_arr as $request ) {
153
+ $req = trim(strtolower($request));
154
+
155
+ if ( in_array($req, array_keys(Mage::app()->getRequest()->getPost())) ||
156
+ in_array($req, array_keys(Mage::app()->getRequest()->getParams())) ||
157
+ $req === Mage::app()->getRequest()->getModuleName() ||
158
+ $req === Mage::app()->getRequest()->getControllerName() ||
159
+ $req === Mage::app()->getRequest()->getActionName() )
160
+ {
161
+ $check = true;
162
+ break;
163
+ }
164
+ }
165
+
166
+ if ( $check ) {
167
+ if ( $this->_ipCheck() ) {
168
+ $this->_errorMsg();
169
+ }
170
+ }
171
+ }
172
+ }
173
+
174
+ protected function _getUserIp($address) {
175
+ $user_ip = $address;
176
+ return $user_ip;
177
+ }
178
+
179
+ protected function _ipCheck() {
180
+ if ( $this->_checkTable() ) {
181
+ if ( $this->_getUserIp(Mage::app()->getRequest()->getClientIp()) ) {
182
+ $user2long = ip2long(Mage::app()->getRequest()->getClientIp());
183
+ $tor_address = $this->_torUser($user2long);
184
+
185
+ if ( $tor_address ) {
186
+ return true;
187
+ }
188
+ }
189
+
190
+ /*if ( $this->_getUserIp($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
191
+ $user2long = ip2long($this->_getUserIp($_SERVER['HTTP_X_FORWARDED_FOR']));
192
+ $tor_address = $this->_torUser($user2long);
193
+
194
+ if ( $tor_address ) {
195
+ return true;
196
+ }
197
+ }*/
198
+ } else {
199
+ die("Table with ip addresses from tor exit list does not exist.");
200
+ }
201
+
202
+ return $this->_check;
203
+ }
204
+
205
+ protected function _torUser($ip_long) {
206
+ $ip_long = Mage::getSingleton('core/resource')->getConnection('core/read')->quote($ip_long, Zend_Db::INT_TYPE);
207
+ $sql = "SELECT * FROM $this->_table WHERE `ip`=$ip_long";
208
+ $query = $this->_read->fetchOne($sql);
209
+ return $query;
210
+ }
211
+
212
+ protected function _setTime() {
213
+ Mage::getConfig()->saveConfig('settings/main/time', time());
214
+ }
215
+
216
+ protected function _checkTable() {
217
+ return $this->_table;
218
+ }
219
+
220
+ protected function _updateTable() {
221
+ $model = Mage::getModel('hqpeaktorblocker/ipaddress');
222
+ if ( !$model )
223
+ die("Model cannot be loaded or doesn\'t exist");
224
+
225
+ $model->setTable($this->_table);
226
+ $model->setRead($this->_read);
227
+ $model->setWrite($this->_write);
228
+ $model->setVersion();
229
+
230
+ if ( !$model->emptyData() ) {
231
+ die("Failed to insert data in table");
232
+ }
233
+
234
+ $table = $model->getTable();
235
+ $read = $model->getRead();
236
+ $write = $model->getWrite();
237
+ $default_version = $model->getVersion();
238
+
239
+ $ip_arr = $model->torGetIp($default_version);
240
+ if ( is_array($ip_arr) && !empty($ip_arr) ) {
241
+ $ip_long = $model->torToLong($ip_arr);
242
+ $model->torFillTable($ip_long, $write, $table);
243
+ $this->_setTime();
244
+ }
245
+ }
246
+
247
+ protected function _errorMsg() {
248
+ echo htmlspecialchars_decode($this->_template);
249
+ die();
250
+ }
251
+ }
252
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Resource/Ipaddress.php ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Resource_Ipaddress extends Mage_Core_Model_Resource_Abstract
3
+ {
4
+ public function _construct() {
5
+ $this->_init('hqpeaktorblocker/ipaddress', 'ip');
6
+ }
7
+ }
8
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Resource/Ipaddress/Collection.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Resource_Ipaddress_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
3
+ {
4
+ public function _construct() {
5
+ parent::_construct();
6
+ $this->_init('hqpeaktorblocker/ipaddress');
7
+ }
8
+ }
9
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Resource/Setup.php ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Resource_Setup extends Mage_Core_Model_Resource_Setup {
3
+
4
+ }
5
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Source.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Source
3
+ {
4
+ public function toOptionArray() {
5
+ return array(
6
+ array('value' => 'visit', 'label' => Mage::helper('hqpeaktorblocker')->__('Visits <small>(Read only public content on the site)</small>')),
7
+ array('value' => 'front', 'label' => Mage::helper('hqpeaktorblocker')->__('Front-end <small>(Access only the front-end of the site)</small>')),
8
+ array('value' => 'admin', 'label' => Mage::helper('hqpeaktorblocker')->__('Admin <small>(Access the administration dashboard)</small>')),
9
+ array('value' => 'register', 'label' => Mage::helper('hqpeaktorblocker')->__('Registration <small>(Register for the site)</small>')),
10
+ array('value' => 'post', 'label' => Mage::helper('hqpeaktorblocker')->__('Request <small>(Send POST requests)</small>'))
11
+ );
12
+ }
13
+ }
14
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Validation/TimeValidate.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Validation_TimeValidate extends Mage_Core_Model_Config_Data
3
+ {
4
+ public function save() {
5
+ $time = $this->getValue();
6
+ $url = Mage::getStoreConfig('settings/main/url');
7
+
8
+ if ( $time === '' ) {
9
+ Mage::throwException('The time interval field should not be empty');
10
+ }
11
+
12
+ if ( ($url === 'http://hqpeak.com/torexitlist/free/?format=json' && $time < 18000) ||
13
+ (preg_match('/^http(s)?:\/\/(w{3}\.)?hqpeak.com(\/.+)+\?id=[0-9a-zA-Z]{40}&format=json/', $url) && $time < 300) )
14
+ {
15
+ Mage::throwException('The time interval is too short');
16
+ }
17
+
18
+ return parent::save();
19
+ }
20
+ }
21
+ ?>
app/code/community/Hqpeak/TorBlocker/Model/Validation/UrlValidate.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Model_Validation_UrlValidate extends Mage_Core_Model_Config_Data
3
+ {
4
+ public function save() {
5
+ $url = $this->getValue();
6
+ $urlCheck = Mage::getStoreConfig('settings/main/version');
7
+
8
+ if ( $url === '' ) {
9
+ Mage::throwException('The URL address field should not be empty');
10
+ }
11
+
12
+ if ( $url !== 'http://hqpeak.com/torexitlist/free/?format=json' && !preg_match('/^http(s)?:\/\/(w{3}\.)?hqpeak.com(\/.+)+\?id=[0-9a-zA-Z]{40}&format=json/', $url) ) {
13
+ Mage::throwException('The URL address supplied is not valid');
14
+ }
15
+
16
+ return parent::save();
17
+ }
18
+ }
19
+ ?>
app/code/community/Hqpeak/TorBlocker/controllers/Adminhtml/TorblockerController.php ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hqpeak_TorBlocker_Adminhtml_TorblockerController extends Mage_Adminhtml_Controller_Action
3
+ {
4
+ public function indexAction() {
5
+ print 'Default controller';
6
+ }
7
+ }
8
+ ?>
app/code/community/Hqpeak/TorBlocker/data/hqpeaktorblocker_setup/data-install-1.0.0.php ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $checkUrl = Mage::getStoreConfig('settings/configuration/url');
3
+ $model = Mage::getModel('hqpeaktorblocker/ipaddress');
4
+
5
+ if ( $checkUrl && $checkUrl !== 'http://hqpeak.com/torexitlist/free/?format=json' && !preg_match('/^http(s)?:\/\/(w{3}\.)?hqpeak.com(\/.+)+\?id=[0-9a-zA-Z]{40}&format=json/', $checkUrl) )
6
+ die('The URL address supplied is not valid');
7
+
8
+ if ( !$model )
9
+ die('Model cannot be loaded or doesn\'t exist');
10
+
11
+ $model->setTable(Mage::getSingleton('core/resource')->getTableName('hqpeaktorblocker/ipaddress'));
12
+ $model->setRead(Mage::getSingleton('core/resource')->getConnection('core/read'));
13
+ $model->setWrite(Mage::getSingleton('core/resource')->getConnection('core/write'));
14
+ $model->setVersion();
15
+
16
+ $table = $model->getTable();
17
+ $read = $model->getRead();
18
+ $write = $model->getWrite();
19
+ $default_version = $model->getVersion();
20
+
21
+ $ip_arr = $model->torGetIp($default_version);
22
+
23
+ if ( is_array($ip_arr) && !empty($ip_arr) ) {
24
+ $ip_long = $model->torToLong($ip_arr);
25
+ $model->torFillTable($ip_long, $write, $table);
26
+ }
27
+ ?>
app/code/community/Hqpeak/TorBlocker/etc/config.xml ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Hqpeak_TorBlocker>
5
+ <name>Tor Blocker</name>
6
+ <version>1.0.0</version>
7
+ <description>
8
+ <en_US>
9
+ <![CDATA[Most of the time Tor exit nodes are used to enumerate vulnerabilities of our online product, to perform attack or to be used as a spam source.
10
+ This extension allow us to limit the actions that could be performed by the users that are coming from a Tor exit nodes using http://hqpeak.com/torexitlist/ free service.
11
+ Could be upgraded to premium or could be set up any url to service that will give you response in the described json format.
12
+ Premium list is updated on every 5 minutes and free on 5 h.]]>
13
+ </en_US>
14
+ </description>
15
+ <extlink>
16
+ <en_US><![CDATA[<a href="#" target="_blank">Tor Blocker</a>]]></en_US>
17
+ </extlink>
18
+ </Hqpeak_TorBlocker>
19
+ </modules>
20
+ <global>
21
+ <helpers>
22
+ <hqpeaktorblocker>
23
+ <class>Hqpeak_TorBlocker_Helper</class>
24
+ </hqpeaktorblocker>
25
+ </helpers>
26
+ <blocks>
27
+ <hqpeaktorblocker>
28
+ <class>Hqpeak_TorBlocker_Block</class>
29
+ </hqpeaktorblocker>
30
+ </blocks>
31
+ <models>
32
+ <hqpeaktorblocker>
33
+ <class>Hqpeak_TorBlocker_Model</class>
34
+ <resourceModel>hqpeaktorblocker_resource</resourceModel>
35
+ </hqpeaktorblocker>
36
+ <hqpeaktorblocker_resource>
37
+ <class>Hqpeak_TorBlocker_Model_Resource</class>
38
+ <entities>
39
+ <ipaddress>
40
+ <table>torblocker_ip</table>
41
+ </ipaddress>
42
+ </entities>
43
+ </hqpeaktorblocker_resource>
44
+ </models>
45
+ <resources>
46
+ <hqpeaktorblocker_setup>
47
+ <setup>
48
+ <module>Hqpeak_TorBlocker</module>
49
+ <class>Hqpeak_TorBlocker_Model_Resource_Setup</class>
50
+ </setup>
51
+ </hqpeaktorblocker_setup>
52
+ </resources>
53
+ <events>
54
+ <controller_front_init_before>
55
+ <observers>
56
+ <hqpeaktorblocker_observer>
57
+ <type>singleton</type>
58
+ <class>hqpeaktorblocker/observer</class>
59
+ <method>serviceCheck</method>
60
+ </hqpeaktorblocker_observer>
61
+ </observers>
62
+ </controller_front_init_before>
63
+ <controller_front_init_routers>
64
+ <observers>
65
+ <hqpeaktorblocker_observer>
66
+ <type>singleton</type>
67
+ <class>hqpeaktorblocker/observer</class>
68
+ <method>ipCheckVisit</method>
69
+ </hqpeaktorblocker_observer>
70
+ </observers>
71
+ </controller_front_init_routers>
72
+ <controller_action_predispatch>
73
+ <observers>
74
+ <hqpeaktorblocker_observer>
75
+ <type>singleton</type>
76
+ <class>hqpeaktorblocker/observer</class>
77
+ <method>ipCheckUrl</method>
78
+ </hqpeaktorblocker_observer>
79
+ </observers>
80
+ </controller_action_predispatch>
81
+ <controller_action_predispatch_customer_account_create>
82
+ <observers>
83
+ <hqpeaktorblocker_observer>
84
+ <type>singleton</type>
85
+ <class>hqpeaktorblocker/observer</class>
86
+ <method>checkRegister</method>
87
+ </hqpeaktorblocker_observer>
88
+ </observers>
89
+ </controller_action_predispatch_customer_account_create>
90
+ </events>
91
+ </global>
92
+ <default>
93
+ <settings>
94
+ <main>
95
+ <url>http://hqpeak.com/torexitlist/free/?format=json</url>
96
+ <timeupdate>18000</timeupdate>
97
+ <errortemplate>
98
+ <![CDATA[<!DOCTYPE html>
99
+ <html>
100
+ <head>
101
+ <title>Access Denied</title>
102
+ </head>
103
+ <body>
104
+ <h2>Tor user caught! Sorry, you stop here</h2>
105
+ </body>
106
+ </html>]]>
107
+ </errortemplate>
108
+ <check>0</check>
109
+ <version>http://hqpeak.com/torexitlist/free/?format=json</version>
110
+ </main>
111
+ </settings>
112
+ </default>
113
+ <frontend>
114
+ <events>
115
+ <controller_action_predispatch>
116
+ <observers>
117
+ <hqpeaktorblocker_observer>
118
+ <type>singleton</type>
119
+ <class>hqpeaktorblocker/observer</class>
120
+ <method>ipCheckFront</method>
121
+ </hqpeaktorblocker_observer>
122
+ </observers>
123
+ </controller_action_predispatch>
124
+ </events>
125
+ </frontend>
126
+ <admin>
127
+ <routers>
128
+ <adminhtml>
129
+ <args>
130
+ <modules>
131
+ <hqpeaktorblocker before="Mage_Adminhtml">Hqpeak_TorBlocker_Adminhtml</hqpeaktorblocker>
132
+ </modules>
133
+ </args>
134
+ </adminhtml>
135
+ </routers>
136
+ </admin>
137
+ <adminhtml>
138
+ <events>
139
+ <controller_action_predispatch>
140
+ <observers>
141
+ <hqpeaktorblocker_observer>
142
+ <type>singleton</type>
143
+ <class>hqpeaktorblocker/observer</class>
144
+ <method>ipCheckAdmin</method>
145
+ </hqpeaktorblocker_observer>
146
+ </observers>
147
+ </controller_action_predispatch>
148
+ <admin_system_config_changed_section_settings>
149
+ <observers>
150
+ <hqpeaktorblocker_observer>
151
+ <type>singleton</type>
152
+ <class>hqpeaktorblocker/observer</class>
153
+ <method>saveConfig</method>
154
+ </hqpeaktorblocker_observer>
155
+ </observers>
156
+ </admin_system_config_changed_section_settings>
157
+ </events>
158
+ <acl>
159
+ <resources>
160
+ <admin>
161
+ <children>
162
+ <system>
163
+ <children>
164
+ <config>
165
+ <children>
166
+ <settings translate="title" module="hqpeaktorblocker">
167
+ <title>Tor Blocker Settings</title>
168
+ <sort_order>100</sort_order>
169
+ </settings>
170
+ </children>
171
+ </config>
172
+ </children>
173
+ </system>
174
+ </children>
175
+ </admin>
176
+ </resources>
177
+ </acl>
178
+ </adminhtml>
179
+ </config>
app/code/community/Hqpeak/TorBlocker/etc/system.xml ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <tabs>
4
+ <torblocker translate="label" module="hqpeaktorblocker">
5
+ <label>HQPeak Extensions</label>
6
+ <sort_order>1300</sort_order>
7
+ <show_in_default>1</show_in_default>
8
+ <show_in_web>1</show_in_web>
9
+ <show_in_store>1</show_in_store>
10
+ </torblocker>
11
+ </tabs>
12
+ <sections>
13
+ <settings translate="label" module="hqpeaktorblocker">
14
+ <label>Tor Blocker</label>
15
+ <tab>torblocker</tab>
16
+ <frontend_type>text</frontend_type>
17
+ <sort_order>10</sort_order>
18
+ <show_in_default>1</show_in_default>
19
+ <show_in_web>1</show_in_web>
20
+ <show_in_store>1</show_in_store>
21
+ <groups>
22
+ <info translate="label">
23
+ <label>Tor Blocker - Information</label>
24
+ <sort_order>1</sort_order>
25
+ <show_in_default>1</show_in_default>
26
+ <show_in_website>1</show_in_website>
27
+ <show_in_store>1</show_in_store>
28
+ <fields>
29
+ <information>
30
+ <frontend_model>hqpeaktorblocker/adminhtml_information</frontend_model>
31
+ <sort_order>10</sort_order>
32
+ <show_in_default>1</show_in_default>
33
+ <show_in_website>1</show_in_website>
34
+ <show_ins_store>1</show_ins_store>
35
+ </information>
36
+ </fields>
37
+ </info>
38
+ <main translate="label">
39
+ <label>Tor Blocker - General Configuration</label>
40
+ <sort_order>2</sort_order>
41
+ <show_in_default>1</show_in_default>
42
+ <show_in_website>1</show_in_website>
43
+ <show_in_store>1</show_in_store>
44
+ <fields>
45
+ <url translate="label comment">
46
+ <label>Default Tor Blocker List</label>
47
+ <comment><![CDATA[Default is free version of the tor exit list service. <a href="http://hqpeak.com/torexitlist/" target="_blank">Learn more</a> or get <a href="http://hqpeak.com/torexitlist/account/" target="_blank">premium service</a> access]]></comment>
48
+ <frontend_type>text</frontend_type>
49
+ <validate>validate-url</validate>
50
+ <backend_model>hqpeaktorblocker/validation_urlValidate</backend_model>
51
+ <sort_order>10</sort_order>
52
+ <show_in_default>1</show_in_default>
53
+ <show_in_website>1</show_in_website>
54
+ <show_in_store>1</show_in_store>
55
+ </url>
56
+ <timeupdate translate="label comment">
57
+ <label>Time interval to update the table with Tor users</label>
58
+ <comment>Enter the time in seconds [Recommended: default version - one hour or more (in seconds); paid version - five minutes or more (in seconds)]</comment>
59
+ <frontend_type>text</frontend_type>
60
+ <validate>validate-number</validate>
61
+ <backend_model>hqpeaktorblocker/validation_timeValidate</backend_model>
62
+ <sort_order>20</sort_order>
63
+ <show_in_default>1</show_in_default>
64
+ <show_in_website>1</show_in_website>
65
+ <show_in_store>1</show_in_store>
66
+ </timeupdate>
67
+ <errortemplate translate="label comment">
68
+ <label>Template for the error page shown to Tor users</label>
69
+ <comment>Define the template by using appropriate HTML syntax and structure</comment>
70
+ <frontend_type>textarea</frontend_type>
71
+ <sort_order>30</sort_order>
72
+ <show_in_default>1</show_in_default>
73
+ <show_in_website>1</show_in_website>
74
+ <show_in_store>1</show_in_store>
75
+ </errortemplate>
76
+ <time>
77
+ <frontend_type>hidden</frontend_type>
78
+ <sort_order>40</sort_order>
79
+ <show_in_default>0</show_in_default>
80
+ <show_in_website>0</show_in_website>
81
+ <show_in_store>0</show_in_store>
82
+ </time>
83
+ <check>
84
+ <frontend_type>hidden</frontend_type>
85
+ <sort_order>50</sort_order>
86
+ <show_in_default>0</show_in_default>
87
+ <show_in_website>0</show_in_website>
88
+ <show_in_store>0</show_in_store>
89
+ </check>
90
+ <version>
91
+ <frontend_type>hidden</frontend_type>
92
+ <sort_order>60</sort_order>
93
+ <show_in_default>0</show_in_default>
94
+ <show_in_website>0</show_in_website>
95
+ <show_in_store>0</show_in_store>
96
+ </version>
97
+ </fields>
98
+ </main>
99
+ <configuration>
100
+ <label>Tor Blocker - Access Configuration</label>
101
+ <sort_order>3</sort_order>
102
+ <show_in_default>1</show_in_default>
103
+ <show_in_website>1</show_in_website>
104
+ <show_in_store>1</show_in_store>
105
+ <fields>
106
+ <options translate="label">
107
+ <label>Requests to allow to the Tor users</label>
108
+ <frontend_model>hqpeaktorblocker/adminhtml_system_config_checkbox</frontend_model>
109
+ <source_model>hqpeaktorblocker/source</source_model>
110
+ <sort_order>10</sort_order>
111
+ <show_in_default>1</show_in_default>
112
+ <show_in_website>1</show_in_website>
113
+ <show_in_store>1</show_in_store>
114
+ </options>
115
+ <request translate="label comment">
116
+ <label>Requests to deny to the Tor users</label>
117
+ <comment>Here go all the parameters that are not allowed in the URL, such as POST or GET requests, Modules, Controllers or Actions (enter them one by one, separated by comma)</comment>
118
+ <frontend_type>textarea</frontend_type>
119
+ <sort_order>20</sort_order>
120
+ <show_in_default>1</show_in_default>
121
+ <show_in_website>1</show_in_website>
122
+ <show_in_store>1</show_in_store>
123
+ </request>
124
+ </fields>
125
+ </configuration>
126
+ </groups>
127
+ </settings>
128
+ </sections>
129
+ </config>
app/code/community/Hqpeak/TorBlocker/sql/hqpeaktorblocker_setup/install-1.0.0.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $installer = $this;
3
+ $installer->startSetup();
4
+ $installer->run("DROP TABLE IF EXISTS {$this->getTable('hqpeaktorblocker/ipaddress')}");
5
+ $table = $installer->getConnection()
6
+ ->newTable($installer->getTable('hqpeaktorblocker/ipaddress'))
7
+ ->addColumn('ip', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
8
+ 'unsigned' => false,
9
+ 'nullable' => false,
10
+ 'primary' => true,
11
+ 'identity' => false,
12
+ ), 'IP Address');
13
+ $installer->getConnection()->createTable($table);
14
+ $installer->endSetup();
15
+ ?>
app/design/adminhtml/default/default/template/hqpeaktorblocker/system/config/checkbox.phtml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <input type="hidden" name="<?php echo $this->getNamePrefix() ?>" value="" /><!-- this is send if nothing is checked -->
2
+ <ul class="checkboxes">
3
+ <?php foreach ($this->getValues() as $name => $label): ?>
4
+ <li>
5
+ <input type="checkbox" value="<?php echo $name?>" name="<?php echo $this->getNamePrefix() ?>[]" id="<?php echo $this->getHtmlId() . '_' . $name ?>" <?php echo (Mage::getStoreConfig('settings/configuration/check') ? ($this->getIsChecked($name) ? 'checked="checked"' : '') : ($name == 'visit' ? 'checked="checked"' : '')) ?>/>
6
+ <label for="<?php echo $this->getHtmlId() . '_' . $name ?>">
7
+ <?php echo $label ?>
8
+ </label>
9
+ </li>
10
+ <?php endforeach; ?>
11
+ </ul>
app/etc/modules/Hqpeak_TorBlocker.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Hqpeak_TorBlocker>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </Hqpeak_TorBlocker>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Hqpeak_TorBlocker</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/gpl-license">GPL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Most of the time Tor exit nodes are used to enumerate vulnerabilities of our online product, to perform attack or to be used as a spam source. This extension allow us to limit the actions that could be performed by the users that are coming from a Tor exit nodes using http://hqpeak.com/torexitlist/ free service. Could be upgraded to premium or could be set up any url to service that will give you response in the described json format. Premium list is updated on every 5 minutes, while free version list is updated on 5 h.</summary>
10
+ <description>Most of the time Tor exit nodes are used to enumerate vulnerabilities of our online product, to perform attack or to be used as a spam source. This extension allow us to limit the actions that could be performed by the users that are coming from a Tor exit nodes using http://hqpeak.com/torexitlist/ free service. Could be upgraded to premium or could be set up any url to service that will give you response in the described json format. Premium list is updated on every 5 minutes, while free version list is updated on 5 h.&#xD;
11
+ &#xD;
12
+ With this extension you can apply following constraints to the Tor visitors:&#xD;
13
+ &#xD;
14
+ Visits (Tor users can read only public content on the site)&#xD;
15
+ Front-end (Tor users can visit the client side of the site)&#xD;
16
+ Administration (Tor users can access the administration panel)&#xD;
17
+ Registration (Tor users can register for the site)&#xD;
18
+ Request (Tor users can send POST requests)&#xD;
19
+ &#xD;
20
+ Or to ban any action by its URL e.g. not allow accessing resources that POST or GET requests, module names, controller names or action names.</description>
21
+ <notes>This is a stable v1.0.0 Tor Blocker extension which is developed and tested under Magento v1.8.1.0</notes>
22
+ <authors><author><name>HQPeak</name><user>hqpeak</user><email>contact@hqpeak.com</email></author></authors>
23
+ <date>2014-06-16</date>
24
+ <time>12:15:29</time>
25
+ <contents><target name="magecommunity"><dir name="Hqpeak"><dir name="TorBlocker"><dir name="Block"><dir name="Adminhtml"><file name="Information.php" hash="db02c1820a47500aff71e29c3976d36f"/><dir name="System"><dir name="Config"><file name="Checkbox.php" hash="7daa5d861bc37569616fdf9885e4ede1"/></dir></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="9b46cc53bb3d4dba58d7b8fed79afd9f"/></dir><dir name="Model"><file name="Ipaddress.php" hash="ae5ad2d0d7624e5d12914141b09a61ed"/><file name="Observer.php" hash="15459a9faf8e0ca99ae897a1651d3689"/><dir name="Resource"><dir name="Ipaddress"><file name="Collection.php" hash="d85fe68d2401e0c02ef83d44c16f7ee7"/></dir><file name="Ipaddress.php" hash="fb8c3c07d8415f63ffb0439d57049904"/><file name="Setup.php" hash="e70d1d82caa5b155950a2da8165ae197"/></dir><file name="Source.php" hash="aa2cdca6f16463bef3c8facae86cca76"/><dir name="Validation"><file name="TimeValidate.php" hash="eb01dd905f466deb7f16f06b9a04961b"/><file name="UrlValidate.php" hash="f0073c9d308b9b5daf8ec78ffbc10192"/></dir></dir><dir name="controllers"><dir name="Adminhtml"><file name="TorblockerController.php" hash="7b1c389a8f592f334825d589a7f5b409"/></dir></dir><dir name="data"><dir name="hqpeaktorblocker_setup"><file name="data-install-1.0.0.php" hash="a4e7eac5c4aebd12bec1c0507c4fa132"/></dir></dir><dir name="etc"><file name="config.xml" hash="49ffffd2b40585242a8a68a4d9d53806"/><file name="system.xml" hash="4c8c041dc3703316647d12320cfac26f"/></dir><dir name="sql"><dir name="hqpeaktorblocker_setup"><file name="install-1.0.0.php" hash="51f30592f85cbf446d0cda6a6c93bf65"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="hqpeaktorblocker"><dir name="system"><dir name="config"><file name="checkbox.phtml" hash="f5970f58f8ce8fef05775eea60f9da04"/></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Hqpeak_TorBlocker.xml" hash="a87809b3d10c4a60052939e6a1cf06db"/></dir></target></contents>
26
+ <compatible/>
27
+ <dependencies><required><php><min>5.3.10</min><max>5.5.12</max></php></required></dependencies>
28
+ </package>