/** * @param Database $db * @param string $ip * @param string $xfor * @return array conditions */ function getIpConds($db, $ip, $xfor = false) { $type = $xfor ? 'xff' : 'ip'; // IPv4 CIDR, 16-32 bits if (preg_match('#^(\\d+\\.\\d+\\.\\d+\\.\\d+)/(\\d+)$#', $ip, $matches)) { if ($matches[2] < 16 || $matches[2] > 32) { return array('cuc_' . $type . '_hex' => -1); } list($start, $end) = IP::parseRange($ip); return array('cuc_' . $type . '_hex BETWEEN ' . $db->addQuotes($start) . ' AND ' . $db->addQuotes($end)); } else { if (preg_match('#^\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}/(\\d+)$#', $ip, $matches)) { // IPv6 CIDR, 64-128 bits if ($matches[1] < 64 || $matches[1] > 128) { return array('cuc_' . $type . '_hex' => -1); } list($start, $end) = IP::parseRange6($ip); return array('cuc_' . $type . '_hex BETWEEN ' . $db->addQuotes($start) . ' AND ' . $db->addQuotes($end)); } else { if (preg_match('#^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$#', $ip)) { // 32 bit IPv4 $ip_hex = IP::toHex($ip); return array('cuc_' . $type . '_hex' => $ip_hex); } else { if (preg_match('#^\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}:\\w{1,4}$#', $ip)) { // 128 bit IPv6 $ip_hex = IP::toHex($ip); return array('cuc_' . $type . '_hex' => $ip_hex); } else { // throw away this query, incomplete IP, these don't get through the entry point anyway return array('cuc_' . $type . '_hex' => -1); } } } } }