0; } } return false; } /** * Check if IP falls between two others. * @param $ip * @param $startip * @param $endip * @return bool */ function qa_ip_between($ip, $startip, $endip) { $uip = unpack('C*', @inet_pton($ip)); $ustartip = unpack('C*', @inet_pton($startip)); $uendip = unpack('C*', @inet_pton($endip)); if (count($uip) != count($ustartip) || count($uip) != count($uendip)) return false; foreach ($uip as $i => $byte) { if ($byte < $ustartip[$i] || $byte > $uendip[$i]) { return false; } } return true; } /** * Expands an IPv6 address (possibly containing wildcards), e.g. ::ffff:1 to 0000:0000:0000:0000:0000:0000:ffff:0001. * Based on http://stackoverflow.com/a/12095836/753676 * @param string $ip The IP address to expand. * @return string */ function qa_ipv6_expand($ip) { $ipv6_wildcard = false; $wildcards = ''; $wildcards_matched = array(); if (strpos($ip, "*") !== false) { $ipv6_wildcard = true; } if ($ipv6_wildcard) { $wildcards = explode(":", $ip); foreach ($wildcards as $index => $value) { if ($value == "*") { $wildcards_matched[] = count($wildcards) - 1 - $index; $wildcards[$index] = "0"; } } $ip = implode($wildcards, ":"); } $hex = unpack("H*hex", @inet_pton($ip)); $ip = substr(preg_replace("/([0-9A-Fa-f]{4})/", "$1:", $hex['hex']), 0, -1); if ($ipv6_wildcard) { $wildcards = explode(":", $ip); foreach ($wildcards_matched as $value) { $i = count($wildcards) - 1 - $value; $wildcards[$i] = "*"; } $ip = implode($wildcards, ":"); } return $ip; } /** * Called after a database write $action performed by a user identified by $userid and/or $cookieid. * @param int $userid * @param string $cookieid * @param string $action * @param int $questionid * @param int $answerid * @param int $commentid */ function qa_report_write_action($userid, $cookieid, $action, $questionid, $answerid, $commentid) { } /** * Take note for rate limits that a user and/or the requesting IP just performed an action. * @param int $userid User performing the action. * @param string $action One of the QA_LIMIT_* constants defined above. * @return mixed */ function qa_limits_increment($userid, $action) { if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } require_once QA_INCLUDE_DIR . 'db/limits.php'; $period = (int)(qa_opt('db_time') / 3600); if (isset($userid)) qa_db_limits_user_add($userid, $action, $period, 1); qa_db_limits_ip_add(qa_remote_ip_address(), $action, $period, 1); }