Controller.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Thomas Tanghus <thomas@tanghus.net>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. /**
  28. * Public interface of ownCloud for apps to use.
  29. * AppFramework\Controller class
  30. */
  31. namespace OCP\AppFramework;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\AppFramework\Http\JSONResponse;
  34. use OCP\AppFramework\Http\DataResponse;
  35. use OCP\AppFramework\Http\Response;
  36. use OCP\IRequest;
  37. /**
  38. * Base class to inherit your controllers from
  39. * @since 6.0.0
  40. */
  41. abstract class Controller {
  42. /**
  43. * app name
  44. * @var string
  45. * @since 7.0.0
  46. */
  47. protected $appName;
  48. /**
  49. * current request
  50. * @var \OCP\IRequest
  51. * @since 6.0.0
  52. */
  53. protected $request;
  54. /**
  55. * @var array
  56. * @since 7.0.0
  57. */
  58. private $responders;
  59. /**
  60. * constructor of the controller
  61. * @param string $appName the name of the app
  62. * @param IRequest $request an instance of the request
  63. * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0
  64. */
  65. public function __construct($appName,
  66. IRequest $request) {
  67. $this->appName = $appName;
  68. $this->request = $request;
  69. // default responders
  70. $this->responders = array(
  71. 'json' => function ($data) {
  72. if ($data instanceof DataResponse) {
  73. $response = new JSONResponse(
  74. $data->getData(),
  75. $data->getStatus()
  76. );
  77. $dataHeaders = $data->getHeaders();
  78. $headers = $response->getHeaders();
  79. // do not overwrite Content-Type if it already exists
  80. if (isset($dataHeaders['Content-Type'])) {
  81. unset($headers['Content-Type']);
  82. }
  83. $response->setHeaders(array_merge($dataHeaders, $headers));
  84. return $response;
  85. }
  86. return new JSONResponse($data);
  87. }
  88. );
  89. }
  90. /**
  91. * Parses an HTTP accept header and returns the supported responder type
  92. * @param string $acceptHeader
  93. * @return string the responder type
  94. * @since 7.0.0
  95. * @since 9.1.0 Added default parameter
  96. */
  97. public function getResponderByHTTPHeader($acceptHeader, $default='json') {
  98. $headers = explode(',', $acceptHeader);
  99. // return the first matching responder
  100. foreach ($headers as $header) {
  101. $header = strtolower(trim($header));
  102. $responder = str_replace('application/', '', $header);
  103. if (array_key_exists($responder, $this->responders)) {
  104. return $responder;
  105. }
  106. }
  107. // no matching header return default
  108. return $default;
  109. }
  110. /**
  111. * Registers a formatter for a type
  112. * @param string $format
  113. * @param \Closure $responder
  114. * @since 7.0.0
  115. */
  116. protected function registerResponder($format, \Closure $responder) {
  117. $this->responders[$format] = $responder;
  118. }
  119. /**
  120. * Serializes and formats a response
  121. * @param mixed $response the value that was returned from a controller and
  122. * is not a Response instance
  123. * @param string $format the format for which a formatter has been registered
  124. * @throws \DomainException if format does not match a registered formatter
  125. * @return Response
  126. * @since 7.0.0
  127. */
  128. public function buildResponse($response, $format='json') {
  129. if(array_key_exists($format, $this->responders)) {
  130. $responder = $this->responders[$format];
  131. return $responder($response);
  132. }
  133. throw new \DomainException('No responder registered for format '.
  134. $format . '!');
  135. }
  136. /**
  137. * Lets you access post and get parameters by the index
  138. * @deprecated 7.0.0 write your parameters as method arguments instead
  139. * @param string $key the key which you want to access in the URL Parameter
  140. * placeholder, $_POST or $_GET array.
  141. * The priority how they're returned is the following:
  142. * 1. URL parameters
  143. * 2. POST parameters
  144. * 3. GET parameters
  145. * @param string $default If the key is not found, this value will be returned
  146. * @return mixed the content of the array
  147. * @since 6.0.0
  148. */
  149. public function params($key, $default=null){
  150. return $this->request->getParam($key, $default);
  151. }
  152. /**
  153. * Returns all params that were received, be it from the request
  154. * (as GET or POST) or through the URL by the route
  155. * @deprecated 7.0.0 use $this->request instead
  156. * @return array the array with all parameters
  157. * @since 6.0.0
  158. */
  159. public function getParams() {
  160. return $this->request->getParams();
  161. }
  162. /**
  163. * Returns the method of the request
  164. * @deprecated 7.0.0 use $this->request instead
  165. * @return string the method of the request (POST, GET, etc)
  166. * @since 6.0.0
  167. */
  168. public function method() {
  169. return $this->request->getMethod();
  170. }
  171. /**
  172. * Shortcut for accessing an uploaded file through the $_FILES array
  173. * @deprecated 7.0.0 use $this->request instead
  174. * @param string $key the key that will be taken from the $_FILES array
  175. * @return array the file in the $_FILES element
  176. * @since 6.0.0
  177. */
  178. public function getUploadedFile($key) {
  179. return $this->request->getUploadedFile($key);
  180. }
  181. /**
  182. * Shortcut for getting env variables
  183. * @deprecated 7.0.0 use $this->request instead
  184. * @param string $key the key that will be taken from the $_ENV array
  185. * @return array the value in the $_ENV element
  186. * @since 6.0.0
  187. */
  188. public function env($key) {
  189. return $this->request->getEnv($key);
  190. }
  191. /**
  192. * Shortcut for getting cookie variables
  193. * @deprecated 7.0.0 use $this->request instead
  194. * @param string $key the key that will be taken from the $_COOKIE array
  195. * @return array the value in the $_COOKIE element
  196. * @since 6.0.0
  197. */
  198. public function cookie($key) {
  199. return $this->request->getCookie($key);
  200. }
  201. /**
  202. * Shortcut for rendering a template
  203. * @deprecated 7.0.0 return a template response instead
  204. * @param string $templateName the name of the template
  205. * @param array $params the template parameters in key => value structure
  206. * @param string $renderAs user renders a full page, blank only your template
  207. * admin an entry in the admin settings
  208. * @param string[] $headers set additional headers in name/value pairs
  209. * @return \OCP\AppFramework\Http\TemplateResponse containing the page
  210. * @since 6.0.0
  211. */
  212. public function render($templateName, array $params=array(),
  213. $renderAs='user', array $headers=array()){
  214. $response = new TemplateResponse($this->appName, $templateName);
  215. $response->setParams($params);
  216. $response->renderAs($renderAs);
  217. foreach($headers as $name => $value){
  218. $response->addHeader($name, $value);
  219. }
  220. return $response;
  221. }
  222. }