| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 11:17:40 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @package    Grav\Plugin\Admin | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2024-01-05 11:50:46 +00:00
										 |  |  |  * @copyright  Copyright (c) 2015 - 2024 Trilby Media, LLC. All rights reserved. | 
					
						
							| 
									
										
										
										
											2023-01-02 11:17:40 -07:00
										 |  |  |  * @license    MIT License; see LICENSE file for details. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | namespace Grav\Plugin\Admin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  | use Grav\Common\Grav; | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | use Grav\Common\Processors\ProcessorBase; | 
					
						
							|  |  |  | use Grav\Framework\Route\Route; | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  | use Grav\Plugin\Admin\Routers\LoginRouter; | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | use Psr\Http\Message\ResponseInterface; | 
					
						
							|  |  |  | use Psr\Http\Message\ServerRequestInterface; | 
					
						
							|  |  |  | use Psr\Http\Server\RequestHandlerInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Router extends ProcessorBase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public $id = 'admin_router'; | 
					
						
							|  |  |  |     public $title = 'Admin Panel'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  |     /** @var Admin */ | 
					
						
							|  |  |  |     protected $admin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(Grav $container, Admin $admin) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct($container); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->admin = $admin; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Handle routing to the dashboard, group and build objects. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param ServerRequestInterface $request | 
					
						
							|  |  |  |      * @param RequestHandlerInterface $handler | 
					
						
							|  |  |  |      * @return ResponseInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->startTimer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $context = $request->getAttributes(); | 
					
						
							| 
									
										
										
										
											2021-03-26 18:15:42 +02:00
										 |  |  |         $query = $request->getQueryParams(); | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var Route $route */ | 
					
						
							|  |  |  |         $route = $context['route']; | 
					
						
							|  |  |  |         $normalized = mb_strtolower(trim($route->getRoute(), '/')); | 
					
						
							|  |  |  |         $parts = explode('/', $normalized); | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  |         array_shift($parts); // Admin path
 | 
					
						
							|  |  |  |         $routeStr = implode('/', $parts); | 
					
						
							|  |  |  |         $view = array_shift($parts); | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  |         $path = implode('/', $parts); | 
					
						
							| 
									
										
										
										
											2021-03-26 18:15:42 +02:00
										 |  |  |         $task = $this->container['task'] ?? $query['task'] ?? null; | 
					
						
							|  |  |  |         $action = $this->container['action'] ?? $query['action'] ?? null; | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  |         $params = ['view' => $view, 'route' => $routeStr, 'path' => $path, 'parts' => $parts, 'task' => $task, 'action' => $action]; | 
					
						
							|  |  |  |         $request = $request->withAttribute('admin', $params); | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  |         // Run login controller if user isn't fully logged in or asks to logout.
 | 
					
						
							|  |  |  |         $user = $this->admin->user; | 
					
						
							|  |  |  |         if (!$user->authorized || !$user->authorize('admin.login')) { | 
					
						
							|  |  |  |             $params = (new LoginRouter())->matchServerRequest($request); | 
					
						
							|  |  |  |             $request = $request->withAttribute('admin', $params + $request->getAttribute('admin')); | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 14:39:37 +02:00
										 |  |  |         $this->admin->request = $request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $response = $handler->handle($request); | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->stopTimer(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 09:51:43 +02:00
										 |  |  |         // Never allow admin pages to be rendered in <frame>, <iframe>, <embed> or <object> for improved security.
 | 
					
						
							| 
									
										
										
										
											2021-09-01 13:17:21 +03:00
										 |  |  |         return $response->withHeader('X-Frame-Options', 'DENY'); | 
					
						
							| 
									
										
										
										
											2019-06-18 12:08:57 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | } |