Blame view

sources/lib/public/appframework/controller.php 6.03 KB
03e52840d   Kload   Init
1
  <?php
03e52840d   Kload   Init
2
3
4
5
  /**
   * ownCloud - App Framework
   *
   * @author Bernhard Posselt
6d9380f96   Cédric Dupont   Update sources OC...
6
   * @copyright 2012, 2014 Bernhard Posselt <dev@bernhard-posselt.com>
03e52840d   Kload   Init
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
31b7f2792   Kload   Upgrade to ownclo...
22
23
24
25
26
27
  /**
   * Public interface of ownCloud for apps to use.
   * AppFramework\Controller class
   */
  
  namespace OCP\AppFramework;
03e52840d   Kload   Init
28

31b7f2792   Kload   Upgrade to ownclo...
29
  use OCP\AppFramework\Http\TemplateResponse;
6d9380f96   Cédric Dupont   Update sources OC...
30
  use OCP\AppFramework\Http\JSONResponse;
31b7f2792   Kload   Upgrade to ownclo...
31
  use OCP\IRequest;
03e52840d   Kload   Init
32

03e52840d   Kload   Init
33
34
  
  /**
31b7f2792   Kload   Upgrade to ownclo...
35
   * Base class to inherit your controllers from
03e52840d   Kload   Init
36
   */
