-
Notifications
You must be signed in to change notification settings - Fork 0
/
iprestrictions.php
142 lines (129 loc) · 4.93 KB
/
iprestrictions.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
<?php
require_once 'iprestrictions.civix.php';
use CRM_Iprestrictions_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function iprestrictions_civicrm_config(&$config) {
_iprestrictions_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function iprestrictions_civicrm_install() {
CRM_Core_DAO::executeQuery("CREATE TABLE `civicrm_ip_tracker` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`entity_id` int(11) DEFAULT NULL,
`entity_name` varchar(255) DEFAULT NULL,
`ip_address` varchar(255) DEFAULT NULL,
`counter` int(11) DEFAULT NULL,
`last_submitted` timestamp NULL DEFAULT NULL,
`modified_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
_iprestrictions_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function iprestrictions_civicrm_enable() {
_iprestrictions_civix_civicrm_enable();
}
function iprestrictions_civicrm_cron($jobManager) {
$interval = Civi::settings()->get('block_interval');
if ($interval) {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_ip_tracker
WHERE modified_date < DATE_SUB(NOW(), INTERVAL {$interval} MINUTE)");
}
}
function iprestrictions_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
$blockInterval = Civi::settings()->get('block_interval');
$maxTrials = Civi::settings()->get('no_of_trials');
$maxTrialsInterval = Civi::settings()->get('time_for_trials');
if (empty($blockInterval) || empty($maxTrials) || empty($maxTrialsInterval)) {
return;
}
if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_ip_tracker
WHERE modified_date < DATE_SUB(NOW(), INTERVAL {$blockInterval} MINUTE)");
$currentTime = date('YmdHis');
$pageId = $form->getVar( '_id' );
$ipAddress = CRM_Utils_System::ipAddress();
$query = "SELECT counter, last_submitted
FROM civicrm_ip_tracker
WHERE ip_address = '{$ipAddress}'
AND entity_name = 'civicrm_contribution_page'
AND entity_id = {$pageId}
AND last_submitted > DATE_SUB(NOW(), INTERVAL {$maxTrialsInterval} MINUTE)";
$dao = CRM_Core_DAO::executeQuery($query);
if ($dao->fetch()) {
if ($dao->counter >= $maxTrials) {
$errors[] = $errorMsg = ts('You have exceeded the maximum number of payment attempts. Please wait a minute and try again.');
CRM_Core_Session::setStatus($errorMsg, '', 'error');
return;
}
$dao->counter += 1;
CRM_Core_DAO::executeQuery("UPDATE civicrm_ip_tracker
SET counter = {$dao->counter}, modified_date = {$currentTime}
WHERE ip_address = '{$ipAddress}'");
}
elseif ($dao->N == 0) {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_ip_tracker(entity_id, entity_name, ip_address, counter, last_submitted, modified_date)
VALUES ({$pageId}, 'civicrm_contribution_page', '{$ipAddress}', 1, '{$currentTime}', '{$currentTime}');
");
}
}
elseif ($formName == 'CRM_Contribute_Form_Contribution_Confirm') {
$pageId = $form->getVar( '_id' );
$currentTime = date('YmdHis');
$ipAddress = CRM_Utils_System::ipAddress();
$query = "SELECT counter
FROM civicrm_ip_tracker
WHERE ip_address = '{$ipAddress}'
AND entity_name = 'civicrm_contribution_page'
AND entity_id = {$pageId}
AND last_submitted > DATE_SUB(NOW(), INTERVAL {$maxTrialsInterval} MINUTE)";
$dao = CRM_Core_DAO::executeQuery($query);
if ($dao->fetch()) {
if ($dao->counter >= $maxTrials) {
$errors[] = $errorMsg = ts('You have exceeded the maximum number of payment attempts. Please wait a minute and try again.');
CRM_Core_Session::setStatus($errorMsg, '', 'error');
return;
}
$dao->counter += 1;
CRM_Core_DAO::executeQuery("UPDATE civicrm_ip_tracker
SET counter = {$dao->counter}, modified_date = {$currentTime}
WHERE ip_address = '{$ipAddress}'");
}
}
return;
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
// */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function iprestrictions_civicrm_navigationMenu(&$menu) {
_iprestrictions_civix_insert_navigation_menu($menu, 'Mailings', array(
'label' => E::ts('New subliminal message'),
'name' => 'mailing_subliminal_message',
'url' => 'civicrm/mailing/subliminal',
'permission' => 'access CiviMail',
'operator' => 'OR',
'separator' => 0,
));
_iprestrictions_civix_navigationMenu($menu);
} // */