AbstractController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. declare(strict_types=1);
  3. namespace Grav\Plugin\Admin\Controllers;
  4. use Grav\Common\Debugger;
  5. use Grav\Common\Grav;
  6. use Grav\Common\Inflector;
  7. use Grav\Common\Language\Language;
  8. use Grav\Common\Utils;
  9. use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
  10. use Grav\Framework\Form\Interfaces\FormInterface;
  11. use Grav\Framework\Psr7\Response;
  12. use Grav\Framework\RequestHandler\Exception\NotFoundException;
  13. use Grav\Framework\RequestHandler\Exception\PageExpiredException;
  14. use Grav\Framework\RequestHandler\Exception\RequestException;
  15. use Grav\Framework\Route\Route;
  16. use Grav\Framework\Session\SessionInterface;
  17. use Psr\Http\Message\ResponseInterface;
  18. use Psr\Http\Message\ServerRequestInterface;
  19. use Psr\Http\Server\RequestHandlerInterface;
  20. use RocketTheme\Toolbox\Event\Event;
  21. use RocketTheme\Toolbox\Session\Message;
  22. abstract class AbstractController implements RequestHandlerInterface
  23. {
  24. /** @var string */
  25. protected $nonce_action = 'admin-form';
  26. /** @var string */
  27. protected $nonce_name = 'admin-nonce';
  28. /** @var ServerRequestInterface */
  29. protected $request;
  30. /** @var Grav */
  31. protected $grav;
  32. /** @var string */
  33. protected $type;
  34. /** @var string */
  35. protected $key;
  36. /**
  37. * Handle request.
  38. *
  39. * Fires event: admin.[directory].[task|action].[command]
  40. *
  41. * @param ServerRequestInterface $request
  42. * @return Response
  43. */
  44. public function handle(ServerRequestInterface $request): ResponseInterface
  45. {
  46. $attributes = $request->getAttributes();
  47. $this->request = $request;
  48. $this->grav = $attributes['grav'] ?? Grav::instance();
  49. $this->type = $attributes['type'] ?? null;
  50. $this->key = $attributes['key'] ?? null;
  51. /** @var Route $route */
  52. $route = $attributes['route'];
  53. $post = $this->getPost();
  54. if ($this->isFormSubmit()) {
  55. $form = $this->getForm();
  56. $this->nonce_name = $attributes['nonce_name'] ?? $form->getNonceName();
  57. $this->nonce_action = $attributes['nonce_action'] ?? $form->getNonceAction();
  58. }
  59. try {
  60. $task = $request->getAttribute('task') ?? $post['task'] ?? $route->getParam('task');
  61. if ($task) {
  62. if (empty($attributes['forwarded'])) {
  63. $this->checkNonce($task);
  64. }
  65. $type = 'task';
  66. $command = $task;
  67. } else {
  68. $type = 'action';
  69. $command = $request->getAttribute('action') ?? $post['action'] ?? $route->getParam('action') ?? 'display';
  70. }
  71. $command = strtolower($command);
  72. $event = new Event(
  73. [
  74. 'controller' => $this,
  75. 'response' => null
  76. ]
  77. );
  78. $this->grav->fireEvent("admin.{$this->type}.{$type}.{$command}", $event);
  79. $response = $event['response'];
  80. if (!$response) {
  81. /** @var Inflector $inflector */
  82. $inflector = $this->grav['inflector'];
  83. $method = $type . $inflector::camelize($command);
  84. if ($method && method_exists($this, $method)) {
  85. $response = $this->{$method}($request);
  86. } else {
  87. throw new NotFoundException($request);
  88. }
  89. }
  90. } catch (\Exception $e) {
  91. /** @var Debugger $debugger */
  92. $debugger = $this->grav['debugger'];
  93. $debugger->addException($e);
  94. $response = $this->createErrorResponse($e);
  95. }
  96. if ($response instanceof Response) {
  97. return $response;
  98. }
  99. return $this->createJsonResponse($response);
  100. }
  101. /**
  102. * Get request.
  103. *
  104. * @return ServerRequestInterface
  105. */
  106. public function getRequest(): ServerRequestInterface
  107. {
  108. return $this->request;
  109. }
  110. /**
  111. * @param string|null $name
  112. * @param mixed $default
  113. * @return mixed
  114. */
  115. public function getPost(string $name = null, $default = null)
  116. {
  117. $body = $this->request->getParsedBody();
  118. if ($name) {
  119. return $body[$name] ?? $default;
  120. }
  121. return $body;
  122. }
  123. /**
  124. * Check if a form has been submitted.
  125. *
  126. * @return bool
  127. */
  128. public function isFormSubmit(): bool
  129. {
  130. return (bool)$this->getPost('__form-name__');
  131. }
  132. /**
  133. * Get form.
  134. *
  135. * @param string|null $type
  136. * @return FormInterface
  137. */
  138. public function getForm(string $type = null): FormInterface
  139. {
  140. $object = $this->getObject();
  141. if (!$object) {
  142. throw new \RuntimeException('Not Found', 404);
  143. }
  144. $formName = $this->getPost('__form-name__');
  145. $uniqueId = $this->getPost('__unique_form_id__') ?: $formName;
  146. $form = $object->getForm($type ?? 'edit');
  147. if ($uniqueId) {
  148. $form->setUniqueId($uniqueId);
  149. }
  150. return $form;
  151. }
  152. /**
  153. * @return FlexObjectInterface
  154. */
  155. abstract public function getObject();
  156. /**
  157. * Get Grav instance.
  158. *
  159. * @return Grav
  160. */
  161. public function getGrav(): Grav
  162. {
  163. return $this->grav;
  164. }
  165. /**
  166. * Get session.
  167. *
  168. * @return SessionInterface
  169. */
  170. public function getSession(): SessionInterface
  171. {
  172. return $this->getGrav()['session'];
  173. }
  174. /**
  175. * Display the current admin page.
  176. *
  177. * @return Response
  178. */
  179. public function createDisplayResponse(): ResponseInterface
  180. {
  181. return new Response(418);
  182. }
  183. /**
  184. * Create custom HTML response.
  185. *
  186. * @param string $content
  187. * @param int $code
  188. * @return Response
  189. */
  190. public function createHtmlResponse(string $content, int $code = null): ResponseInterface
  191. {
  192. return new Response($code ?: 200, [], $content);
  193. }
  194. /**
  195. * Create JSON response.
  196. *
  197. * @param array $content
  198. * @return Response
  199. */
  200. public function createJsonResponse(array $content): ResponseInterface
  201. {
  202. $code = $content['code'] ?? 200;
  203. if ($code >= 301 && $code <= 307) {
  204. $code = 200;
  205. }
  206. return new Response($code, ['Content-Type' => 'application/json'], json_encode($content));
  207. }
  208. /**
  209. * Create redirect response.
  210. *
  211. * @param string $url
  212. * @param int $code
  213. * @return Response
  214. */
  215. public function createRedirectResponse(string $url, int $code = null): ResponseInterface
  216. {
  217. if (null === $code || $code < 301 || $code > 307) {
  218. $code = $this->grav['config']->get('system.pages.redirect_default_code', 302);
  219. }
  220. $accept = $this->getAccept(['application/json', 'text/html']);
  221. if ($accept === 'application/json') {
  222. return $this->createJsonResponse(['code' => $code, 'status' => 'redirect', 'redirect' => $url]);
  223. }
  224. return new Response($code, ['Location' => $url]);
  225. }
  226. /**
  227. * Create error response.
  228. *
  229. * @param \Exception $exception
  230. * @return Response
  231. */
  232. public function createErrorResponse(\Exception $exception): ResponseInterface
  233. {
  234. $validCodes = [
  235. 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418,
  236. 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 511
  237. ];
  238. if ($exception instanceof RequestException) {
  239. $code = $exception->getHttpCode();
  240. $reason = $exception->getHttpReason();
  241. } else {
  242. $code = $exception->getCode();
  243. $reason = null;
  244. }
  245. if (!in_array($code, $validCodes, true)) {
  246. $code = 500;
  247. }
  248. $message = $exception->getMessage();
  249. $response = [
  250. 'code' => $code,
  251. 'status' => 'error',
  252. 'message' => htmlspecialchars($message, ENT_QUOTES | ENT_HTML5, 'UTF-8')
  253. ];
  254. $accept = $this->getAccept(['application/json', 'text/html']);
  255. if ($accept === 'text/html') {
  256. $method = $this->getRequest()->getMethod();
  257. // On POST etc, redirect back to the previous page.
  258. if ($method !== 'GET' && $method !== 'HEAD') {
  259. $this->setMessage($message, 'error');
  260. $referer = $this->request->getHeaderLine('Referer');
  261. return $this->createRedirectResponse($referer, 303);
  262. }
  263. // TODO: improve error page
  264. return $this->createHtmlResponse($response['message']);
  265. }
  266. return new Response($code, ['Content-Type' => 'application/json'], json_encode($response), '1.1', $reason);
  267. }
  268. /**
  269. * Translate a string.
  270. *
  271. * @param string $string
  272. * @return string
  273. */
  274. public function translate(string $string): string
  275. {
  276. /** @var Language $language */
  277. $language = $this->grav['language'];
  278. return $language->translate($string);
  279. }
  280. /**
  281. * Set message to be shown in the admin.
  282. *
  283. * @param string $message
  284. * @param string $type
  285. * @return $this
  286. */
  287. public function setMessage($message, $type = 'info')
  288. {
  289. /** @var Message $messages */
  290. $messages = $this->grav['messages'];
  291. $messages->add($message, $type);
  292. return $this;
  293. }
  294. /**
  295. * Check if request nonce is valid.
  296. *
  297. * @param string $task
  298. * @throws PageExpiredException If nonce is not valid.
  299. */
  300. protected function checkNonce(string $task): void
  301. {
  302. $nonce = null;
  303. if (\in_array(strtoupper($this->request->getMethod()), ['POST', 'PUT', 'PATCH', 'DELETE'])) {
  304. $nonce = $this->getPost($this->nonce_name);
  305. }
  306. if (!$nonce) {
  307. $nonce = $this->grav['uri']->param($this->nonce_name);
  308. }
  309. if (!$nonce) {
  310. $nonce = $this->grav['uri']->query($this->nonce_name);
  311. }
  312. if (!$nonce || !Utils::verifyNonce($nonce, $this->nonce_action)) {
  313. throw new PageExpiredException($this->request);
  314. }
  315. }
  316. /**
  317. * Return the best matching mime type for the request.
  318. *
  319. * @param string[] $compare
  320. * @return string|null
  321. */
  322. protected function getAccept(array $compare): ?string
  323. {
  324. $accepted = [];
  325. foreach ($this->request->getHeader('Accept') as $accept) {
  326. foreach (explode(',', $accept) as $item) {
  327. if (!$item) {
  328. continue;
  329. }
  330. $split = explode(';q=', $item);
  331. $mime = array_shift($split);
  332. $priority = array_shift($split) ?? 1.0;
  333. $accepted[$mime] = $priority;
  334. }
  335. }
  336. arsort($accepted);
  337. // TODO: add support for image/* etc
  338. $list = array_intersect($compare, array_keys($accepted));
  339. if (!$list && (isset($accepted['*/*']) || isset($accepted['*']))) {
  340. return reset($compare) ?: null;
  341. }
  342. return reset($list) ?: null;
  343. }
  344. }