Exemple #1
0
 public function testYouCanGetTheClientUserIpForHttpRequestIfAvailable()
 {
     if (isset($_SERVER['REMOTE_ADDR'])) {
         unset($_SERVER['REMOTE_ADDR']);
     }
     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
         unset($_SERVER['HTTP_X_FORWARDED_FOR']);
     }
     $this->assertEquals('noip', IpUtil::getUserIp());
     $_SERVER['REMOTE_ADDR'] = '192.168.10.1';
     $this->assertEquals('192.168.10.1', IpUtil::getUserIp());
     $_SERVER['HTTP_X_FORWARDED_FOR'] = null;
     $this->assertEquals('192.168.10.1', IpUtil::getUserIp());
     $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.10.56';
     $this->assertEquals('192.168.10.56', IpUtil::getUserIp());
 }
Exemple #2
0
 /**
  * Do the validation
  *
  * @param mixed $ip IP Address to check - if null, then use current IP of requestor
  *
  * @return boolean
  */
 protected function validate($ip = null)
 {
     $ip = empty($ip) ? IpUtil::getUserIp() : $ip;
     return Match::on(FTry::with(function () use($ip) {
         return array_reduce($this->netmasks, function (&$result, $cidr) use($ip) {
             return $result || IpUtil::cidrMatch($ip, $cidr);
         }, false);
     }))->Monad_FTry_Success(function ($value) {
         return Match::on(Option::create($value->flatten(), false))->Monad_Option_Some(true)->Monad_Option_None(function () {
             $this->messenger->add(new StringType(self::ERR_MSG1));
             return false;
         })->value();
     })->Monad_FTry_Failure(function ($e) {
         return Match::on(Option::create(strpos($e->value()->getMessage(), 'cidr'), false))->Monad_Option_Some(function () {
             $this->messenger->add(new StringType(self::ERR_MSG2));
             return false;
         })->Monad_Option_None(function () {
             $this->messenger->add(new StringType(self::ERR_MSG1));
             return false;
         })->value();
     })->value();
 }