public static function fromHexToIp($ip) { $hex = pack('H*', $ip); if (str_repeat('0', 24) === substr($hex, 0, 24)) { $hex = substr($hex, 24); } return inet_ntop($hex); }
/** * Returns the IP of the current client. * The IP address is validated against the security validator. In case of an invalid IP, IP_INVALID is returned. * @param bool Get the shorthand notation of an address. * @return string The ip address of the client. */ public static final function getClientIP($shortHand = true) { //validate the handle to the server variable $handle = self::getServerHandle(); $ip = self::IP_INVALID; switch (true) { case isset($handle['HTTP_CLIENT_IP']): //if there is a HTTP_CLIENT_IP variable, we parse this and use the first value in it. //this allows us to properly handle loadbalancers and cachers. $ar = explode(',', $handle['HTTP_CLIENT_IP']); $ip = trim(array_shift($ar)); break; case isset($handle['HTTP_X_FORWARDED_FOR']): //if there is a HTTP_X_FORWARDED_FOR variable, we parse this and use the first value in it. //this allows us to properly handle loadbalancers and cachers. $ar = explode(',', $handle['HTTP_X_FORWARDED_FOR']); $ip = trim(array_shift($ar)); break; case isset($handle['REMOTE_ADDR']): //if there is a REMOTE_ADDR variable, we parse this and use the first value in it. $ar = explode(',', $handle['REMOTE_ADDR']); $ip = trim(array_shift($ar)); break; } $val = new \System\Security\Validate(); if ($val->isIPAddress($ip, 'ip', true, true, false, false, true) == \System\Security\ValidateResult::VALIDATE_OK) { if ($shortHand) { return inet_ntop(inet_pton($ip)); } return $ip; } return self::IP_INVALID; }
/** * Number to Protocol * * Converts an unpacked binary string into a printable IP address. * * @static * @access public * @param binary(string) $ip * @param boolean $shrink * @return string|false */ public static function ntop($ip, $shrink = false) { // Dealing with IP protocol addresses is really tricky. Convert it to a binary sequence and it's really // easy! Run it through the pton() method and if it returns as a string it means the IP is valid and can be // converted to a protocol address using the built in PHP functionality. Otherwise return false. return is_string($ip = self::pton($ip, $shrink)) ? inet_ntop(pack('A' . strlen($ip), $ip)) : false; }
function get_cache($host, $value) { global $dev_cache; if (!isset($dev_cache[$host][$value])) { switch ($value) { case 'device_id': // Try by hostname $ip = inet_pton($host); if (inet_ntop($ip) === false) { $dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ?', array($host, $host)); } else { $dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ? OR `ip` = ?', array($host, $host, $ip)); } // If failed, try by IP if (!is_numeric($dev_cache[$host]['device_id'])) { $dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM `ipv4_addresses` AS A, `ports` AS I WHERE A.ipv4_address = ? AND I.port_id = A.port_id', array($host)); } break; case 'os': $dev_cache[$host]['os'] = dbFetchCell('SELECT `os` FROM devices WHERE `device_id` = ?', array(get_cache($host, 'device_id'))); break; case 'version': $dev_cache[$host]['version'] = dbFetchCell('SELECT `version` FROM devices WHERE `device_id`= ?', array(get_cache($host, 'device_id'))); break; default: return null; } //end switch } //end if return $dev_cache[$host][$value]; }
function nel_render_ban_panel_list($dataforce, $dbh) { $render = new nel_render(); nel_render_header($dataforce, $render, array()); nel_render_ban_panel_top($dataforce, $render); $result = $dbh->query('SELECT * FROM ' . BANTABLE . ' ORDER BY id DESC'); $bgclass = 'row1'; while ($baninfo = $result->fetch(PDO::FETCH_ASSOC)) { $render->add_data('ban_panel_loop', TRUE); $render->add_data('host', @inet_ntop($render->retrieve_data('host')) ? inet_ntop($render->retrieve_data('host')) : 'Unknown'); $render->add_data('ban_appeal_response', $baninfo['appeal_response']); $render->add_data('ban_expire', date("D F jS Y H:i:s", $render->retrieve_data('length') + $render->retrieve_data('ban_time'))); if ($bgclass === 'row1') { $render->add_data('bg_class', 'row2'); $bgclass = 'row2'; } else { $render->add_data('bg_class', 'row1'); $bgclass = 'row1'; } $render->parse('bans_panel_list_bans.tpl', 'management'); } unset($result); nel_render_ban_panel_bottom($dataforce, $render); nel_render_basic_footer($render); $render->output(TRUE); }
public function getValue() { if ($this->_value === null) { $this->_value = inet_ntop($this->_binary); } return $this->_value; }
/** * Converts gRPC node object to NetAssist node * @param \ng_rpc\Node $rpc_node gRPC Node object * @return \NetAssist\Graph\Node Node */ public function getNode($rpc_node) { $node = new \NetAssist\Graph\Node($rpc_node->getId($rpc_node->GetId())); $node->db_id = $rpc_node->getDbId(); $node->name = $rpc_node->getName(); $node->model = $rpc_node->getModel(); $node->serial = $rpc_node->getSerial(); $node->icinga_name = $rpc_node->getIcingaName(); $node->ip = long2ip($rpc_node->getIp4()); if ($rpc_node->hasIp6()) { $node->ip6 = inet_ntop($rpc_node->getIp6()); } $node->mac_address = $rpc_node->getMacAddress(); $node->comment = $rpc_node->getComment(); $node->ports_number = $rpc_node->getNumPorts(); $node->address = $rpc_node->getAddress(); $node->type = $rpc_node->getType(); $states = $rpc_node->getStatesList(); foreach ($states as $state) { $node->status[] = $this->getNodeStatus($state); } $rpc_metrics = $rpc_node->getLastMetricValuesList(); foreach ($rpc_metrics as $rpc_metric) { $metric = new \NetAssist\Graph\NodeMetric(); $metric->name = $rpc_metric->getKey(); $metric->values = array(); $rpc_values = $rpc_metric->getValue()->getValuesList(); foreach ($rpc_values as $rpc_value) { $metric->values[] = $this->getNodeMetricValue($rpc_value); } $node->metrics[] = $metric; } return $node; }
protected function getIpAttribute($value) { if (!is_null($value)) { $value = inet_ntop(gmp_export(gmp_init($value))); } return $value; }
public function __construct($cidr = null, $end = null) { if ($cidr === null || $cidr === "") { return parent::__construct(Request::ip()); } // Passing a static, return it. if ($cidr instanceof static) { // If no end, return $cidr. if ($end === null) { return parent::__construct($cidr->getStart(), $cidr->getEnd()); } // If end is a static, we are creating a range with two exising IPs. if ($end instanceof static) { $end = $end->getEnd(); } // If $end is binary, return it to base64. if (is_binary($end)) { $end = inet_ntop($end); } return parent::__construct($cidr->getStart(), $end); } else { if (is_binary($cidr)) { try { $start = inet_ntop($cidr); } catch (\Exception $e) { Log::warning("App\\Support\\IP::__construct trying to make IP from \$cidr binary value \"{$cidr}\" 0x" . bin2hex($cidr) . ", but it's not a real IP!"); if (!env('APP_DEBUG', false)) { $start = "127.0.0.1"; } else { throw $e; } } } else { try { $start = inet_ntop($cidr); } catch (\Exception $e) { $start = $cidr; } } } // Passing a static, return it. if ($end instanceof static) { return parent::__construct($start, $end->getEnd()); } else { if (is_binary($end)) { try { $end = inet_ntop($end); } catch (\Exception $e) { Log::warning("App\\Support\\IP::__construct trying to make IP from \$end binary value \"{$cidr}\" 0x" . bin2hex($end) . ", but it's not a real IP!"); if (!env('APP_DEBUG', false)) { $end = "127.0.0.1"; } else { throw $e; } } } } return parent::__construct($start, $end); }
/** * Render the grid cell value * * @param Varien_Object $row * @return string */ public function render(Varien_Object $row) { /** * The output of the "inet_ntop" function was disabled to prevent an error throwing * in case when the database value is not an ipv6 or an ipv4 binary representation (ex. NULL). */ return @inet_ntop($row->getData($this->getColumn()->getIndex())); }
/** * format * * This makes the Access object a nice fuzzy human readable object, spiffy * ain't it. */ public function format() { $this->f_start = inet_ntop($this->start); $this->f_end = inet_ntop($this->end); $this->f_user = $this->get_user_name(); $this->f_level = $this->get_level_name(); $this->f_type = $this->get_type_name(); }
/** * Get Short Address * * Converts an IP address into the smallest protocol notation it can; dot-notation * for IPv4, and compacted (double colons) notation for IPv6. * * @return string */ public function getShortAddress() { // If the binary representation of the IP address begins with 12 zeros, // it means it's an IPv4 address. Remove the zero's first so that PHP's // in-built number-to-protocol function will convert it accordingly. $ip = preg_replace('/^\\0{12}/', '', $this->getBinary()); return inet_ntop(pack('A' . $this->getIpLength($ip), $ip)); }
/** * Helper function to transform the ip and format it correctly for extraction. * If inputted with a string, it will leave it the same. * If inputted with a binary string, it will output an unpacked human readable string. * * @return $ip string */ public function extract($ip) { //human readable string if (ctype_print($ip)) { return $ip; } return inet_ntop($ip); }
/** * Return dot or colon notation of IPv4 or IPv6 address. * * @param string $ip * @return string|bool */ public static function inet_ntop($ip) { // trim this to the IPv4 equiv if it's in the mapped range if (wfWAFUtils::strlen($ip) == 16 && wfWAFUtils::substr($ip, 0, 12) == "ÿÿ") { $ip = wfWAFUtils::substr($ip, 12, 4); } return self::hasIPv6Support() ? inet_ntop($ip) : self::_inet_ntop($ip); }
/** * Convert a binary-encoded address to string * * @param string $bin_addr * @param string $always_v6 * @return string|boolean */ public static function bin2str($bin_addr, $always_v6 = false) { $str_addr = inet_ntop($bin_addr); if (!$always_v6 && self::isSixToFour($str_addr)) { return self::convSixToFour($str_addr); } else { return $str_addr; } }
public function value($value) { $value = parent::value($value); if (is_null($value)) { return inet_pton($_SERVER['REMOTE_ADDR']); } else { return Validate::ip($value) ? inet_pton($value) : inet_ntop($value); } }
public static function normalizeHostip($hostip) { $normalized = false; if ($hostip === false || $hostip === "") { $normalized = $hostip; } elseif (($pton = @inet_pton($hostip)) !== false) { $normalized = strtolower(inet_ntop($pton)); } return $normalized; }
/** * Does the actual work of each specific transformations plugin. * * @param string $buffer text to be transformed. a binary string containing * an IP address, as returned from MySQL's INET6_ATON * function * @param array $options transformation options * @param string $meta meta information * * @return string IP address */ public function applyTransformation($buffer, $options = array(), $meta = '') { $length = strlen($buffer); if ($length == 4 || $length == 16) { $val = @inet_ntop(pack('A' . $length, $buffer)); if ($val !== false) { return $val; } } return $buffer; }
/** * Returns the IPAddress string, null if no ip address has been set * @param int $format Uses the IPAddress::FORMAT_* constants * @throws \Exception Throws an exception if the value is not null and no valid format constant is given * @return string|null */ public function getIp($format = self::FORMAT_HEX) { if ($this->ipHex === null) { return null; } elseif ($format === self::FORMAT_HEX) { return $this->ipHex; } elseif ($format === self::FORMAT_IP_STRING) { return inet_ntop($this->hex2bin($this->ipHex)); } throw new \Exception('Invalid IP format'); }
/** * {@inheritdoc} */ public function handle(Record $recordToHandle, Record $oppositeRecord) { $this->validate($recordToHandle); $value = $recordToHandle->getValue($this->field); if (!$value) { $recordToHandle->setValue($this->field, 0); return; } $newValue = $this->isIpPacked ? ip2long(inet_ntop($value)) : ip2long($value); $recordToHandle->setValue($this->field, $newValue); }
function GetSnortSensorSids($conn2) { $query = "SELECT d.id,s.ip as sensor_ip FROM alienvault_siem.device d, alienvault.sensor s WHERE d.sensor_id=s.id"; if (!($rs =& $conn2->Execute($query))) { print $conn2->ErrorMsg(); exit; } while (!$rs->EOF) { $ret[inet_ntop($rs->fields['sensor_ip'])][] = $rs->fields['id']; $rs->MoveNext(); } return $ret; }
/** * Get the current visitor's IP address * * @return string */ public static function getIp() { if (is_null(static::$ip)) { $ip = self::detectAndCleanIP(); if (!empty($ip) && $ip != '0.0.0.0' && function_exists('inet_pton') && function_exists('inet_ntop')) { $myIP = @inet_pton($ip); if ($myIP !== false) { $ip = inet_ntop($myIP); } } static::setIp($ip); } return static::$ip; }
/** * Get the current visitor's IP address * * @return string */ public static function getIp() { if (is_null(static::$ip)) { $ip = array_key_exists('REMOTE_ADDR', $_SERVER) ? htmlspecialchars($_SERVER['REMOTE_ADDR']) : '0.0.0.0'; if (!empty($ip) && $ip != '0.0.0.0' && function_exists('inet_pton') && function_exists('inet_ntop')) { $myIP = @inet_pton($ip); if ($myIP !== false) { $ip = inet_ntop($myIP); } } static::setIp($ip); } return static::$ip; }
function form_region() { global $db, $vars; $form_region = new form(array('FORM_NAME' => 'form_region')); $form_region->db_data('regions.id, regions.name, regions.ip_start, regions.ip_end, regions.v6net, regions.v6prefix, regions.info'); $form_region->db_data_values("regions", "id", get('region')); if (get('region') != 'add') { $form_region->data[2]['value'] = long2ip($form_region->data[2]['value']); $form_region->data[3]['value'] = long2ip($form_region->data[3]['value']); $form_region->data[4]['value'] = inet_ntop($form_region->data[4]['value']); } $form_region->db_data_remove('regions__id'); return $form_region; }
public static function LongtoIPv6($bin) { $pad = 128 - strlen($bin); for ($i = 1; $i <= $pad; $i++) { $bin = "0" . $bin; } $bits = 0; $ipv6 = ''; while ($bits <= 7) { $bin_part = substr($bin, $bits * 16, 16); $ipv6 .= dechex(bindec($bin_part)) . ":"; $bits++; } return inet_ntop(inet_pton(substr($ipv6, 0, -1))); }
/** * {@inheritdoc} */ public function handle(Record $recordToHandle, Record $oppositeRecord) { $this->validate($recordToHandle); $value = $recordToHandle->getValue($this->field); if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $newValue = (int) ip2long($value); } else { if (@inet_ntop($value) !== false) { $newValue = (int) ip2long(inet_ntop($value)); } else { $newValue = 0; } } $recordToHandle->setValue($this->field, $newValue); }
/** * @return void */ public function testHandle() { $ip = '127.0.0.1'; $ipDbValue = inet_pton($ip); $fieldName = 'fieldname'; /** @var \Migration\ResourceModel\Record|\PHPUnit_Framework_MockObject_MockObject $record */ $record = $this->getMock('Migration\\ResourceModel\\Record', ['setValue', 'getValue', 'getFields'], [], '', false); $record->expects($this->any())->method('getFields')->willReturn([$fieldName]); $record->expects($this->any())->method('getValue')->with($fieldName)->willReturn($ipDbValue); $record->expects($this->any())->method('setValue')->with($fieldName, ip2long(inet_ntop($ipDbValue))); $record2 = $this->getMockBuilder('Migration\\ResourceModel\\Record')->disableOriginalConstructor()->getMock(); $handler = new ConvertIp(); $handler->setField($fieldName); $handler->handle($record, $record2); }
function nel_render_thread_panel_thread($dataforce, $render, $thread_data) { $render->add_data('has_file', $thread_data['has_file']); $render->add_multiple_data($thread_data); switch (BS_DATE_FORMAT) { case 'ISO': $render->add_data('post_time', date("Y/m/d H:i:s", floor($thread_data['post_time'] / 1000))); break; case 'US': $render->add_data('post_time', date("m/d/Y H:i:s", floor($thread_data['post_time'] / 1000))); break; case 'COM': $render->add_data('post_time', date("d/m/Y H:i:s", floor($thread_data['post_time'] / 1000))); break; } if (utf8_strlen($thread_data['name']) > 12) { $render->add_data('post_name', utf8_substr($thread_data['name'], 0, 11) . "..."); } if (utf8_strlen($thread_data['subject']) > 12) { $render->add_data('subject', utf8_substr($thread_data['subject'], 0, 11) . "..."); } if ($thread_data['email']) { $render->add_data('post_name', '"<a href="mailto:' . $thread_data['email'] . '">' . $thread_data['name'] . '</a>'); } $thread_data['comment'] = utf8_str_replace("<br>", " ", $thread_data['comment']); $render->add_data('comment', htmlspecialchars($thread_data['comment'])); if (utf8_strlen($thread_data['comment']) > 20) { $render->add_data('comment', utf8_substr($render->retrieve_data('comment'), 0, 19) . "..."); } $render->add_data('host', @inet_ntop($thread_data['host']) ? inet_ntop($thread_data['host']) : 'Unknown'); if ($thread_data['response_to'] == '0') { $render->add_data('is_op', TRUE); } else { $render->add_data('is_op', FALSE); } if (!empty($thread_data['files'])) { $files = $thread_data['files']; $filecount = count($files); $i = 0; while ($i < $filecount) { $files[$i]['filesize'] = (int) ceil($files[$i]['filesize'] / 1024); ++$i; } $render->add_data('files', $files); } $render->add_data('bg_class', $dataforce['j_increment'] % 2 ? 'row1' : 'row2'); $render->parse('thread_panel_thread.tpl', 'management'); }
function table_ip_ranges_v6() { global $db; $table_ip_ranges_v6 = new table(array('TABLE_NAME' => 'table_ip_ranges_v6', 'FORM_NAME' => 'table_ip_ranges_v6')); $table_ip_ranges_v6->db_data('ip_ranges_v6.id, "" AS ip_range_v6, ipv6_node_repos.v6net AS v6net, ip_ranges_v6.date_in, ip_ranges_v6.status, ip_ranges_v6.delete_req', 'ip_ranges_v6, ipv6_node_repos', 'ip_ranges_v6.node_id = ' . intval(get('node')) . ' and ip_ranges_v6.v6net_id = ipv6_node_repos.id', "", "ip_ranges_v6.date_in ASC"); foreach ((array) $table_ip_ranges_v6->data as $key => $value) { if ($key != 0) { $table_ip_ranges_v6->data[$key]['v6net'] = inet_ntop($table_ip_ranges_v6->data[$key]['v6net']); } } $table_ip_ranges_v6->db_data_multichoice('ip_ranges_v6', 'id'); $table_ip_ranges_v6->db_data_multichoice_checked('delete_req', 'Y'); $table_ip_ranges_v6->info['MULTICHOICE_LABEL'] = 'delete_request'; $table_ip_ranges_v6->db_data_remove('id', 'ip_range_v6', 'delete_req'); $table_ip_ranges_v6->db_data_translate('ip_ranges_v6__status'); return $table_ip_ranges_v6; }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if $value is a valid IP address * * @param mixed $value * @return boolean */ public function isValid($value) { $valueString = (string) $value; $this->_setValue($valueString); if (ip2long($valueString) === false || long2ip(ip2long($valueString)) !== $valueString) { if (!function_exists('inet_pton')) { $this->_error(); return false; } else { if (@inet_pton($value) === false || inet_ntop(@inet_pton($value)) !== $valueString) { $this->_error(); return false; } } } return true; }