Permalink
Browse files

phpcs

  • Loading branch information...
1 parent df76e07 commit b3d6052631cd9b485d268fc81df2c136ab7d0a1d @dongilbert dongilbert committed Nov 1, 2013
View
@@ -1,4 +1,5 @@
/.idea
/vendor
/composer.phar
+/composer.lock
.DS_Store
View
@@ -1,22 +1,21 @@
{
- "name": "ugli/stack-attack",
- "description": "Ugli StackAttack Middleware for StackPHP",
- "license": "MIT",
- "require": {
- "php": ">=5.3.10",
+ "name": "league/stack-attack",
+ "description": "StackAttack Middleware for StackPHP",
+ "license": "MIT",
+ "require": {
+ "php": ">=5.3.10",
"symfony/http-foundation": "~2.1",
"symfony/http-kernel": "~2.1"
- },
+ },
"require-dev" : {
- "silex/silex": "1.1.*@dev",
"stack/builder" : "1.0.*@dev",
"stack/callable-http-kernel": "~1.0@dev",
"stack/inline": "~1.0@dev",
"stack/run": "~1.0@dev"
},
- "autoload": {
- "psr-0": {
- "Ugli\\StackAttack\\": "src/"
+ "autoload": {
+ "psr-0": {
+ "League\\StackAttack\\": "src/"
}
- }
+ }
}
@@ -0,0 +1,74 @@
+<?php
+
+namespace League\StackAttack;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+
+class Attack implements HttpKernelInterface
+{
+ /**
+ * @var \Symfony\Component\HttpKernel\HttpKernelInterface
+ */
+ private $app;
+
+ /**
+ * @var string
+ */
+ private $blacklistMessage = 'Unauthorized';
+
+ /**
+ * @var FilterCollection
+ */
+ private $filters;
+
+ public function __construct(HttpKernelInterface $app, FilterCollection $filters)
+ {
+ $this->app = $app;
+ $this->filters = $filters;
+ }
+
+ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
+ {
+ if ($this->whitelisted($request)) {
+ return $this->app->handle($request, $type, $catch);
+ } elseif ($this->blacklisted($request)) {
+ return new Response($this->blacklistMessage, 401);
+ }
+
+ return $this->app->handle($request, $type, $catch);
+ }
+
+ private function whitelisted(Request $request)
+ {
+ $whitelist = $this->filters->getWhitelist();
+ if (!empty($whitelist)) {
+ foreach ($whitelist as $rule) {
+ // If the rule passes, then this is a whitelisted request.
+ if ($rule->test($request) === true) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private function blacklisted(Request $request)
+ {
+ $blacklist = $this->filters->getBlacklist();
+ if (!empty($blacklist)) {
+ foreach ($blacklist as $rule) {
+ // If the rule passes, then this is a blacklisted request.
+ if ($rule->test($request)) {
+ $this->blacklistMessage = $request->attributes->get('stackattack.match.message');
+
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+}
@@ -0,0 +1,33 @@
+<?php
+
+namespace League\StackAttack;
+
+class FilterCollection
+{
+ protected $whitelist = array();
+ protected $blacklist = array();
+
+ public function whitelist($message, \Closure $rule)
+ {
+ $this->whitelist[] = new Filters\Whitelist($message, $rule);
+
+ return $this;
+ }
+
+ public function blacklist($message, \Closure $rule)
+ {
+ $this->blacklist[] = new Filters\Blacklist($message, $rule);
+
+ return $this;
+ }
+
+ public function getWhitelist()
+ {
+ return $this->whitelist;
+ }
+
+ public function getBlacklist()
+ {
+ return $this->blacklist;
+ }
+}
@@ -0,0 +1,8 @@
+<?php
+
+namespace League\StackAttack\Filters;
+
+class Blacklist extends Check
+{
+ protected $type = 'blacklist';
+}
@@ -0,0 +1,51 @@
+<?php
+
+namespace League\StackAttack\Filters;
+
+use Symfony\Component\HttpFoundation\Request;
+
+abstract class Check
+{
+ /**
+ * @var string
+ */
+ protected $message;
+
+ /**
+ * @var \Closure
+ */
+ protected $rule;
+
+ /**
+ * @var string
+ */
+ protected $type;
+
+ public function __construct($message, \Closure $rule)
+ {
+ $this->message = $message;
+ $this->rule = $rule;
+ }
+
+ public function test(Request $request)
+ {
+ if (call_user_func($this->rule, $request)) {
+ $request->attributes->set('stackattack.match.message', $this->getMessage());
+ $request->attributes->set('stackattack.match.type', $this->getType());
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public function getMessage()
+ {
+ return $this->message;
+ }
+
+ public function getType()
+ {
+ return $this->type;
+ }
+}
@@ -0,0 +1,8 @@
+<?php
+
+namespace League\StackAttack\Filters;
+
+class Whitelist extends Check
+{
+ protected $type = 'whitelist';
+}
@@ -1,73 +0,0 @@
-<?php
-
-namespace Ugli\StackAttack;
-
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\HttpKernel\HttpKernelInterface;
-
-class Attack implements HttpKernelInterface
-{
- /**
- * @var \Symfony\Component\HttpKernel\HttpKernelInterface
- */
- private $app;
-
- /**
- * @var string
- */
- private $blacklistMessage = 'Unauthorized';
-
- /**
- * @var FilterCollection
- */
- private $filters;
-
- public function __construct(HttpKernelInterface $app, FilterCollection $filters)
- {
- $this->app = $app;
- $this->filters = $filters;
- }
-
- public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
- {
- if ($this->whitelisted($request)) {
- return $this->app->handle($request, $type, $catch);
- } elseif ($this->blacklisted($request)) {
- return new Response($this->blacklistMessage, 401);
- }
-
- return $this->app->handle($request, $type, $catch);
- }
-
- private function whitelisted(Request $request)
- {
- $whitelist = $this->filters->getWhitelist();
- if (!empty($whitelist)) {
- foreach ($whitelist as $rule) {
- // If the rule passes, then this is a whitelisted request.
- if ($rule->test($request) === true) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- private function blacklisted(Request $request)
- {
- $blacklist = $this->filters->getBlacklist();
- if (!empty($blacklist)) {
- foreach ($blacklist as $rule) {
- // If the rule passes, then this is a blacklisted request.
- if ($rule->test($request)) {
- $this->blacklistMessage = $request->attributes->get('stackattack.match.message');
- return true;
- }
- }
- }
-
- return false;
- }
-}
@@ -1,33 +0,0 @@
-<?php
-
-namespace Ugli\StackAttack;
-
-class FilterCollection
-{
- protected $whitelist = array();
- protected $blacklist = array();
-
- public function whitelist($message, \Closure $rule)
- {
- $this->whitelist[] = new Filters\Whitelist($message, $rule);
-
- return $this;
- }
-
- public function blacklist($message, \Closure $rule)
- {
- $this->blacklist[] = new Filters\Blacklist($message, $rule);
-
- return $this;
- }
-
- public function getWhitelist()
- {
- return $this->whitelist;
- }
-
- public function getBlacklist()
- {
- return $this->blacklist;
- }
-}
@@ -1,8 +0,0 @@
-<?php
-
-namespace Ugli\StackAttack\Filters;
-
-class Blacklist extends Check
-{
- protected $type = 'blacklist';
-}
@@ -1,51 +0,0 @@
-<?php
-
-namespace Ugli\StackAttack\Filters;
-
-use Symfony\Component\HttpFoundation\Request;
-
-abstract class Check
-{
- /**
- * @var string
- */
- protected $message;
-
- /**
- * @var \Closure
- */
- protected $rule;
-
- /**
- * @var string
- */
- protected $type;
-
- public function __construct($message, \Closure $rule)
- {
- $this->message = $message;
- $this->rule = $rule;
- }
-
- public function test(Request $request)
- {
- if (call_user_func($this->rule, $request)) {
- $request->attributes->set('stackattack.match.message', $this->getMessage());
- $request->attributes->set('stackattack.match.type', $this->getType());
-
- return true;
- }
-
- return false;
- }
-
- public function getMessage()
- {
- return $this->message;
- }
-
- public function getType()
- {
- return $this->type;
- }
-}
@@ -1,8 +0,0 @@
-<?php
-
-namespace Ugli\StackAttack\Filters;
-
-class Whitelist extends Check
-{
- protected $type = 'whitelist';
-}
Oops, something went wrong.

0 comments on commit b3d6052

Please sign in to comment.