| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace Grav\Plugin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  | use Grav\Common\Plugin; | 
					
						
							|  |  |  | use Grav\Common\Page\Page; | 
					
						
							|  |  |  | use Grav\Common\Page\Pages; | 
					
						
							|  |  |  | use Grav\Common\Filesystem\File; | 
					
						
							|  |  |  | use Grav\Common\Grav; | 
					
						
							|  |  |  | use Grav\Common\Uri; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class AdminPlugin extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $active = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $template; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var  string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $theme; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $route; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Uri | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $uri; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Admin | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $admin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     protected $popularity; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getSubscribedEvents() { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |             'onPluginsInitialized' => [['login', 100000], ['onPluginsInitialized', 1000]], | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |             'onShutdown' => ['onShutdown', 1000] | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Initialize administration plugin if admin path matches. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Disables system cache. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |     public function login() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |         // Check for Pro version and disable this plugin if found
 | 
					
						
							|  |  |  |         // if (file_exists(PLUGINS_DIR . 'admin_pro/admin_pro.php')) {
 | 
					
						
							|  |  |  |         //     $this->enabled = false;
 | 
					
						
							|  |  |  |         //     return;
 | 
					
						
							|  |  |  |         // }
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $route = $this->config->get('plugins.admin.route'); | 
					
						
							|  |  |  |         if (!$route) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |         $this->base = '/' . trim($route, '/'); | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $this->uri = $this->grav['uri']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Only activate admin if we're inside the admin path.
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |         if (substr($this->uri->route(), 0, strlen($this->base)) == $this->base) { | 
					
						
							|  |  |  |             // Disable system caching.
 | 
					
						
							|  |  |  |             $this->config->set('system.cache.enabled', false); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |             // Change login behavior.
 | 
					
						
							|  |  |  |             $this->config->set('plugins.login', $this->config->get('plugins.admin.login')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $this->active = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |      * Initialize administration plugin if admin path matches. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Disables system cache. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onPluginsInitialized() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Only activate admin if we're inside the admin path.
 | 
					
						
							|  |  |  |         if ($this->active) { | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |             $this->initializeAdmin(); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-09 14:03:01 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // We need popularity no matter what
 | 
					
						
							|  |  |  |         require_once __DIR__ . '/classes/popularity.php'; | 
					
						
							|  |  |  |         $this->popularity = new Popularity(); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Sets longer path to the home page allowing us to have list of pages when we enter to pages section. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     public function onPagesInitialized() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         // Set original route for the home page.
 | 
					
						
							|  |  |  |         $home = '/' . trim($this->config->get('system.home.alias'), '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Pages $pages */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $pages = $this->grav['pages']; | 
					
						
							| 
									
										
										
										
											2014-09-05 22:27:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->grav['admin']->routes = $pages->routes(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $pages->dispatch('/', true)->route($home); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Make local copy of POST.
 | 
					
						
							|  |  |  |         $post = !empty($_POST) ? $_POST : array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Handle tasks.
 | 
					
						
							|  |  |  |         $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task'); | 
					
						
							|  |  |  |         if ($task) { | 
					
						
							|  |  |  |             require_once __DIR__ . '/classes/controller.php'; | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |             $controller = new AdminController($this->grav, $this->template, $task, $this->route, $post); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             $success = $controller->execute(); | 
					
						
							|  |  |  |             $controller->redirect(); | 
					
						
							|  |  |  |         } elseif ($this->template == 'logs' && $this->route) { | 
					
						
							|  |  |  |             // Display RAW error message.
 | 
					
						
							|  |  |  |             echo $this->admin->logEntry(); | 
					
						
							|  |  |  |             exit(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |         $self = $this; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |         // Replace page service with admin.
 | 
					
						
							|  |  |  |         $this->grav['page'] = function ($c) use ($self) { | 
					
						
							|  |  |  |             $page = new Page; | 
					
						
							|  |  |  |             $page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md")); | 
					
						
							|  |  |  |             $page->slug(basename($self->template)); | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 06:10:31 +03:00
										 |  |  |             return $page; | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Add twig paths to plugin templates. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     public function onTwigTemplatePaths() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |         $this->theme = $this->config->get('plugins.admin.theme', 'grav'); | 
					
						
							|  |  |  |         $this->grav['twig']->twig_paths = array(__DIR__ . '/themes/'.$this->theme.'/templates'); | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set all twig variables for generating output. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |     public function onTwigSiteVariables() | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         // TODO: use real plugin name instead
 | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |         $theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/admin/themes/'.$this->theme; | 
					
						
							| 
									
										
										
										
											2014-08-29 11:27:53 +03:00
										 |  |  |         $twig = $this->grav['twig']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $twig->template = $this->template . '.html.twig'; | 
					
						
							|  |  |  |         $twig->twig_vars['location'] = $this->template; | 
					
						
							|  |  |  |         $twig->twig_vars['base_url_relative'] .= | 
					
						
							|  |  |  |             ($twig->twig_vars['base_url_relative'] != '/' ? '/' : '') . trim($this->config->get('plugins.admin.route'), '/'); | 
					
						
							|  |  |  |         $twig->twig_vars['theme_url'] = $theme_url; | 
					
						
							| 
									
										
										
										
											2014-08-29 11:59:43 +03:00
										 |  |  |         $twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative']; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         $twig->twig_vars['admin'] = $this->admin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |         // fake grav update
 | 
					
						
							| 
									
										
										
										
											2014-09-10 11:29:49 -06:00
										 |  |  |         $twig->twig_vars['grav_update'] = array('current'=>'0.9.1', 'available'=>'0.9.1'); | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |         switch ($this->template) { | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |             case 'dashboard': | 
					
						
							|  |  |  |                 $twig->twig_vars['popularity'] = $this->popularity; | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  |             case 'pages': | 
					
						
							|  |  |  |                 $twig->twig_vars['file'] = File\General::instance($this->admin->page(true)->filePath()); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     public function onShutdown() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-09-09 14:03:01 -06:00
										 |  |  |         // Just so we know that we're in this debug mode
 | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |         echo '<span style="color:red">system.debugger.shutdown.close_connection = false</span>'; | 
					
						
							| 
									
										
										
										
											2014-09-06 18:13:04 -06:00
										 |  |  |         if ($this->config->get('plugins.admin.popularity.enabled')) { | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Only track non-admin
 | 
					
						
							|  |  |  |             if (!$this->active) { | 
					
						
							|  |  |  |                 $this->popularity->trackHit(); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |     protected function initializeAdmin() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         $this->enable([ | 
					
						
							|  |  |  |             'onPagesInitialized' => ['onPagesInitialized', 1000], | 
					
						
							|  |  |  |             'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], | 
					
						
							|  |  |  |             'onTwigSiteVariables' => ['onTwigSiteVariables', 1000] | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         // Disable system caching.
 | 
					
						
							|  |  |  |         $this->config->set('system.cache.enabled', false); | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         // Change login behavior.
 | 
					
						
							|  |  |  |         $this->config->set('plugins.login', $this->config->get('plugins.admin.login')); | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         // Decide admin template and route.
 | 
					
						
							|  |  |  |         $path = trim(substr($this->uri->route(), strlen($this->base)), '/'); | 
					
						
							|  |  |  |         $this->template = 'dashboard'; | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         if ($path) { | 
					
						
							|  |  |  |             $array = explode('/', $path, 2); | 
					
						
							|  |  |  |             $this->template = array_shift($array); | 
					
						
							|  |  |  |             $this->route = array_shift($array); | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |             // Set path for new page.
 | 
					
						
							|  |  |  |             if ($this->uri->param('new')) { | 
					
						
							|  |  |  |                 $this->route .= '/new'; | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         // Initialize admin class.
 | 
					
						
							|  |  |  |         require_once __DIR__ . '/classes/admin.php'; | 
					
						
							|  |  |  |         $this->admin = new Admin($this->grav, $this->base, $this->template, $this->route); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 07:57:53 +03:00
										 |  |  |         // And store the class into DI container.
 | 
					
						
							|  |  |  |         $this->grav['admin'] = $this->admin; | 
					
						
							| 
									
										
										
										
											2014-09-03 22:22:03 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-05 13:06:38 -07:00
										 |  |  | } |