You are here

function ip_login_check in IP Login 7.2

Same name and namespace in other branches
  1. 6.2 ip_login.module \ip_login_check()
  2. 7.3 ip_login.module \ip_login_check()

Compares the current request's IP address to the ip_login_user table and then does a proper match for each match on exact, ranges and wildcards

Parameters

$ip: An ip address string, usually from the current user's request

Return value

$uid_matched The uid of the matching user account

4 calls to ip_login_check()
ip_login_attempt_login in ./ip_login.module
Checks the request IP and logs user in there's a match by calling ip_login_check then ip_login_attempt_login
ip_login_boot in ./ip_login.module
Implementation of hook_boot().
ip_login_form_alter in ./ip_login.module
Implementation of hook_form_alter().
_ip_login_can_login_as_another_user in ./ip_login.module

File

./ip_login.module, line 353
Allow user login by IP addresses, ranges or wildcards.

Code

function ip_login_check($ip, $diagnostics = FALSE) {

  // have we checked user IP already this session?
  if (!empty($_SESSION[IP_CHECKED])) {
    return $_SESSION[IP_UID_MATCH];
  }

  // break up IP for ip address
  $addr = explode(".", check_plain($ip));
  $matches = FALSE;
  $uid_matched = 0;

  // Find user ip matches on the first part of the user's IP ANYWHERE except the end.
  // Not desperately efficient but works consistently with comma separated IP ranges and spaces,
  // and these checks are only done once per session anyway.
  $partial_matches = db_query("SELECT uid, ip_match\n       FROM {ip_login_user}\n       WHERE ip_match LIKE (:addr)\n       ORDER by LENGTH(ip_match) ASC", array(
    ':addr' => '%' . $addr[0] . '.%',
  ));
  foreach ($partial_matches as $row) {

    // multiple values are separated with commas so try each in turn
    $user_ip_ranges = explode(",", $row->ip_match);
    foreach ($user_ip_ranges as $ip_range) {

      // clear any whitespace, break into quads, then compare
      $ip_range = explode('.', trim($ip_range));
      foreach ($ip_range as $index => $quad) {
        $matches = ip_login_match_quad($addr[$index], $quad);
        if (!$matches) {
          break;
        }

        // no match, escape this foreach and move on to next IP range
      }

      // if it matches, stop here and do login
      if ($matches) {
        $uid_matched = $row->uid;
        break 2;

        // escape the foreach (ranges) and while (db_result)
      }
    }
  }

  // if not diagnostic test, set processed session flag, store matching user (if there is one)
  if (!$diagnostics) {
    $_SESSION[IP_CHECKED] = TRUE;
    $_SESSION[IP_UID_MATCH] = $uid_matched;
  }
  return $uid_matched;
}