31b7f2792   Kload   Upgrade to ownclo...
37
38
39
  abstract class Controller {
  
  	/**
837968727   Kload   [enh] Upgrade to ...
40
41
  	 * app name
  	 * @var string
31b7f2792   Kload   Upgrade to ownclo...
42
  	 */
837968727   Kload   [enh] Upgrade to ...
43
  	protected $appName;
03e52840d   Kload   Init
44
45
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
46
47
  	 * current request
  	 * @var \OCP\IRequest
03e52840d   Kload   Init
48
49
  	 */
  	protected $request;
6d9380f96   Cédric Dupont   Update sources OC...
50
  	private $responders;
837968727   Kload   [enh] Upgrade to ...
51

31b7f2792   Kload   Upgrade to ownclo...
52
53
  	/**
  	 * constructor of the controller
837968727   Kload   [enh] Upgrade to ...
54
  	 * @param string $appName the name of the app
31b7f2792   Kload   Upgrade to ownclo...
55
56
  	 * @param IRequest $request an instance of the request
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  	public function __construct($appName,
  	                            IRequest $request){
  		$this->appName = $appName;
  		$this->request = $request;
  
  		// default responders
  		$this->responders = array(
  			'json' => function ($response) {
  				return new JSONResponse($response);
  			}
  		);
  	}
  
  
  	/**
  	 * Parses an HTTP accept header and returns the supported responder type
  	 * @param string $acceptHeader
  	 * @return string the responder type
  	 */
  	public function getResponderByHTTPHeader($acceptHeader) {
  		$headers = explode(',', $acceptHeader);
  
  		// return the first matching responder
  		foreach ($headers as $header) {
  			$header = strtolower(trim($header));
  
  			$responder = str_replace('application/', '', $header);
  
  			if (array_key_exists($responder, $this->responders)) {
  				return $responder;
  			}
  		}
  
  		// no matching header defaults to json
  		return 'json';
  	}
  
  
  	/**
  	 * Registers a formatter for a type
  	 * @param string $format
  	 * @param \Closure $responder
  	 */
  	protected function registerResponder($format, \Closure $responder) {
  		$this->responders[$format] = $responder;
  	}
  
  
  	/**
  	 * Serializes and formats a response
  	 * @param mixed $response the value that was returned from a controller and
  	 * is not a Response instance
  	 * @param string $format the format for which a formatter has been registered
  	 * @throws \DomainException if format does not match a registered formatter
  	 * @return Response
  	 */
  	public function buildResponse($response, $format='json') {
  		if(array_key_exists($format, $this->responders)) {
  
  			$responder = $this->responders[$format];
  
  			return $responder($response);
837968727   Kload   [enh] Upgrade to ...
119
  		} else {
6d9380f96   Cédric Dupont   Update sources OC...
120
121
  			throw new \DomainException('No responder registered for format ' .
  				$format . '!');
837968727   Kload   [enh] Upgrade to ...
122
  		}
03e52840d   Kload   Init
123
124
125
126
127
  	}
  
  
  	/**
  	 * Lets you access post and get parameters by the index
6d9380f96   Cédric Dupont   Update sources OC...
128
  	 * @deprecated write your parameters as method arguments instead
03e52840d   Kload   Init
129
130
131
132
133
134
  	 * @param string $key the key which you want to access in the URL Parameter
  	 *                     placeholder, $_POST or $_GET array.
  	 *                     The priority how they're returned is the following:
  	 *                     1. URL parameters
  	 *                     2. POST parameters
  	 *                     3. GET parameters
6d9380f96   Cédric Dupont   Update sources OC...
135
  	 * @param string $default If the key is not found, this value will be returned
03e52840d   Kload   Init
136
137
138
  	 * @return mixed the content of the array
  	 */
  	public function params($key, $default=null){
31b7f2792   Kload   Upgrade to ownclo...
139
  		return $this->request->getParam($key, $default);
03e52840d   Kload   Init
140
  	}
31b7f2792   Kload   Upgrade to ownclo...
141

03e52840d   Kload   Init
142
143
  	/**
  	 * Returns all params that were received, be it from the request
6d9380f96   Cédric Dupont   Update sources OC...
144
145
  	 * (as GET or POST) or through the URL by the route
  	 * @deprecated use $this->request instead
03e52840d   Kload   Init
146
147
148
  	 * @return array the array with all parameters
  	 */
  	public function getParams() {
31b7f2792   Kload   Upgrade to ownclo...
149
  		return $this->request->getParams();
03e52840d   Kload   Init
150
  	}
31b7f2792   Kload   Upgrade to ownclo...
151

03e52840d   Kload   Init
152
153
  	/**
  	 * Returns the method of the request
6d9380f96   Cédric Dupont   Update sources OC...
154
  	 * @deprecated use $this->request instead
03e52840d   Kload   Init
155
156
157
  	 * @return string the method of the request (POST, GET, etc)
  	 */
  	public function method() {
31b7f2792   Kload   Upgrade to ownclo...
158
  		return $this->request->getMethod();
03e52840d   Kload   Init
159
  	}
31b7f2792   Kload   Upgrade to ownclo...
160

03e52840d   Kload   Init
161
162
  	/**
  	 * Shortcut for accessing an uploaded file through the $_FILES array
6d9380f96   Cédric Dupont   Update sources OC...
163
  	 * @deprecated use $this->request instead
03e52840d   Kload   Init
164
165
166
167
  	 * @param string $key the key that will be taken from the $_FILES array
  	 * @return array the file in the $_FILES element
  	 */
  	public function getUploadedFile($key) {
31b7f2792   Kload   Upgrade to ownclo...
168
  		return $this->request->getUploadedFile($key);
03e52840d   Kload   Init
169
  	}
31b7f2792   Kload   Upgrade to ownclo...
170

03e52840d   Kload   Init
171
172
  	/**
  	 * Shortcut for getting env variables
6d9380f96   Cédric Dupont   Update sources OC...
173
  	 * @deprecated use $this->request instead
03e52840d   Kload   Init
174
175
176
177
  	 * @param string $key the key that will be taken from the $_ENV array
  	 * @return array the value in the $_ENV element
  	 */
  	public function env($key) {
31b7f2792   Kload   Upgrade to ownclo...
178
  		return $this->request->getEnv($key);
03e52840d   Kload   Init
179
  	}
03e52840d   Kload   Init
180
181
182
  
  	/**
  	 * Shortcut for getting cookie variables
6d9380f96   Cédric Dupont   Update sources OC...
183
  	 * @deprecated use $this->request instead
03e52840d   Kload   Init
184
185
186
187
  	 * @param string $key the key that will be taken from the $_COOKIE array
  	 * @return array the value in the $_COOKIE element
  	 */
  	public function cookie($key) {
31b7f2792   Kload   Upgrade to ownclo...
188
189
190
191
192
193
  		return $this->request->getCookie($key);
  	}
  
  
  	/**
  	 * Shortcut for rendering a template
6d9380f96   Cédric Dupont   Update sources OC...
194
  	 * @deprecated return a template response instead
31b7f2792   Kload   Upgrade to ownclo...
195
196
197
198
  	 * @param string $templateName the name of the template
  	 * @param array $params the template parameters in key => value structure
  	 * @param string $renderAs user renders a full page, blank only your template
  	 *                          admin an entry in the admin settings
6d9380f96   Cédric Dupont   Update sources OC...
199
  	 * @param string[] $headers set additional headers in name/value pairs
31b7f2792   Kload   Upgrade to ownclo...
200
201
202
203
  	 * @return \OCP\AppFramework\Http\TemplateResponse containing the page
  	 */
  	public function render($templateName, array $params=array(),
  							$renderAs='user', array $headers=array()){
837968727   Kload   [enh] Upgrade to ...
204
  		$response = new TemplateResponse($this->appName, $templateName);
31b7f2792   Kload   Upgrade to ownclo...
205
206
207
208
209
210
211
212
  		$response->setParams($params);
  		$response->renderAs($renderAs);
  
  		foreach($headers as $name => $value){
  			$response->addHeader($name, $value);
  		}
  
  		return $response;
03e52840d   Kload   Init
213
  	}
31b7f2792   Kload   Upgrade to ownclo...
214

03e52840d   Kload   Init
215
  }