PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class Network_Simple
- {
- protected $sCurrentIP;
- );
- function __construct($sCurrentIp=null)
- {
- $this->sCurrentIp=$sCurrentIp?$sCurrentIp:$_SERVER['REMOTE_ADDR'];
- }
- /**
- * Useful for DHCP purposes. Returns first free IP in specified subnet
- * @param array $rgIPs List of busy IP
- * @param array $rgSubnet Subnet presentation. Must contain keys 'net' and 'mask'
- * @return mixed
- */
- public function leaseAddressDHCP($rgIPs, $rgSubnet)
- {
- {
- return null;
- }
- }
- /**
- * Returns full table of free IP addresses in $rgSubnet
- * @param array $rgIPs List of busy IP
- * @param array $rgSubnet Subnet presentation. Must contain keys 'net' and 'mask'
- * @return mixed
- */
- public function leaseTableDHCP($rgIPs, $rgSubnet)
- {
- {
- return null;
- }
- return $rgResult;
- }
- /**
- * Returns MAC-address (Ethernet 2-level in OSI model address)
- * @param string $ip Address to search MAC for. If not set, IP-address from constructor'll be used
- * @return mixed
- */
- public function getMAC($ip=null)
- {
- if((!$ip && !$this->sCurrentIP) || !$this->_arp_allowed())
- {
- return null;
- }
- $ip=$ip?$ip:$this->sCurrentIP;
- if(PHP_OS=='WINNT')
- {
- $sMacTemplate="/[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}/i";
- foreach($rgResult as $key=>$value)
- {
- {
- break;
- }
- };
- }
- else
- {
- {
- $sMacTemplate="/[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}/i";
- }
- }
- }
- /**
- * Get client remote IP address
- * @param boolean $bReturnLocalIP If set to true, local subnet address'll be returned (if present)
- * @return string
- */
- public function getRemoteIP($bReturnLocalIP=false)
- {
- if($_SERVER['HTTP_X_FORWARDED_FOR'])
- {
- foreach($rgIPs as $ip)
- {
- if($bReturnLocalIP)
- {
- return $ip;
- }
- else
- {
- $bIsLocal=false;
- foreach($this->rgLocalSubnets as $rgSubnet)
- {
- if($this->isInSubnet($rgSubnet, $ip))
- {
- $bIsLocal=true;
- }
- }
- if(!$bIsLocal)
- {
- return $ip;
- }
- }
- }
- }
- return $_SERVER['REMOTE_ADDR'];
- }
- /**
- *
- * @param array $rgSubnet Subnet presentation. Must contain keys 'net' and 'mask'
- * @param string $ip IP address to compare with subnet
- * @return boolean
- */
- public function isInSubnet($rgSubnet, $ip=null)
- {
- {
- return false;
- }
- $ip=$ip?$ip:$this->sCurrentIP;
- {
- $rgSubnet['mask']=(int)log(($this->ip2Long('255.255.255.255')-$this->ip2Long($rgSubnet['mask'])),2);
- }
- }
- /**
- *
- * @param string $ip Address to convert. If not set, IP-address from constructor'll be used
- * @return mixed
- */
- {
- //lazy:
- if(!$ip && !$this->sCurrentIP)
- {
- return null;
- }
- $ip=$ip?$ip:$this->sCurrentIP;
- }
- //arp may be unreachable due to system security; we still rely on 'which' command
- protected function _arp_allowed()
- {
- if(PHP_OS=='WINNT')
- {
- return true;
- }
- {
- return false;
- }
- }
- }