Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implmentation of hook_civicrm_postProcess
- * Inserting form submission time into the cache table for flood control checking
- *
- */
- function yourmodule_civicrm_postProcess($formName, &$form) {
- if($formName == 'CRM_Contribute_Form_Contribution_Main') {
- $id = $form->getVar('_id');
- // note the ip could be a proxy server ip
- $ip = ip_address();
- $data = array(
- 'id' => $id,
- 'ip' => $ip,
- 'timestamp' => time(),
- );
- $query = "INSERT INTO civicrm_cache
- (`group_name`, `path`, `data`, `created_date`)
- VALUES (%1, %2, %3, %4)";
- $params = array(
- 1 => array('contribution flood control', 'String'),
- 2 => array('CRM_Contribute_Form_Contribution_Main_' . session_id(), 'String'),
- 3 => array(serialize($data), 'String'),
- 4 => array(date('Y-m-d H:i:s'), 'String'),
- );
- CRM_Core_DAO::executeQuery($query, $params);
- }
- }
- /**
- * Implmentation of hook_civicrm_validate
- * Checking timestamp of previous submission time given the ip address
- *
- */
- function yourmodule_civicrm_validate($formName, &$fields, &$files, &$form) {
- $errors = array();
- // verify that the last submission recorded is the same contribution page id
- // we are only going to check the date range within the same day?
- if($formName == 'CRM_Contribute_Form_Contribution_Main') {
- $id = $form->getVar('_id');
- $ip = ip_address();
- // get the last time a user has submitted the form
- $query = sprintf("SELECT data FROM civicrm_cache WHERE `data` LIKE '%%%s%%' AND group_name = 'contribution flood control' ORDER BY created_date DESC LIMIT 0, 1", $ip);
- $data = CRM_Core_DAO::singleValueQuery($query);
- $data = unserialize($data);
- if($data && !empty($data)) {
- if($id == $data['id']) {
- // check flooding interval
- $interval = time() - $data['timestamp'];
- if($interval < 60) {
- $errors['qfKey'] = 'Maximum attempt reached, please try again later';
- }
- }
- }
- }
- return (empty($errors)) ? TRUE : $errors;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement