4

I want to know is there any function in DRUPAL 7 that I can send it an IP address as a parameter and it adds this ip to blacked_ips table ??

Indeed I have a rule that contains a Execution PHP action . In my action I want to add an IP to black list.

5

There isn't in core; system_block_ip_action() is about as close it you'll get, but that blocks the current IP.

Such a function would be trivial to implement though:

function block_ip($ip) {
  if (!(bool)db_query("SELECT 1 FROM {blocked_ips} WHERE ip = :ip", array(':ip' => $ip))->fetchField()) {
    db_insert('blocked_ips')->fields(array('ip' => $ip))->execute();    
  }
}
  • Thanks a lot for your reply. Now my another question is if I want to use your mentioned function (block_ip) in my rules Execution php action, I need to define it at first. Please tell me where can I define this function ? in a sepecific PHP file or in execution php action ? – Mehrdad201 Nov 3 '14 at 5:33
  • Custom code goes in a custom module @Mehrdad201, is that what you meant? – Clive Nov 3 '14 at 13:49
  • I mean I want to place this function in a php file and then call it from rules PHP execution action. If I write it in a .module file, I must enable this module. right? – Mehrdad201 Nov 4 '14 at 7:37
4

You can use drupal_form_submit() to block the IP.

Case 1:

    function block_ip($ip) {
        $form_state['values'] = array();
        drupal_form_submit('system_ip_blocking_form', $form_state, $ip);
    }

Case 2:

    function block_ip($ip) {
        $form_state = array();
        $form_state['values']['ip'] = $ip;
        drupal_form_submit('system_ip_blocking_form', $form_state, $ip);
    }

Line $form_state['values']['ip'] = $ip; is optional as system_ip_blocking_form required IP as 3rd param. You can see function declaration at line 1392 in system.admin.inc

The advantage of using drupal_form_submit() is, it will handle all validation and set watchdog messages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.