1
0

TemplateResponse.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCP\AppFramework\Http;
  30. /**
  31. * Response for a normal template
  32. * @since 6.0.0
  33. */
  34. class TemplateResponse extends Response {
  35. /**
  36. * @since 20.0.0
  37. */
  38. public const RENDER_AS_GUEST = 'guest';
  39. /**
  40. * @since 20.0.0
  41. */
  42. public const RENDER_AS_BLANK = '';
  43. /**
  44. * @since 20.0.0
  45. */
  46. public const RENDER_AS_BASE = 'base';
  47. /**
  48. * @since 20.0.0
  49. */
  50. public const RENDER_AS_USER = 'user';
  51. /**
  52. * @since 20.0.0
  53. */
  54. public const RENDER_AS_ERROR = 'error';
  55. /**
  56. * @since 20.0.0
  57. */
  58. public const RENDER_AS_PUBLIC = 'public';
  59. /**
  60. * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent
  61. */
  62. public const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts';
  63. /**
  64. * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent
  65. */
  66. public const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn';
  67. /**
  68. * name of the template
  69. * @var string
  70. */
  71. protected $templateName;
  72. /**
  73. * parameters
  74. * @var array
  75. */
  76. protected $params;
  77. /**
  78. * rendering type (admin, user, blank)
  79. * @var string
  80. */
  81. protected $renderAs;
  82. /**
  83. * app name
  84. * @var string
  85. */
  86. protected $appName;
  87. /**
  88. * constructor of TemplateResponse
  89. * @param string $appName the name of the app to load the template from
  90. * @param string $templateName the name of the template
  91. * @param array $params an array of parameters which should be passed to the
  92. * template
  93. * @param string $renderAs how the page should be rendered, defaults to user
  94. * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
  95. */
  96. public function __construct($appName, $templateName, array $params = [],
  97. $renderAs = self::RENDER_AS_USER) {
  98. parent::__construct();
  99. $this->templateName = $templateName;
  100. $this->appName = $appName;
  101. $this->params = $params;
  102. $this->renderAs = $renderAs;
  103. $this->setContentSecurityPolicy(new ContentSecurityPolicy());
  104. $this->setFeaturePolicy(new FeaturePolicy());
  105. }
  106. /**
  107. * Sets template parameters
  108. * @param array $params an array with key => value structure which sets template
  109. * variables
  110. * @return TemplateResponse Reference to this object
  111. * @since 6.0.0 - return value was added in 7.0.0
  112. */
  113. public function setParams(array $params) {
  114. $this->params = $params;
  115. return $this;
  116. }
  117. /**
  118. * Used for accessing the set parameters
  119. * @return array the params
  120. * @since 6.0.0
  121. */
  122. public function getParams() {
  123. return $this->params;
  124. }
  125. /**
  126. * @return string the app id of the used template
  127. * @since 25.0.0
  128. */
  129. public function getApp(): string {
  130. return $this->appName;
  131. }
  132. /**
  133. * Used for accessing the name of the set template
  134. * @return string the name of the used template
  135. * @since 6.0.0
  136. */
  137. public function getTemplateName() {
  138. return $this->templateName;
  139. }
  140. /**
  141. * Sets the template page
  142. * @param string $renderAs admin, user or blank. Admin also prints the admin
  143. * settings header and footer, user renders the normal
  144. * normal page including footer and header and blank
  145. * just renders the plain template
  146. * @return TemplateResponse Reference to this object
  147. * @since 6.0.0 - return value was added in 7.0.0
  148. */
  149. public function renderAs($renderAs) {
  150. $this->renderAs = $renderAs;
  151. return $this;
  152. }
  153. /**
  154. * Returns the set renderAs
  155. * @return string the renderAs value
  156. * @since 6.0.0
  157. */
  158. public function getRenderAs() {
  159. return $this->renderAs;
  160. }
  161. /**
  162. * Returns the rendered html
  163. * @return string the rendered html
  164. * @since 6.0.0
  165. */
  166. public function render() {
  167. $renderAs = self::RENDER_AS_USER;
  168. if ($this->renderAs === 'blank') {
  169. // Legacy fallback as \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
  170. $renderAs = self::RENDER_AS_BLANK;
  171. } elseif (in_array($this->renderAs, [
  172. self::RENDER_AS_GUEST,
  173. self::RENDER_AS_BLANK,
  174. self::RENDER_AS_BASE,
  175. self::RENDER_AS_ERROR,
  176. self::RENDER_AS_PUBLIC,
  177. self::RENDER_AS_USER], true)) {
  178. $renderAs = $this->renderAs;
  179. }
  180. \OCP\Util::addHeader('meta', ['name' => 'robots', 'content' => 'noindex, nofollow']);
  181. $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
  182. foreach ($this->params as $key => $value) {
  183. $template->assign($key, $value);
  184. }
  185. return $template->fetchPage($this->params);
  186. }
  187. }