TemplateResponse.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. * @author Kate Döen <kate.doeen@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http;
  32. /**
  33. * Response for a normal template
  34. * @since 6.0.0
  35. *
  36. * @template S of int
  37. * @template H of array<string, mixed>
  38. * @template-extends Response<int, array<string, mixed>>
  39. */
  40. class TemplateResponse extends Response {
  41. /**
  42. * @since 20.0.0
  43. */
  44. public const RENDER_AS_GUEST = 'guest';
  45. /**
  46. * @since 20.0.0
  47. */
  48. public const RENDER_AS_BLANK = '';
  49. /**
  50. * @since 20.0.0
  51. */
  52. public const RENDER_AS_BASE = 'base';
  53. /**
  54. * @since 20.0.0
  55. */
  56. public const RENDER_AS_USER = 'user';
  57. /**
  58. * @since 20.0.0
  59. */
  60. public const RENDER_AS_ERROR = 'error';
  61. /**
  62. * @since 20.0.0
  63. */
  64. public const RENDER_AS_PUBLIC = 'public';
  65. /**
  66. * name of the template
  67. * @var string
  68. */
  69. protected $templateName;
  70. /**
  71. * parameters
  72. * @var array
  73. */
  74. protected $params;
  75. /**
  76. * rendering type (admin, user, blank)
  77. * @var string
  78. */
  79. protected $renderAs;
  80. /**
  81. * app name
  82. * @var string
  83. */
  84. protected $appName;
  85. /**
  86. * constructor of TemplateResponse
  87. * @param string $appName the name of the app to load the template from
  88. * @param string $templateName the name of the template
  89. * @param array $params an array of parameters which should be passed to the
  90. * template
  91. * @param string $renderAs how the page should be rendered, defaults to user
  92. * @param S $status
  93. * @param H $headers
  94. * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
  95. */
  96. public function __construct(string $appName, string $templateName, array $params = [], string $renderAs = self::RENDER_AS_USER, int $status = Http::STATUS_OK, array $headers = []) {
  97. parent::__construct($status, $headers);
  98. $this->templateName = $templateName;
  99. $this->appName = $appName;
  100. $this->params = $params;
  101. $this->renderAs = $renderAs;
  102. $this->setContentSecurityPolicy(new ContentSecurityPolicy());
  103. $this->setFeaturePolicy(new FeaturePolicy());
  104. }
  105. /**
  106. * Sets template parameters
  107. * @param array $params an array with key => value structure which sets template
  108. * variables
  109. * @return TemplateResponse Reference to this object
  110. * @since 6.0.0 - return value was added in 7.0.0
  111. */
  112. public function setParams(array $params) {
  113. $this->params = $params;
  114. return $this;
  115. }
  116. /**
  117. * Used for accessing the set parameters
  118. * @return array the params
  119. * @since 6.0.0
  120. */
  121. public function getParams() {
  122. return $this->params;
  123. }
  124. /**
  125. * @return string the app id of the used template
  126. * @since 25.0.0
  127. */
  128. public function getApp(): string {
  129. return $this->appName;
  130. }
  131. /**
  132. * Used for accessing the name of the set template
  133. * @return string the name of the used template
  134. * @since 6.0.0
  135. */
  136. public function getTemplateName() {
  137. return $this->templateName;
  138. }
  139. /**
  140. * Sets the template page
  141. * @param string $renderAs admin, user or blank. Admin also prints the admin
  142. * settings header and footer, user renders the normal
  143. * normal page including footer and header and blank
  144. * just renders the plain template
  145. * @return TemplateResponse Reference to this object
  146. * @since 6.0.0 - return value was added in 7.0.0
  147. */
  148. public function renderAs($renderAs) {
  149. $this->renderAs = $renderAs;
  150. return $this;
  151. }
  152. /**
  153. * Returns the set renderAs
  154. * @return string the renderAs value
  155. * @since 6.0.0
  156. */
  157. public function getRenderAs() {
  158. return $this->renderAs;
  159. }
  160. /**
  161. * Returns the rendered html
  162. * @return string the rendered html
  163. * @since 6.0.0
  164. */
  165. public function render() {
  166. $renderAs = self::RENDER_AS_USER;
  167. if ($this->renderAs === 'blank') {
  168. // Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
  169. $renderAs = self::RENDER_AS_BLANK;
  170. } elseif (in_array($this->renderAs, [
  171. self::RENDER_AS_GUEST,
  172. self::RENDER_AS_BLANK,
  173. self::RENDER_AS_BASE,
  174. self::RENDER_AS_ERROR,
  175. self::RENDER_AS_PUBLIC,
  176. self::RENDER_AS_USER], true)) {
  177. $renderAs = $this->renderAs;
  178. }
  179. $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
  180. foreach ($this->params as $key => $value) {
  181. $template->assign($key, $value);
  182. }
  183. return $template->fetchPage($this->params);
  184. }
  185. }