From 0b6726742615c6cba104370b0ee57d6fdc272323 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 21 Aug 2016 01:23:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AF=20=D1=82=D1=83=D1=82=20=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BB=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=B0?= =?UTF-8?q?=20=D0=BA=20=D0=BA=D0=B0=D0=BA=D0=B8=D0=BC-=D1=82=D0=BE=20?= =?UTF-8?q?=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B5=D0=BD=D0=BD=D0=B8=D0=BC=20?= =?UTF-8?q?API=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0=D0=BC,=20?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BC=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BD=D1=8F=D0=BB,=20=D1=87=D1=82=D0=BE=20=D0=BF=D0=BE=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B8=D1=85=20=D0=BA=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=BE=D0=B2=20=D0=BD=D0=B5=20=D0=B1=D1=83=D0=B4=D0=B5?= =?UTF-8?q?=D1=82,=20=D0=BD=D0=BE=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BF=D1=83=D1=81=D1=82=D1=8C=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=82=D1=81=D1=8F=20:)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/filters/RequestFilter.php | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 api/filters/RequestFilter.php diff --git a/api/filters/RequestFilter.php b/api/filters/RequestFilter.php new file mode 100644 index 0000000..c722118 --- /dev/null +++ b/api/filters/RequestFilter.php @@ -0,0 +1,68 @@ +getRequest()->getUserIP(); + if ($this->checkIp($ip) || $this->checkByHost($ip)) { + return true; + } + + Yii::warning( + 'Access to ' . $action->controller->id . '::' . $action->id . + ' is denied due to IP address restriction. The requesting IP address is ' . $ip, + __METHOD__ + ); + + throw new ForbiddenHttpException('You are not allowed to access this page.'); + } + + protected function checkIp(string $ip) : bool { + foreach ($this->allowedIPs as $filter) { + if ($filter === '*' + || $filter === $ip + || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos)) + ) { + return true; + } + } + + return false; + } + + protected function checkByHost(string $ip) : bool { + foreach ($this->allowedHosts as $hostname) { + $filter = gethostbyname($hostname); + if ($filter === $ip) { + return true; + } + } + + return false; + } + +}