Completed
Push — stable13 ( 8a0ced...f67879 )
by
unknown
23:00 queued 11:14
created
lib/private/AppFramework/Http/Dispatcher.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -43,142 +43,142 @@
 block discarded – undo
43 43
  */
44 44
 class Dispatcher {
45 45
 
46
-	private $middlewareDispatcher;
47
-	private $protocol;
48
-	private $reflector;
49
-	private $request;
50
-
51
-	/**
52
-	 * @param Http $protocol the http protocol with contains all status headers
53
-	 * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which
54
-	 * runs the middleware
55
-	 * @param ControllerMethodReflector $reflector the reflector that is used to inject
56
-	 * the arguments for the controller
57
-	 * @param IRequest $request the incoming request
58
-	 */
59
-	public function __construct(Http $protocol,
60
-								MiddlewareDispatcher $middlewareDispatcher,
61
-								ControllerMethodReflector $reflector,
62
-								IRequest $request) {
63
-		$this->protocol = $protocol;
64
-		$this->middlewareDispatcher = $middlewareDispatcher;
65
-		$this->reflector = $reflector;
66
-		$this->request = $request;
67
-	}
68
-
69
-
70
-	/**
71
-	 * Handles a request and calls the dispatcher on the controller
72
-	 * @param Controller $controller the controller which will be called
73
-	 * @param string $methodName the method name which will be called on
74
-	 * the controller
75
-	 * @return array $array[0] contains a string with the http main header,
76
-	 * $array[1] contains headers in the form: $key => value, $array[2] contains
77
-	 * the response output
78
-	 * @throws \Exception
79
-	 */
80
-	public function dispatch(Controller $controller, $methodName) {
81
-		$out = array(null, array(), null);
82
-
83
-		try {
84
-			// prefill reflector with everything thats needed for the
85
-			// middlewares
86
-			$this->reflector->reflect($controller, $methodName);
87
-
88
-			$this->middlewareDispatcher->beforeController($controller,
89
-				$methodName);
90
-			$response = $this->executeController($controller, $methodName);
91
-
92
-			// if an exception appears, the middleware checks if it can handle the
93
-			// exception and creates a response. If no response is created, it is
94
-			// assumed that theres no middleware who can handle it and the error is
95
-			// thrown again
96
-		} catch(\Exception $exception){
97
-			$response = $this->middlewareDispatcher->afterException(
98
-				$controller, $methodName, $exception);
99
-			if (is_null($response)) {
100
-				throw $exception;
101
-			}
102
-		}
103
-
104
-		$response = $this->middlewareDispatcher->afterController(
105
-			$controller, $methodName, $response);
106
-
107
-		// depending on the cache object the headers need to be changed
108
-		$out[0] = $this->protocol->getStatusHeader($response->getStatus(),
109
-			$response->getLastModified(), $response->getETag());
110
-		$out[1] = array_merge($response->getHeaders());
111
-		$out[2] = $response->getCookies();
112
-		$out[3] = $this->middlewareDispatcher->beforeOutput(
113
-			$controller, $methodName, $response->render()
114
-		);
115
-		$out[4] = $response;
116
-
117
-		return $out;
118
-	}
119
-
120
-
121
-	/**
122
-	 * Uses the reflected parameters, types and request parameters to execute
123
-	 * the controller
124
-	 * @param Controller $controller the controller to be executed
125
-	 * @param string $methodName the method on the controller that should be executed
126
-	 * @return Response
127
-	 */
128
-	private function executeController($controller, $methodName) {
129
-		$arguments = array();
130
-
131
-		// valid types that will be casted
132
-		$types = array('int', 'integer', 'bool', 'boolean', 'float');
133
-
134
-		foreach($this->reflector->getParameters() as $param => $default) {
135
-
136
-			// try to get the parameter from the request object and cast
137
-			// it to the type annotated in the @param annotation
138
-			$value = $this->request->getParam($param, $default);
139
-			$type = $this->reflector->getType($param);
140
-
141
-			// if this is submitted using GET or a POST form, 'false' should be
142
-			// converted to false
143
-			if(($type === 'bool' || $type === 'boolean') &&
144
-				$value === 'false' &&
145
-				(
146
-					$this->request->method === 'GET' ||
147
-					strpos($this->request->getHeader('Content-Type'),
148
-						'application/x-www-form-urlencoded') !== false
149
-				)
150
-			) {
151
-				$value = false;
152
-
153
-			} elseif($value !== null && in_array($type, $types)) {
154
-				settype($value, $type);
155
-			}
156
-
157
-			$arguments[] = $value;
158
-		}
159
-
160
-		$response = call_user_func_array(array($controller, $methodName), $arguments);
161
-
162
-		// format response
163
-		if($response instanceof DataResponse || !($response instanceof Response)) {
164
-
165
-			// get format from the url format or request format parameter
166
-			$format = $this->request->getParam('format');
167
-
168
-			// if none is given try the first Accept header
169
-			if($format === null) {
170
-				$headers = $this->request->getHeader('Accept');
171
-				$format = $controller->getResponderByHTTPHeader($headers, null);
172
-			}
173
-
174
-			if ($format !== null) {
175
-				$response = $controller->buildResponse($response, $format);
176
-			} else {
177
-				$response = $controller->buildResponse($response);
178
-			}
179
-		}
180
-
181
-		return $response;
182
-	}
46
+    private $middlewareDispatcher;
47
+    private $protocol;
48
+    private $reflector;
49
+    private $request;
50
+
51
+    /**
52
+     * @param Http $protocol the http protocol with contains all status headers
53
+     * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which
54
+     * runs the middleware
55
+     * @param ControllerMethodReflector $reflector the reflector that is used to inject
56
+     * the arguments for the controller
57
+     * @param IRequest $request the incoming request
58
+     */
59
+    public function __construct(Http $protocol,
60
+                                MiddlewareDispatcher $middlewareDispatcher,
61
+                                ControllerMethodReflector $reflector,
62
+                                IRequest $request) {
63
+        $this->protocol = $protocol;
64
+        $this->middlewareDispatcher = $middlewareDispatcher;
65
+        $this->reflector = $reflector;
66
+        $this->request = $request;
67
+    }
68
+
69
+
70
+    /**
71
+     * Handles a request and calls the dispatcher on the controller
72
+     * @param Controller $controller the controller which will be called
73
+     * @param string $methodName the method name which will be called on
74
+     * the controller
75
+     * @return array $array[0] contains a string with the http main header,
76
+     * $array[1] contains headers in the form: $key => value, $array[2] contains
77
+     * the response output
78
+     * @throws \Exception
79
+     */
80
+    public function dispatch(Controller $controller, $methodName) {
81
+        $out = array(null, array(), null);
82
+
83
+        try {
84
+            // prefill reflector with everything thats needed for the
85
+            // middlewares
86
+            $this->reflector->reflect($controller, $methodName);
87
+
88
+            $this->middlewareDispatcher->beforeController($controller,
89
+                $methodName);
90
+            $response = $this->executeController($controller, $methodName);
91
+
92
+            // if an exception appears, the middleware checks if it can handle the
93
+            // exception and creates a response. If no response is created, it is
94
+            // assumed that theres no middleware who can handle it and the error is
95
+            // thrown again
96
+        } catch(\Exception $exception){
97
+            $response = $this->middlewareDispatcher->afterException(
98
+                $controller, $methodName, $exception);
99
+            if (is_null($response)) {
100
+                throw $exception;
101
+            }
102
+        }
103
+
104
+        $response = $this->middlewareDispatcher->afterController(
105
+            $controller, $methodName, $response);
106
+
107
+        // depending on the cache object the headers need to be changed
108
+        $out[0] = $this->protocol->getStatusHeader($response->getStatus(),
109
+            $response->getLastModified(), $response->getETag());
110
+        $out[1] = array_merge($response->getHeaders());
111
+        $out[2] = $response->getCookies();
112
+        $out[3] = $this->middlewareDispatcher->beforeOutput(
113
+            $controller, $methodName, $response->render()
114
+        );
115
+        $out[4] = $response;
116
+
117
+        return $out;
118
+    }
119
+
120
+
121
+    /**
122
+     * Uses the reflected parameters, types and request parameters to execute
123
+     * the controller
124
+     * @param Controller $controller the controller to be executed
125
+     * @param string $methodName the method on the controller that should be executed
126
+     * @return Response
127
+     */
128
+    private function executeController($controller, $methodName) {
129
+        $arguments = array();
130
+
131
+        // valid types that will be casted
132
+        $types = array('int', 'integer', 'bool', 'boolean', 'float');
133
+
134
+        foreach($this->reflector->getParameters() as $param => $default) {
135
+
136
+            // try to get the parameter from the request object and cast
137
+            // it to the type annotated in the @param annotation
138
+            $value = $this->request->getParam($param, $default);
139
+            $type = $this->reflector->getType($param);
140
+
141
+            // if this is submitted using GET or a POST form, 'false' should be
142
+            // converted to false
143
+            if(($type === 'bool' || $type === 'boolean') &&
144
+                $value === 'false' &&
145
+                (
146
+                    $this->request->method === 'GET' ||
147
+                    strpos($this->request->getHeader('Content-Type'),
148
+                        'application/x-www-form-urlencoded') !== false
149
+                )
150
+            ) {
151
+                $value = false;
152
+
153
+            } elseif($value !== null && in_array($type, $types)) {
154
+                settype($value, $type);
155
+            }
156
+
157
+            $arguments[] = $value;
158
+        }
159
+
160
+        $response = call_user_func_array(array($controller, $methodName), $arguments);
161
+
162
+        // format response
163
+        if($response instanceof DataResponse || !($response instanceof Response)) {
164
+
165
+            // get format from the url format or request format parameter
166
+            $format = $this->request->getParam('format');
167
+
168
+            // if none is given try the first Accept header
169
+            if($format === null) {
170
+                $headers = $this->request->getHeader('Accept');
171
+                $format = $controller->getResponderByHTTPHeader($headers, null);
172
+            }
173
+
174
+            if ($format !== null) {
175
+                $response = $controller->buildResponse($response, $format);
176
+            } else {
177
+                $response = $controller->buildResponse($response);
178
+            }
179
+        }
180
+
181
+        return $response;
182
+    }
183 183
 
184 184
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/Middleware/Security/CORSMiddleware.php 1 patch
Indentation   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -45,116 +45,116 @@
 block discarded – undo
45 45
  * https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
46 46
  */
47 47
 class CORSMiddleware extends Middleware {
48
-	/** @var IRequest  */
49
-	private $request;
50
-	/** @var ControllerMethodReflector */
51
-	private $reflector;
52
-	/** @var Session */
53
-	private $session;
54
-	/** @var Throttler */
55
-	private $throttler;
48
+    /** @var IRequest  */
49
+    private $request;
50
+    /** @var ControllerMethodReflector */
51
+    private $reflector;
52
+    /** @var Session */
53
+    private $session;
54
+    /** @var Throttler */
55
+    private $throttler;
56 56
 
57
-	/**
58
-	 * @param IRequest $request
59
-	 * @param ControllerMethodReflector $reflector
60
-	 * @param Session $session
61
-	 * @param Throttler $throttler
62
-	 */
63
-	public function __construct(IRequest $request,
64
-								ControllerMethodReflector $reflector,
65
-								Session $session,
66
-								Throttler $throttler) {
67
-		$this->request = $request;
68
-		$this->reflector = $reflector;
69
-		$this->session = $session;
70
-		$this->throttler = $throttler;
71
-	}
57
+    /**
58
+     * @param IRequest $request
59
+     * @param ControllerMethodReflector $reflector
60
+     * @param Session $session
61
+     * @param Throttler $throttler
62
+     */
63
+    public function __construct(IRequest $request,
64
+                                ControllerMethodReflector $reflector,
65
+                                Session $session,
66
+                                Throttler $throttler) {
67
+        $this->request = $request;
68
+        $this->reflector = $reflector;
69
+        $this->session = $session;
70
+        $this->throttler = $throttler;
71
+    }
72 72
 
73
-	/**
74
-	 * This is being run in normal order before the controller is being
75
-	 * called which allows several modifications and checks
76
-	 *
77
-	 * @param Controller $controller the controller that is being called
78
-	 * @param string $methodName the name of the method that will be called on
79
-	 *                           the controller
80
-	 * @throws SecurityException
81
-	 * @since 6.0.0
82
-	 */
83
-	public function beforeController($controller, $methodName){
84
-		// ensure that @CORS annotated API routes are not used in conjunction
85
-		// with session authentication since this enables CSRF attack vectors
86
-		if ($this->reflector->hasAnnotation('CORS') &&
87
-			!$this->reflector->hasAnnotation('PublicPage'))  {
88
-			$user = $this->request->server['PHP_AUTH_USER'];
89
-			$pass = $this->request->server['PHP_AUTH_PW'];
73
+    /**
74
+     * This is being run in normal order before the controller is being
75
+     * called which allows several modifications and checks
76
+     *
77
+     * @param Controller $controller the controller that is being called
78
+     * @param string $methodName the name of the method that will be called on
79
+     *                           the controller
80
+     * @throws SecurityException
81
+     * @since 6.0.0
82
+     */
83
+    public function beforeController($controller, $methodName){
84
+        // ensure that @CORS annotated API routes are not used in conjunction
85
+        // with session authentication since this enables CSRF attack vectors
86
+        if ($this->reflector->hasAnnotation('CORS') &&
87
+            !$this->reflector->hasAnnotation('PublicPage'))  {
88
+            $user = $this->request->server['PHP_AUTH_USER'];
89
+            $pass = $this->request->server['PHP_AUTH_PW'];
90 90
 
91
-			$this->session->logout();
92
-			try {
93
-				if (!$this->session->logClientIn($user, $pass, $this->request, $this->throttler)) {
94
-					throw new SecurityException('CORS requires basic auth', Http::STATUS_UNAUTHORIZED);
95
-				}
96
-			} catch (PasswordLoginForbiddenException $ex) {
97
-				throw new SecurityException('Password login forbidden, use token instead', Http::STATUS_UNAUTHORIZED);
98
-			}
99
-		}
100
-	}
91
+            $this->session->logout();
92
+            try {
93
+                if (!$this->session->logClientIn($user, $pass, $this->request, $this->throttler)) {
94
+                    throw new SecurityException('CORS requires basic auth', Http::STATUS_UNAUTHORIZED);
95
+                }
96
+            } catch (PasswordLoginForbiddenException $ex) {
97
+                throw new SecurityException('Password login forbidden, use token instead', Http::STATUS_UNAUTHORIZED);
98
+            }
99
+        }
100
+    }
101 101
 
102
-	/**
103
-	 * This is being run after a successful controllermethod call and allows
104
-	 * the manipulation of a Response object. The middleware is run in reverse order
105
-	 *
106
-	 * @param Controller $controller the controller that is being called
107
-	 * @param string $methodName the name of the method that will be called on
108
-	 *                           the controller
109
-	 * @param Response $response the generated response from the controller
110
-	 * @return Response a Response object
111
-	 * @throws SecurityException
112
-	 */
113
-	public function afterController($controller, $methodName, Response $response){
114
-		// only react if its a CORS request and if the request sends origin and
102
+    /**
103
+     * This is being run after a successful controllermethod call and allows
104
+     * the manipulation of a Response object. The middleware is run in reverse order
105
+     *
106
+     * @param Controller $controller the controller that is being called
107
+     * @param string $methodName the name of the method that will be called on
108
+     *                           the controller
109
+     * @param Response $response the generated response from the controller
110
+     * @return Response a Response object
111
+     * @throws SecurityException
112
+     */
113
+    public function afterController($controller, $methodName, Response $response){
114
+        // only react if its a CORS request and if the request sends origin and
115 115
 
116
-		if(isset($this->request->server['HTTP_ORIGIN']) &&
117
-			$this->reflector->hasAnnotation('CORS')) {
116
+        if(isset($this->request->server['HTTP_ORIGIN']) &&
117
+            $this->reflector->hasAnnotation('CORS')) {
118 118
 
119
-			// allow credentials headers must not be true or CSRF is possible
120
-			// otherwise
121
-			foreach($response->getHeaders() as $header => $value) {
122
-				if(strtolower($header) === 'access-control-allow-credentials' &&
123
-				   strtolower(trim($value)) === 'true') {
124
-					$msg = 'Access-Control-Allow-Credentials must not be '.
125
-						   'set to true in order to prevent CSRF';
126
-					throw new SecurityException($msg);
127
-				}
128
-			}
119
+            // allow credentials headers must not be true or CSRF is possible
120
+            // otherwise
121
+            foreach($response->getHeaders() as $header => $value) {
122
+                if(strtolower($header) === 'access-control-allow-credentials' &&
123
+                   strtolower(trim($value)) === 'true') {
124
+                    $msg = 'Access-Control-Allow-Credentials must not be '.
125
+                            'set to true in order to prevent CSRF';
126
+                    throw new SecurityException($msg);
127
+                }
128
+            }
129 129
 
130
-			$origin = $this->request->server['HTTP_ORIGIN'];
131
-			$response->addHeader('Access-Control-Allow-Origin', $origin);
132
-		}
133
-		return $response;
134
-	}
130
+            $origin = $this->request->server['HTTP_ORIGIN'];
131
+            $response->addHeader('Access-Control-Allow-Origin', $origin);
132
+        }
133
+        return $response;
134
+    }
135 135
 
136
-	/**
137
-	 * If an SecurityException is being caught return a JSON error response
138
-	 *
139
-	 * @param Controller $controller the controller that is being called
140
-	 * @param string $methodName the name of the method that will be called on
141
-	 *                           the controller
142
-	 * @param \Exception $exception the thrown exception
143
-	 * @throws \Exception the passed in exception if it can't handle it
144
-	 * @return Response a Response object or null in case that the exception could not be handled
145
-	 */
146
-	public function afterException($controller, $methodName, \Exception $exception){
147
-		if($exception instanceof SecurityException){
148
-			$response =  new JSONResponse(['message' => $exception->getMessage()]);
149
-			if($exception->getCode() !== 0) {
150
-				$response->setStatus($exception->getCode());
151
-			} else {
152
-				$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
153
-			}
154
-			return $response;
155
-		}
136
+    /**
137
+     * If an SecurityException is being caught return a JSON error response
138
+     *
139
+     * @param Controller $controller the controller that is being called
140
+     * @param string $methodName the name of the method that will be called on
141
+     *                           the controller
142
+     * @param \Exception $exception the thrown exception
143
+     * @throws \Exception the passed in exception if it can't handle it
144
+     * @return Response a Response object or null in case that the exception could not be handled
145
+     */
146
+    public function afterException($controller, $methodName, \Exception $exception){
147
+        if($exception instanceof SecurityException){
148
+            $response =  new JSONResponse(['message' => $exception->getMessage()]);
149
+            if($exception->getCode() !== 0) {
150
+                $response->setStatus($exception->getCode());
151
+            } else {
152
+                $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
153
+            }
154
+            return $response;
155
+        }
156 156
 
157
-		throw $exception;
158
-	}
157
+        throw $exception;
158
+    }
159 159
 
160 160
 }
Please login to merge, or discard this patch.
AppFramework/Middleware/Security/Exceptions/NotConfirmedException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@
 block discarded – undo
31 31
  * @package OC\AppFramework\Middleware\Security\Exceptions
32 32
  */
33 33
 class NotConfirmedException extends SecurityException {
34
-	public function __construct() {
35
-		parent::__construct('Password confirmation is required', Http::STATUS_FORBIDDEN);
36
-	}
34
+    public function __construct() {
35
+        parent::__construct('Password confirmation is required', Http::STATUS_FORBIDDEN);
36
+    }
37 37
 }
Please login to merge, or discard this patch.
Middleware/Security/Exceptions/CrossSiteRequestForgeryException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class CrossSiteRequestForgeryException extends SecurityException {
37
-	public function __construct() {
38
-		parent::__construct('CSRF check failed', Http::STATUS_PRECONDITION_FAILED);
39
-	}
37
+    public function __construct() {
38
+        parent::__construct('CSRF check failed', Http::STATUS_PRECONDITION_FAILED);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
AppFramework/Middleware/Security/Exceptions/AppNotEnabledException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class AppNotEnabledException extends SecurityException {
37
-	public function __construct() {
38
-		parent::__construct('App is not enabled', Http::STATUS_PRECONDITION_FAILED);
39
-	}
37
+    public function __construct() {
38
+        parent::__construct('App is not enabled', Http::STATUS_PRECONDITION_FAILED);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
private/AppFramework/Middleware/Security/Exceptions/NotAdminException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class NotAdminException extends SecurityException {
37
-	public function __construct($message = 'Logged in user must be an admin') {
38
-		parent::__construct($message, Http::STATUS_FORBIDDEN);
39
-	}
37
+    public function __construct($message = 'Logged in user must be an admin') {
38
+        parent::__construct($message, Http::STATUS_FORBIDDEN);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
AppFramework/Middleware/Security/Exceptions/NotLoggedInException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
  * @package OC\AppFramework\Middleware\Security\Exceptions
35 35
  */
36 36
 class NotLoggedInException extends SecurityException {
37
-	public function __construct() {
38
-		parent::__construct('Current user is not logged in', Http::STATUS_UNAUTHORIZED);
39
-	}
37
+    public function __construct() {
38
+        parent::__construct('Current user is not logged in', Http::STATUS_UNAUTHORIZED);
39
+    }
40 40
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/Middleware/MiddlewareDispatcher.php 1 patch
Indentation   +123 added lines, -123 removed lines patch added patch discarded remove patch
@@ -37,128 +37,128 @@
 block discarded – undo
37 37
  */
38 38
 class MiddlewareDispatcher {
39 39
 
40
-	/**
41
-	 * @var array array containing all the middlewares
42
-	 */
43
-	private $middlewares;
44
-
45
-	/**
46
-	 * @var int counter which tells us what middlware was executed once an
47
-	 *                  exception occurs
48
-	 */
49
-	private $middlewareCounter;
50
-
51
-
52
-	/**
53
-	 * Constructor
54
-	 */
55
-	public function __construct(){
56
-		$this->middlewares = array();
57
-		$this->middlewareCounter = 0;
58
-	}
59
-
60
-
61
-	/**
62
-	 * Adds a new middleware
63
-	 * @param Middleware $middleWare the middleware which will be added
64
-	 */
65
-	public function registerMiddleware(Middleware $middleWare){
66
-		array_push($this->middlewares, $middleWare);
67
-	}
68
-
69
-
70
-	/**
71
-	 * returns an array with all middleware elements
72
-	 * @return array the middlewares
73
-	 */
74
-	public function getMiddlewares(){
75
-		return $this->middlewares;
76
-	}
77
-
78
-
79
-	/**
80
-	 * This is being run in normal order before the controller is being
81
-	 * called which allows several modifications and checks
82
-	 *
83
-	 * @param Controller $controller the controller that is being called
84
-	 * @param string $methodName the name of the method that will be called on
85
-	 *                           the controller
86
-	 */
87
-	public function beforeController(Controller $controller, $methodName){
88
-		// we need to count so that we know which middlewares we have to ask in
89
-		// case there is an exception
90
-		$middlewareCount = count($this->middlewares);
91
-		for($i = 0; $i < $middlewareCount; $i++){
92
-			$this->middlewareCounter++;
93
-			$middleware = $this->middlewares[$i];
94
-			$middleware->beforeController($controller, $methodName);
95
-		}
96
-	}
97
-
98
-
99
-	/**
100
-	 * This is being run when either the beforeController method or the
101
-	 * controller method itself is throwing an exception. The middleware is asked
102
-	 * in reverse order to handle the exception and to return a response.
103
-	 * If the response is null, it is assumed that the exception could not be
104
-	 * handled and the error will be thrown again
105
-	 *
106
-	 * @param Controller $controller the controller that is being called
107
-	 * @param string $methodName the name of the method that will be called on
108
-	 *                            the controller
109
-	 * @param \Exception $exception the thrown exception
110
-	 * @return Response a Response object if the middleware can handle the
111
-	 * exception
112
-	 * @throws \Exception the passed in exception if it can't handle it
113
-	 */
114
-	public function afterException(Controller $controller, $methodName, \Exception $exception){
115
-		for($i=$this->middlewareCounter-1; $i>=0; $i--){
116
-			$middleware = $this->middlewares[$i];
117
-			try {
118
-				return $middleware->afterException($controller, $methodName, $exception);
119
-			} catch(\Exception $exception){
120
-				continue;
121
-			}
122
-		}
123
-		throw $exception;
124
-	}
125
-
126
-
127
-	/**
128
-	 * This is being run after a successful controllermethod call and allows
129
-	 * the manipulation of a Response object. The middleware is run in reverse order
130
-	 *
131
-	 * @param Controller $controller the controller that is being called
132
-	 * @param string $methodName the name of the method that will be called on
133
-	 *                            the controller
134
-	 * @param Response $response the generated response from the controller
135
-	 * @return Response a Response object
136
-	 */
137
-	public function afterController(Controller $controller, $methodName, Response $response){
138
-		for($i=count($this->middlewares)-1; $i>=0; $i--){
139
-			$middleware = $this->middlewares[$i];
140
-			$response = $middleware->afterController($controller, $methodName, $response);
141
-		}
142
-		return $response;
143
-	}
144
-
145
-
146
-	/**
147
-	 * This is being run after the response object has been rendered and
148
-	 * allows the manipulation of the output. The middleware is run in reverse order
149
-	 *
150
-	 * @param Controller $controller the controller that is being called
151
-	 * @param string $methodName the name of the method that will be called on
152
-	 *                           the controller
153
-	 * @param string $output the generated output from a response
154
-	 * @return string the output that should be printed
155
-	 */
156
-	public function beforeOutput(Controller $controller, $methodName, $output){
157
-		for($i=count($this->middlewares)-1; $i>=0; $i--){
158
-			$middleware = $this->middlewares[$i];
159
-			$output = $middleware->beforeOutput($controller, $methodName, $output);
160
-		}
161
-		return $output;
162
-	}
40
+    /**
41
+     * @var array array containing all the middlewares
42
+     */
43
+    private $middlewares;
44
+
45
+    /**
46
+     * @var int counter which tells us what middlware was executed once an
47
+     *                  exception occurs
48
+     */
49
+    private $middlewareCounter;
50
+
51
+
52
+    /**
53
+     * Constructor
54
+     */
55
+    public function __construct(){
56
+        $this->middlewares = array();
57
+        $this->middlewareCounter = 0;
58
+    }
59
+
60
+
61
+    /**
62
+     * Adds a new middleware
63
+     * @param Middleware $middleWare the middleware which will be added
64
+     */
65
+    public function registerMiddleware(Middleware $middleWare){
66
+        array_push($this->middlewares, $middleWare);
67
+    }
68
+
69
+
70
+    /**
71
+     * returns an array with all middleware elements
72
+     * @return array the middlewares
73
+     */
74
+    public function getMiddlewares(){
75
+        return $this->middlewares;
76
+    }
77
+
78
+
79
+    /**
80
+     * This is being run in normal order before the controller is being
81
+     * called which allows several modifications and checks
82
+     *
83
+     * @param Controller $controller the controller that is being called
84
+     * @param string $methodName the name of the method that will be called on
85
+     *                           the controller
86
+     */
87
+    public function beforeController(Controller $controller, $methodName){
88
+        // we need to count so that we know which middlewares we have to ask in
89
+        // case there is an exception
90
+        $middlewareCount = count($this->middlewares);
91
+        for($i = 0; $i < $middlewareCount; $i++){
92
+            $this->middlewareCounter++;
93
+            $middleware = $this->middlewares[$i];
94
+            $middleware->beforeController($controller, $methodName);
95
+        }
96
+    }
97
+
98
+
99
+    /**
100
+     * This is being run when either the beforeController method or the
101
+     * controller method itself is throwing an exception. The middleware is asked
102
+     * in reverse order to handle the exception and to return a response.
103
+     * If the response is null, it is assumed that the exception could not be
104
+     * handled and the error will be thrown again
105
+     *
106
+     * @param Controller $controller the controller that is being called
107
+     * @param string $methodName the name of the method that will be called on
108
+     *                            the controller
109
+     * @param \Exception $exception the thrown exception
110
+     * @return Response a Response object if the middleware can handle the
111
+     * exception
112
+     * @throws \Exception the passed in exception if it can't handle it
113
+     */
114
+    public function afterException(Controller $controller, $methodName, \Exception $exception){
115
+        for($i=$this->middlewareCounter-1; $i>=0; $i--){
116
+            $middleware = $this->middlewares[$i];
117
+            try {
118
+                return $middleware->afterException($controller, $methodName, $exception);
119
+            } catch(\Exception $exception){
120
+                continue;
121
+            }
122
+        }
123
+        throw $exception;
124
+    }
125
+
126
+
127
+    /**
128
+     * This is being run after a successful controllermethod call and allows
129
+     * the manipulation of a Response object. The middleware is run in reverse order
130
+     *
131
+     * @param Controller $controller the controller that is being called
132
+     * @param string $methodName the name of the method that will be called on
133
+     *                            the controller
134
+     * @param Response $response the generated response from the controller
135
+     * @return Response a Response object
136
+     */
137
+    public function afterController(Controller $controller, $methodName, Response $response){
138
+        for($i=count($this->middlewares)-1; $i>=0; $i--){
139
+            $middleware = $this->middlewares[$i];
140
+            $response = $middleware->afterController($controller, $methodName, $response);
141
+        }
142
+        return $response;
143
+    }
144
+
145
+
146
+    /**
147
+     * This is being run after the response object has been rendered and
148
+     * allows the manipulation of the output. The middleware is run in reverse order
149
+     *
150
+     * @param Controller $controller the controller that is being called
151
+     * @param string $methodName the name of the method that will be called on
152
+     *                           the controller
153
+     * @param string $output the generated output from a response
154
+     * @return string the output that should be printed
155
+     */
156
+    public function beforeOutput(Controller $controller, $methodName, $output){
157
+        for($i=count($this->middlewares)-1; $i>=0; $i--){
158
+            $middleware = $this->middlewares[$i];
159
+            $output = $middleware->beforeOutput($controller, $methodName, $output);
160
+        }
161
+        return $output;
162
+    }
163 163
 
164 164
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/OCS/BaseResponse.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -27,70 +27,70 @@
 block discarded – undo
27 27
 use OCP\AppFramework\Http\Response;
28 28
 
29 29
 abstract class BaseResponse extends Response   {
30
-	/** @var array */
31
-	protected $data;
30
+    /** @var array */
31
+    protected $data;
32 32
 
33
-	/** @var string */
34
-	protected $format;
33
+    /** @var string */
34
+    protected $format;
35 35
 
36
-	/** @var string */
37
-	protected $statusMessage;
36
+    /** @var string */
37
+    protected $statusMessage;
38 38
 
39
-	/** @var int */
40
-	protected $itemsCount;
39
+    /** @var int */
40
+    protected $itemsCount;
41 41
 
42
-	/** @var int */
43
-	protected $itemsPerPage;
42
+    /** @var int */
43
+    protected $itemsPerPage;
44 44
 
45
-	/**
46
-	 * BaseResponse constructor.
47
-	 *
48
-	 * @param DataResponse|null $dataResponse
49
-	 * @param string $format
50
-	 * @param string|null $statusMessage
51
-	 * @param int|null $itemsCount
52
-	 * @param int|null $itemsPerPage
53
-	 */
54
-	public function __construct(DataResponse $dataResponse,
55
-								$format = 'xml',
56
-								$statusMessage = null,
57
-								$itemsCount = null,
58
-								$itemsPerPage = null) {
59
-		$this->format = $format;
60
-		$this->statusMessage = $statusMessage;
61
-		$this->itemsCount = $itemsCount;
62
-		$this->itemsPerPage = $itemsPerPage;
45
+    /**
46
+     * BaseResponse constructor.
47
+     *
48
+     * @param DataResponse|null $dataResponse
49
+     * @param string $format
50
+     * @param string|null $statusMessage
51
+     * @param int|null $itemsCount
52
+     * @param int|null $itemsPerPage
53
+     */
54
+    public function __construct(DataResponse $dataResponse,
55
+                                $format = 'xml',
56
+                                $statusMessage = null,
57
+                                $itemsCount = null,
58
+                                $itemsPerPage = null) {
59
+        $this->format = $format;
60
+        $this->statusMessage = $statusMessage;
61
+        $this->itemsCount = $itemsCount;
62
+        $this->itemsPerPage = $itemsPerPage;
63 63
 
64
-		$this->data = $dataResponse->getData();
64
+        $this->data = $dataResponse->getData();
65 65
 
66
-		$this->setHeaders($dataResponse->getHeaders());
67
-		$this->setStatus($dataResponse->getStatus());
68
-		$this->setETag($dataResponse->getETag());
69
-		$this->setLastModified($dataResponse->getLastModified());
70
-		$this->setCookies($dataResponse->getCookies());
71
-		$this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
66
+        $this->setHeaders($dataResponse->getHeaders());
67
+        $this->setStatus($dataResponse->getStatus());
68
+        $this->setETag($dataResponse->getETag());
69
+        $this->setLastModified($dataResponse->getLastModified());
70
+        $this->setCookies($dataResponse->getCookies());
71
+        $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
72 72
 
73
-		if ($format === 'json') {
74
-			$this->addHeader(
75
-				'Content-Type', 'application/json; charset=utf-8'
76
-			);
77
-		} else {
78
-			$this->addHeader(
79
-				'Content-Type', 'application/xml; charset=utf-8'
80
-			);
81
-		}
82
-	}
73
+        if ($format === 'json') {
74
+            $this->addHeader(
75
+                'Content-Type', 'application/json; charset=utf-8'
76
+            );
77
+        } else {
78
+            $this->addHeader(
79
+                'Content-Type', 'application/xml; charset=utf-8'
80
+            );
81
+        }
82
+    }
83 83
 
84
-	/**
85
-	 * @param string[] $meta
86
-	 * @return string
87
-	 */
88
-	protected function renderResult($meta) {
89
-		// TODO rewrite functions
90
-		return \OC_API::renderResult($this->format, $meta, $this->data);
91
-	}
84
+    /**
85
+     * @param string[] $meta
86
+     * @return string
87
+     */
88
+    protected function renderResult($meta) {
89
+        // TODO rewrite functions
90
+        return \OC_API::renderResult($this->format, $meta, $this->data);
91
+    }
92 92
 
93
-	public function getOCSStatus() {
94
-		return parent::getStatus();
95
-	}
93
+    public function getOCSStatus() {
94
+        return parent::getStatus();
95
+    }
96 96
 }
Please login to merge, or discard this patch.