function system_ban_ip_settings_form_submit

7.x system.module system_ban_ip_settings_form_submit($form, &$form_state)

File

modules/system/system.module, line 758

Code

function system_ban_ip_settings_form_submit($form, &$form_state) {
  $manual_ip = trim($form_state ['values']['system_manually_add_ip'] '');
  $ban_file = $form_state ['values']['ban_ip_file_path'];

  if ($manual_ip != '') {

    // Erase it from the ban_file if its already there    
    $c = file_get_contents($ban_file);
    $c = str_replace("$manual_ip", '', $c);
    //$c = str_replace("\r\n", "\n", $c);
    $c = str_replace("  ", '', $c);
    $c = str_replace(" \n", "", $c);
    $c = str_replace("\n\n", "", $c);
    $c = trim($c);


    // Put into our file
    $res = file_put_contents($ban_file, "$c\n$manual_ip");

    if ($res === FALSE) {
      fp_add_message("Unable to write to the ban ip file. File permission issue?", 'error');
    }

    // Reset our apcu cache, if installed.
    if (function_exists('apcu_store')) {
      apcu_delete('banned_ips'); // force reload of banned ips next time a page is loaded.
    }


    // Also put in our table with current timestamp, so cron can expire it later.
    db_query("DELETE FROM banned_ips WHERE ip_address = ?", array($manual_ip));
    db_query("INSERT INTO banned_ips (ip_address, posted_ts) VALUES (?, ?)", array($manual_ip, time()));

  }

}