Controller.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. } else {
  86. return new JSONResponse($data);
  87. }
  88. }
  89. );
  90. }
  91. /**
  92. * Parses an HTTP accept header and returns the supported responder type
  93. * @param string $acceptHeader
  94. * @return string the responder type
  95. * @since 7.0.0
  96. * @since 9.1.0 Added default parameter
  97. */
  98. public function getResponderByHTTPHeader($acceptHeader, $default='json') {
  99. $headers = explode(',', $acceptHeader);
  100. // return the first matching responder
  101. foreach ($headers as $header) {
  102. $header = strtolower(trim($header));
  103. $responder = str_replace('application/', '', $header);
  104. if (array_key_exists($responder, $this->responders)) {
  105. return $responder;
  106. }
  107. }
  108. // no matching header return default
  109. return $default;
  110. }
  111. /**
  112. * Registers a formatter for a type
  113. * @param string $format
  114. * @param \Closure $responder
  115. * @since 7.0.0
  116. */
  117. protected function registerResponder($format, \Closure $responder) {
  118. $this->responders[$format] = $responder;
  119. }
  120. /**
  121. * Serializes and formats a response
  122. * @param mixed $response the value that was returned from a controller and
  123. * is not a Response instance
  124. * @param string $format the format for which a formatter has been registered
  125. * @throws \DomainException if format does not match a registered formatter
  126. * @return Response
  127. * @since 7.0.0
  128. */
  129. public function buildResponse($response, $format='json') {
  130. if(array_key_exists($format, $this->responders)) {
  131. $responder = $this->responders[$format];
  132. return $responder($response);
  133. } else {
  134. throw new \DomainException('No responder registered for format ' .
  135. $format . '!');
  136. }
  137. }
  138. /**
  139. * Lets you access post and get parameters by the index
  140. * @deprecated 7.0.0 write your parameters as method arguments instead
  141. * @param string $key the key which you want to access in the URL Parameter
  142. * placeholder, $_POST or $_GET array.
  143. * The priority how they're returned is the following:
  144. * 1. URL parameters
  145. * 2. POST parameters
  146. * 3. GET parameters
  147. * @param string $default If the key is not found, this value will be returned
  148. * @return mixed the content of the array
  149. * @since 6.0.0
  150. */
  151. public function params($key, $default=null){
  152. return $this->request->getParam($key, $default);
  153. }
  154. /**
  155. * Returns all params that were received, be it from the request
  156. * (as GET or POST) or through the URL by the route
  157. * @deprecated 7.0.0 use $this->request instead
  158. * @return array the array with all parameters
  159. * @since 6.0.0
  160. */
  161. public function getParams() {
  162. return $this->request->getParams();
  163. }
  164. /**
  165. * Returns the method of the request
  166. * @deprecated 7.0.0 use $this->request instead
  167. * @return string the method of the request (POST, GET, etc)
  168. * @since 6.0.0
  169. */
  170. public function method() {
  171. return $this->request->getMethod();
  172. }
  173. /**
  174. * Shortcut for accessing an uploaded file through the $_FILES array
  175. * @deprecated 7.0.0 use $this->request instead
  176. * @param string $key the key that will be taken from the $_FILES array
  177. * @return array the file in the $_FILES element
  178. * @since 6.0.0
  179. */
  180. public function getUploadedFile($key) {
  181. return $this->request->getUploadedFile($key);
  182. }
  183. /**
  184. * Shortcut for getting env variables
  185. * @deprecated 7.0.0 use $this->request instead
  186. * @param string $key the key that will be taken from the $_ENV array
  187. * @return array the value in the $_ENV element
  188. * @since 6.0.0
  189. */
  190. public function env($key) {
  191. return $this->request->getEnv($key);
  192. }
  193. /**
  194. * Shortcut for getting cookie variables
  195. * @deprecated 7.0.0 use $this->request instead
  196. * @param string $key the key that will be taken from the $_COOKIE array
  197. * @return array the value in the $_COOKIE element
  198. * @since 6.0.0
  199. */
  200. public function cookie($key) {
  201. return $this->request->getCookie($key);
  202. }
  203. /**
  204. * Shortcut for rendering a template
  205. * @deprecated 7.0.0 return a template response instead
  206. * @param string $templateName the name of the template
  207. * @param array $params the template parameters in key => value structure
  208. * @param string $renderAs user renders a full page, blank only your template
  209. * admin an entry in the admin settings
  210. * @param string[] $headers set additional headers in name/value pairs
  211. * @return \OCP\AppFramework\Http\TemplateResponse containing the page
  212. * @since 6.0.0
  213. */
  214. public function render($templateName, array $params=array(),
  215. $renderAs='user', array $headers=array()){
  216. $response = new TemplateResponse($this->appName, $templateName);
  217. $response->setParams($params);
  218. $response->renderAs($renderAs);
  219. foreach($headers as $name => $value){
  220. $response->addHeader($name, $value);
  221. }
  222. return $response;
  223. }
  224. }